Răsfoiți Sursa

onAlloc/onDelete becomes onAdd/onRemove + onParentChangedRec becomes onParentChanged (same as onAdd/onRemove)
fixed SpriteBatch missing super.onRemove()

ncannasse 8 ani în urmă
părinte
comite
8f097cf52b
14 a modificat fișierele cu 59 adăugiri și 59 ștergeri
  1. 4 4
      h2d/Console.hx
  2. 2 2
      h2d/Graphics.hx
  3. 5 4
      h2d/Interactive.hx
  4. 1 1
      h2d/Layers.hx
  5. 3 2
      h2d/Mask.hx
  6. 2 2
      h2d/Scene.hx
  7. 15 16
      h2d/Sprite.hx
  8. 2 1
      h2d/SpriteBatch.hx
  9. 2 2
      h2d/Text.hx
  10. 2 2
      h2d/TileGroup.hx
  11. 2 2
      h3d/impl/Benchmark.hx
  12. 4 4
      h3d/scene/Interactive.hx
  13. 14 16
      h3d/scene/Object.hx
  14. 1 1
      h3d/scene/Scene.hx

+ 4 - 4
h2d/Console.hx

@@ -68,14 +68,14 @@ class Console extends h2d.Sprite {
 		handleCommand(commandLine);
 		handleCommand(commandLine);
 	}
 	}
 
 
-	override function onAlloc() {
-		super.onAlloc();
+	override function onAdd() {
+		super.onAdd();
 		hxd.Stage.getInstance().addEventTarget(onEvent);
 		hxd.Stage.getInstance().addEventTarget(onEvent);
 	}
 	}
 
 
-	override function onDelete() {
+	override function onRemove() {
 		hxd.Stage.getInstance().removeEventTarget(onEvent);
 		hxd.Stage.getInstance().removeEventTarget(onEvent);
-		super.onDelete();
+		super.onRemove();
 	}
 	}
 
 
 	function onEvent( e : hxd.Event ) {
 	function onEvent( e : hxd.Event ) {

+ 2 - 2
h2d/Graphics.hx

@@ -137,8 +137,8 @@ class Graphics extends Drawable {
 		clear();
 		clear();
 	}
 	}
 
 
-	override function onDelete() {
-		super.onDelete();
+	override function onRemove() {
+		super.onRemove();
 		clear();
 		clear();
 	}
 	}
 
 

+ 5 - 4
h2d/Interactive.hx

@@ -27,11 +27,11 @@ class Interactive extends Drawable implements hxd.SceneEvents.Interactive {
 		cursor = Button;
 		cursor = Button;
 	}
 	}
 
 
-	override function onAlloc() {
+	override function onAdd() {
 		this.scene = getScene();
 		this.scene = getScene();
 		if( scene != null ) scene.addEventTarget(this);
 		if( scene != null ) scene.addEventTarget(this);
 		updateMask();
 		updateMask();
-		super.onAlloc();
+		super.onAdd();
 	}
 	}
 
 
 	override function draw( ctx : RenderContext ) {
 	override function draw( ctx : RenderContext ) {
@@ -44,6 +44,7 @@ class Interactive extends Drawable implements hxd.SceneEvents.Interactive {
 	}
 	}
 
 
 	override function onParentChanged() {
 	override function onParentChanged() {
+		super.onParentChanged();
 		if( scene != null ) {
 		if( scene != null ) {
 			scene.removeEventTarget(this);
 			scene.removeEventTarget(this);
 			scene.addEventTarget(this);
 			scene.addEventTarget(this);
@@ -64,12 +65,12 @@ class Interactive extends Drawable implements hxd.SceneEvents.Interactive {
 		}
 		}
 	}
 	}
 
 
-	override function onDelete() {
+	override function onRemove() {
 		if( scene != null ) {
 		if( scene != null ) {
 			scene.removeEventTarget(this, true);
 			scene.removeEventTarget(this, true);
 			scene = null;
 			scene = null;
 		}
 		}
-		super.onDelete();
+		super.onRemove();
 	}
 	}
 
 
 	function checkBounds( e : hxd.Event ) {
 	function checkBounds( e : hxd.Event ) {

+ 1 - 1
h2d/Layers.hx

@@ -39,7 +39,7 @@ class Layers extends Sprite {
 		for( i in 0...childs.length ) {
 		for( i in 0...childs.length ) {
 			if( childs[i] == s ) {
 			if( childs[i] == s ) {
 				childs.splice(i, 1);
 				childs.splice(i, 1);
-				if( s.allocated ) s.onDelete();
+				if( s.allocated ) s.onRemove();
 				s.parent = null;
 				s.parent = null;
 				var k = layerCount - 1;
 				var k = layerCount - 1;
 				while( k >= 0 && layersIndexes[k] > i ) {
 				while( k >= 0 && layersIndexes[k] > i ) {

+ 3 - 2
h2d/Mask.hx

@@ -13,11 +13,12 @@ class Mask extends Sprite {
 	}
 	}
 
 
 	override function onParentChanged() {
 	override function onParentChanged() {
+		super.onParentChanged();
 		updateMask();
 		updateMask();
 	}
 	}
 
 
-	override function onAlloc() {
-		super.onAlloc();
+	override function onAdd() {
+		super.onAdd();
 		updateMask();
 		updateMask();
 	}
 	}
 
 

+ 2 - 2
h2d/Scene.hx

@@ -351,7 +351,7 @@ class Scene extends Layers implements h3d.IDrawable implements hxd.SceneEvents.I
 
 
 	public function dispose() {
 	public function dispose() {
 		if( allocated )
 		if( allocated )
-			onDelete();
+			onRemove();
 		ctx.dispose();
 		ctx.dispose();
 	}
 	}
 
 
@@ -386,7 +386,7 @@ class Scene extends Layers implements h3d.IDrawable implements hxd.SceneEvents.I
 
 
 	override function sync( ctx : RenderContext ) {
 	override function sync( ctx : RenderContext ) {
 		if( !allocated )
 		if( !allocated )
-			onAlloc();
+			onAdd();
 		if( !fixedSize && (width != ctx.engine.width || height != ctx.engine.height) ) {
 		if( !fixedSize && (width != ctx.engine.width || height != ctx.engine.height) ) {
 			width = ctx.engine.width;
 			width = ctx.engine.width;
 			height = ctx.engine.height;
 			height = ctx.engine.height;

+ 15 - 16
h2d/Sprite.hx

@@ -221,7 +221,7 @@ class Sprite {
 			p = p.parent;
 			p = p.parent;
 		}
 		}
 		if( s.parent != null ) {
 		if( s.parent != null ) {
-			// prevent calling onDelete
+			// prevent calling onRemove
 			var old = s.allocated;
 			var old = s.allocated;
 			s.allocated = false;
 			s.allocated = false;
 			s.parent.removeChild(s);
 			s.parent.removeChild(s);
@@ -229,16 +229,16 @@ class Sprite {
 		}
 		}
 		childs.insert(pos, s);
 		childs.insert(pos, s);
 		if( !allocated && s.allocated )
 		if( !allocated && s.allocated )
-			s.onDelete();
+			s.onRemove();
 		s.parent = this;
 		s.parent = this;
 		s.parentContainer = parentContainer;
 		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 ) {
 			if( !s.allocated )
 			if( !s.allocated )
-				s.onAlloc();
+				s.onAdd();
 			else
 			else
-				s.onParentChangedRec();
+				s.onParentChanged();
 		}
 		}
 		onContentChanged();
 		onContentChanged();
 	}
 	}
@@ -247,28 +247,24 @@ class Sprite {
 		if( parentContainer != null ) parentContainer.contentChanged(this);
 		if( parentContainer != null ) parentContainer.contentChanged(this);
 	}
 	}
 
 
-	function onParentChangedRec() {
-		onParentChanged();
-		for( c in childs )
-			c.onParentChangedRec();
-	}
-
 	// called when we're allocated already but moved in hierarchy
 	// called when we're allocated already but moved in hierarchy
 	function onParentChanged() {
 	function onParentChanged() {
+		for( c in childs )
+			c.onParentChanged();
 	}
 	}
 
 
 	// kept for internal init
 	// kept for internal init
-	function onAlloc() {
+	function onAdd() {
 		allocated = true;
 		allocated = true;
 		for( c in childs )
 		for( c in childs )
-			c.onAlloc();
+			c.onAdd();
 	}
 	}
 
 
 	// kept for internal cleanup
 	// kept for internal cleanup
-	function onDelete() {
+	function onRemove() {
 		allocated = false;
 		allocated = false;
 		for( c in childs )
 		for( c in childs )
-			c.onDelete();
+			c.onRemove();
 	}
 	}
 
 
 	function getMatrix( m : h2d.col.Matrix ) {
 	function getMatrix( m : h2d.col.Matrix ) {
@@ -282,7 +278,7 @@ class Sprite {
 
 
 	public function removeChild( s : Sprite ) {
 	public function removeChild( s : Sprite ) {
 		if( childs.remove(s) ) {
 		if( childs.remove(s) ) {
-			if( s.allocated ) s.onDelete();
+			if( s.allocated ) s.onRemove();
 			s.parent = null;
 			s.parent = null;
 			if( s.parentContainer != null ) s.setParentContainer(null);
 			if( s.parentContainer != null ) s.setParentContainer(null);
 			s.posChanged = true;
 			s.posChanged = true;
@@ -301,7 +297,10 @@ class Sprite {
 			removeChild( getChildAt(0) );
 			removeChild( getChildAt(0) );
 	}
 	}
 
 
-	// shortcut for parent.removeChild
+	/**
+		Same as parent.removeChild(this), but does nothing if parent is null.
+		In order to capture add/removal from scene, you can override onAdd/onRemove/onParentChanged
+	**/
 	public inline function remove() {
 	public inline function remove() {
 		if( this != null && parent != null ) parent.removeChild(this);
 		if( this != null && parent != null ) parent.removeChild(this);
 	}
 	}

+ 2 - 1
h2d/SpriteBatch.hx

@@ -294,7 +294,8 @@ class SpriteBatch extends Drawable {
 		return new ElementsIterator(first);
 		return new ElementsIterator(first);
 	}
 	}
 
 
-	override function onDelete()  {
+	override function onRemove()  {
+		super.onRemove();
 		if( buffer != null ) {
 		if( buffer != null ) {
 			buffer.dispose();
 			buffer.dispose();
 			buffer = null;
 			buffer = null;

+ 2 - 2
h2d/Text.hx

@@ -87,8 +87,8 @@ class Text extends Drawable {
 		updateConstraint();
 		updateConstraint();
 	}
 	}
 
 
-	override function onAlloc() {
-		super.onAlloc();
+	override function onAdd() {
+		super.onAdd();
 		rebuild();
 		rebuild();
 	}
 	}
 
 

+ 2 - 2
h2d/TileGroup.hx

@@ -423,9 +423,9 @@ class TileGroup extends Drawable {
 		return content.triCount() >> 1;
 		return content.triCount() >> 1;
 	}
 	}
 
 
-	override function onDelete() {
+	override function onRemove() {
 		content.dispose();
 		content.dispose();
-		super.onDelete();
+		super.onRemove();
 	}
 	}
 
 
 	public function setDefaultColor( rgb : Int, alpha = 1.0 ) {
 	public function setDefaultColor( rgb : Int, alpha = 1.0 ) {

+ 2 - 2
h3d/impl/Benchmark.hx

@@ -124,8 +124,8 @@ class Benchmark extends h2d.Graphics {
 		if( interact != null ) interact.width = interact.height = 0;
 		if( interact != null ) interact.width = interact.height = 0;
 	}
 	}
 
 
-	override function onDelete() {
-		super.onDelete();
+	override function onRemove() {
+		super.onRemove();
 		cleanup();
 		cleanup();
 	}
 	}
 
 

+ 4 - 4
h3d/scene/Interactive.hx

@@ -25,18 +25,18 @@ class Interactive extends Object implements hxd.SceneEvents.Interactive {
 		cursor = Button;
 		cursor = Button;
 	}
 	}
 
 
-	override function onAlloc() {
+	override function onAdd() {
 		this.scene = getScene();
 		this.scene = getScene();
 		if( scene != null ) scene.addEventTarget(this);
 		if( scene != null ) scene.addEventTarget(this);
-		super.onAlloc();
+		super.onAdd();
 	}
 	}
 
 
-	override function onDelete() {
+	override function onRemove() {
 		if( scene != null ) {
 		if( scene != null ) {
 			scene.removeEventTarget(this);
 			scene.removeEventTarget(this);
 			scene = null;
 			scene = null;
 		}
 		}
-		super.onDelete();
+		super.onRemove();
 	}
 	}
 
 
 	/**
 	/**

+ 14 - 16
h3d/scene/Object.hx

@@ -287,15 +287,15 @@ class Object {
 		}
 		}
 		childs.insert(pos, o);
 		childs.insert(pos, o);
 		if( !allocated && o.allocated )
 		if( !allocated && o.allocated )
-			o.onDelete();
+			o.onRemove();
 		o.parent = this;
 		o.parent = this;
 		o.posChanged = true;
 		o.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 ) {
 			if( !o.allocated )
 			if( !o.allocated )
-				o.onAlloc();
+				o.onAdd();
 			else
 			else
-				o.onParentChangedRec();
+				o.onParentChanged();
 		}
 		}
 	}
 	}
 
 
@@ -310,33 +310,28 @@ class Object {
 			o.iterVisibleMeshes(callb);
 			o.iterVisibleMeshes(callb);
 	}
 	}
 
 
-	function onParentChangedRec() {
-		onParentChanged();
-		for( c in childs )
-			c.onParentChangedRec();
-	}
-
-	// called when we're allocated already but moved in hierarchy
 	function onParentChanged() {
 	function onParentChanged() {
+		for( c in childs )
+			c.onParentChanged();
 	}
 	}
 
 
 	// kept for internal init
 	// kept for internal init
-	function onAlloc() {
+	function onAdd() {
 		allocated = true;
 		allocated = true;
 		for( c in childs )
 		for( c in childs )
-			c.onAlloc();
+			c.onAdd();
 	}
 	}
 
 
 	// kept for internal cleanup
 	// kept for internal cleanup
-	function onDelete() {
+	function onRemove() {
 		allocated = false;
 		allocated = false;
 		for( c in childs )
 		for( c in childs )
-			c.onDelete();
+			c.onRemove();
 	}
 	}
 
 
 	public function removeChild( o : Object ) {
 	public function removeChild( o : Object ) {
 		if( childs.remove(o) ) {
 		if( childs.remove(o) ) {
-			if( o.allocated ) o.onDelete();
+			if( o.allocated ) o.onRemove();
 			o.parent = null;
 			o.parent = null;
 			o.posChanged = true;
 			o.posChanged = true;
 		}
 		}
@@ -367,7 +362,10 @@ class Object {
 		throw this + " is not a Mesh";
 		throw this + " is not a Mesh";
 	}
 	}
 
 
-	// shortcut for parent.removeChild
+	/**
+		Same as parent.removeChild(this), but does nothing if parent is null.
+		In order to capture add/removal from scene, you can override onAdd/onRemove/onParentChanged
+	**/
 	public inline function remove() {
 	public inline function remove() {
 		if( this != null && parent != null ) parent.removeChild(this);
 		if( this != null && parent != null ) parent.removeChild(this);
 	}
 	}

+ 1 - 1
h3d/scene/Scene.hx

@@ -284,7 +284,7 @@ class Scene extends Object implements h3d.IDrawable implements hxd.SceneEvents.I
 	public function render( engine : h3d.Engine ) {
 	public function render( engine : h3d.Engine ) {
 
 
 		if( !allocated )
 		if( !allocated )
-			onAlloc();
+			onAdd();
 
 
 		var t = engine.getCurrentTarget();
 		var t = engine.getCurrentTarget();
 		if( t == null )
 		if( t == null )