BTUndoActions.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2014 Guy Allard
  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 "BTUndoActions.h"
  23. #include "console/consoleTypes.h"
  24. #include "console/simSet.h"
  25. S32 getNextObjectInGroup(SimObject *object, SimGroup *group)
  26. {
  27. group->lock();
  28. S32 nextId = -1;
  29. if(object != group->last() && group->find( group->begin(), group->end(), object ) != group->end())
  30. {
  31. for( SimSet::iterator i = group->begin(); i != group->end(); i++)
  32. {
  33. if( *i == object )
  34. {
  35. nextId = (*++i)->getId();
  36. break;
  37. }
  38. }
  39. group->unlock();
  40. }
  41. return nextId;
  42. }
  43. IMPLEMENT_CONOBJECT( BTDeleteUndoAction );
  44. ConsoleDocClass( BTDeleteUndoAction,
  45. "@brief Behavior Tree Editor delete undo instance\n\n"
  46. "Not intended for game development, for editors or internal use only.\n\n "
  47. "@internal");
  48. BTDeleteUndoAction::BTDeleteUndoAction( const UTF8 *actionName )
  49. : UndoAction( actionName )
  50. {
  51. }
  52. BTDeleteUndoAction::~BTDeleteUndoAction()
  53. {
  54. }
  55. void BTDeleteUndoAction::initPersistFields()
  56. {
  57. Parent::initPersistFields();
  58. }
  59. void BTDeleteUndoAction::deleteObject( SimObject *object )
  60. {
  61. AssertFatal( object, "BTDeleteUndoAction::deleteObject() - Got null object!" );
  62. AssertFatal( object->isProperlyAdded(),
  63. "BTDeleteUndoAction::deleteObject() - Object should be registered!" );
  64. // Capture the object id.
  65. mObject.id = object->getId();
  66. // Save the state.
  67. mObject.memento.save( object );
  68. // Store the group.
  69. SimGroup *group = object->getGroup();
  70. if ( group )
  71. {
  72. mObject.groupId = group->getId();
  73. // and the next object in the group
  74. mObject.nextId = getNextObjectInGroup(object, group);
  75. }
  76. // Now delete the object.
  77. object->deleteObject();
  78. }
  79. ConsoleMethod( BTDeleteUndoAction, deleteObject, void, 3, 3, "( SimObject obj )")
  80. {
  81. SimObject *obj = NULL;
  82. if ( Sim::findObject( argv[2], obj ) && obj )
  83. object->deleteObject( obj );
  84. }
  85. void BTDeleteUndoAction::undo()
  86. {
  87. // Create the object.
  88. SimObject::setForcedId(mObject.id); // Restore the object's Id
  89. SimObject *object = mObject.memento.restore();
  90. if ( !object )
  91. return;
  92. // Now restore its group.
  93. SimGroup *group;
  94. if ( Sim::findObject( mObject.groupId, group ) )
  95. {
  96. group->addObject( object );
  97. // restore its position in the group
  98. SimObject *nextObj;
  99. if ( Sim::findObject( mObject.nextId, nextObj ) )
  100. {
  101. group->reOrder(object, nextObj);
  102. }
  103. }
  104. Con::executef( this, "onUndone" );
  105. }
  106. void BTDeleteUndoAction::redo()
  107. {
  108. SimObject *object = Sim::findObject( mObject.id );
  109. if ( object )
  110. object->deleteObject();
  111. Con::executef( this, "onRedone" );
  112. }