BsUndoRedo.h 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. #pragma once
  2. #include "BsEditorPrerequisites.h"
  3. #include "BsModule.h"
  4. namespace BansheeEngine
  5. {
  6. /**
  7. * @brief Handles editor-wide undo & redo operations.
  8. */
  9. class UndoRedo : public Module<UndoRedo>
  10. {
  11. /**
  12. * @brief Contains data about a single undo/redo group.
  13. * Groups allow you to create context sensitive undo/redo operations.
  14. */
  15. struct GroupData
  16. {
  17. String name;
  18. UINT32 numEntries;
  19. };
  20. public:
  21. UndoRedo();
  22. ~UndoRedo();
  23. /**
  24. * @brief Executes the last command on the undo stack, undoing its operations.
  25. */
  26. void undo();
  27. /**
  28. * @brief Executes the last command on the redo stack (last command we called undo on),
  29. * re-applying its operation.
  30. */
  31. void redo();
  32. /**
  33. * @brief Creates a new undo/redo group. All new commands will be registered to this group.
  34. * You may remove the group and all of its commands by calling ::popGroup.
  35. *
  36. * For example you might require global editor-wide undo/redo operations, and also
  37. * more specific ones like input in an input box. When the user is done with the input
  38. * box you no longer require its undo operations and you may use groups to easily
  39. * remove them.
  40. *
  41. * @param name Unique name for the group.
  42. */
  43. void pushGroup(const String& name);
  44. /**
  45. * @brief Removes all the command registered to the current undo/redo group.
  46. *
  47. * @param name Unique name for the group.
  48. */
  49. void popGroup(const String& name);
  50. /**
  51. * @brief Registers a new undo command.
  52. */
  53. void registerCommand(EditorCommand* command);
  54. private:
  55. /**
  56. * @brief Removes the last undo command from the undo stack, and returns it.
  57. */
  58. EditorCommand* removeLastFromUndoStack();
  59. /**
  60. * @brief Adds a new command to the undo stack.
  61. */
  62. void addToUndoStack(EditorCommand* command);
  63. /**
  64. * @brief Removes all entries from the undo stack.
  65. */
  66. void clearUndoStack();
  67. /**
  68. * @brief Removes all entries from the redo stack.
  69. */
  70. void clearRedoStack();
  71. static const UINT32 MAX_STACK_ELEMENTS;
  72. EditorCommand** mUndoStack;
  73. EditorCommand** mRedoStack;
  74. UINT32 mUndoStackPtr;
  75. UINT32 mUndoNumElements;
  76. UINT32 mRedoStackPtr;
  77. UINT32 mRedoNumElements;
  78. Stack<GroupData> mGroups;
  79. };
  80. }