editor_undo_redo_manager.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. };
  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 is_committing = false;
  61. History *_get_newest_undo();
  62. protected:
  63. static void _bind_methods();
  64. public:
  65. History &get_or_create_history(int p_idx);
  66. UndoRedo *get_history_undo_redo(int p_idx) const;
  67. int get_history_id_for_object(Object *p_object) const;
  68. History &get_history_for_object(Object *p_object);
  69. void create_action_for_history(const String &p_name, int p_history_id, UndoRedo::MergeMode p_mode = UndoRedo::MERGE_DISABLE);
  70. void create_action(const String &p_name = "", UndoRedo::MergeMode p_mode = UndoRedo::MERGE_DISABLE, Object *p_custom_context = nullptr);
  71. void add_do_methodp(Object *p_object, const StringName &p_method, const Variant **p_args, int p_argcount);
  72. void add_undo_methodp(Object *p_object, const StringName &p_method, const Variant **p_args, int p_argcount);
  73. template <typename... VarArgs>
  74. void add_do_method(Object *p_object, const StringName &p_method, VarArgs... p_args) {
  75. Variant args[sizeof...(p_args) + 1] = { p_args..., Variant() }; // +1 makes sure zero sized arrays are also supported.
  76. const Variant *argptrs[sizeof...(p_args) + 1];
  77. for (uint32_t i = 0; i < sizeof...(p_args); i++) {
  78. argptrs[i] = &args[i];
  79. }
  80. add_do_methodp(p_object, p_method, sizeof...(p_args) == 0 ? nullptr : (const Variant **)argptrs, sizeof...(p_args));
  81. }
  82. template <typename... VarArgs>
  83. void add_undo_method(Object *p_object, const StringName &p_method, VarArgs... p_args) {
  84. Variant args[sizeof...(p_args) + 1] = { p_args..., Variant() }; // +1 makes sure zero sized arrays are also supported.
  85. const Variant *argptrs[sizeof...(p_args) + 1];
  86. for (uint32_t i = 0; i < sizeof...(p_args); i++) {
  87. argptrs[i] = &args[i];
  88. }
  89. add_undo_methodp(p_object, p_method, sizeof...(p_args) == 0 ? nullptr : (const Variant **)argptrs, sizeof...(p_args));
  90. }
  91. void _add_do_method(const Variant **p_args, int p_argcount, Callable::CallError &r_error);
  92. void _add_undo_method(const Variant **p_args, int p_argcount, Callable::CallError &r_error);
  93. void add_do_property(Object *p_object, const StringName &p_property, const Variant &p_value);
  94. void add_undo_property(Object *p_object, const StringName &p_property, const Variant &p_value);
  95. void add_do_reference(Object *p_object);
  96. void add_undo_reference(Object *p_object);
  97. void commit_action(bool p_execute = true);
  98. bool is_committing_action() const;
  99. bool undo();
  100. bool undo_history(int p_id);
  101. bool redo();
  102. bool redo_history(int p_id);
  103. void clear_history(bool p_increase_version = true, int p_idx = INVALID_HISTORY);
  104. void set_history_as_saved(int p_idx);
  105. void set_history_as_unsaved(int p_idx);
  106. bool is_history_unsaved(int p_idx);
  107. bool has_undo();
  108. bool has_redo();
  109. String get_current_action_name();
  110. int get_current_action_history_id();
  111. void discard_history(int p_idx, bool p_erase_from_map = true);
  112. static EditorUndoRedoManager *get_singleton();
  113. EditorUndoRedoManager();
  114. ~EditorUndoRedoManager();
  115. };
  116. VARIANT_ENUM_CAST(EditorUndoRedoManager::SpecialHistory);
  117. #endif // EDITOR_UNDO_REDO_MANAGER_H