editor_undo_redo_manager.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  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/ref_counted.h"
  34. #include "core/object/undo_redo.h"
  35. class EditorUndoRedoManager : public RefCounted {
  36. GDCLASS(EditorUndoRedoManager, RefCounted);
  37. public:
  38. enum SpecialHistory {
  39. GLOBAL_HISTORY = 0,
  40. INVALID_HISTORY = -99,
  41. };
  42. private:
  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. };
  49. struct History {
  50. int id = INVALID_HISTORY;
  51. UndoRedo *undo_redo = nullptr;
  52. uint64_t saved_version = 1;
  53. List<Action> undo_stack;
  54. List<Action> redo_stack;
  55. };
  56. HashMap<int, History> history_map;
  57. Action pending_action;
  58. bool is_committing = false;
  59. protected:
  60. static void _bind_methods();
  61. public:
  62. History &get_or_create_history(int p_idx);
  63. UndoRedo *get_history_undo_redo(int p_idx) const;
  64. int get_history_id_for_object(Object *p_object) const;
  65. History &get_history_for_object(Object *p_object);
  66. void create_action_for_history(const String &p_name, int p_history_id, UndoRedo::MergeMode p_mode = UndoRedo::MERGE_DISABLE);
  67. void create_action(const String &p_name = "", UndoRedo::MergeMode p_mode = UndoRedo::MERGE_DISABLE, Object *p_custom_context = nullptr);
  68. void add_do_methodp(Object *p_object, const StringName &p_method, const Variant **p_args, int p_argcount);
  69. void add_undo_methodp(Object *p_object, const StringName &p_method, const Variant **p_args, int p_argcount);
  70. template <typename... VarArgs>
  71. void add_do_method(Object *p_object, const StringName &p_method, VarArgs... p_args) {
  72. Variant args[sizeof...(p_args) + 1] = { p_args..., Variant() }; // +1 makes sure zero sized arrays are also supported.
  73. const Variant *argptrs[sizeof...(p_args) + 1];
  74. for (uint32_t i = 0; i < sizeof...(p_args); i++) {
  75. argptrs[i] = &args[i];
  76. }
  77. add_do_methodp(p_object, p_method, sizeof...(p_args) == 0 ? nullptr : (const Variant **)argptrs, sizeof...(p_args));
  78. }
  79. template <typename... VarArgs>
  80. void add_undo_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_undo_methodp(p_object, p_method, sizeof...(p_args) == 0 ? nullptr : (const Variant **)argptrs, sizeof...(p_args));
  87. }
  88. void _add_do_method(const Variant **p_args, int p_argcount, Callable::CallError &r_error);
  89. void _add_undo_method(const Variant **p_args, int p_argcount, Callable::CallError &r_error);
  90. void add_do_property(Object *p_object, const StringName &p_property, const Variant &p_value);
  91. void add_undo_property(Object *p_object, const StringName &p_property, const Variant &p_value);
  92. void add_do_reference(Object *p_object);
  93. void add_undo_reference(Object *p_object);
  94. void commit_action(bool p_execute = true);
  95. bool is_committing_action() const;
  96. bool undo();
  97. bool redo();
  98. void clear_history(bool p_increase_version = true, int p_idx = INVALID_HISTORY);
  99. void set_history_as_saved(int p_idx);
  100. void set_history_as_unsaved(int p_idx);
  101. bool is_history_unsaved(int p_idx);
  102. bool has_undo();
  103. bool has_redo();
  104. String get_current_action_name();
  105. void discard_history(int p_idx, bool p_erase_from_map = true);
  106. ~EditorUndoRedoManager();
  107. };
  108. VARIANT_ENUM_CAST(EditorUndoRedoManager::SpecialHistory);
  109. #endif // EDITOR_UNDO_REDO_MANAGER_H