Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

98 Problem with the inputbox #105

Merged
merged 1 commit into from
Oct 20, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 40 additions & 17 deletions src/Input.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Texture, utils, Ticker } from '@pixi/core';
import { Container } from '@pixi/display';
import { Container, IDestroyOptions } from '@pixi/display';
import { Graphics } from '@pixi/graphics';
import { Sprite } from '@pixi/sprite';
import { TextStyle, Text } from '@pixi/text';
Expand Down Expand Up @@ -45,6 +45,9 @@ export class Input extends Container
protected readonly options: InputOptions;
protected _keyboard: HTMLInputElement;

protected handleActivationBinding = this.handleActivation.bind(this);
protected onKeyDownBinding = this.onKeyDown.bind(this);

/** Fires when input loses focus. */
onEnter: Signal<(text: string) => void>;

Expand Down Expand Up @@ -81,26 +84,13 @@ export class Input extends Container

if (utils.isMobile.any)
{
window.addEventListener('touchstart', () => this.handleActivation());
window.addEventListener('touchstart', this.handleActivationBinding);
}
else if (!utils.isMobile.any)
{
window.addEventListener('click', () => this.handleActivation());

window.addEventListener('keydown', (e) =>
{
const key = e.key;
window.addEventListener('click', this.handleActivationBinding);

if (key === 'Backspace')
{
this._delete();
}
else if (key === 'Escape' || key === 'Enter')
{
this.stopEditing();
}
else if (key.length === 1) this._add(key);
});
window.addEventListener('keydown', this.onKeyDownBinding);
}

this.onEnter = new Signal();
Expand All @@ -118,6 +108,21 @@ export class Input extends Container
}
}

protected onKeyDown(e: KeyboardEvent)
{
const key = e.key;

if (key === 'Backspace')
{
this._delete();
}
else if (key === 'Escape' || key === 'Enter')
{
this.stopEditing();
}
else if (key.length === 1) this._add(key);
}

protected init()
{
const options = this.options;
Expand Down Expand Up @@ -427,4 +432,22 @@ export class Input extends Container
{
return [this.paddingTop, this.paddingRight, this.paddingBottom, this.paddingLeft];
}

override destroy(options?: IDestroyOptions | boolean)
{
this.off('pointertap');

if (utils.isMobile.any)
{
window.removeEventListener('touchstart', this.handleActivationBinding);
}
else if (!utils.isMobile.any)
{
window.removeEventListener('click', this.handleActivationBinding);

window.removeEventListener('keydown', this.onKeyDownBinding);
}

super.destroy(options);
}
}
Loading