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