浏览代码

flow changes propagation

ncannasse 8 年之前
父节点
当前提交
00178b3266
共有 3 个文件被更改,包括 50 次插入7 次删除
  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();
 
@@ -269,6 +269,15 @@ class Flow extends Sprite {
 		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
 		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.insert(pos, fp);
 		needReflow = true;
+		s.parentContainer = this;
 	}
 
 	override public function removeChild(s:Sprite) {
@@ -443,6 +453,8 @@ class Flow extends Sprite {
 	**/
 	public function reflow() {
 
+		onBeforeReflow();
+
 		var cw, ch;
 		if( !isVertical ) {
 			var halign = horizontalAlign == null ? Left : horizontalAlign;
@@ -634,7 +646,7 @@ class Flow extends Sprite {
 				if( p.calculatedWidth > maxColWidth ) maxColWidth = p.calculatedWidth;
 			}
 			alignLine(childs.length);
-			ch += paddingTop + borderHeight;
+			ch += paddingBottom + borderHeight;
 			cw = x + maxColWidth + paddingRight + borderWidth;
 
 
@@ -730,13 +742,20 @@ class Flow extends Sprite {
 			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;
 
 	var childs : Array<Sprite>;
+	var parentContainer : Container;
 	public var parent(default, null) : Sprite;
 	public var numChildren(get, never) : Int;
 
@@ -15,7 +16,7 @@ class Sprite {
 	public var scaleX(default,set) : Float;
 	public var scaleY(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 alpha : Float = 1.;
 
@@ -199,6 +200,14 @@ class Sprite {
 		return Std.instance(p, Scene);
 	}
 
+	function set_visible(b) {
+		if( visible == b )
+			return b;
+		visible = b;
+		onContentChanged();
+		return b;
+	}
+
 	public function addChild( s : Sprite ) {
 		addChildAt(s, childs.length);
 	}
@@ -222,6 +231,7 @@ class Sprite {
 		if( !allocated && s.allocated )
 			s.onDelete();
 		s.parent = this;
+		s.parentContainer = parentContainer;
 		s.posChanged = true;
 		// ensure that proper alloc/delete is done if we change parent
 		if( allocated ) {
@@ -230,6 +240,11 @@ class Sprite {
 			else
 				s.onParentChangedRec();
 		}
+		onContentChanged();
+	}
+
+	inline function onContentChanged() {
+		if( parentContainer != null ) parentContainer.contentChanged(this);
 	}
 
 	function onParentChangedRec() {
@@ -269,7 +284,9 @@ class Sprite {
 		if( childs.remove(s) ) {
 			if( s.allocated ) s.onDelete();
 			s.parent = null;
+			s.parentContainer = null;
 			s.posChanged = true;
+			onContentChanged();
 		}
 	}
 
@@ -700,4 +717,10 @@ class Sprite {
 		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() {
 		calcDone = false;
 		if( allocated && text != null && font != null ) initGlyphs(text);
+		onContentChanged();
 	}
 
 	public function calcTextWidth( text : hxd.UString ) {