ソースを参照

fixed correct text size calculus, fixed getSize() when align Center/Right

ncannasse 9 年 前
コミット
84bd8fd925
2 ファイル変更18 行追加6 行削除
  1. 1 1
      h2d/HtmlText.hx
  2. 17 5
      h2d/Text.hx

+ 1 - 1
h2d/HtmlText.hx

@@ -32,7 +32,7 @@ class HtmlText extends Text {
 		return null;
 	}
 
-	override function initGlyphs( text : String, rebuild = true, ?lines : Array<Int> ) {
+	override function initGlyphs( text : String, rebuild = true, handleAlign = true, ?lines : Array<Int> ) {
 		if( rebuild ) {
 			glyphs.clear();
 			for( i in images ) i.remove();

+ 17 - 5
h2d/Text.hx

@@ -27,7 +27,7 @@ class Text extends Drawable {
 	var calcWidth:Int;
 	var calcHeight:Int;
 	var calcSizeHeight:Int;
-	
+
 	#if lime
 	var waShader : h3d.shader.WhiteAlpha;
 	#end
@@ -189,14 +189,14 @@ class Text extends Drawable {
 		return lines.join("\n");
 	}
 
-	function initGlyphs( text : hxd.UString, rebuild = true, lines : Array<Int> = null ) : Void {
+	function initGlyphs( text : hxd.UString, rebuild = true, handleAlign = true, lines : Array<Int> = null ) : Void {
 		if( rebuild ) glyphs.clear();
 		var x = 0, y = 0, xMax = 0, prevChar = -1;
-		var align = rebuild ? textAlign : Left;
+		var align = handleAlign ? textAlign : Left;
 		switch( align ) {
 		case Center, Right:
 			lines = [];
-			initGlyphs(text, false, lines);
+			initGlyphs(text, false, false, lines);
 			var max = maxWidth == null ? 0 : Std.int(maxWidth);
 			var k = align == Center ? 1 : 0;
 			for( i in 0...lines.length )
@@ -294,7 +294,19 @@ class Text extends Drawable {
 	override function getBoundsRec( relativeTo : Sprite, out : h2d.col.Bounds, forSize : Bool ) {
 		super.getBoundsRec(relativeTo, out, forSize);
 		updateSize();
-		addBounds(relativeTo, out, 0, forSize ? 0 : calcYMin, calcWidth, forSize ? calcSizeHeight : calcHeight - calcYMin);
+		var x, y, w, h;
+		if( forSize ) {
+			x = 0;
+			y = 0;
+			w = maxWidth != null && textAlign != Left && maxWidth > calcWidth ? maxWidth : calcWidth;
+			h = calcSizeHeight;
+		} else {
+			x = 0;
+			y = calcYMin;
+			w = calcWidth;
+			h = calcHeight - calcYMin;
+		}
+		addBounds(relativeTo, out, x, y, w, h);
 	}
 
 }