BsUndoRedo.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #pragma once
  4. #include "BsEditorPrerequisites.h"
  5. #include "BsModule.h"
  6. namespace BansheeEngine
  7. {
  8. /**
  9. * @brief Provides functionality to undo or redo recently performed operations in the editor.
  10. */
  11. class BS_ED_EXPORT UndoRedo : public Module<UndoRedo>
  12. {
  13. /**
  14. * @brief Contains data about a single undo/redo group.
  15. * Groups allow you to create context sensitive undo/redo operations.
  16. */
  17. struct GroupData
  18. {
  19. String name;
  20. UINT32 numEntries;
  21. };
  22. public:
  23. UndoRedo();
  24. ~UndoRedo();
  25. /**
  26. * @brief Executes the last command on the undo stack, undoing its operations.
  27. */
  28. void undo();
  29. /**
  30. * @brief Executes the last command on the redo stack (last command we called undo on),
  31. * re-applying its operation.
  32. */
  33. void redo();
  34. /**
  35. * @brief Creates a new undo/redo group. All new commands will be registered to this group.
  36. * You may remove the group and all of its commands by calling ::popGroup.
  37. *
  38. * For example you might require global editor-wide undo/redo operations, and also
  39. * more specific ones like input in an input box. When the user is done with the input
  40. * box you no longer require its undo operations and you may use groups to easily
  41. * remove them.
  42. *
  43. * @param name Unique name for the group.
  44. */
  45. void pushGroup(const String& name);
  46. /**
  47. * @brief Removes all the command registered to the current undo/redo group.
  48. *
  49. * @param name Unique name for the group.
  50. */
  51. void popGroup(const String& name);
  52. /**
  53. * @brief Registers a new undo command.
  54. */
  55. void registerCommand(EditorCommand* command);
  56. /**
  57. * @brief Returns the unique identifier for the command on top of the undo stack.
  58. */
  59. UINT32 getTopCommandId() const;
  60. /**
  61. * @brief Removes a command from the undo/redo list, without executing it.
  62. *
  63. * @param id Identifier of the command returned by ::getTopCommandIdx.
  64. */
  65. void popCommand(UINT32 id);
  66. /**
  67. * @brief Resets the undo/redo stacks.
  68. */
  69. void clear();
  70. private:
  71. /**
  72. * @brief Removes the last undo command from the undo stack, and returns it.
  73. */
  74. EditorCommand* removeLastFromUndoStack();
  75. /**
  76. * @brief Adds a new command to the undo stack.
  77. */
  78. void addToUndoStack(EditorCommand* command);
  79. /**
  80. * @brief Removes all entries from the undo stack.
  81. */
  82. void clearUndoStack();
  83. /**
  84. * @brief Removes all entries from the redo stack.
  85. */
  86. void clearRedoStack();
  87. static const UINT32 MAX_STACK_ELEMENTS;
  88. EditorCommand** mUndoStack;
  89. EditorCommand** mRedoStack;
  90. UINT32 mUndoStackPtr;
  91. UINT32 mUndoNumElements;
  92. UINT32 mRedoStackPtr;
  93. UINT32 mRedoNumElements;
  94. UINT32 mNextCommandId;
  95. Stack<GroupData> mGroups;
  96. };
  97. }