input textfield for html5

This commit is contained in:
2014-08-13 12:43:14 +04:00
parent 26044f4820
commit a484fa6fba
3 changed files with 72 additions and 2 deletions

View File

@@ -0,0 +1,63 @@
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;
}
}

View File

@@ -22,7 +22,6 @@ class InputView extends TextView implements IDisposable {
public function new() { public function new() {
super(); super();
dispatcher = new Dispatcher<InputViewListener>(); dispatcher = new Dispatcher<InputViewListener>();
textField.type = TextFieldType.INPUT;
textField.addEventListener(Event.CHANGE, onTextChange); textField.addEventListener(Event.CHANGE, onTextChange);
textField.addEventListener(KeyboardEvent.KEY_UP, onKeyUp); textField.addEventListener(KeyboardEvent.KEY_UP, onKeyUp);
textField.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown); textField.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
@@ -31,6 +30,10 @@ class InputView extends TextView implements IDisposable {
content.addChild(hintTextField); content.addChild(hintTextField);
} }
override private function buildTextField():TextField {
return new InputTextField();
}
private function set_hint(value:String):String { private function set_hint(value:String):String {
if (hint != value) { if (hint != value) {
hint = value; hint = value;

View File

@@ -32,7 +32,7 @@ class TextView extends SpriteView implements ITextView<Sprite, TextField> {
super(); super();
layoutHAlign = HAlign.NONE; layoutHAlign = HAlign.NONE;
layoutVAlign = VAlign.NONE; layoutVAlign = VAlign.NONE;
textField = new TextField(); textField = buildTextField();
textField.width = 1; textField.width = 1;
textField.height = #if html5 25 #else 1 #end; textField.height = #if html5 25 #else 1 #end;
textField.wordWrap = true; textField.wordWrap = true;
@@ -42,6 +42,10 @@ class TextView extends SpriteView implements ITextView<Sprite, TextField> {
content.addChild(textField); content.addChild(textField);
} }
private function buildTextField():TextField {
return new TextField();
}
private function set_paddings(value:Float):Float { private function set_paddings(value:Float):Float {
if (paddings != value) { if (paddings != value) {
paddings = value; paddings = value;