editor_undo_redo_manager.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /**************************************************************************/
  2. /* editor_undo_redo_manager.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #ifndef EDITOR_UNDO_REDO_MANAGER_H
  31. #define EDITOR_UNDO_REDO_MANAGER_H
  32. #include "core/object/object.h"
  33. #include "core/object/undo_redo.h"
  34. class EditorUndoRedoManager : public Object {
  35. GDCLASS(EditorUndoRedoManager, Object);
  36. static EditorUndoRedoManager *singleton;
  37. public:
  38. enum SpecialHistory {
  39. GLOBAL_HISTORY = 0,
  40. REMOTE_HISTORY = -9,
  41. INVALID_HISTORY = -99,
  42. };
  43. struct Action {
  44. int history_id = INVALID_HISTORY;
  45. double timestamp = 0;
  46. String action_name;
  47. UndoRedo::MergeMode merge_mode = UndoRedo::MERGE_DISABLE;
  48. bool backward_undo_ops = false;
  49. };
  50. struct History {
  51. int id = INVALID_HISTORY;
  52. UndoRedo *undo_redo = nullptr;
  53. uint64_t saved_version = 1;
  54. List<Action> undo_stack;
  55. List<Action> redo_stack;
  56. };
  57. private:
  58. HashMap<int, History> history_map;
  59. Action pending_action;
  60. bool forced_history = false;
  61. bool is_committing = false;
  62. History *_get_newest_undo();
  63. protected:
  64. static void _bind_methods();
  65. public:
  66. History &get_or_create_history(int p_idx);
  67. UndoRedo *get_history_undo_redo(int p_idx) const;
  68. int get_history_id_for_object(Object *p_object) const;
  69. History &get_history_for_object(Object *p_object);
  70. void force_fixed_history();
  71. void create_action_for_history(const String &p_name, int p_history_id, UndoRedo::MergeMode p_mode = UndoRedo::MERGE_DISABLE, bool p_backward_undo_ops = false);
  72. void create_action(const String &p_name = "", UndoRedo::MergeMode p_mode = UndoRedo::MERGE_DISABLE, Object *p_custom_context = nullptr, bool p_backward_undo_ops = false);
  73. void add_do_methodp(Object *p_object, const StringName &p_method, const Variant **p_args, int p_argcount);
  74. void add_undo_methodp(Object *p_object, const StringName &p_method, const Variant **p_args, int p_argcount);
  75. template <typename... VarArgs>
  76. void add_do_method(Object *p_object, const StringName &p_method, VarArgs... p_args) {
  77. Variant args[sizeof...(p_args) + 1] = { p_args..., Variant() }; // +1 makes sure zero sized arrays are also supported.
  78. const Variant *argptrs[sizeof...(p_args) + 1];
  79. for (uint32_t i = 0; i < sizeof...(p_args); i++) {
  80. argptrs[i] = &args[i];
  81. }
  82. add_do_methodp(p_object, p_method, sizeof...(p_args) == 0 ? nullptr : (const Variant **)argptrs, sizeof...(p_args));
  83. }
  84. template <typename... VarArgs>
  85. void add_undo_method(Object *p_object, const StringName &p_method, VarArgs... p_args) {
  86. Variant args[sizeof...(p_args) + 1] = { p_args..., Variant() }; // +1 makes sure zero sized arrays are also supported.
  87. const Variant *argptrs[sizeof...(p_args) + 1];
  88. for (uint32_t i = 0; i < sizeof...(p_args); i++) {
  89. argptrs[i] = &args[i];
  90. }
  91. add_undo_methodp(p_object, p_method, sizeof...(p_args) == 0 ? nullptr : (const Variant **)argptrs, sizeof...(p_args));
  92. }
  93. void _add_do_method(const Variant **p_args, int p_argcount, Callable::CallError &r_error);
  94. void _add_undo_method(const Variant **p_args, int p_argcount, Callable::CallError &r_error);
  95. void add_do_property(Object *p_object, const StringName &p_property, const Variant &p_value);
  96. void add_undo_property(Object *p_object, const StringName &p_property, const Variant &p_value);
  97. void add_do_reference(Object *p_object);
  98. void add_undo_reference(Object *p_object);
  99. void commit_action(bool p_execute = true);
  100. bool is_committing_action() const;
  101. bool undo();
  102. bool undo_history(int p_id);
  103. bool redo();
  104. bool redo_history(int p_id);
  105. void clear_history(int p_idx = INVALID_HISTORY, bool p_increase_version = true);
  106. void set_history_as_saved(int p_idx);
  107. void set_history_as_unsaved(int p_idx);
  108. bool is_history_unsaved(int p_idx);
  109. bool has_undo();
  110. bool has_redo();
  111. bool has_history(int p_idx) const;
  112. String get_current_action_name();
  113. int get_current_action_history_id();
  114. void discard_history(int p_idx, bool p_erase_from_map = true);
  115. static EditorUndoRedoManager *get_singleton();
  116. EditorUndoRedoManager();
  117. ~EditorUndoRedoManager();
  118. };
  119. VARIANT_ENUM_CAST(EditorUndoRedoManager::SpecialHistory);
  120. #endif // EDITOR_UNDO_REDO_MANAGER_H