BsUndoRedo.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 Returns the unique identifier for the command on top of the undo stack.
  56. */
  57. UINT32 getTopCommandId() const;
  58. /**
  59. * @brief Removes a command from the undo/redo list, without executing it.
  60. *
  61. * @param id Identifier of the command returned by ::getTopCommandIdx.
  62. */
  63. void popCommand(UINT32 id);
  64. /**
  65. * @brief Resets the undo/redo stacks.
  66. */
  67. void clear();
  68. private:
  69. /**
  70. * @brief Removes the last undo command from the undo stack, and returns it.
  71. */
  72. EditorCommand* removeLastFromUndoStack();
  73. /**
  74. * @brief Adds a new command to the undo stack.
  75. */
  76. void addToUndoStack(EditorCommand* command);
  77. /**
  78. * @brief Removes all entries from the undo stack.
  79. */
  80. void clearUndoStack();
  81. /**
  82. * @brief Removes all entries from the redo stack.
  83. */
  84. void clearRedoStack();
  85. static const UINT32 MAX_STACK_ELEMENTS;
  86. EditorCommand** mUndoStack;
  87. EditorCommand** mRedoStack;
  88. UINT32 mUndoStackPtr;
  89. UINT32 mUndoNumElements;
  90. UINT32 mRedoStackPtr;
  91. UINT32 mRedoNumElements;
  92. UINT32 mNextCommandId;
  93. Stack<GroupData> mGroups;
  94. };
  95. }