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

Support Android browser text input #263

Open
wants to merge 4 commits into
base: dev
Choose a base branch
from
Open
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
40 changes: 16 additions & 24 deletions flixel/addons/ui/FlxInputText.hx
Original file line number Diff line number Diff line change
Expand Up @@ -232,11 +232,6 @@ class FlxInputText extends FlxText
Text = "";
}

// Register paste events for the HTML5 parent window
#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)

calcFrame();
Expand All @@ -248,6 +243,9 @@ class FlxInputText extends FlxText
override public function destroy():Void
{
FlxG.stage.removeEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
#if (js && html5)
FlxG.stage.window.onTextInput.remove(windowInputText);
#end

backgroundSprite = FlxDestroyUtil.destroy(backgroundSprite);
fieldBorderSprite = FlxDestroyUtil.destroy(fieldBorderSprite);
Expand All @@ -264,10 +262,6 @@ class FlxInputText extends FlxText
}
#end

#if (js && html5)
FlxG.stage.window.onTextInput.remove(handleClipboardText);
#end

super.destroy();
}

Expand Down Expand Up @@ -386,10 +380,10 @@ class FlxInputText extends FlxText
#else
var clipboardText:String = Clipboard.text;
if (clipboardText != null)
pasteClipboardText(clipboardText);
windowInputText(clipboardText);
#end
default:
// Actually add some text
#if !(js && html5) // Actually add some text
if (e.charCode == 0) // non-printable characters crash String.fromCharCode
{
return;
Expand All @@ -402,6 +396,7 @@ class FlxInputText extends FlxText
caretIndex++;
onChange(INPUT_ACTION);
}
#end
}
}
}
Expand All @@ -414,17 +409,9 @@ class FlxInputText extends FlxText
}
}

#if (html5 && js)
function handleClipboardText(clipboardText:String)
{
@:privateAccess if (Clipboard._text == clipboardText)
pasteClipboardText(clipboardText);
}
#end

function pasteClipboardText(clipboardText:String)
function windowInputText(inputText:String)
{
final newText = filter(clipboardText).substring(0, maxLength > 0 ? (maxLength - text.length) : clipboardText.length);
final newText = filter(inputText).substring(0, maxLength > 0 ? (maxLength - text.length) : inputText.length);

text = insertSubstring(text, newText, caretIndex);
caretIndex += newText.length;
Expand Down Expand Up @@ -827,7 +814,10 @@ class FlxInputText extends FlxText
caret.visible = true;
caretIndex = text.length;

#if mobile
#if (js && html5)
if (!FlxG.stage.window.onTextInput.has(windowInputText))
FlxG.stage.window.onTextInput.add(windowInputText);
#elseif mobile
// Initialize soft keyboard
FlxG.stage.window.textInputEnabled = true;
#end
Expand All @@ -842,7 +832,9 @@ class FlxInputText extends FlxText
_caretTimer.cancel();
}

#if mobile
#if (js && html5)
FlxG.stage.window.onTextInput.remove(windowInputText);
#elseif mobile
// Remove soft keyboard
FlxG.stage.window.textInputEnabled = false;
#end
Expand Down Expand Up @@ -1034,4 +1026,4 @@ class FlxInputText extends FlxText
calcFrame();
return backgroundColor;
}
}
}
Loading