Browse Source

fixes for input max size and extra cursor pixel, fixes to report empty text size as 1xH

ncannasse 8 years ago
parent
commit
2ff60a169c
3 changed files with 21 additions and 4 deletions
  1. 2 2
      h2d/Text.hx
  2. 13 1
      h2d/TextInput.hx
  3. 6 1
      samples/Input.hx

+ 2 - 2
h2d/Text.hx

@@ -315,11 +315,11 @@ class Text extends Drawable {
 	override function getBoundsRec( relativeTo : Sprite, out : h2d.col.Bounds, forSize : Bool ) {
 		super.getBoundsRec(relativeTo, out, forSize);
 		updateSize();
-		var x, y, w, h;
+		var x, y, w : Float, h;
 		if( forSize ) {
 			x = 0;
 			y = 0;
-			w = realMaxWidth >= 0 && textAlign != Left && realMaxWidth > calcWidth ? realMaxWidth : calcWidth;
+			w = realMaxWidth >= 0 && textAlign != Left && realMaxWidth > calcWidth ? realMaxWidth : (calcWidth == 0 ? 1 : calcWidth);
 			h = calcSizeHeight;
 		} else {
 			x = 0;

+ 13 - 1
h2d/TextInput.hx

@@ -104,8 +104,8 @@ class TextInput extends Text {
 			case K.BACKSPACE:
 				if( cursorIndex > 0 && canEdit ) {
 					beforeChange();
-					text = text.substr(0, cursorIndex - 1) + text.substr(cursorIndex);
 					cursorIndex--;
+					text = text.substr(0, cursorIndex) + text.substr(cursorIndex + 1);
 					onChange();
 				}
 			case K.ENTER, K.NUMPAD_ENTER:
@@ -228,6 +228,12 @@ class TextInput extends Text {
 		return selectionRange == null ? null : text.substr(selectionRange.start, selectionRange.length);
 	}
 
+	override function set_text(t:hxd.UString) {
+		super.set_text(t);
+		if( cursorIndex > t.length ) cursorIndex = t.length;
+		return t;
+	}
+
 	override function set_font(f) {
 		super.set_font(f);
 		cursorTile = h2d.Tile.fromColor(0xFFFFFF, 1, font.size);
@@ -236,6 +242,11 @@ class TextInput extends Text {
 		return f;
 	}
 
+	override function initGlyphs(text:hxd.UString, rebuild = true, handleAlign = true, lines:Array<Int> = null):Void {
+		super.initGlyphs(text, rebuild, handleAlign, lines);
+		if( rebuild ) this.calcWidth += cursorTile.width; // cursor end pos
+	}
+
 	function textPos( x : Float, y : Float ) {
 		x += scrollX;
 		var pos = 0;
@@ -277,6 +288,7 @@ class TextInput extends Text {
 			if( selectionSize == 0 ) {
 				selectionPos = calcTextWidth(text.substr(0, selectionRange.start));
 				selectionSize = calcTextWidth(text.substr(selectionRange.start, selectionRange.length));
+				if( selectionRange.start + selectionRange.length == text.length ) selectionSize += cursorTile.width; // last pixel
 			}
 			selectionTile.dx += selectionPos;
 			selectionTile.width += selectionSize;

+ 6 - 1
samples/Input.hx

@@ -16,7 +16,7 @@ class Input extends hxd.App {
 
 		input = new h2d.TextInput(font, s2d);
 		input.backgroundColor = 0x80808080;
-		input.inputWidth = 100;
+	//	input.inputWidth = 100;
 
 		input.text = "Click to édit";
 		input.textColor = 0xAAAAAA;
@@ -30,6 +30,11 @@ class Input extends hxd.App {
 		input.onFocusLost = function(_) {
 			input.textColor = 0xAAAAAA;
 		}
+
+		input.onChange = function() {
+			while( input.text.length > 20 )
+				input.text = input.text.substr(0, -1);
+		}
 	}
 
 	override function update(dt:Float) {