TextInput.hx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. package h2d;
  2. import hxd.Key in K;
  3. private typedef TextHistoryElement = { t : String, c : Int, sel : { start : Int, length : Int } };
  4. class TextInput extends Text {
  5. public var cursorIndex : Int = -1;
  6. public var cursorTile : h2d.Tile;
  7. public var selectionTile : h2d.Tile;
  8. public var cursorBlinkTime = 0.5;
  9. public var inputWidth : Null<Int>;
  10. public var selectionRange : { start : Int, length : Int };
  11. public var canEdit = true;
  12. public var backgroundColor(get, set) : Null<Int>;
  13. var interactive : h2d.Interactive;
  14. var cursorText : String;
  15. var cursorX : Int;
  16. var cursorXIndex : Int;
  17. var cursorBlink = 0.;
  18. var cursorScroll = 0;
  19. var scrollX = 0;
  20. var selectionPos : Int;
  21. var selectionSize : Int;
  22. var undo : Array<TextHistoryElement> = [];
  23. var redo : Array<TextHistoryElement> = [];
  24. var lastChange = 0.;
  25. var lastClick = 0.;
  26. var maxHistorySize = 100;
  27. public function new(font, ?parent) {
  28. super(font, parent);
  29. interactive = new h2d.Interactive(0, 0);
  30. interactive.cursor = TextInput;
  31. interactive.onPush = function(e:hxd.Event) {
  32. onPush(e);
  33. if( !e.cancel && e.button == 0 ) {
  34. if( !interactive.hasFocus() ) {
  35. e.kind = EFocus;
  36. onFocus(e);
  37. e.kind = EPush;
  38. if( e.cancel ) return;
  39. interactive.focus();
  40. }
  41. cursorBlink = 0;
  42. var startIndex = textPos(e.relX, e.relY);
  43. cursorIndex = startIndex;
  44. selectionRange = null;
  45. var pt = new h2d.col.Point();
  46. var scene = getScene();
  47. scene.startDrag(function(e) {
  48. pt.x = e.relX;
  49. pt.y = e.relY;
  50. globalToLocal(pt);
  51. var index = textPos(pt.x, pt.y);
  52. if( index == startIndex )
  53. selectionRange = null;
  54. else if( index < startIndex )
  55. selectionRange = { start : index, length : startIndex - index };
  56. else
  57. selectionRange = { start : startIndex, length : index - startIndex };
  58. selectionSize = 0;
  59. cursorIndex = index;
  60. if( e.kind == ERelease || getScene() != scene )
  61. scene.stopDrag();
  62. });
  63. }
  64. };
  65. interactive.onKeyDown = function(e:hxd.Event) {
  66. onKeyDown(e);
  67. handleKey(e);
  68. };
  69. interactive.onTextInput = function(e:hxd.Event) {
  70. onTextInput(e);
  71. handleKey(e);
  72. };
  73. interactive.onFocusLost = function(e) {
  74. cursorIndex = -1;
  75. selectionRange = null;
  76. onFocusLost(e);
  77. };
  78. interactive.onClick = function(e) {
  79. onClick(e);
  80. if( e.cancel ) return;
  81. var t = haxe.Timer.stamp();
  82. // double click to select all
  83. if( t - lastClick < 0.3 && text.length != 0 ) {
  84. selectionRange = { start : 0, length : text.length };
  85. selectionSize = 0;
  86. cursorIndex = text.length;
  87. }
  88. lastClick = t;
  89. };
  90. interactive.onKeyUp = function(e) onKeyUp(e);
  91. interactive.onRelease = function(e) onRelease(e);
  92. interactive.onFocus = function(e) onFocus(e);
  93. interactive.onKeyUp = function(e) onKeyUp(e);
  94. interactive.onMove = function(e) onMove(e);
  95. interactive.onOver = function(e) onOver(e);
  96. interactive.onOut = function(e) onOut(e);
  97. interactive.cursor = TextInput;
  98. addChildAt(interactive, 0);
  99. }
  100. override function constraintSize(width:Float, height:Float) {
  101. // disable (don't allow multiline textinput for now)
  102. }
  103. function handleKey( e : hxd.Event ) {
  104. if( e.cancel || cursorIndex < 0 )
  105. return;
  106. var oldIndex = cursorIndex;
  107. var oldText = text;
  108. switch( e.keyCode ) {
  109. case K.LEFT:
  110. if( cursorIndex > 0 )
  111. cursorIndex--;
  112. case K.RIGHT:
  113. if( cursorIndex < text.length )
  114. cursorIndex++;
  115. case K.HOME:
  116. cursorIndex = 0;
  117. case K.END:
  118. cursorIndex = text.length;
  119. case K.BACKSPACE, K.DELETE if( selectionRange != null ):
  120. if( !canEdit ) return;
  121. beforeChange();
  122. cutSelection();
  123. onChange();
  124. case K.DELETE:
  125. if( cursorIndex < text.length && canEdit ) {
  126. beforeChange();
  127. text = text.substr(0, cursorIndex) + text.substr(cursorIndex + 1);
  128. onChange();
  129. }
  130. case K.BACKSPACE:
  131. if( cursorIndex > 0 && canEdit ) {
  132. beforeChange();
  133. cursorIndex--;
  134. text = text.substr(0, cursorIndex) + text.substr(cursorIndex + 1);
  135. onChange();
  136. }
  137. case K.ENTER, K.NUMPAD_ENTER:
  138. cursorIndex = -1;
  139. interactive.blur();
  140. return;
  141. case K.Z if( K.isDown(K.CTRL) ):
  142. if( undo.length > 0 && canEdit ) {
  143. redo.push(curHistoryState());
  144. setState(undo.pop());
  145. }
  146. return;
  147. case K.Y if( K.isDown(K.CTRL) ):
  148. if( redo.length > 0 && canEdit ) {
  149. undo.push(curHistoryState());
  150. setState(redo.pop());
  151. }
  152. return;
  153. default:
  154. if( e.kind == EKeyDown )
  155. return;
  156. if( e.charCode != 0 && canEdit ) {
  157. if( !font.hasChar(e.charCode) ) return; // don't allow chars not supported by font
  158. beforeChange();
  159. if( selectionRange != null )
  160. cutSelection();
  161. text = text.substr(0, cursorIndex) + String.fromCharCode(e.charCode) + text.substr(cursorIndex);
  162. cursorIndex++;
  163. onChange();
  164. }
  165. }
  166. cursorBlink = 0.;
  167. if( K.isDown(K.SHIFT) && text == oldText ) {
  168. if( cursorIndex == oldIndex ) return;
  169. if( selectionRange == null )
  170. selectionRange = oldIndex < cursorIndex ? { start : oldIndex, length : cursorIndex - oldIndex } : { start : cursorIndex, length : oldIndex - cursorIndex };
  171. else if( oldIndex == selectionRange.start ) {
  172. selectionRange.length += oldIndex - cursorIndex;
  173. selectionRange.start = cursorIndex;
  174. } else
  175. selectionRange.length += cursorIndex - oldIndex;
  176. if( selectionRange.length == 0 )
  177. selectionRange = null;
  178. else if( selectionRange.length < 0 ) {
  179. selectionRange.start += selectionRange.length;
  180. selectionRange.length = -selectionRange.length;
  181. }
  182. selectionSize = 0;
  183. } else
  184. selectionRange = null;
  185. }
  186. function cutSelection() {
  187. if(selectionRange == null) return false;
  188. cursorIndex = selectionRange.start;
  189. var end = cursorIndex + selectionRange.length;
  190. text = text.substr(0, cursorIndex) + text.substr(end);
  191. selectionRange = null;
  192. return true;
  193. }
  194. function setState(h:TextHistoryElement) {
  195. text = h.t;
  196. cursorIndex = h.c;
  197. selectionRange = h.sel;
  198. if( selectionRange != null )
  199. cursorIndex = selectionRange.start + selectionRange.length;
  200. }
  201. function curHistoryState() : TextHistoryElement {
  202. return { t : text, c : cursorIndex, sel : selectionRange == null ? null : { start : selectionRange.start, length : selectionRange.length } };
  203. }
  204. function beforeChange() {
  205. var t = haxe.Timer.stamp();
  206. if( t - lastChange < 1 ) {
  207. lastChange = t;
  208. return;
  209. }
  210. lastChange = t;
  211. undo.push(curHistoryState());
  212. redo = [];
  213. while( undo.length > maxHistorySize ) undo.shift();
  214. }
  215. public function getSelectedText() {
  216. return selectionRange == null ? null : text.substr(selectionRange.start, selectionRange.length);
  217. }
  218. override function set_text(t:hxd.UString) {
  219. super.set_text(t);
  220. if( cursorIndex > t.length ) cursorIndex = t.length;
  221. return t;
  222. }
  223. override function set_font(f) {
  224. super.set_font(f);
  225. cursorTile = h2d.Tile.fromColor(0xFFFFFF, 1, font.size);
  226. cursorTile.dy = 2;
  227. selectionTile = h2d.Tile.fromColor(0x3399FF, 0, font.lineHeight);
  228. return f;
  229. }
  230. override function initGlyphs(text:hxd.UString, rebuild = true, handleAlign = true, lines:Array<Int> = null):Void {
  231. super.initGlyphs(text, rebuild, handleAlign, lines);
  232. if( rebuild ) {
  233. this.calcWidth += cursorTile.width; // cursor end pos
  234. if( inputWidth != null && this.calcWidth > inputWidth ) this.calcWidth = inputWidth;
  235. }
  236. }
  237. function textPos( x : Float, y : Float ) {
  238. x += scrollX;
  239. var pos = 0;
  240. while( pos < text.length ) {
  241. if( calcTextWidth(text.substr(0,pos+1)) > x )
  242. break;
  243. pos++;
  244. }
  245. return pos;
  246. }
  247. override function sync(ctx) {
  248. interactive.width = (inputWidth != null ? inputWidth : maxWidth != null ? Math.ceil(maxWidth) : textWidth);
  249. interactive.height = font.lineHeight;
  250. super.sync(ctx);
  251. }
  252. override function draw(ctx:RenderContext) {
  253. if( inputWidth != null ) {
  254. var h = localToGlobal(new h2d.col.Point(inputWidth, font.lineHeight));
  255. ctx.setRenderZone(absX, absY, h.x - absX, h.y - absY);
  256. }
  257. if( cursorIndex >= 0 && (text != cursorText || cursorIndex != cursorXIndex) ) {
  258. if( cursorIndex > text.length ) cursorIndex = text.length;
  259. cursorText = text;
  260. cursorXIndex = cursorIndex;
  261. cursorX = calcTextWidth(text.substr(0, cursorIndex));
  262. if( inputWidth != null && cursorX - scrollX >= inputWidth )
  263. scrollX = cursorX - inputWidth + 1;
  264. else if( cursorX < scrollX )
  265. scrollX = cursorX;
  266. }
  267. absX -= scrollX * matA;
  268. absY -= scrollX * matC;
  269. if( selectionRange != null ) {
  270. if( selectionSize == 0 ) {
  271. selectionPos = calcTextWidth(text.substr(0, selectionRange.start));
  272. selectionSize = calcTextWidth(text.substr(selectionRange.start, selectionRange.length));
  273. if( selectionRange.start + selectionRange.length == text.length ) selectionSize += cursorTile.width; // last pixel
  274. }
  275. selectionTile.dx += selectionPos;
  276. selectionTile.width += selectionSize;
  277. emitTile(ctx, selectionTile);
  278. selectionTile.dx -= selectionPos;
  279. selectionTile.width -= selectionSize;
  280. }
  281. super.draw(ctx);
  282. absX += scrollX * matA;
  283. absY += scrollX * matC;
  284. if( cursorIndex >= 0 ) {
  285. cursorBlink += ctx.elapsedTime;
  286. if( cursorBlink % (cursorBlinkTime * 2) < cursorBlinkTime ) {
  287. cursorTile.dx += cursorX - scrollX;
  288. emitTile(ctx, cursorTile);
  289. cursorTile.dx -= cursorX - scrollX;
  290. }
  291. }
  292. if( inputWidth != null )
  293. ctx.clearRenderZone();
  294. }
  295. public function focus() {
  296. interactive.focus();
  297. }
  298. public function hasFocus() {
  299. return interactive.hasFocus();
  300. }
  301. public dynamic function onOut(e:hxd.Event) {
  302. }
  303. public dynamic function onOver(e:hxd.Event) {
  304. }
  305. public dynamic function onMove(e:hxd.Event) {
  306. }
  307. public dynamic function onClick(e:hxd.Event) {
  308. }
  309. public dynamic function onPush(e:hxd.Event) {
  310. }
  311. public dynamic function onRelease(e:hxd.Event) {
  312. }
  313. public dynamic function onKeyDown(e:hxd.Event) {
  314. }
  315. public dynamic function onKeyUp(e:hxd.Event) {
  316. }
  317. public dynamic function onTextInput(e:hxd.Event) {
  318. }
  319. public dynamic function onFocus(e:hxd.Event) {
  320. }
  321. public dynamic function onFocusLost(e:hxd.Event) {
  322. }
  323. public dynamic function onChange() {
  324. }
  325. override function drawRec(ctx:RenderContext) {
  326. var old = interactive.visible;
  327. interactive.visible = false;
  328. interactive.draw(ctx);
  329. super.drawRec(ctx);
  330. interactive.visible = true;
  331. }
  332. function get_backgroundColor() return interactive.backgroundColor;
  333. function set_backgroundColor(v) return interactive.backgroundColor = v;
  334. }