Skip to content

Commit

Permalink
Merge pull request #58 from Starmapo/new-text-input
Browse files Browse the repository at this point in the history
Implement core text input from Flixel 5.9.0
  • Loading branch information
ianharrigan authored Dec 23, 2024
2 parents bd7ebd9 + 884e7cb commit c0db1db
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 27 deletions.
6 changes: 1 addition & 5 deletions haxe/ui/backend/TextInputImpl.hx
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,8 @@ package haxe.ui.backend;

typedef TextInputEvent = {type:String, stageX:Float, stageY:Float};

#if flixel_text_input

#if (flixel >= "5.9.0")
typedef TextInputImpl = haxe.ui.backend.flixel.textinputs.FlxTextInput;

#else

typedef TextInputImpl = haxe.ui.backend.flixel.textinputs.OpenFLTextInput;

#end
45 changes: 23 additions & 22 deletions haxe/ui/backend/flixel/textinputs/FlxTextInput.hx
Original file line number Diff line number Diff line change
@@ -1,39 +1,39 @@
package haxe.ui.backend.flixel.textinputs;

import flixel.FlxSprite;
import flixel.text.FlxInputText;
import flixel.util.FlxColor;
import haxe.ui.backend.TextInputImpl.TextInputEvent;
import haxe.ui.core.Component;
import openfl.events.Event;
import openfl.events.KeyboardEvent;

#if flixel_text_input

#if (flixel >= "5.9.0")
class FlxTextInput extends TextBase {
public static var USE_ON_ADDED:Bool = false;
public static var USE_ON_REMOVED:Bool = false;

private static inline var PADDING_X:Int = 4;
private static inline var PADDING_Y:Int = 2;

private var tf:flixel.addons.text.FlxTextInput;
private var tf:FlxInputText;

public function new() {
super();
tf = new flixel.addons.text.FlxTextInput();
tf.onChange.add(onInternalChange);
tf.onScroll.add(onScroll);
tf = new FlxInputText(0, 0, 0, null, 8, FlxColor.BLACK, FlxColor.TRANSPARENT);
tf.onTextChange.add(onInternalChange);
tf.onScrollChange.add(onScroll);
tf.pixelPerfectRender = true;
tf.moves = false;
_inputData.vscrollPageStep = 1;
_inputData.vscrollNativeWheel = true;
}

public override function focus() {
tf.focus = true;
tf.startFocus();
}

public override function blur() {
tf.focus = false;
tf.endFocus();
}

public function attach() {
Expand Down Expand Up @@ -184,21 +184,23 @@ class FlxTextInput extends TextBase {

if (tf.multiline != _displayData.multiline) {
tf.multiline = _displayData.multiline;
measureTextRequired = true;
// `multiline` only decides whether the user can add new lines,
// so measuring the text is not required.
}

if (tf.displayAsPassword != _inputData.password) {
tf.displayAsPassword = _inputData.password;
if (tf.passwordMode != _inputData.password) {
tf.passwordMode = _inputData.password;
measureTextRequired = true;
}

tf.type = (parentComponent.disabled ? DYNAMIC : INPUT);
tf.editable = !parentComponent.disabled;

return measureTextRequired;
}

private override function validatePosition() {
_left = Math.round(_left * Toolkit.scaleX);
_top = Math.round(_top * Toolkit.scaleY) + (PADDING_Y / 2);
_top = Math.round(_top * Toolkit.scaleY);
}

private override function validateDisplay() {
Expand Down Expand Up @@ -300,16 +302,16 @@ class FlxTextInput extends TextBase {
public var onChange(null, set):TextInputEvent->Void;
private function set_onChange(value:TextInputEvent->Void):TextInputEvent->Void {
if (_onChange != null) {
tf.onChange.remove(__onTextInputChangeEvent);
tf.onTextChange.remove(__onTextInputChangeEvent);
}
_onChange = value;
if (_onChange != null) {
tf.onChange.add(__onTextInputChangeEvent);
tf.onTextChange.add(__onTextInputChangeEvent);
}
return value;
}

private function __onTextInputChangeEvent() {
private function __onTextInputChangeEvent(text:String, action:FlxInputTextChange) {
if (_onChange != null) {
_onChange({
type: "change",
Expand Down Expand Up @@ -359,7 +361,7 @@ class FlxTextInput extends TextBase {
}
*/

private function onInternalChange() {
private function onInternalChange(text:String, action:FlxInputTextChange) {
_text = tf.text;

measureText();
Expand All @@ -368,8 +370,8 @@ class FlxTextInput extends TextBase {
_inputData.onChangedCallback();
}
}
private function onScroll() {

private function onScroll(scrollH:Int, scrollV:Int) {
_inputData.hscrollPos = tf.scrollH;
_inputData.vscrollPos = tf.scrollV - 1;

Expand All @@ -388,10 +390,9 @@ class FlxTextInput extends TextBase {

public function destroy(component:Component) {
tf.visible = false;
component.remove(tf);
component.remove(tf, true);
tf.destroy();
tf = null;
}
}

#end

0 comments on commit c0db1db

Please sign in to comment.