undoActions.cpp 8.1 KB

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