prefab.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. static void initPersistFields();
  51. // SimObject
  52. virtual bool onAdd();
  53. virtual void onRemove();
  54. virtual void onEditorEnable();
  55. virtual void onEditorDisable();
  56. virtual void inspectPostApply();
  57. // NetObject
  58. U32 packUpdate( NetConnection *conn, U32 mask, BitStream *stream );
  59. void unpackUpdate( NetConnection *conn, BitStream *stream );
  60. // SceneObject
  61. virtual void setTransform( const MatrixF &mat );
  62. virtual void setScale(const VectorF & scale);
  63. // Prefab
  64. /// If the passed object is a child of any Prefab return that Prefab.
  65. /// Note that this call is only valid if the editor is open and when
  66. /// passed server-side objects.
  67. static Prefab* getPrefabByChild( SimObject *child );
  68. /// Returns false if the passed object is of a type that is not allowed
  69. /// as a child within a Prefab.
  70. static bool isValidChild( SimObject *child, bool logWarnings );
  71. ///
  72. void render( ObjectRenderInst *ri, SceneRenderState *state, BaseMatInstance *overrideMat );
  73. ///
  74. void setFile(StringTableEntry file );
  75. /// Removes all children from this Prefab and puts them into a SimGroup
  76. /// which is added to the Scene and returned to the caller.
  77. SimGroup* explode();
  78. bool buildPolyList(PolyListContext context, AbstractPolyList* polyList, const Box3F &box, const SphereF& sphere);
  79. bool buildExportPolyList(ColladaUtils::ExportData* exportData, const Box3F &box, const SphereF &);
  80. virtual void getUtilizedAssets(Vector<StringTableEntry>* usedAssetsList);
  81. S32 getChildGroup() {
  82. if (mChildGroup.isValid())
  83. return mChildGroup->getId();
  84. return 0;
  85. }
  86. protected:
  87. void _closeFile( bool removeFileNotify );
  88. void _loadFile( bool addFileNotify );
  89. void _updateChildTransform( SceneObject* child );
  90. void _updateChildren();
  91. void _onFileChanged( const Torque::Path &path );
  92. static bool protectedSetFile( void *object, const char *index, const char *data );
  93. /// @name Callbacks
  94. /// @{
  95. DECLARE_CALLBACK( void, onLoad, ( SimGroup *children ) );
  96. /// @}
  97. protected:
  98. /// Prefab file which defines our children objects.
  99. StringTableEntry mFilename;
  100. /// Group which holds all children objects.
  101. SimObjectPtr<SimGroup> mChildGroup;
  102. /// Structure to keep track of child object initial transform and scale
  103. struct Transform
  104. {
  105. MatrixF mat;
  106. VectorF scale;
  107. Transform() : mat(true), scale(Point3F::One) { }
  108. Transform( const MatrixF& m, const VectorF& s ) : mat(m), scale(s) { }
  109. };
  110. typedef Map<SimObjectId,Transform> ChildToMatMap;
  111. /// Lookup from a child object's id to its transform in
  112. /// this Prefab's object space.
  113. ChildToMatMap mChildMap;
  114. typedef Map<SimObjectId,SimObjectId> ChildToPrefabMap;
  115. /// Lookup from a SimObject to its parent Prefab if it has one.
  116. static ChildToPrefabMap smChildToPrefabMap;
  117. };
  118. class ExplodePrefabUndoAction : public UndoAction
  119. {
  120. typedef UndoAction Parent;
  121. friend class WorldEditor;
  122. public:
  123. ExplodePrefabUndoAction( Prefab *prefab );
  124. // UndoAction
  125. virtual void undo();
  126. virtual void redo();
  127. protected:
  128. SimGroup *mGroup;
  129. SimObjectId mPrefabId;
  130. };
  131. #endif // _PREFAB_H_