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

Add paste support for desktop and web + fix maxLength input #253

Merged
merged 7 commits into from
Oct 16, 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
63 changes: 54 additions & 9 deletions flixel/addons/ui/FlxInputText.hx
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,20 @@ package flixel.addons.ui;
import openfl.errors.Error;
import openfl.events.KeyboardEvent;
import openfl.geom.Rectangle;
import flixel.addons.ui.FlxUI.NamedString;
import flixel.FlxG;
import flixel.FlxSprite;
import flixel.addons.ui.FlxUI.NamedString;
import flixel.input.keyboard.FlxKey;
import flixel.math.FlxPoint;
import flixel.math.FlxRect;
import flixel.text.FlxText;
import flixel.util.FlxColor;
import flixel.util.FlxDestroyUtil;
import flixel.util.FlxTimer;
import openfl.desktop.Clipboard;
import lime.system.Clipboard;
#if (html5 && js)
import lime.app.Application;
#end

/**
* FlxInputText v1.11, ported to Haxe
Expand Down Expand Up @@ -225,13 +228,19 @@ class FlxInputText extends FlxText
}

lines = 1;
FlxG.stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
FlxG.stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown, false, 1);

if (Text == null)
{
Text = "";
}

// Register paste events for the HTML5 parent window
#if (html5 && js)
var window = Application.current.window;
@:privateAccess 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 Down Expand Up @@ -259,6 +268,11 @@ class FlxInputText extends FlxText
}
#end

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

super.destroy();
}

Expand Down Expand Up @@ -370,6 +384,16 @@ class FlxInputText extends FlxText
}
case ENTER:
onChange(ENTER_ACTION);
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;
#else
var clipboardText:String = Clipboard.text;
if (clipboardText != null)
pasteClipboardText(clipboardText);
#end
default:
// Actually add some text
if (e.charCode == 0) // non-printable characters crash String.fromCharCode
Expand All @@ -378,7 +402,7 @@ class FlxInputText extends FlxText
}
final newText = filter(String.fromCharCode(e.charCode));

if (newText.length > 0 && (maxLength == 0 || (text.length + newText.length) < maxLength))
if (newText.length > 0 && (maxLength == 0 || (text.length + newText.length) <= maxLength))
{
text = insertSubstring(text, newText, caretIndex);
caretIndex++;
Expand All @@ -396,6 +420,23 @@ class FlxInputText extends FlxText
}
}

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

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

text = insertSubstring(text, newText, caretIndex);
caretIndex += newText.length;
onChange(INPUT_ACTION);
}

/**
* Inserts a substring into a string at a specific index
*
Expand Down Expand Up @@ -512,11 +553,9 @@ class FlxInputText extends FlxText
switch (getAlignStr())
{
case RIGHT:
X = X - textField.width + textField.textWidth
;
X = X - textField.width + textField.textWidth;
case CENTER:
X = X - textField.width / 2 + textField.textWidth / 2
;
X = X - textField.width / 2 + textField.textWidth / 2;
default:
}
}
Expand Down Expand Up @@ -808,6 +847,12 @@ class FlxInputText extends FlxText
if (newFocus != hasFocus)
{
calcFrame();

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