Reference.hx 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. package hrt.prefab;
  2. class Reference extends Object3D {
  3. public var refpath : String;
  4. var ref: Prefab = null;
  5. public function new(?parent) {
  6. super(parent);
  7. type = "reference";
  8. }
  9. public function isFile() {
  10. // TODO: Use source instead?
  11. return refpath != null && refpath.charAt(0) == "/";
  12. }
  13. override function save() {
  14. var obj : Dynamic = super.save();
  15. // Recalc abs path if ref has been resolved to supprot renaming
  16. obj.refpath = ref != null && !isFile() ? ref.getAbsPath() : refpath;
  17. return obj;
  18. }
  19. override function load( o : Dynamic ) {
  20. super.load(o);
  21. refpath = o.refpath;
  22. }
  23. public function resolveRef(shared : hrt.prefab.ContextShared) {
  24. if(ref != null)
  25. return ref;
  26. if(refpath == null)
  27. return null;
  28. if(isFile()) {
  29. if(shared == null) { // Allow resolving ref in Hide prefore makeInstance
  30. #if editor
  31. ref = hide.Ide.inst.loadPrefab(refpath.substr(1));
  32. #else
  33. return null;
  34. #end
  35. }
  36. else
  37. ref = shared.loadPrefab(refpath.substr(1));
  38. return ref;
  39. }
  40. else {
  41. var lib = getParent(hrt.prefab.Library);
  42. if(lib == null)
  43. return null;
  44. var all = lib.getAll(Prefab);
  45. for(p in all) {
  46. if(!Std.is(p, Reference) && p.getAbsPath() == refpath) {
  47. ref = p;
  48. return ref;
  49. }
  50. }
  51. }
  52. return null;
  53. }
  54. override function updateInstance( ctx: Context, ?propName : String ) {
  55. var p = resolveRef(ctx.shared);
  56. if(p == null)
  57. return;
  58. var parentCtx = ctx.shared.contexts.get(parent);
  59. if(parentCtx == null || parentCtx.local3d != ctx.local3d) {
  60. // Only apply reference Object3D properties (pos, scale...) to own local3D
  61. // Not all refs will create their own scene object
  62. super.updateInstance(ctx, propName);
  63. }
  64. }
  65. override function makeInstance(ctx: Context) : Context {
  66. var p = resolveRef(ctx.shared);
  67. if(p == null)
  68. return ctx;
  69. if(isFile()) {
  70. ctx = super.makeInstance(ctx);
  71. ctx.isRef = true;
  72. p.make(ctx);
  73. }
  74. else {
  75. ctx = ctx.clone(this);
  76. ctx.isRef = true;
  77. var refCtx = p.make(ctx);
  78. ctx.local3d = refCtx.local3d;
  79. updateInstance(ctx);
  80. }
  81. return ctx;
  82. }
  83. override function to<T:Prefab>( c : Class<T> ) : Null<T> {
  84. var base = super.to(c);
  85. if(base != null)
  86. return base;
  87. var p = resolveRef(null);
  88. if(p == null) return null;
  89. return Std.instance(p, c);
  90. }
  91. #if editor
  92. override function edit( ctx : EditContext ) {
  93. var element = new hide.Element('
  94. <div class="group" name="Reference">
  95. <dl>
  96. <dt>Reference</dt><dd><input type="text" field="refpath"/></dd>
  97. </dl>
  98. </div>');
  99. function updateProps() {
  100. var input = element.find("input");
  101. var found = resolveRef(ctx.rootContext.shared) != null;
  102. input.toggleClass("error", !found);
  103. }
  104. updateProps();
  105. var props = ctx.properties.add(element, this, function(pname) {
  106. ctx.onChange(this, pname);
  107. if(pname == "refpath") {
  108. ref = null;
  109. updateProps();
  110. if(!ctx.properties.isTempChange)
  111. ctx.rebuildPrefab(this);
  112. }
  113. });
  114. var parentCtx = ctx.getContext(parent);
  115. var selfCtx = ctx.getContext(this);
  116. var p = resolveRef(ctx.rootContext.shared);
  117. if(selfCtx != null && parentCtx != null && parentCtx.local3d != selfCtx.local3d) {
  118. super.edit(ctx);
  119. }
  120. }
  121. override function getHideProps() : HideProps {
  122. return { icon : "share", name : "Reference" };
  123. }
  124. #end
  125. static var _ = Library.register("reference", Reference);
  126. }