@@ -71,8 +71,6 @@ class TextInput extends Text {
7171 var cursorBlink = 0. ;
7272 var constraintHeight = - 1. ;
7373 var scrollX = 0. ;
74- var selectionPos : Float ;
75- var selectionSize : Float ;
7674 var undo : Array <TextHistoryElement > = [];
7775 var redo : Array <TextHistoryElement > = [];
7876 var lastChange = 0. ;
@@ -88,6 +86,7 @@ class TextInput extends Text {
8886 **/
8987 public function new (font , ? parent ) {
9088 super (font , parent );
89+ trimTrailingSpaces = false ;
9190 interactive = new h2d. Interactive (0 , 0 );
9291 interactive .cursor = TextInput ;
9392 interactive .onPush = function (e : hxd. Event ) {
@@ -121,7 +120,6 @@ class TextInput extends Text {
121120 selectionRange = { start : index , length : startIndex - index };
122121 else
123122 selectionRange = { start : startIndex , length : index - startIndex };
124- selectionSize = 0 ;
125123 cursorIndex = index ;
126124 onCursorChange ();
127125 if ( e .kind == ERelease || getScene () != scene )
@@ -155,7 +153,6 @@ class TextInput extends Text {
155153 if ( t - lastClick < 0.3 && getTextLength () != 0 ) {
156154 var start = getWordStart ();
157155 selectionRange = { start : getWordStart (), length : getWordEnd () - start };
158- selectionSize = 0 ;
159156 cursorIndex = selectionRange .start + selectionRange .length ;
160157 }
161158 lastClick = t ;
@@ -239,7 +236,7 @@ class TextInput extends Text {
239236 beforeChange ();
240237 if ( selectionRange == null )
241238 selectionRange = { start : cursorIndex , length : K .isDown (K .CTRL ) ? getWordEnd () - cursorIndex : 1 };
242- cutSelection ();
239+ cutSelection (false );
243240 onChange ();
244241 }
245242 case K .BACKSPACE :
@@ -249,7 +246,7 @@ class TextInput extends Text {
249246 var newIndex = K .isDown (K .CTRL ) ? getWordStart () : cursorIndex - 1 ;
250247 selectionRange = { start : newIndex , length : cursorIndex - newIndex };
251248 }
252- cutSelection ();
249+ cutSelection (true );
253250 onChange ();
254251 }
255252 case K .ESCAPE :
@@ -282,7 +279,6 @@ class TextInput extends Text {
282279 if (text != " " ) {
283280 cursorIndex = getTextLength ();
284281 selectionRange = {start : 0 , length : cursorIndex };
285- selectionSize = 0 ;
286282 onCursorChange ();
287283 }
288284 return ;
@@ -335,7 +331,6 @@ class TextInput extends Text {
335331 selectionRange .start + = selectionRange .length ;
336332 selectionRange .length = - selectionRange .length ;
337333 }
338- selectionSize = 0 ;
339334 onCursorChange ();
340335
341336 } else if ( oldText != text || cursorIndex != oldIndex )
@@ -354,10 +349,9 @@ class TextInput extends Text {
354349 var pos = 0 ;
355350 for ( line in lines ) {
356351 for ( p in splitRawText (line ).split (" \n " ) ) {
357- if ( cursor < p .length )
352+ if ( cursor <= p .length )
358353 return pos + cursor ;
359354 pos + = p .length ;
360- if ( font .charset .isSpace (StringTools .fastCodeAt (text ,pos )) ) pos ++ ;
361355 cursor - = p .length + 1 ;
362356 }
363357 pos ++ ;
@@ -374,10 +368,9 @@ class TextInput extends Text {
374368 var spos = 0 ;
375369 for ( line in lines ) {
376370 for ( p in splitRawText (line ).split (" \n " ) ) {
377- if ( (pos - spos ) < p .length )
371+ if ( (pos - spos ) <= p .length )
378372 return (pos - spos ) + cursor ;
379373 spos + = p .length ;
380- if ( font .charset .isSpace (StringTools .fastCodeAt (text ,spos )) ) spos ++ ;
381374 cursor + = p .length + 1 ;
382375 }
383376 spos ++ ;
@@ -406,15 +399,20 @@ class TextInput extends Text {
406399 cutSelection ();
407400 var pos = getTextPos (cursorIndex );
408401 text = text .substr (0 , pos ) + t + text .substr (pos );
409- cursorIndex + = t .length ;
402+ pos + = t .length ;
403+ cursorIndex = getCursorPos (pos );
410404 onChange ();
411405 }
412406
413- function cutSelection () {
407+ function cutSelection ( ? back ) {
414408 if (selectionRange == null ) return false ;
415- cursorIndex = selectionRange .start ;
416- var end = cursorIndex + selectionRange .length ;
417- text = text .substr (0 , getTextPos (cursorIndex )) + text .substr (getTextPos (end ));
409+ var pos = getTextPos (selectionRange .start );
410+ var end = getTextPos (selectionRange .start + selectionRange .length );
411+ if ( pos == end && back != null ) {
412+ if ( back ) pos -- else end ++ ;
413+ }
414+ text = text .substr (0 , pos ) + text .substr (end );
415+ cursorIndex = getCursorPos (pos );
418416 selectionRange = null ;
419417 return true ;
420418 }
@@ -778,17 +776,17 @@ class TextInput extends Text {
778776
779777 var selEnd = line .length ;
780778
781- if (selectionRange .start > lineOffset + line .length || selectionRange .start + selectionRange .length < lineOffset ) {
779+ if (selectionRange .start >= lineOffset + line .length || selectionRange .start + selectionRange .length < lineOffset ) {
782780 lineOffset + = line .length ;
783781 continue ;
784782 }
785783
786784 var selStart = Math .floor (Math .max (0 , selectionRange .start - lineOffset ));
787785 var selEnd = Math .floor (Math .min (line .length - selStart , selectionRange .length + selectionRange .start - lineOffset - selStart ));
788786
789- selectionPos = calcTextWidth (line .substr (0 , selStart ));
790- selectionSize = calcTextWidth (line .substr (selStart , selEnd ));
791- if ( selectionRange .start + selectionRange .length == cursorIndex ) selectionSize + = cursorTile .width ; // last pixel
787+ var selectionPos = calcTextWidth (line .substr (0 , selStart ));
788+ var selectionSize = calcTextWidth (line .substr (selStart , selEnd ));
789+ if ( selectionRange .start + selectionRange .length == cursorIndex || selectionSize == 0 ) selectionSize + = cursorTile .width ; // last pixel
792790
793791 selectionTile .dx + = selectionPos ;
794792 selectionTile .dy + = i * font .lineHeight ;
0 commit comments