prefab.h 4.6 KB

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