Guest User

Untitled

a guest
Sep 26th, 2016
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Haxe 2.44 KB | None | 0 0
  1. package;
  2. import flash.display.Sprite;
  3. import openfl.events.Event;
  4.  
  5. /**
  6.  * ...
  7.  * @author Mateusz Narolewski
  8.  */
  9. class HTMLInputField extends Sprite
  10. {
  11.     public var input(default, null):Dynamic;
  12.     public var style(default, null):Dynamic;
  13.     private var div:Dynamic;
  14.     private var canvas:Dynamic;
  15.     public var inputWidth(default, default):Float = 150;
  16.     public var inputHeight(default, default):Float = 30;
  17.     public var textSize(default, default):Float = 20;
  18.     public var text(get_text, set_text):String;
  19.     public var textColor(default, set_textColor):UInt;
  20.     public var outline(default, set_outline):Bool;
  21.     public var maxChars(default, set_maxChars):UInt;
  22.    
  23.     public static inline var ALIGN_LEFT = "left";
  24.     public static inline var ALIGN_RIGHT = "right";
  25.     public static inline var ALIGN_CENTER = "center";
  26.    
  27.     public function new(divId:String = "openfl-content")
  28.     {
  29.         super();
  30.        
  31.         input = untyped document.createElement("input");
  32.         input.type = "text";
  33.         style = input.style;
  34.         style.position = "absolute";
  35.         div = untyped document.body;
  36.         canvas = div.querySelector('canvas');
  37.         div.appendChild(input);
  38.        
  39.         textColor = 0x000000;
  40.        
  41.         this.addEventListener(Event.ENTER_FRAME, onEnterFrame);
  42.         this.addEventListener(Event.REMOVED_FROM_STAGE, removed);
  43.     }
  44.    
  45.     private function removed(e:Event):Void
  46.     {
  47.         removeEventListener(Event.REMOVED_FROM_STAGE, removed);
  48.         removeEventListener(Event.ENTER_FRAME, onEnterFrame);
  49.         div.removeChild(input);
  50.     }
  51.     private function onEnterFrame(e:Event):Void
  52.     {
  53.         var scale:Float = Std.parseFloat(canvas.style.width) / stage.stageWidth;
  54.         style.left = (this.x * scale) + Std.parseFloat(canvas.style.marginLeft) + 'px';
  55.         style.top =  (this.y * scale) + Std.parseFloat(canvas.style.marginTop) + 'px';
  56.         style.width = (inputWidth * scale) + "px";
  57.         style.height = (inputHeight * scale) + "px";
  58.         style.fontSize = (textSize * scale) + "px";
  59.     }
  60.    
  61.     public function setAlign(align:String):Void
  62.     {
  63.         style.textAlign = align;
  64.     }
  65.     public function get_text():String
  66.     {
  67.         return cast input.value;
  68.     }
  69.     public function set_text(txt:String):String
  70.     {
  71.         input.value = txt;
  72.         return txt;
  73.     }
  74.     public function set_maxChars(chars:UInt):UInt
  75.     {
  76.         input.maxLength = chars;
  77.         return chars;
  78.     }
  79.     public function set_textColor(color:UInt):UInt
  80.     {
  81.         style.color = "#" + StringTools.hex(color);
  82.         return color;
  83.     }
  84.     public function set_outline(b:Bool):Bool
  85.     {
  86.         style.outline = b ? "solid" : "none";
  87.         return b;
  88.     }
  89. }
Add Comment
Please, Sign In to add comment