63 lines
1.7 KiB
Haxe
63 lines
1.7 KiB
Haxe
package haxework.gui;
|
|
|
|
import flash.events.Event;
|
|
import flash.ui.Keyboard;
|
|
import flash.text.TextField;
|
|
import flash.events.KeyboardEvent;
|
|
import haxe.Timer;
|
|
import flash.events.MouseEvent;
|
|
|
|
class InputTextField extends TextField {
|
|
|
|
private var focused:Bool;
|
|
|
|
public function new() {
|
|
super();
|
|
#if flash
|
|
type = TextFieldType.INPUT;
|
|
#elseif html5
|
|
addEventListener(MouseEvent.CLICK, onMouseClick);
|
|
#end
|
|
}
|
|
|
|
private function onMouseClick(event:MouseEvent):Void {
|
|
focused = true;
|
|
border = true;
|
|
borderColor = 0x00ff00;
|
|
Timer.delay(function() {
|
|
stage.addEventListener(MouseEvent.CLICK, onFocusOut);
|
|
}, 1);
|
|
addEventListener(Event.REMOVED_FROM_STAGE, onFocusOut);
|
|
stage.addEventListener(KeyboardEvent.KEY_UP, onStageKeyUp);
|
|
stage.addEventListener(KeyboardEvent.KEY_DOWN, onStageKeyDown);
|
|
}
|
|
|
|
private function onStageKeyDown(event:KeyboardEvent):Void {
|
|
event.stopPropagation();
|
|
event.stopImmediatePropagation();
|
|
untyped __js__("window.event.preventDefault()");
|
|
}
|
|
|
|
private function onStageKeyUp(event:KeyboardEvent):Void {
|
|
event.stopPropagation();
|
|
event.stopImmediatePropagation();
|
|
untyped __js__("window.event.preventDefault()");
|
|
switch (event.keyCode) {
|
|
case Keyboard.BACKSPACE:
|
|
text = text.substring(0, text.length - 1);
|
|
case x if (x >= 65 && x <= 90):
|
|
text += String.fromCharCode(event.keyCode + (event.shiftKey ? 0 : 32));
|
|
}
|
|
}
|
|
|
|
private function onFocusOut(_):Void {
|
|
if (stage != null) {
|
|
stage.removeEventListener(MouseEvent.CLICK, onFocusOut);
|
|
stage.removeEventListener(KeyboardEvent.KEY_UP, onStageKeyUp);
|
|
stage.removeEventListener(KeyboardEvent.KEY_DOWN, onStageKeyDown);
|
|
}
|
|
focused = false;
|
|
border = false;
|
|
}
|
|
|
|
} |