Skip to content

Commit

Permalink
Add an action to focus a text input object (#5268)
Browse files Browse the repository at this point in the history
* Useful to let the player type something without having them manually clicking/touching the input on the screen.
  • Loading branch information
Bouh committed May 1, 2023
1 parent a0fb289 commit cc0a01b
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
14 changes: 14 additions & 0 deletions Extensions/TextInput/JsExtension.js
Original file line number Diff line number Diff line change
Expand Up @@ -561,6 +561,20 @@ module.exports = {
.getCodeExtraInformation()
.setFunctionName('isFocused');

object
.addScopedAction(
'Focus',
_('Focus'),
_('Focus the input so that text can be entered (like if it was touched/clicked).'),
_('Focus _PARAM0_'),
_(''),
'res/conditions/surObjet24.png',
'res/conditions/surObjet.png'
)
.addParameter('object', _('Text input'), 'TextInputObject', false)
.getCodeExtraInformation()
.setFunctionName('focus');

return extension;
},
/**
Expand Down
6 changes: 6 additions & 0 deletions Extensions/TextInput/textinputruntimeobject-pixi-renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,12 @@ namespace gdjs {
isFocused() {
return this._input === document.activeElement;
}

focus() {
if (!this._input) return;

this._input.focus();
}
}

export const TextInputRuntimeObjectRenderer = TextInputRuntimeObjectPixiRenderer;
Expand Down
4 changes: 4 additions & 0 deletions Extensions/TextInput/textinputruntimeobject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,10 @@ namespace gdjs {
isFocused(): boolean {
return this._renderer.isFocused();
}

focus(): void {
this._renderer.focus();
}
}
gdjs.registerObject(
'TextInput::TextInputObject',
Expand Down

0 comments on commit cc0a01b

Please sign in to comment.