prefab.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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( String file );
  75. /// Removes all children from this Prefab and puts them into a SimGroup
  76. /// which is added to the MissionGroup and returned to the caller.
  77. SimGroup* explode();
  78. bool buildPolyList(PolyListContext context, AbstractPolyList* polyList, const Box3F &box, const SphereF& sphere);
  79. protected:
  80. void _closeFile( bool removeFileNotify );
  81. void _loadFile( bool addFileNotify );
  82. void _updateChildTransform( SceneObject* child );
  83. void _updateChildren();
  84. void _onFileChanged( const Torque::Path &path );
  85. static bool protectedSetFile( void *object, const char *index, const char *data );
  86. /// @name Callbacks
  87. /// @{
  88. DECLARE_CALLBACK( void, onLoad, ( SimGroup *children ) );
  89. /// @}
  90. protected:
  91. /// Prefab file which defines our children objects.
  92. String mFilename;
  93. /// Group which holds all children objects.
  94. SimObjectPtr<SimGroup> mChildGroup;
  95. /// Structure to keep track of child object initial transform and scale
  96. struct Transform
  97. {
  98. MatrixF mat;
  99. VectorF scale;
  100. Transform() : mat(true), scale(Point3F::One) { }
  101. Transform( const MatrixF& m, const VectorF& s ) : mat(m), scale(s) { }
  102. };
  103. typedef Map<SimObjectId,Transform> ChildToMatMap;
  104. /// Lookup from a child object's id to its transform in
  105. /// this Prefab's object space.
  106. ChildToMatMap mChildMap;
  107. typedef Map<SimObjectId,SimObjectId> ChildToPrefabMap;
  108. /// Lookup from a SimObject to its parent Prefab if it has one.
  109. static ChildToPrefabMap smChildToPrefabMap;
  110. };
  111. class ExplodePrefabUndoAction : public UndoAction
  112. {
  113. typedef UndoAction Parent;
  114. friend class WorldEditor;
  115. public:
  116. ExplodePrefabUndoAction( Prefab *prefab );
  117. // UndoAction
  118. virtual void undo();
  119. virtual void redo();
  120. protected:
  121. SimGroup *mGroup;
  122. SimObjectId mPrefabId;
  123. };
  124. #endif // _PREFAB_H_