undoActions.cpp 8.1 KB

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