prefab.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #ifndef _PREFAB_H_
  23. #define _PREFAB_H_
  24. #ifndef _SCENEOBJECT_H_
  25. #include "scene/sceneObject.h"
  26. #endif
  27. #ifndef _PATH_H_
  28. #include "core/util/path.h"
  29. #endif
  30. #ifndef _UNDO_H_
  31. #include "util/undo.h"
  32. #endif
  33. #ifndef _TDICTIONARY_H_
  34. #include "core/util/tDictionary.h"
  35. #endif
  36. class BaseMatInstance;
  37. class Prefab : public SceneObject
  38. {
  39. typedef SceneObject Parent;
  40. enum MaskBits
  41. {
  42. TransformMask = Parent::NextFreeMask << 0,
  43. FileMask = Parent::NextFreeMask << 1,
  44. NextFreeMask = Parent::NextFreeMask << 2
  45. };
  46. public:
  47. Prefab();
  48. virtual ~Prefab();
  49. DECLARE_CONOBJECT(Prefab);
  50. DECLARE_CATEGORY("Object \t Collection");
  51. static void initPersistFields();
  52. /// returns the filename for this object
  53. StringTableEntry getTypeHint() const override;
  54. // SimObject
  55. bool onAdd() override;
  56. void onRemove() override;
  57. void onEditorEnable() override;
  58. void onEditorDisable() override;
  59. void inspectPostApply() override;
  60. // NetObject
  61. U32 packUpdate( NetConnection *conn, U32 mask, BitStream *stream ) override;
  62. void unpackUpdate( NetConnection *conn, BitStream *stream ) override;
  63. // SceneObject
  64. void setTransform( const MatrixF &mat ) override;
  65. void setScale(const VectorF & scale) override;
  66. // Prefab
  67. /// If the passed object is a child of any Prefab return that Prefab.
  68. /// Note that this call is only valid if the editor is open and when
  69. /// passed server-side objects.
  70. static Prefab* getPrefabByChild( SimObject *child );
  71. /// Returns false if the passed object is of a type that is not allowed
  72. /// as a child within a Prefab.
  73. static bool isValidChild( SimObject *child, bool logWarnings );
  74. ///
  75. void render( ObjectRenderInst *ri, SceneRenderState *state, BaseMatInstance *overrideMat );
  76. ///
  77. void setFile(StringTableEntry file );
  78. /// Removes all children from this Prefab and puts them into a SimGroup
  79. /// which is added to the Scene and returned to the caller.
  80. SimGroup* explode();
  81. bool buildPolyList(PolyListContext context, AbstractPolyList* polyList, const Box3F &box, const SphereF& sphere) override;
  82. bool buildExportPolyList(ColladaUtils::ExportData* exportData, const Box3F &box, const SphereF &) override;
  83. void getUtilizedAssets(Vector<StringTableEntry>* usedAssetsList) override;
  84. S32 getChildGroup() {
  85. if (mChildGroup.isValid())
  86. return mChildGroup->getId();
  87. return 0;
  88. }
  89. protected:
  90. void _closeFile( bool removeFileNotify );
  91. void _loadFile( bool addFileNotify );
  92. void _updateChildTransform( SceneObject* child );
  93. void _updateChildren();
  94. void _onFileChanged( const Torque::Path &path );
  95. static bool protectedSetFile( void *object, const char *index, const char *data );
  96. /// @name Callbacks
  97. /// @{
  98. DECLARE_CALLBACK( void, onLoad, ( SimGroup *children ) );
  99. /// @}
  100. protected:
  101. /// Prefab file which defines our children objects.
  102. StringTableEntry mFilename;
  103. /// Group which holds all children objects.
  104. SimObjectPtr<SimGroup> mChildGroup;
  105. /// Structure to keep track of child object initial transform and scale
  106. struct Transform
  107. {
  108. MatrixF mat;
  109. VectorF scale;
  110. Transform() : mat(true), scale(Point3F::One) { }
  111. Transform( const MatrixF& m, const VectorF& s ) : mat(m), scale(s) { }
  112. };
  113. typedef Map<SimObjectId,Transform> ChildToMatMap;
  114. /// Lookup from a child object's id to its transform in
  115. /// this Prefab's object space.
  116. ChildToMatMap mChildMap;
  117. typedef Map<SimObjectId,SimObjectId> ChildToPrefabMap;
  118. /// Lookup from a SimObject to its parent Prefab if it has one.
  119. static ChildToPrefabMap smChildToPrefabMap;
  120. };
  121. class ExplodePrefabUndoAction : public UndoAction
  122. {
  123. typedef UndoAction Parent;
  124. friend class WorldEditor;
  125. public:
  126. ExplodePrefabUndoAction( Prefab *prefab );
  127. // UndoAction
  128. void undo() override;
  129. void redo() override;
  130. protected:
  131. SimGroup *mGroup;
  132. SimObjectId mPrefabId;
  133. };
  134. #endif // _PREFAB_H_