Преглед на файлове

Support FX instances in L3D

trethaller преди 7 години
родител
ревизия
2c669b65f0
променени са 6 файла, в които са добавени 102 реда и са изтрити 34 реда
  1. 31 9
      hide/comp/SceneEditor.hx
  2. 4 0
      hide/prefab/ContextShared.hx
  3. 59 19
      hide/prefab/Reference.hx
  4. 2 0
      hide/prefab/fx/FX.hx
  5. 1 1
      hide/view/FXEditor.hx
  6. 5 5
      hide/view/l3d/Level3D.hx

+ 31 - 9
hide/comp/SceneEditor.hx

@@ -256,7 +256,7 @@ class SceneEditor {
 				{ label : "Reference", enabled : current != null, click : function() createRef(current, current.parent) },
 			];
 
-			if(current != null && current.to(Object3D) != null) {
+			if(current != null && current.to(Object3D) != null && current.to(hide.prefab.Reference) == null) {
 				var visible = current.to(Object3D).visible;
 				menuItems = menuItems.concat([
 					{ label : "Visible", checked : visible, click : function() setVisible(curEdit.elements, !visible) },
@@ -774,7 +774,7 @@ class SceneEditor {
 		return localMat;
 	}
 
-	public function dropModels(paths: Array<String>, parent: PrefabElement) {
+	public function dropObjects(paths: Array<String>, parent: PrefabElement) {
 		var localMat = getPickTransform(parent);
 		if(localMat == null) return;
 
@@ -782,16 +782,26 @@ class SceneEditor {
 		localMat.ty = hxd.Math.round(localMat.ty);
 		localMat.tz = hxd.Math.round(localMat.tz);
 
-		var models: Array<PrefabElement> = [];
+		var elts: Array<PrefabElement> = [];
 		for(path in paths) {
-			var model = new hide.prefab.Model(parent);
-			model.setTransform(localMat);
+			var obj3d : Object3D;
 			var relative = ide.makeRelative(path);
-			model.source = relative;
-			autoName(model);
-			models.push(model);
+
+			if(StringTools.endsWith(path, ".fx")) {
+				var ref = new hide.prefab.Reference(parent);
+				ref.refpath = "/" + relative;
+				obj3d = ref;
+				obj3d.name = new haxe.io.Path(relative).file;
+			}
+			else {
+				obj3d = new hide.prefab.Model(parent);
+				obj3d.source = relative;
+			}
+			obj3d.setTransform(localMat);
+			autoName(obj3d);
+			elts.push(obj3d);
 		}
-		refresh(() -> selectObjects(models));
+		refresh(() -> selectObjects(elts));
 	}
 
 	function canGroupSelection() {
@@ -1004,6 +1014,18 @@ class SceneEditor {
 		var ref = new hide.prefab.Reference(toParent);
 		ref.name = elt.name;
 		ref.refpath = elt.getAbsPath();
+		var obj3d = Std.instance(elt, Object3D);
+		if(obj3d != null) {
+			ref.x = obj3d.x;
+			ref.y = obj3d.y;
+			ref.z = obj3d.z;
+			ref.scaleX = obj3d.scaleX;
+			ref.scaleY = obj3d.scaleY;
+			ref.scaleZ = obj3d.scaleZ;
+			ref.rotationX = obj3d.rotationX;
+			ref.rotationY = obj3d.rotationY;
+			ref.rotationZ = obj3d.rotationZ;
+		}
 		addObject(ref);
 	}
 

+ 4 - 0
hide/prefab/ContextShared.hx

@@ -17,6 +17,10 @@ class ContextShared extends hxd.prefab.ContextShared {
 		hide.Ide.inst.error(e);
 	}
 
+	override function loadPrefab( path : String ) : Prefab {
+		return hide.Ide.inst.loadPrefab(path);
+	}
+
 	override function loadShader( path : String ) {
 		return hide.Ide.inst.shaderLoader.loadSharedShader(path);
 	}

+ 59 - 19
hide/prefab/Reference.hx

@@ -1,6 +1,6 @@
 package hide.prefab;
 
-class Reference extends Prefab {
+class Reference extends Object3D {
 
 	public var refpath : String;
 	var ref: Prefab = null;
@@ -10,50 +10,81 @@ class Reference extends Prefab {
 		type = "reference";
 	}
 
+	public function isFile() {
+		// TODO: Use source instead?
+		return refpath != null && refpath.charAt(0) == "/";
+	}
+
 	override function save() {
-		return {
-			// Recalc abs path if ref has been resolved to supprot renaming
-			refpath: ref != null ? ref.getAbsPath() : refpath
-		};
+		var obj : Dynamic = super.save();
+		// Recalc abs path if ref has been resolved to supprot renaming
+		obj.refpath = ref != null && !isFile() ? ref.getAbsPath() : refpath;
+		return obj;
 	}
 
 	override function load( o : Dynamic ) {
+		super.load(o);
 		refpath = o.refpath;
 	}
 
-	function resolveRef() {
+	public function resolveRef(shared : hxd.prefab.ContextShared) {
 		if(ref != null)
 			return ref;
 		if(refpath == null)
 			return null;
-		var lib = getParent(hxd.prefab.Library);
-		if(lib == null)
-			return null;
-		var all = lib.getAll(Prefab);
-		for(p in all) {
-			if(!Std.is(p, Reference) && p.getAbsPath() == refpath) {
-				ref = p;
-				return ref;
+		if(isFile()) {
+			#if editor
+			if(shared == null) // Allow resolving ref in Hide prefore makeInstance 
+				ref = hide.Ide.inst.loadPrefab(refpath.substr(1));
+			else
+			#else
+			ref = shared.loadPrefab(refpath.substr(1));
+			#end
+			return ref;
+		}
+		else {
+			var lib = getParent(hxd.prefab.Library);
+			if(lib == null)
+				return null;
+			var all = lib.getAll(Prefab);
+			for(p in all) {
+				if(!Std.is(p, Reference) && p.getAbsPath() == refpath) {
+					ref = p;
+					return ref;
+				}
 			}
 		}
 		return null;
 	}
 
+	override function updateInstance( ctx: Context, ?propName : String ) {
+		var p = resolveRef(ctx.shared);
+		if(p == null)
+			return;
+		var parentCtx = ctx.shared.contexts.get(parent);
+		if(parentCtx != null && parentCtx.local3d != ctx.local3d) {
+			super.updateInstance(ctx, propName);
+		}
+	}
+
 	override function makeInstance(ctx: Context) : Context {
-		var p = resolveRef();
+		var p = resolveRef(ctx.shared);
 		if(p == null)
 			return ctx;
 
 		ctx = ctx.clone(this);
 		ctx.isRef = true;
-		return p.makeInstance(ctx);
+		var refCtx = p.makeInstance(ctx);
+		ctx.local3d = refCtx.local3d;
+		updateInstance(ctx);
+		return ctx;
 	}
 
 	override function to<T:Prefab>( c : Class<T> ) : Null<T> {
 		var base = super.to(c);
 		if(base != null)
 			return base;
-		var p = resolveRef();
+		var p = resolveRef(null);
 		if(p == null) return null;
 		return Std.instance(p, c);
 	}
@@ -63,13 +94,15 @@ class Reference extends Prefab {
 
 	override function edit( ctx : EditContext ) {
 		var element = new hide.Element('
+			<div class="group" name="Reference">
 			<dl>
 				<dt>Reference</dt><dd><input type="text" field="refpath"/></dd>
-			</dl>');
+			</dl>
+			</div>');
 
 		function updateProps() {
 			var input = element.find("input");
-			var found = resolveRef() != null;
+			var found = resolveRef(ctx.rootContext.shared) != null;
 			input.toggleClass("error", !found);
 		}
 		updateProps();
@@ -83,6 +116,13 @@ class Reference extends Prefab {
 					ctx.rebuildPrefab(this);
 			}
 		});
+		
+		var parentCtx = ctx.getContext(parent);
+		var selfCtx = ctx.getContext(this);
+		var p = resolveRef(ctx.rootContext.shared);
+		if(selfCtx != null && parentCtx != null && parentCtx.local3d != selfCtx.local3d) {
+			super.edit(ctx);
+		}
 	}
 
 	override function getHideProps() : HideProps {

+ 2 - 0
hide/prefab/fx/FX.hx

@@ -51,6 +51,7 @@ class FXAnimation extends h3d.scene.Object {
 	public var objects: Array<ObjectAnimation> = [];
 	public var shaderAnims : Array<ShaderAnimation> = [];
 	public var emitters : Array<hide.prefab.fx.Emitter.EmitterObject> = [];
+	public var currentTime(default, null) : Float = 0.0;
 	var evaluator : Evaluator;
 	var random : hxd.Rand;
 
@@ -69,6 +70,7 @@ class FXAnimation extends h3d.scene.Object {
 
 	static var tempMat = new h3d.Matrix();
 	public function setTime(time: Float) {
+		currentTime = time;
 		for(anim in objects) {
 			var m = tempMat;
 			if(anim.scale != null) {

+ 1 - 1
hide/view/FXEditor.hx

@@ -371,7 +371,7 @@ class FXEditor extends FileView {
 		if(models.length > 0) {
 			if(isDrop) {
 				var parent : PrefabElement = data;
-				sceneEditor.dropModels(models, parent);
+				sceneEditor.dropObjects(models, parent);
 			}
 			return true;
 		}

+ 5 - 5
hide/view/l3d/Level3D.hx

@@ -458,15 +458,15 @@ class Level3D extends FileView {
 	}
 
 	override function onDragDrop(items : Array<String>, isDrop : Bool) {
-		var supported = ["fbx"];
-		var models = [];
+		var supported = ["fbx", "fx"];
+		var paths = [];
 		for(path in items) {
 			var ext = haxe.io.Path.extension(path).toLowerCase();
 			if(supported.indexOf(ext) >= 0) {
-				models.push(path);
+				paths.push(path);
 			}
 		}
-		if(models.length > 0) {
+		if(paths.length > 0) {
 			if(isDrop) {
 				var curSel = sceneEditor.getSelection();
 				var parent : PrefabElement = data;
@@ -484,7 +484,7 @@ class Level3D extends FileView {
 							parent = curLayer;
 					}
 				}
-				sceneEditor.dropModels(models, parent);
+				sceneEditor.dropObjects(paths, parent);
 			}
 			return true;
 		}