Sfoglia il codice sorgente

Merge pull request #142 from motion-twin/master

Fix textWidth with align Center, Right
Nicolas Cannasse 9 anni fa
parent
commit
6cb47a4e1b
1 ha cambiato i file con 14 aggiunte e 6 eliminazioni
  1. 14 6
      h2d/Text.hx

+ 14 - 6
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;
 
 
@@ -113,13 +115,14 @@ class Text extends Drawable {
 
 
 	public function calcTextWidth( text : String ) {
 	public function calcTextWidth( text : String ) {
 		if( calcDone ) {
 		if( calcDone ) {
-			var ow = calcWidth, oh = calcHeight, osh = calcSizeHeight, oy = calcYMin;
+			var ow = calcWidth, oh = calcHeight, osh = calcSizeHeight, oy = calcYMin, ox = calcXMax;
 			initGlyphs(text, false);
 			initGlyphs(text, false);
 			var w = calcWidth;
 			var w = calcWidth;
 			calcWidth = ow;
 			calcWidth = ow;
 			calcHeight = oh;
 			calcHeight = oh;
 			calcSizeHeight = osh;
 			calcSizeHeight = osh;
 			calcYMin = oy;
 			calcYMin = oy;
+			calcXMax = ox;
 			return w;
 			return w;
 		} else {
 		} else {
 			initGlyphs(text, false);
 			initGlyphs(text, false);
@@ -219,7 +222,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 +247,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 +292,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);
 	}
 	}
-
 }
 }