BsUndoRedo.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 "Utility/BsModule.h"
  6. namespace bs
  7. {
  8. /** @addtogroup UndoRedo
  9. * @{
  10. */
  11. /** Provides functionality to undo or redo recently performed operations in the editor. */
  12. class BS_ED_EXPORT UndoRedo : public Module<UndoRedo>
  13. {
  14. /**
  15. * Contains data about a single undo/redo group. 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. /** Executes the last command on the undo stack, undoing its operations. */
  26. void undo();
  27. /** Executes the last command on the redo stack (last command we called undo on), re-applying its operation. */
  28. void redo();
  29. /**
  30. * Creates a new undo/redo group. All new commands will be registered to this group. You may remove the group and
  31. * all of its commands by calling popGroup().
  32. *
  33. * For example you might require global editor-wide undo/redo operations, and also more specific ones like input in
  34. * an input box. When the user is done with the input box you no longer require its undo operations and you may use
  35. * groups to easily remove them.
  36. *
  37. * @param[in] name Unique name for the group.
  38. */
  39. void pushGroup(const String& name);
  40. /**
  41. * Removes all the command registered to the current undo/redo group.
  42. *
  43. * @param[in] name Unique name for the group.
  44. */
  45. void popGroup(const String& name);
  46. /** Registers a new undo command. */
  47. void registerCommand(const SPtr<EditorCommand>& command);
  48. /** Returns the unique identifier for the command on top of the undo stack. */
  49. UINT32 getTopCommandId() const;
  50. /**
  51. * Removes a command from the undo/redo list, without executing it.
  52. *
  53. * @param[in] id Identifier of the command returned by getTopCommandIdx().
  54. */
  55. void popCommand(UINT32 id);
  56. /** Resets the undo/redo stacks. */
  57. void clear();
  58. private:
  59. /** Removes the last undo command from the undo stack, and returns it. */
  60. SPtr<EditorCommand> removeLastFromUndoStack();
  61. /** Adds a new command to the undo stack. Returns the command that was replaced. */
  62. SPtr<EditorCommand> addToUndoStack(const SPtr<EditorCommand>& command);
  63. /** Removes all entries from the undo stack. */
  64. void clearUndoStack();
  65. /** Removes all entries from the redo stack. */
  66. void clearRedoStack();
  67. static const UINT32 MAX_STACK_ELEMENTS;
  68. SPtr<EditorCommand>* mUndoStack;
  69. SPtr<EditorCommand>* mRedoStack;
  70. UINT32 mUndoStackPtr;
  71. UINT32 mUndoNumElements;
  72. UINT32 mRedoStackPtr;
  73. UINT32 mRedoNumElements;
  74. UINT32 mNextCommandId;
  75. Vector<GroupData> mGroups;
  76. };
  77. /** @} */
  78. }