Created
September 16, 2016 02:28
-
-
Save tienery/fccaf9699c87da5cfb93848708b4ae12 to your computer and use it in GitHub Desktop.
TextField class using Kha in Haxe
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package render; | |
import kha.Key; | |
import kha.input.Keyboard; | |
import kha.Font; | |
import kha.Color; | |
import kha.input.Mouse; | |
import kha.graphics2.Graphics; | |
class TextField | |
{ | |
private var _keyboard:Keyboard; | |
private var _caretIndex:Int; | |
private var _keyDown:Bool; | |
private var _minWidth:Int; | |
public var caretPos(get, never):Int; | |
function get_caretPos() return _caretIndex; | |
public var defaultFont:Font; | |
public var fontSize:Int; | |
public var color:Color; | |
public var text:String; | |
public var x:Float; | |
public var y:Float; | |
private var _hasFocus:Bool; | |
public var hasFocus(get, never):Bool; | |
function get_hasFocus() return _hasFocus; | |
private var _hasInput:Bool; | |
public var hasInput(get, set):Bool; | |
function get_hasInput() return _hasInput; | |
function set_hasInput(val) return _hasInput = val; | |
public var textWidth(get, never):Float; | |
function get_textWidth() | |
{ | |
var width = defaultFont.width(fontSize, text); | |
if (width < _minWidth) | |
width = _minWidth; | |
return width; | |
} | |
public var textHeight(get, never):Float; | |
function get_textHeight() | |
{ | |
return defaultFont.height(fontSize); | |
} | |
public function new() | |
{ | |
fontSize = 12; | |
_keyboard = Keyboard.get(); | |
_minWidth = 30; | |
Mouse.get().notify(null, onMouseUp, null, null); | |
hasInput = true; | |
_caretIndex = 0; | |
_keyDown = false; | |
} | |
public function render(g:Graphics): Void | |
{ | |
g.font = defaultFont; | |
g.fontSize = fontSize; | |
if (color != null) | |
g.color = color; | |
else | |
g.color = Color.White; | |
g.drawString(text, x, y); | |
if (_caretIndex > -1 && _hasFocus && _hasInput) | |
{ | |
var subTextToCaret = text.substr(0, _caretIndex); | |
var subTextLength:Float = 0; | |
if (subTextToCaret != "") | |
subTextLength = defaultFont.width(fontSize, subTextToCaret); | |
var textHeight = defaultFont.height(fontSize); | |
g.color = Color.White; | |
g.drawLine(x + subTextLength, y, x + subTextLength, y + textHeight); | |
} | |
} | |
private function onMouseUp(button:Int, mouseX:Int, mouseY:Int):Void | |
{ | |
if (button > -1) | |
{ | |
if (mouseX > this.x && mouseY > this.y && mouseX < this.x + textWidth && mouseY < this.y + textHeight) | |
{ | |
_keyboard.notify(onKeyDown, onKeyUp); | |
_hasFocus = true; | |
} | |
else | |
{ | |
_keyboard.remove(onKeyDown, onKeyUp); | |
_hasFocus = false; | |
} | |
} | |
} | |
private function onKeyDown(key:Key, char:String):Void | |
{ | |
} | |
private function onKeyUp(key:Key, char:String):Void | |
{ | |
if (!_hasInput) | |
return; | |
switch(key) | |
{ | |
case CHAR: | |
if (_caretIndex < text.length) | |
{ | |
if (_caretIndex == 0) | |
{ | |
text += char; | |
_caretIndex++; | |
} | |
else | |
{ | |
var leftText = text.substr(0, _caretIndex); | |
var rightText = text.substr(_caretIndex); | |
leftText += char; | |
text = leftText + rightText; | |
_caretIndex = leftText.length; | |
} | |
} | |
else | |
{ | |
if (_caretIndex > 0) | |
{ | |
text += char; | |
_caretIndex = text.length; | |
} | |
else | |
{ | |
text = char; | |
_caretIndex = 1; | |
} | |
} | |
case ALT: | |
case BACK: | |
case BACKSPACE: | |
if (_caretIndex > 0 && _caretIndex < text.length) | |
{ | |
var leftText = text.substr(0, _caretIndex - 1); | |
var rightText = text.substr(_caretIndex); | |
text = leftText + rightText; | |
_caretIndex = leftText.length; | |
} | |
else | |
{ | |
if (_caretIndex != 0) | |
{ | |
text = text.substr(0, text.length - 1); | |
_caretIndex--; | |
} | |
} | |
case CTRL: | |
case DEL: | |
case DOWN: | |
case ENTER: | |
case ESC: | |
case LEFT: | |
if (_caretIndex - 1 < 0) | |
_caretIndex = 0; | |
else | |
_caretIndex--; | |
case RIGHT: | |
if (_caretIndex + 1 > text.length) | |
_caretIndex = text.length; | |
else | |
_caretIndex++; | |
case SHIFT: | |
case TAB: | |
case UP: | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Cool!