Jelajahi Sumber

flow changes propagation

ncannasse 8 tahun lalu
induk
melakukan
00178b3266
3 mengubah file dengan 50 tambahan dan 7 penghapusan
  1. 24 5
      h2d/Flow.hx
  2. 25 2
      h2d/Sprite.hx
  3. 1 0
      h2d/Text.hx

+ 24 - 5
h2d/Flow.hx

@@ -40,7 +40,7 @@ class FlowProperties {
 
 
 }
 }
 
 
-class Flow extends Sprite {
+class Flow extends Sprite.Container {
 
 
 	static var tmpBounds = new h2d.col.Bounds();
 	static var tmpBounds = new h2d.col.Bounds();
 
 
@@ -269,6 +269,15 @@ class Flow extends Sprite {
 		return paddingBottom = v;
 		return paddingBottom = v;
 	}
 	}
 
 
+	override function contentChanged( s : Sprite ) {
+		while( s.parent != this )
+			s = s.parent;
+		if( getProperties(s).isAbsolute )
+			return;
+		needReflow = true;
+		onContentChanged();
+	}
+
 	/**
 	/**
 		Adds some spacing by either increasing the padding of the latest
 		Adds some spacing by either increasing the padding of the latest
 		non absolute element or the padding of the flow if no element was found.
 		non absolute element or the padding of the flow if no element was found.
@@ -318,6 +327,7 @@ class Flow extends Sprite {
 			properties.remove(fp);
 			properties.remove(fp);
 		properties.insert(pos, fp);
 		properties.insert(pos, fp);
 		needReflow = true;
 		needReflow = true;
+		s.parentContainer = this;
 	}
 	}
 
 
 	override public function removeChild(s:Sprite) {
 	override public function removeChild(s:Sprite) {
@@ -443,6 +453,8 @@ class Flow extends Sprite {
 	**/
 	**/
 	public function reflow() {
 	public function reflow() {
 
 
+		onBeforeReflow();
+
 		var cw, ch;
 		var cw, ch;
 		if( !isVertical ) {
 		if( !isVertical ) {
 			var halign = horizontalAlign == null ? Left : horizontalAlign;
 			var halign = horizontalAlign == null ? Left : horizontalAlign;
@@ -634,7 +646,7 @@ class Flow extends Sprite {
 				if( p.calculatedWidth > maxColWidth ) maxColWidth = p.calculatedWidth;
 				if( p.calculatedWidth > maxColWidth ) maxColWidth = p.calculatedWidth;
 			}
 			}
 			alignLine(childs.length);
 			alignLine(childs.length);
-			ch += paddingTop + borderHeight;
+			ch += paddingBottom + borderHeight;
 			cw = x + maxColWidth + paddingRight + borderWidth;
 			cw = x + maxColWidth + paddingRight + borderWidth;
 
 
 
 
@@ -730,13 +742,20 @@ class Flow extends Sprite {
 			debugGraphics.drawRect(0, 0, cw, ch);
 			debugGraphics.drawRect(0, 0, cw, ch);
 		}
 		}
 
 
-		onReflow();
+		onContentChanged();
+		onAfterReflow();
+	}
+
+	/**
+		Called before each reflow() is done.
+	**/
+	public dynamic function onBeforeReflow() {
 	}
 	}
 
 
 	/**
 	/**
-		Called each time a reflow() was done.
+		Called after each time a reflow() was done.
 	**/
 	**/
-	public dynamic function onReflow() {
+	public dynamic function onAfterReflow() {
 	}
 	}
 
 
 }
 }

+ 25 - 2
h2d/Sprite.hx

@@ -7,6 +7,7 @@ class Sprite {
 	static var nullDrawable : h2d.Drawable;
 	static var nullDrawable : h2d.Drawable;
 
 
 	var childs : Array<Sprite>;
 	var childs : Array<Sprite>;
+	var parentContainer : Container;
 	public var parent(default, null) : Sprite;
 	public var parent(default, null) : Sprite;
 	public var numChildren(get, never) : Int;
 	public var numChildren(get, never) : Int;
 
 
@@ -15,7 +16,7 @@ class Sprite {
 	public var scaleX(default,set) : Float;
 	public var scaleX(default,set) : Float;
 	public var scaleY(default,set) : Float;
 	public var scaleY(default,set) : Float;
 	public var rotation(default, set) : Float;
 	public var rotation(default, set) : Float;
-	public var visible : Bool;
+	public var visible(default, set) : Bool;
 	public var name : String;
 	public var name : String;
 	public var alpha : Float = 1.;
 	public var alpha : Float = 1.;
 
 
@@ -199,6 +200,14 @@ class Sprite {
 		return Std.instance(p, Scene);
 		return Std.instance(p, Scene);
 	}
 	}
 
 
+	function set_visible(b) {
+		if( visible == b )
+			return b;
+		visible = b;
+		onContentChanged();
+		return b;
+	}
+
 	public function addChild( s : Sprite ) {
 	public function addChild( s : Sprite ) {
 		addChildAt(s, childs.length);
 		addChildAt(s, childs.length);
 	}
 	}
@@ -222,6 +231,7 @@ class Sprite {
 		if( !allocated && s.allocated )
 		if( !allocated && s.allocated )
 			s.onDelete();
 			s.onDelete();
 		s.parent = this;
 		s.parent = this;
+		s.parentContainer = parentContainer;
 		s.posChanged = true;
 		s.posChanged = true;
 		// ensure that proper alloc/delete is done if we change parent
 		// ensure that proper alloc/delete is done if we change parent
 		if( allocated ) {
 		if( allocated ) {
@@ -230,6 +240,11 @@ class Sprite {
 			else
 			else
 				s.onParentChangedRec();
 				s.onParentChangedRec();
 		}
 		}
+		onContentChanged();
+	}
+
+	inline function onContentChanged() {
+		if( parentContainer != null ) parentContainer.contentChanged(this);
 	}
 	}
 
 
 	function onParentChangedRec() {
 	function onParentChangedRec() {
@@ -269,7 +284,9 @@ class Sprite {
 		if( childs.remove(s) ) {
 		if( childs.remove(s) ) {
 			if( s.allocated ) s.onDelete();
 			if( s.allocated ) s.onDelete();
 			s.parent = null;
 			s.parent = null;
+			s.parentContainer = null;
 			s.posChanged = true;
 			s.posChanged = true;
+			onContentChanged();
 		}
 		}
 	}
 	}
 
 
@@ -700,4 +717,10 @@ class Sprite {
 		return name == null ? c : name + "(" + c + ")";
 		return name == null ? c : name + "(" + c + ")";
 	}
 	}
 
 
-}
+}
+
+class Container extends Sprite {
+	@:noCompletion public function contentChanged( s : Sprite ) {
+	}
+}
+

+ 1 - 0
h2d/Text.hx

@@ -120,6 +120,7 @@ class Text extends Drawable {
 	function rebuild() {
 	function rebuild() {
 		calcDone = false;
 		calcDone = false;
 		if( allocated && text != null && font != null ) initGlyphs(text);
 		if( allocated && text != null && font != null ) initGlyphs(text);
+		onContentChanged();
 	}
 	}
 
 
 	public function calcTextWidth( text : hxd.UString ) {
 	public function calcTextWidth( text : hxd.UString ) {