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 Scene and returned to the caller. 98 SimGroup* explode(); 99 100 bool buildPolyList(PolyListContext context, AbstractPolyList* polyList, const Box3F &box, const SphereF& sphere); 101 102 bool buildExportPolyList(ColladaUtils::ExportData* exportData, const Box3F &box, const SphereF &); 103 104 virtual void getUtilizedAssets(Vector<StringTableEntry>* usedAssetsList); 105 106protected: 107 108 void _closeFile( bool removeFileNotify ); 109 void _loadFile( bool addFileNotify ); 110 void _updateChildTransform( SceneObject* child ); 111 void _updateChildren(); 112 void _onFileChanged( const Torque::Path &path ); 113 114 static bool protectedSetFile( void *object, const char *index, const char *data ); 115 116 /// @name Callbacks 117 /// @{ 118 119 DECLARE_CALLBACK( void, onLoad, ( SimGroup *children ) ); 120 121 /// @} 122 123protected: 124 125 /// Prefab file which defines our children objects. 126 String mFilename; 127 128 /// Group which holds all children objects. 129 SimObjectPtr<SimGroup> mChildGroup; 130 131 /// Structure to keep track of child object initial transform and scale 132 struct Transform 133 { 134 MatrixF mat; 135 VectorF scale; 136 Transform() : mat(true), scale(Point3F::One) { } 137 Transform( const MatrixF& m, const VectorF& s ) : mat(m), scale(s) { } 138 }; 139 typedef Map<SimObjectId,Transform> ChildToMatMap; 140 141 /// Lookup from a child object's id to its transform in 142 /// this Prefab's object space. 143 ChildToMatMap mChildMap; 144 145 typedef Map<SimObjectId,SimObjectId> ChildToPrefabMap; 146 147 /// Lookup from a SimObject to its parent Prefab if it has one. 148 static ChildToPrefabMap smChildToPrefabMap; 149}; 150 151 152class ExplodePrefabUndoAction : public UndoAction 153{ 154 typedef UndoAction Parent; 155 friend class WorldEditor; 156 157public: 158 159 ExplodePrefabUndoAction( Prefab *prefab ); 160 161 // UndoAction 162 virtual void undo(); 163 virtual void redo(); 164 165protected: 166 167 SimGroup *mGroup; 168 SimObjectId mPrefabId; 169}; 170 171 172#endif // _PREFAB_H_ 173