BsUndoRedo.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #pragma once
  2. #include "BsEditorPrerequisites.h"
  3. #include "BsModule.h"
  4. namespace BansheeEngine
  5. {
  6. /**
  7. * @brief Provides functionality to undo or redo recently performed operations in the editor.
  8. */
  9. class BS_ED_EXPORT 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. /**
  55. * @brief Resets the undo/redo stacks.
  56. */
  57. void clear();
  58. private:
  59. /**
  60. * @brief Removes the last undo command from the undo stack, and returns it.
  61. */
  62. EditorCommand* removeLastFromUndoStack();
  63. /**
  64. * @brief Adds a new command to the undo stack.
  65. */
  66. void addToUndoStack(EditorCommand* command);
  67. /**
  68. * @brief Removes all entries from the undo stack.
  69. */
  70. void clearUndoStack();
  71. /**
  72. * @brief Removes all entries from the redo stack.
  73. */
  74. void clearRedoStack();
  75. static const UINT32 MAX_STACK_ELEMENTS;
  76. EditorCommand** mUndoStack;
  77. EditorCommand** mRedoStack;
  78. UINT32 mUndoStackPtr;
  79. UINT32 mUndoNumElements;
  80. UINT32 mRedoStackPtr;
  81. UINT32 mRedoNumElements;
  82. Stack<GroupData> mGroups;
  83. };
  84. }