Browse Source

added followRefs on prefab apis

Nicolas Cannasse 4 years ago
parent
commit
9350fffd41
2 changed files with 31 additions and 8 deletions
  1. 8 8
      hrt/prefab/Prefab.hx
  2. 23 0
      hrt/prefab/Reference.hx

+ 8 - 8
hrt/prefab/Prefab.hx

@@ -327,13 +327,13 @@ class Prefab {
 	/**
 	/**
 		Simlar to get() but returns null if not found.
 		Simlar to get() but returns null if not found.
 	**/
 	**/
-	public function getOpt<T:Prefab>( cl : Class<T>, ?name : String ) : T {
+	public function getOpt<T:Prefab>( cl : Class<T>, ?name : String, ?followRefs : Bool ) : T {
 		if( name == null || this.name == name ) {
 		if( name == null || this.name == name ) {
 			var cval = to(cl);
 			var cval = to(cl);
 			if( cval != null ) return cval;
 			if( cval != null ) return cval;
 		}
 		}
 		for( c in children ) {
 		for( c in children ) {
-			var p = c.getOpt(cl, name);
+			var p = c.getOpt(cl, name, followRefs);
 			if( p != null )
 			if( p != null )
 				return p;
 				return p;
 		}
 		}
@@ -354,19 +354,19 @@ class Prefab {
 	/**
 	/**
 		Return all prefabs in the tree matching the given prefab class.
 		Return all prefabs in the tree matching the given prefab class.
 	**/
 	**/
-	public function getAll<T:Prefab>( cl : Class<T>, ?arr: Array<T> ) : Array<T> {
-		return findAll(function(p) return p.to(cl));
+	public function getAll<T:Prefab>( cl : Class<T>, ?followRefs : Bool, ?arr: Array<T> ) : Array<T> {
+		return findAll(function(p) return p.to(cl), followRefs, arr);
 	}
 	}
 
 
 	/**
 	/**
 		Find a single prefab in the tree by calling `f` on each and returning the first not-null value returned, or null if not found.
 		Find a single prefab in the tree by calling `f` on each and returning the first not-null value returned, or null if not found.
 	**/
 	**/
-	public function find<T>( f : Prefab -> Null<T> ) : Null<T> {
+	public function find<T>( f : Prefab -> Null<T>, ?followRefs : Bool ) : Null<T> {
 		var v = f(this);
 		var v = f(this);
 		if( v != null )
 		if( v != null )
 			return v;
 			return v;
 		for( p in children ) {
 		for( p in children ) {
-			var v = p.find(f);
+			var v = p.find(f, followRefs);
 			if( v != null ) return v;
 			if( v != null ) return v;
 		}
 		}
 		return null;
 		return null;
@@ -375,13 +375,13 @@ class Prefab {
 	/**
 	/**
 		Find several prefabs in the tree by calling `f` on each and returning all the not-null values returned.
 		Find several prefabs in the tree by calling `f` on each and returning all the not-null values returned.
 	**/
 	**/
-	public function findAll<T>( f : Prefab -> Null<T>, ?arr : Array<T> ) : Array<T> {
+	public function findAll<T>( f : Prefab -> Null<T>, ?followRefs : Bool, ?arr : Array<T> ) : Array<T> {
 		if( arr == null ) arr = [];
 		if( arr == null ) arr = [];
 		var v = f(this);
 		var v = f(this);
 		if( v != null )
 		if( v != null )
 			arr.push(v);
 			arr.push(v);
 		for( o in children )
 		for( o in children )
-			o.findAll(f,arr);
+			o.findAll(f,followRefs,arr);
 		return arr;
 		return arr;
 	}
 	}
 
 

+ 23 - 0
hrt/prefab/Reference.hx

@@ -80,6 +80,29 @@ class Reference extends Object3D {
 		}
 		}
 	}
 	}
 
 
+	override function find<T>( f : Prefab -> Null<T>, ?followRefs : Bool ) : T {
+		if( followRefs && ref != null ) {
+			var v = ref.find(f, followRefs);
+			if( v != null ) return v;
+		}
+		return super.find(f, followRefs);
+	}
+
+	override function findAll<T>( f : Prefab -> Null<T>, ?followRefs : Bool, ?arr : Array<T> ) : Array<T> {
+		if( followRefs && ref != null )
+			arr = ref.findAll(f, followRefs, arr);
+		return super.findAll(f, followRefs, arr);
+	}
+
+	override function getOpt<T:Prefab>( cl : Class<T>, ?name : String, ?followRefs ) : T {
+		if( followRefs && ref != null ) {
+			var v = ref.getOpt(cl, name, true);
+			if( v != null )
+				return v;
+		}
+		return super.getOpt(cl, name, followRefs);
+	}
+
 	override function makeInstance(ctx: Context) : Context {
 	override function makeInstance(ctx: Context) : Context {
 		var p = resolveRef(ctx.shared);
 		var p = resolveRef(ctx.shared);
 		if(p == null)
 		if(p == null)