Selaa lähdekoodia

Add Flow.reverse attribute

trethaller 6 vuotta sitten
vanhempi
commit
8ea550b29f
1 muutettua tiedostoa jossa 39 lisäystä ja 20 poistoa
  1. 39 20
      h2d/Flow.hx

+ 39 - 20
h2d/Flow.hx

@@ -171,6 +171,11 @@ class Flow extends Object {
 	**/
 	public var multiline(default,set) : Bool = false;
 
+	/**
+		When set to true, children are aligned in reverse order
+	**/
+	public var reverse(default,set) : Bool = true;
+
 	var background : h2d.ScaleGrid;
 	var debugGraphics : h2d.Graphics;
 	var properties : Array<FlowProperties> = [];
@@ -243,6 +248,13 @@ class Flow extends Object {
 		return multiline = v;
 	}
 
+	function set_reverse(v) {
+		if( reverse == v )
+			return v;
+		needReflow = true;
+		return reverse = v;
+	}
+
 	function set_needReflow(v) {
 		if( needReflow == v )
 			return v;
@@ -570,6 +582,13 @@ class Flow extends Object {
 		if( debug )
 			debugGraphics.clear();
 
+		inline function childAt(i: Int) {
+			return children[ reverse ? children.length - i - 1 : i ];
+		}
+		inline function propAt(i: Int) {
+			return properties[ reverse ? children.length - i - 1 : i ];
+		}
+
 		var cw, ch;
 		if( !isVertical ) {
 			var halign = horizontalAlign == null ? Left : horizontalAlign;
@@ -589,9 +608,9 @@ class Flow extends Object {
 				else if( overflow && minLineHeight != 0 )
 					maxLineHeight = minLineHeight;
 				for( i in lastIndex...maxIndex ) {
-					var p = properties[i];
+					var p = propAt(i);
 					if( p.isAbsolute ) continue;
-					var c = children[i];
+					var c = childAt(i);
 					if( !c.visible ) continue;
 					var a = p.verticalAlign != null ? p.verticalAlign : valign;
 					c.y = y + p.offsetY + p.paddingTop;
@@ -609,8 +628,8 @@ class Flow extends Object {
 			inline function remSize(from: Int) {
 				var size = 0;
 				for( j in from...children.length ) {
-					var p = properties[j];
-					if( p.isAbsolute || !children[j].visible ) continue;
+					var p = propAt(j);
+					if( p.isAbsolute || !childAt(j).visible ) continue;
 					if( p.isBreak ) break;
 					size += horizontalSpacing + p.calculatedWidth;
 				}
@@ -618,9 +637,9 @@ class Flow extends Object {
 			}
 
 			for( i in 0...children.length ) {
-				var p = properties[i];
+				var p = propAt(i);
 				if( p.isAbsolute ) continue;
-				var c = children[i];
+				var c = childAt(i);
 				if( !c.visible ) continue;
 
 				c.constraintSize(
@@ -657,8 +676,8 @@ class Flow extends Object {
 			var xmin = startX, xmax = endX;
 			var midSpace = 0;
 			for( i in 0...children.length ) {
-				var p = properties[i];
-				if( p.isAbsolute || !children[i].visible ) continue;
+				var p = propAt(i);
+				if( p.isAbsolute || !childAt(i).visible ) continue;
 				if( p.isBreak ) {
 					xmin = startX;
 					xmax = endX;
@@ -691,7 +710,7 @@ class Flow extends Object {
 					px = xmin;
 					xmin += p.calculatedWidth + horizontalSpacing;
 				}
-				children[i].x = px + p.offsetX + p.paddingLeft;
+				childAt(i).x = px + p.offsetX + p.paddingLeft;
 			}
 
 		} else {
@@ -713,9 +732,9 @@ class Flow extends Object {
 				else if( overflow && minColWidth != 0 )
 					maxColWidth = minColWidth;
 				for( i in lastIndex...maxIndex ) {
-					var p = properties[i];
+					var p = propAt(i);
 					if( p.isAbsolute ) continue;
-					var c = children[i];
+					var c = childAt(i);
 					if( !c.visible ) continue;
 					var a = p.horizontalAlign != null ? p.horizontalAlign : halign;
 					c.x = x + p.offsetX + p.paddingLeft;
@@ -733,8 +752,8 @@ class Flow extends Object {
 			inline function remSize(from: Int) {
 				var size = 0;
 				for( j in from...children.length ) {
-					var p = properties[j];
-					if( p.isAbsolute || !children[j].visible ) continue;
+					var p = propAt(j);
+					if( p.isAbsolute || !childAt(j).visible ) continue;
 					if( p.isBreak ) break;
 					size += verticalSpacing + p.calculatedHeight;
 				}
@@ -742,10 +761,10 @@ class Flow extends Object {
 			}
 
 			for( i in 0...children.length ) {
-				var p = properties[i];
+				var p = propAt(i);
 				if( p.isAbsolute ) continue;
 
-				var c = children[i];
+				var c = childAt(i);
 				if( !c.visible ) continue;
 
 				c.constraintSize(
@@ -786,8 +805,8 @@ class Flow extends Object {
 			var ymin = startY, ymax = endY;
 			var midSpace = 0;
 			for( i in 0...children.length ) {
-				var p = properties[i];
-				if( p.isAbsolute || !children[i].visible ) continue;
+				var p = propAt(i);
+				if( p.isAbsolute || !childAt(i).visible ) continue;
 				if( p.isBreak ) {
 					ymin = startY;
 					ymax = endY;
@@ -820,7 +839,7 @@ class Flow extends Object {
 					py = ymin;
 					ymin += p.calculatedHeight + verticalSpacing;
 				}
-				children[i].y = py + p.offsetY + p.paddingTop;
+				childAt(i).y = py + p.offsetY + p.paddingTop;
 			}
 		}
 
@@ -856,8 +875,8 @@ class Flow extends Object {
 			}
 			debugGraphics.lineStyle(1, 0x0080FF);
 			for( i in 0...children.length ) {
-				var p = properties[i];
-				var c = children[i];
+				var p = propAt(i);
+				var c = childAt(i);
 				if( p.isAbsolute || !c.visible ) continue;
 				debugGraphics.drawRect(c.x, c.y, p.calculatedWidth, p.calculatedHeight);
 			}