瀏覽代碼

Fix bugs in multiline text input (#1210)

Spitko 1 年之前
父節點
當前提交
5a20074315
共有 1 個文件被更改,包括 16 次插入2 次删除
  1. 16 2
      h2d/TextInput.hx

+ 16 - 2
h2d/TextInput.hx

@@ -367,15 +367,25 @@ class TextInput extends Text {
 			lines.push( { line: line, startIndex: currIndex } );
 			var prevIndex = currIndex;
 			currIndex += line.length;
-			if( cursorIndex > prevIndex && cursorIndex < currIndex )
+			if( cursorIndex >= prevIndex && cursorIndex < currIndex )
 				cursorLineIndex = currLineIndex;
 			currLineIndex++;
 		}
 		if (cursorLineIndex == -1)
 			return;
-		var destinationIndex = hxd.Math.iclamp(cursorLineIndex + yDiff, 0, lines.length - 1);
+		var destinationIndex = hxd.Math.iclamp(cursorLineIndex + yDiff, -1, lines.length);
 		if (destinationIndex == cursorLineIndex)
 			return;
+		// We're moving down from the last line, move to the end of the line
+		if( destinationIndex == lines.length) {
+			cursorIndex = text.length;
+			return;
+		}
+		// We're moving up from the first line, snap to beginning
+		if( destinationIndex == -1 ) {
+			cursorIndex = 0;
+			return;
+		}
 		var current = lines[cursorLineIndex];
 		var xOffset = 0.;
 		var prevCC: Null<Int> = null;
@@ -404,6 +414,10 @@ class TextInput extends Text {
 			prevCC = cc;
 		}
 		cursorIndex = destination.startIndex + destination.line.length;
+		// The last character in this line may be the \n, check for this and move back by one.
+		// we can't just assume this because the last line typically won't end with a newline.
+		if( destination.line.charAt(destination.line.length-1) == "\n")
+			cursorIndex--;
 	}
 
 	function setState(h:TextHistoryElement) {