undoActions.cpp
Engine/source/gui/worldEditor/undoActions.cpp
Public Functions
ConsoleDocClass(InspectorFieldUndoAction , "@brief Inspector Field undo action <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">instance\n\n</a>" "Not intended <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1a2732ab74fa0237854c2ba0f75f88a624">for</a> game development, <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1a2732ab74fa0237854c2ba0f75f88a624">for</a> editors or internal use <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">only.\n\n</a> " " @internal" )
ConsoleDocClass(MECreateUndoAction , "@brief <a href="/coding/class/classmaterial/">Material</a> Editor create undo <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">instance\n\n</a>" "Not intended <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1a2732ab74fa0237854c2ba0f75f88a624">for</a> game development, <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1a2732ab74fa0237854c2ba0f75f88a624">for</a> editors or internal use <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">only.\n\n</a> " " @internal" )
ConsoleDocClass(MEDeleteUndoAction , "@brief <a href="/coding/class/classmaterial/">Material</a> Editor delete undo <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">instance\n\n</a>" "Not intended <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1a2732ab74fa0237854c2ba0f75f88a624">for</a> game development, <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1a2732ab74fa0237854c2ba0f75f88a624">for</a> editors or internal use <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">only.\n\n</a> " " @internal" )
DefineEngineMethod(MECreateUndoAction , addObject , void , (SimObject *obj) , "Add the object being created <a href="/coding/file/cmdgram_8cpp/#cmdgram_8cpp_1a5bafda9519252aa2d0fd038153f77dca">to</a> an undo <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">action.\n</a>" "@param obj <a href="/coding/file/gizmo_8h/#gizmo_8h_1a10fcd3bee2ea25191e31795e36bdeba1a5df911aaca43421a25e32c3002befbc4">Object</a> being created you want <a href="/coding/file/cmdgram_8cpp/#cmdgram_8cpp_1a5bafda9519252aa2d0fd038153f77dca">to</a> create the undo for." )
DefineEngineMethod(MEDeleteUndoAction , deleteObject , void , (SimObject *obj) , "Delete the object and add it <a href="/coding/file/cmdgram_8cpp/#cmdgram_8cpp_1a5bafda9519252aa2d0fd038153f77dca">to</a> the undo <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">action.\n</a>" "@param obj <a href="/coding/file/gizmo_8h/#gizmo_8h_1a10fcd3bee2ea25191e31795e36bdeba1a5df911aaca43421a25e32c3002befbc4">Object</a> <a href="/coding/file/cmdgram_8cpp/#cmdgram_8cpp_1a5bafda9519252aa2d0fd038153f77dca">to</a> delete and add <a href="/coding/file/cmdgram_8cpp/#cmdgram_8cpp_1a5bafda9519252aa2d0fd038153f77dca">to</a> the undo action." )
Detailed Description
Public Functions
ConsoleDocClass(InspectorFieldUndoAction , "@brief Inspector Field undo action <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">instance\n\n</a>" "Not intended <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1a2732ab74fa0237854c2ba0f75f88a624">for</a> game development, <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1a2732ab74fa0237854c2ba0f75f88a624">for</a> editors or internal use <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">only.\n\n</a> " " @internal" )
ConsoleDocClass(MECreateUndoAction , "@brief <a href="/coding/class/classmaterial/">Material</a> Editor create undo <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">instance\n\n</a>" "Not intended <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1a2732ab74fa0237854c2ba0f75f88a624">for</a> game development, <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1a2732ab74fa0237854c2ba0f75f88a624">for</a> editors or internal use <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">only.\n\n</a> " " @internal" )
ConsoleDocClass(MEDeleteUndoAction , "@brief <a href="/coding/class/classmaterial/">Material</a> Editor delete undo <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">instance\n\n</a>" "Not intended <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1a2732ab74fa0237854c2ba0f75f88a624">for</a> game development, <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1a2732ab74fa0237854c2ba0f75f88a624">for</a> editors or internal use <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">only.\n\n</a> " " @internal" )
DefineEngineMethod(MECreateUndoAction , addObject , void , (SimObject *obj) , "Add the object being created <a href="/coding/file/cmdgram_8cpp/#cmdgram_8cpp_1a5bafda9519252aa2d0fd038153f77dca">to</a> an undo <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">action.\n</a>" "@param obj <a href="/coding/file/gizmo_8h/#gizmo_8h_1a10fcd3bee2ea25191e31795e36bdeba1a5df911aaca43421a25e32c3002befbc4">Object</a> being created you want <a href="/coding/file/cmdgram_8cpp/#cmdgram_8cpp_1a5bafda9519252aa2d0fd038153f77dca">to</a> create the undo for." )
DefineEngineMethod(MEDeleteUndoAction , deleteObject , void , (SimObject *obj) , "Delete the object and add it <a href="/coding/file/cmdgram_8cpp/#cmdgram_8cpp_1a5bafda9519252aa2d0fd038153f77dca">to</a> the undo <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">action.\n</a>" "@param obj <a href="/coding/file/gizmo_8h/#gizmo_8h_1a10fcd3bee2ea25191e31795e36bdeba1a5df911aaca43421a25e32c3002befbc4">Object</a> <a href="/coding/file/cmdgram_8cpp/#cmdgram_8cpp_1a5bafda9519252aa2d0fd038153f77dca">to</a> delete and add <a href="/coding/file/cmdgram_8cpp/#cmdgram_8cpp_1a5bafda9519252aa2d0fd038153f77dca">to</a> the undo action." )
IMPLEMENT_CONOBJECT(InspectorFieldUndoAction )
IMPLEMENT_CONOBJECT(MECreateUndoAction )
IMPLEMENT_CONOBJECT(MEDeleteUndoAction )
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#include "platform/platform.h" 25#include "gui/worldEditor/undoActions.h" 26 27#include "gui/editor/inspector/field.h" 28#include "gui/editor/guiInspector.h" 29#include "console/consoleTypes.h" 30#include "console/engineAPI.h" 31 32 33IMPLEMENT_CONOBJECT( MECreateUndoAction ); 34 35ConsoleDocClass( MECreateUndoAction, 36 "@brief Material Editor create undo instance\n\n" 37 "Not intended for game development, for editors or internal use only.\n\n " 38 "@internal"); 39 40MECreateUndoAction::MECreateUndoAction( const UTF8* actionName ) 41 : UndoAction( actionName ) 42{ 43} 44 45MECreateUndoAction::~MECreateUndoAction() 46{ 47} 48 49void MECreateUndoAction::initPersistFields() 50{ 51 Parent::initPersistFields(); 52} 53 54void MECreateUndoAction::addObject( SimObject *object ) 55{ 56 AssertFatal( object, "MECreateUndoAction::addObject() - Got null object!" ); 57 58 mObjects.increment(); 59 mObjects.last().id = object->getId(); 60} 61 62DefineEngineMethod( MECreateUndoAction, addObject, void, ( SimObject* obj),, 63 "Add the object being created to an undo action.\n" 64 "@param obj Object being created you want to create the undo for.") 65{ 66 if (obj) 67 object->addObject( obj ); 68} 69 70void MECreateUndoAction::undo() 71{ 72 for ( S32 i= mObjects.size()-1; i >= 0; i-- ) 73 { 74 ObjectState &state = mObjects[i]; 75 76 SimObject *object = Sim::findObject( state.id ); 77 if ( !object ) 78 continue; 79 80 // Save the state. 81 if ( !state.memento.hasState() ) 82 state.memento.save( object ); 83 84 // Store the group. 85 SimGroup *group = object->getGroup(); 86 if ( group ) 87 state.groupId = group->getId(); 88 89 // We got what we need... delete it. 90 object->deleteObject(); 91 } 92 93 Con::executef( this, "onUndone" ); 94} 95 96void MECreateUndoAction::redo() 97{ 98 for ( S32 i=0; i < mObjects.size(); i++ ) 99 { 100 const ObjectState &state = mObjects[i]; 101 102 // Create the object. 103 SimObject::setForcedId(state.id); // Restore the object's Id 104 SimObject *object = state.memento.restore(); 105 if ( !object ) 106 continue; 107 108 // Now restore its group. 109 SimGroup *group; 110 if ( Sim::findObject( state.groupId, group ) ) 111 group->addObject( object ); 112 } 113 114 Con::executef( this, "onRedone" ); 115} 116 117 118IMPLEMENT_CONOBJECT( MEDeleteUndoAction ); 119 120ConsoleDocClass( MEDeleteUndoAction, 121 "@brief Material Editor delete undo instance\n\n" 122 "Not intended for game development, for editors or internal use only.\n\n " 123 "@internal"); 124 125MEDeleteUndoAction::MEDeleteUndoAction( const UTF8 *actionName ) 126 : UndoAction( actionName ) 127{ 128} 129 130MEDeleteUndoAction::~MEDeleteUndoAction() 131{ 132} 133 134void MEDeleteUndoAction::initPersistFields() 135{ 136 Parent::initPersistFields(); 137} 138 139void MEDeleteUndoAction::deleteObject( SimObject *object ) 140{ 141 AssertFatal( object, "MEDeleteUndoAction::deleteObject() - Got null object!" ); 142 AssertFatal( object->isProperlyAdded(), 143 "MEDeleteUndoAction::deleteObject() - Object should be registered!" ); 144 145 mObjects.increment(); 146 ObjectState& state = mObjects.last(); 147 148 // Capture the object id. 149 state.id = object->getId(); 150 151 // Save the state. 152 state.memento.save( object ); 153 154 // Store the group. 155 SimGroup *group = object->getGroup(); 156 if ( group ) 157 state.groupId = group->getId(); 158 159 //Do any special handling of delete actions the object may do 160 object->handleDeleteAction(); 161 162 // Now delete the object. 163 object->deleteObject(); 164} 165 166void MEDeleteUndoAction::deleteObject( const Vector<SimObject*> &objectList ) 167{ 168 for ( S32 i = 0; i < objectList.size(); i++ ) 169 deleteObject( objectList[i] ); 170} 171 172DefineEngineMethod( MEDeleteUndoAction, deleteObject, void, ( SimObject* obj),, 173 "Delete the object and add it to the undo action.\n" 174 "@param obj Object to delete and add to the undo action.") 175{ 176 if (obj) 177 object->deleteObject( obj ); 178} 179 180void MEDeleteUndoAction::undo() 181{ 182 for ( S32 i= mObjects.size()-1; i >= 0; i-- ) 183 { 184 const ObjectState &state = mObjects[i]; 185 186 // Create the object. 187 SimObject::setForcedId(state.id); // Restore the object's Id 188 SimObject *object = state.memento.restore(); 189 if ( !object ) 190 continue; 191 192 // Now restore its group. 193 SimGroup *group; 194 if ( Sim::findObject( state.groupId, group ) ) 195 group->addObject( object ); 196 } 197 198 Con::executef( this, "onUndone" ); 199} 200 201void MEDeleteUndoAction::redo() 202{ 203 for ( S32 i=0; i < mObjects.size(); i++ ) 204 { 205 const ObjectState& state = mObjects[i]; 206 SimObject *object = Sim::findObject( state.id ); 207 if ( object ) 208 object->deleteObject(); 209 } 210 211 Con::executef( this, "onRedone" ); 212} 213 214IMPLEMENT_CONOBJECT( InspectorFieldUndoAction ); 215 216ConsoleDocClass( InspectorFieldUndoAction, 217 "@brief Inspector Field undo action instance\n\n" 218 "Not intended for game development, for editors or internal use only.\n\n " 219 "@internal"); 220 221InspectorFieldUndoAction::InspectorFieldUndoAction() 222{ 223 mInspector = NULL; 224 mObjId = 0; 225 mField = NULL; 226 mSlotName = StringTable->EmptyString(); 227 mArrayIdx = StringTable->EmptyString(); 228} 229 230InspectorFieldUndoAction::InspectorFieldUndoAction( const UTF8 *actionName ) 231: UndoAction( actionName ) 232{ 233 mInspector = NULL; 234 mObjId = 0; 235 mField = NULL; 236 mSlotName = StringTable->EmptyString(); 237 mArrayIdx = StringTable->EmptyString(); 238} 239 240void InspectorFieldUndoAction::initPersistFields() 241{ 242 addField( "inspectorGui", TYPEID< GuiInspector >(), Offset( mInspector, InspectorFieldUndoAction ) ); 243 addField( "objectId", TypeS32, Offset( mObjId, InspectorFieldUndoAction ) ); 244 addField( "fieldName", TypeString, Offset( mSlotName, InspectorFieldUndoAction ) ); 245 addField( "fieldValue", TypeRealString, Offset( mData, InspectorFieldUndoAction ) ); 246 addField( "arrayIndex", TypeString, Offset( mArrayIdx, InspectorFieldUndoAction ) ); 247 248 Parent::initPersistFields(); 249} 250 251void InspectorFieldUndoAction::undo() 252{ 253 SimObject *obj = NULL; 254 if ( !Sim::findObject( mObjId, obj ) ) 255 return; 256 257 if ( mArrayIdx && dStricmp( mArrayIdx, "(null)" ) == 0 ) 258 mArrayIdx = NULL; 259 260 // Grab the current data. 261 String data = obj->getDataField( mSlotName, mArrayIdx ); 262 263 // Call this to mirror the way field changes are done through the inspector. 264 obj->inspectPreApply(); 265 266 // Restore the data from the UndoAction 267 obj->setDataField( mSlotName, mArrayIdx, mData.c_str() ); 268 269 // Call this to mirror the way field changes are done through the inspector. 270 obj->inspectPostApply(); 271 272 // If the affected object is still being inspected, 273 // update the InspectorField to reflect the changed value. 274 if ( mInspector && mInspector->getNumInspectObjects() > 0 && mInspector->getInspectObject() == obj ) 275 mInspector->updateFieldValue( mSlotName, mArrayIdx ); 276 277 // Now save the previous data in this UndoAction 278 // since an undo action must become a redo action and vice-versa 279 mData = data; 280} 281