Parcourir la source

Fix textWidth with align Center, Right

Tipyx il y a 9 ans
Parent
commit
efafcef315
3 fichiers modifiés avec 13 ajouts et 7 suppressions
  1. 12 5
      h2d/Text.hx
  2. 0 1
      h3d/shader/ScreenShader.hx
  3. 1 1
      hxd/App.hx

+ 12 - 5
h2d/Text.hx

@@ -18,13 +18,15 @@ class Text extends Drawable {
 	public var textHeight(get, null) : Int;
 	public var textHeight(get, null) : Int;
 	public var textAlign(default, set) : Align;
 	public var textAlign(default, set) : Align;
 	public var letterSpacing(default, set) : Int;
 	public var letterSpacing(default, set) : Int;
-	public var lineSpacing(default,set) : Int;
+	public var lineSpacing(default, set) : Int;
+	
 
 
 	var glyphs : TileGroup;
 	var glyphs : TileGroup;
 
 
 	var calcDone:Bool;
 	var calcDone:Bool;
 	var calcYMin:Int;
 	var calcYMin:Int;
 	var calcWidth:Int;
 	var calcWidth:Int;
+	var calcXMax:Int;
 	var calcHeight:Int;
 	var calcHeight:Int;
 	var calcSizeHeight:Int;
 	var calcSizeHeight:Int;
 
 
@@ -219,7 +221,7 @@ class Text extends Drawable {
 					if( font.charset.isSpace(cc) ) e = null;
 					if( font.charset.isSpace(cc) ) e = null;
 				}
 				}
 			}
 			}
-			if( e != null ) {
+			if ( e != null ) {
 				if( rebuild ) glyphs.add(x, y, e.t);
 				if( rebuild ) glyphs.add(x, y, e.t);
 				if( y == 0 && e.t.dy < yMin ) yMin = e.t.dy;
 				if( y == 0 && e.t.dy < yMin ) yMin = e.t.dy;
 				x += esize + letterSpacing;
 				x += esize + letterSpacing;
@@ -244,7 +246,13 @@ class Text extends Drawable {
 		if( calcLines ) lines.push(x);
 		if( calcLines ) lines.push(x);
 
 
 		calcYMin = yMin;
 		calcYMin = yMin;
-		calcWidth = x > xMax ? x : xMax;
+		switch( align ){
+			case Left:
+				calcWidth = x > xMax ? x : xMax;
+				calcXMax = calcWidth;
+			case Right, Center:
+				calcXMax = x > xMax ? x : xMax;
+		}
 		calcHeight = y > 0 && x == 0 ? y - lineSpacing : y + font.lineHeight;
 		calcHeight = y > 0 && x == 0 ? y - lineSpacing : y + font.lineHeight;
 		calcSizeHeight = y > 0 && x == 0 ? y + (font.baseLine - dl) : y + font.baseLine;
 		calcSizeHeight = y > 0 && x == 0 ? y + (font.baseLine - dl) : y + font.baseLine;
 		calcDone = true;
 		calcDone = true;
@@ -283,7 +291,6 @@ class Text extends Drawable {
 	override function getBoundsRec( relativeTo : Sprite, out : h2d.col.Bounds, forSize : Bool ) {
 	override function getBoundsRec( relativeTo : Sprite, out : h2d.col.Bounds, forSize : Bool ) {
 		super.getBoundsRec(relativeTo, out, forSize);
 		super.getBoundsRec(relativeTo, out, forSize);
 		updateSize();
 		updateSize();
-		addBounds(relativeTo, out, 0, forSize ? 0 : calcYMin, calcWidth, forSize ? calcSizeHeight : calcHeight - calcYMin);
+		addBounds(relativeTo, out, 0, forSize ? 0 : calcYMin, calcXMax, forSize ? calcSizeHeight : calcHeight - calcYMin);
 	}
 	}
-
 }
 }

+ 0 - 1
h3d/shader/ScreenShader.hx

@@ -17,5 +17,4 @@ class ScreenShader extends hxsl.Shader {
 			output.position = vec4(input.position, 0, 1);
 			output.position = vec4(input.position, 0, 1);
 		}
 		}
 	};
 	};
-
 }
 }

+ 1 - 1
hxd/App.hx

@@ -70,7 +70,7 @@ class App {
 			engine.setDriver(log);
 			engine.setDriver(log);
 			try {
 			try {
 				engine.render(s3d);
 				engine.render(s3d);
-			} catch( e : Dynamic ) {
+			} catch( e : Dynamic ) { 
 				log.logLines.push(Std.string(e));
 				log.logLines.push(Std.string(e));
 			}
 			}
 			driver.logEnable = old;
 			driver.logEnable = old;