editor_undo_redo_manager.h 6.0 KB

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