editor_undo_redo_manager.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. #pragma once
  31. #include "core/object/object.h"
  32. #include "core/object/undo_redo.h"
  33. class EditorUndoRedoManager : public Object {
  34. GDCLASS(EditorUndoRedoManager, Object);
  35. static EditorUndoRedoManager *singleton;
  36. public:
  37. enum SpecialHistory {
  38. GLOBAL_HISTORY = 0,
  39. REMOTE_HISTORY = -9,
  40. INVALID_HISTORY = -99,
  41. };
  42. struct Action {
  43. int history_id = INVALID_HISTORY;
  44. double timestamp = 0;
  45. String action_name;
  46. UndoRedo::MergeMode merge_mode = UndoRedo::MERGE_DISABLE;
  47. bool backward_undo_ops = false;
  48. bool mark_unsaved = true;
  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. #ifndef DISABLE_DEPRECATED
  66. void _create_action_bind_compat_106121(const String &p_name = "", UndoRedo::MergeMode p_mode = UndoRedo::MERGE_DISABLE, Object *p_custom_context = nullptr, bool p_backward_undo_ops = false);
  67. static void _bind_compatibility_methods();
  68. #endif
  69. public:
  70. History &get_or_create_history(int p_idx);
  71. UndoRedo *get_history_undo_redo(int p_idx) const;
  72. int get_history_id_for_object(Object *p_object) const;
  73. History &get_history_for_object(Object *p_object);
  74. void force_fixed_history();
  75. 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, bool p_mark_unsaved = true);
  76. 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, bool p_mark_unsaved = true);
  77. void add_do_methodp(Object *p_object, const StringName &p_method, const Variant **p_args, int p_argcount);
  78. void add_undo_methodp(Object *p_object, const StringName &p_method, const Variant **p_args, int p_argcount);
  79. template <typename... VarArgs>
  80. void add_do_method(Object *p_object, const StringName &p_method, VarArgs... p_args) {
  81. Variant args[sizeof...(p_args) + 1] = { p_args..., Variant() }; // +1 makes sure zero sized arrays are also supported.
  82. const Variant *argptrs[sizeof...(p_args) + 1];
  83. for (uint32_t i = 0; i < sizeof...(p_args); i++) {
  84. argptrs[i] = &args[i];
  85. }
  86. add_do_methodp(p_object, p_method, sizeof...(p_args) == 0 ? nullptr : (const Variant **)argptrs, sizeof...(p_args));
  87. }
  88. template <typename... VarArgs>
  89. void add_undo_method(Object *p_object, const StringName &p_method, VarArgs... p_args) {
  90. Variant args[sizeof...(p_args) + 1] = { p_args..., Variant() }; // +1 makes sure zero sized arrays are also supported.
  91. const Variant *argptrs[sizeof...(p_args) + 1];
  92. for (uint32_t i = 0; i < sizeof...(p_args); i++) {
  93. argptrs[i] = &args[i];
  94. }
  95. add_undo_methodp(p_object, p_method, sizeof...(p_args) == 0 ? nullptr : (const Variant **)argptrs, sizeof...(p_args));
  96. }
  97. void _add_do_method(const Variant **p_args, int p_argcount, Callable::CallError &r_error);
  98. void _add_undo_method(const Variant **p_args, int p_argcount, Callable::CallError &r_error);
  99. void add_do_property(Object *p_object, const StringName &p_property, const Variant &p_value);
  100. void add_undo_property(Object *p_object, const StringName &p_property, const Variant &p_value);
  101. void add_do_reference(Object *p_object);
  102. void add_undo_reference(Object *p_object);
  103. void commit_action(bool p_execute = true);
  104. bool is_committing_action() const;
  105. bool undo();
  106. bool undo_history(int p_id);
  107. bool redo();
  108. bool redo_history(int p_id);
  109. void clear_history(int p_idx = INVALID_HISTORY, bool p_increase_version = true);
  110. void set_history_as_saved(int p_idx);
  111. void set_history_as_unsaved(int p_idx);
  112. bool is_history_unsaved(int p_idx);
  113. bool has_undo();
  114. bool has_redo();
  115. bool has_history(int p_idx) const;
  116. String get_current_action_name();
  117. int get_current_action_history_id();
  118. void discard_history(int p_idx, bool p_erase_from_map = true);
  119. static EditorUndoRedoManager *get_singleton();
  120. EditorUndoRedoManager();
  121. ~EditorUndoRedoManager();
  122. };
  123. VARIANT_ENUM_CAST(EditorUndoRedoManager::SpecialHistory);