editor_undo_redo_manager.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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/class_db.h"
  33. #include "core/object/object.h"
  34. #include "core/object/undo_redo.h"
  35. class EditorUndoRedoManager : public Object {
  36. GDCLASS(EditorUndoRedoManager, Object);
  37. static EditorUndoRedoManager *singleton;
  38. public:
  39. enum SpecialHistory {
  40. GLOBAL_HISTORY = 0,
  41. REMOTE_HISTORY = -9,
  42. INVALID_HISTORY = -99,
  43. };
  44. struct Action {
  45. int history_id = INVALID_HISTORY;
  46. double timestamp = 0;
  47. String action_name;
  48. UndoRedo::MergeMode merge_mode = UndoRedo::MERGE_DISABLE;
  49. bool backward_undo_ops = false;
  50. };
  51. struct History {
  52. int id = INVALID_HISTORY;
  53. UndoRedo *undo_redo = nullptr;
  54. uint64_t saved_version = 1;
  55. List<Action> undo_stack;
  56. List<Action> redo_stack;
  57. };
  58. private:
  59. HashMap<int, History> history_map;
  60. Action pending_action;
  61. bool forced_history = false;
  62. bool is_committing = false;
  63. History *_get_newest_undo();
  64. protected:
  65. static void _bind_methods();
  66. public:
  67. History &get_or_create_history(int p_idx);
  68. UndoRedo *get_history_undo_redo(int p_idx) const;
  69. int get_history_id_for_object(Object *p_object) const;
  70. History &get_history_for_object(Object *p_object);
  71. void force_fixed_history();
  72. 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);
  73. 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);
  74. void add_do_methodp(Object *p_object, const StringName &p_method, const Variant **p_args, int p_argcount);
  75. void add_undo_methodp(Object *p_object, const StringName &p_method, const Variant **p_args, int p_argcount);
  76. template <typename... VarArgs>
  77. void add_do_method(Object *p_object, const StringName &p_method, VarArgs... p_args) {
  78. Variant args[sizeof...(p_args) + 1] = { p_args..., Variant() }; // +1 makes sure zero sized arrays are also supported.
  79. const Variant *argptrs[sizeof...(p_args) + 1];
  80. for (uint32_t i = 0; i < sizeof...(p_args); i++) {
  81. argptrs[i] = &args[i];
  82. }
  83. add_do_methodp(p_object, p_method, sizeof...(p_args) == 0 ? nullptr : (const Variant **)argptrs, sizeof...(p_args));
  84. }
  85. template <typename... VarArgs>
  86. void add_undo_method(Object *p_object, const StringName &p_method, VarArgs... p_args) {
  87. Variant args[sizeof...(p_args) + 1] = { p_args..., Variant() }; // +1 makes sure zero sized arrays are also supported.
  88. const Variant *argptrs[sizeof...(p_args) + 1];
  89. for (uint32_t i = 0; i < sizeof...(p_args); i++) {
  90. argptrs[i] = &args[i];
  91. }
  92. add_undo_methodp(p_object, p_method, sizeof...(p_args) == 0 ? nullptr : (const Variant **)argptrs, sizeof...(p_args));
  93. }
  94. void _add_do_method(const Variant **p_args, int p_argcount, Callable::CallError &r_error);
  95. void _add_undo_method(const Variant **p_args, int p_argcount, Callable::CallError &r_error);
  96. void add_do_property(Object *p_object, const StringName &p_property, const Variant &p_value);
  97. void add_undo_property(Object *p_object, const StringName &p_property, const Variant &p_value);
  98. void add_do_reference(Object *p_object);
  99. void add_undo_reference(Object *p_object);
  100. void commit_action(bool p_execute = true);
  101. bool is_committing_action() const;
  102. bool undo();
  103. bool undo_history(int p_id);
  104. bool redo();
  105. bool redo_history(int p_id);
  106. void clear_history(bool p_increase_version = true, int p_idx = INVALID_HISTORY);
  107. void set_history_as_saved(int p_idx);
  108. void set_history_as_unsaved(int p_idx);
  109. bool is_history_unsaved(int p_idx);
  110. bool has_undo();
  111. bool has_redo();
  112. bool has_history(int p_idx) const;
  113. String get_current_action_name();
  114. int get_current_action_history_id();
  115. void discard_history(int p_idx, bool p_erase_from_map = true);
  116. static EditorUndoRedoManager *get_singleton();
  117. EditorUndoRedoManager();
  118. ~EditorUndoRedoManager();
  119. };
  120. VARIANT_ENUM_CAST(EditorUndoRedoManager::SpecialHistory);
  121. #endif // EDITOR_UNDO_REDO_MANAGER_H