Reference.hx 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. package hrt.prefab;
  2. class Reference extends Object3D {
  3. @:s var editMode : Bool = false;
  4. public var ref: Prefab = null;
  5. public function new(?parent) {
  6. super(parent);
  7. type = "reference";
  8. }
  9. override function load(v:Dynamic) {
  10. super.load(v);
  11. // backward compatibility
  12. var old : String = v.refpath;
  13. if( old != null ) {
  14. source = old.charCodeAt(0) == "/".code ? old.substr(1) : "/"+old;
  15. }
  16. }
  17. override function save() {
  18. var obj : Dynamic = super.save();
  19. #if editor
  20. if( editMode && ref != null )
  21. hide.Ide.inst.savePrefab(source, ref);
  22. #end
  23. return obj;
  24. }
  25. public function resolveRef(shared : hrt.prefab.ContextShared) {
  26. if(ref != null)
  27. return ref;
  28. if(source == null)
  29. return null;
  30. if(shared == null) { // Allow resolving ref in Hide prefore makeInstance
  31. #if editor
  32. ref = hide.Ide.inst.loadPrefab(source, null, true);
  33. #else
  34. return null;
  35. #end
  36. }
  37. else
  38. ref = shared.loadPrefab(source);
  39. return ref;
  40. }
  41. override function updateInstance( ctx: Context, ?propName : String ) {
  42. var p = resolveRef(ctx.shared);
  43. if(p == null)
  44. return;
  45. var parentCtx = parent == null ? null : ctx.shared.contexts.get(parent);
  46. if(parentCtx == null || parentCtx.local3d != ctx.local3d) {
  47. // Only apply reference Object3D properties (pos, scale...) to own local3D
  48. // Not all refs will create their own scene object
  49. super.updateInstance(ctx, propName);
  50. }
  51. }
  52. override function find<T>( f : Prefab -> Null<T>, ?followRefs : Bool ) : T {
  53. if( followRefs && ref != null ) {
  54. var v = ref.find(f, followRefs);
  55. if( v != null ) return v;
  56. }
  57. return super.find(f, followRefs);
  58. }
  59. override function findAll<T>( f : Prefab -> Null<T>, ?followRefs : Bool, ?arr : Array<T> ) : Array<T> {
  60. if( followRefs && ref != null )
  61. arr = ref.findAll(f, followRefs, arr);
  62. return super.findAll(f, followRefs, arr);
  63. }
  64. override function getOpt<T:Prefab>( cl : Class<T>, ?name : String, ?followRefs ) : T {
  65. if( followRefs && ref != null ) {
  66. var v = ref.getOpt(cl, name, true);
  67. if( v != null )
  68. return v;
  69. }
  70. return super.getOpt(cl, name, followRefs);
  71. }
  72. override function makeInstance(ctx: Context) : Context {
  73. var p = resolveRef(ctx.shared);
  74. if(p == null)
  75. return ctx;
  76. ctx = super.makeInstance(ctx);
  77. var prevShared = ctx.shared;
  78. ctx.shared = ctx.shared.cloneRef(this, source);
  79. makeChildren(ctx, p);
  80. ctx.shared = prevShared;
  81. #if editor
  82. if (ctx.local2d == null) {
  83. var path = hide.Ide.inst.appPath + "/res/icons/fileRef.png";
  84. var data = sys.io.File.getBytes(path);
  85. var tile = hxd.res.Any.fromBytes(path, data).toTile().center();
  86. var objFollow = new h2d.ObjectFollower(ctx.local3d, ctx.shared.root2d);
  87. objFollow.followVisibility = true;
  88. var bmp = new h2d.Bitmap(tile, objFollow);
  89. ctx.local2d = objFollow;
  90. }
  91. #end
  92. return ctx;
  93. }
  94. override function removeInstance(ctx:Context):Bool {
  95. if(!super.removeInstance(ctx))
  96. return false;
  97. if(ctx.local2d != null)
  98. ctx.local2d.remove();
  99. return true;
  100. }
  101. override function to<T:Prefab>( c : Class<T> ) : Null<T> {
  102. var base = super.to(c);
  103. if(base != null)
  104. return base;
  105. var p = resolveRef(null);
  106. if(p == null) return null;
  107. return Std.downcast(p, c);
  108. }
  109. #if editor
  110. override function makeInteractive(ctx) {
  111. if( editMode )
  112. return null;
  113. return super.makeInteractive(ctx);
  114. }
  115. override function edit( ctx : EditContext ) {
  116. var element = new hide.Element('
  117. <div class="group" name="Reference">
  118. <dl>
  119. <dt>Reference</dt><dd><input type="fileselect" extensions="prefab l3d" field="source"/></dd>
  120. <dt>Edit</dt><dd><input type="checkbox" field="editMode"/></dd>
  121. </dl>
  122. </div>');
  123. function updateProps() {
  124. var input = element.find("input");
  125. var found = resolveRef(ctx.rootContext.shared) != null;
  126. input.toggleClass("error", !found);
  127. }
  128. updateProps();
  129. var props = ctx.properties.add(element, this, function(pname) {
  130. ctx.onChange(this, pname);
  131. if(pname == "source" || pname == "editMode") {
  132. ref = null;
  133. updateProps();
  134. if(!ctx.properties.isTempChange)
  135. ctx.rebuildPrefab(this);
  136. }
  137. });
  138. super.edit(ctx);
  139. }
  140. override function getHideProps() : HideProps {
  141. return { icon : "share", name : "Reference" };
  142. }
  143. #end
  144. static var _ = Library.register("reference", Reference);
  145. }