Skip to content

Commit

Permalink
Update FlxInputText.hx
Browse files Browse the repository at this point in the history
  • Loading branch information
DigiEggz committed Oct 18, 2023
1 parent dfff192 commit d30242c
Showing 1 changed file with 26 additions and 15 deletions.
41 changes: 26 additions & 15 deletions flixel/addons/ui/FlxInputText.hx
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@ import flixel.util.FlxColor;
import flixel.util.FlxDestroyUtil;
import flixel.util.FlxTimer;
import lime.system.Clipboard;
#if (html5 && js)
import lime.app.Application;
#end

/**
* FlxInputText v1.11, ported to Haxe
Expand Down Expand Up @@ -104,6 +101,11 @@ class FlxInputText extends FlxText
*/
public var hasFocus(default, set):Bool = false;

/**
* Determines if the input loses focus when pressing the Enter key on a physical or soft keyboard
*/
public var loseFocusOnEnter:Bool = #if mobile true; #else false; #end

/**
* The position of the selection cursor. An index of 0 means the carat is before the character at index 0.
*/
Expand Down Expand Up @@ -236,9 +238,8 @@ class FlxInputText extends FlxText
}

// Register paste events for the HTML5 parent window
#if (html5 && js)
var window = Application.current.window;
@:privateAccess window.onTextInput.add(handleClipboardText);
#if (js && html5)
FlxG.stage.window.onTextInput.add(handleClipboardText);
#end

text = Text; // ensure set_text is called to avoid bugs (like not preparing _charBoundaries on sys target, making it impossible to click)
Expand Down Expand Up @@ -268,9 +269,8 @@ class FlxInputText extends FlxText
}
#end

#if (html5 && js)
var window = Application.current.window;
@:privateAccess window.onTextInput.remove(handleClipboardText);
#if (js && html5)
FlxG.stage.window.onTextInput.remove(handleClipboardText);
#end

super.destroy();
Expand Down Expand Up @@ -384,11 +384,12 @@ class FlxInputText extends FlxText
}
case ENTER:
onChange(ENTER_ACTION);
if (loseFocusOnEnter)
hasFocus = false;
case V if (e.ctrlKey):
// Reapply focus when tabbing back into the window and selecting the field
#if (html5 && js)
var window = Application.current.window;
@:privateAccess window.textInputEnabled = true;
#if (js && html5)
FlxG.stage.window.textInputEnabled = true;
#else
var clipboardText:String = Clipboard.text;
if (clipboardText != null)
Expand Down Expand Up @@ -832,6 +833,11 @@ class FlxInputText extends FlxText
_caretTimer = new FlxTimer().start(0.5, toggleCaret, 0);
caret.visible = true;
caretIndex = text.length;

#if mobile
// Initialize soft keyboard
FlxG.stage.window.textInputEnabled = true;
#end
}
}
else
Expand All @@ -842,15 +848,20 @@ class FlxInputText extends FlxText
{
_caretTimer.cancel();
}

#if mobile
// Remove soft keyboard
FlxG.stage.window.textInputEnabled = false;
#end
}

if (newFocus != hasFocus)
{
calcFrame();

// Set focus on background parent text input
#if (html5 && js)
var window = Application.current.window;
#if (js && html5)
var window = FlxG.stage.window;
@:privateAccess window.__backend.setTextInputEnabled(newFocus);
#end
}
Expand Down Expand Up @@ -1030,4 +1041,4 @@ class FlxInputText extends FlxText
calcFrame();
return backgroundColor;
}
}
}

0 comments on commit d30242c

Please sign in to comment.