undo_redo.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*************************************************************************/
  2. /* undo_redo.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 UNDO_REDO_H
  31. #define UNDO_REDO_H
  32. #include "core/object/class_db.h"
  33. #include "core/object/ref_counted.h"
  34. class UndoRedo : public Object {
  35. GDCLASS(UndoRedo, Object);
  36. OBJ_SAVE_TYPE(UndoRedo);
  37. public:
  38. enum MergeMode {
  39. MERGE_DISABLE,
  40. MERGE_ENDS,
  41. MERGE_ALL
  42. };
  43. typedef void (*CommitNotifyCallback)(void *p_ud, const String &p_name);
  44. void _add_do_method(const Variant **p_args, int p_argcount, Callable::CallError &r_error);
  45. void _add_undo_method(const Variant **p_args, int p_argcount, Callable::CallError &r_error);
  46. typedef void (*MethodNotifyCallback)(void *p_ud, Object *p_base, const StringName &p_name, const Variant **p_args, int p_argcount);
  47. typedef void (*PropertyNotifyCallback)(void *p_ud, Object *p_base, const StringName &p_property, const Variant &p_value);
  48. private:
  49. struct Operation {
  50. enum Type {
  51. TYPE_METHOD,
  52. TYPE_PROPERTY,
  53. TYPE_REFERENCE
  54. };
  55. Type type;
  56. bool force_keep_in_merge_ends;
  57. Ref<RefCounted> ref;
  58. ObjectID object;
  59. StringName name;
  60. Vector<Variant> args;
  61. void delete_reference();
  62. };
  63. struct Action {
  64. String name;
  65. List<Operation> do_ops;
  66. List<Operation> undo_ops;
  67. uint64_t last_tick;
  68. };
  69. Vector<Action> actions;
  70. int current_action = -1;
  71. bool force_keep_in_merge_ends = false;
  72. int action_level = 0;
  73. MergeMode merge_mode = MERGE_DISABLE;
  74. bool merging = false;
  75. uint64_t version = 1;
  76. void _pop_history_tail();
  77. void _process_operation_list(List<Operation>::Element *E);
  78. void _discard_redo();
  79. bool _redo(bool p_execute);
  80. CommitNotifyCallback callback = nullptr;
  81. void *callback_ud = nullptr;
  82. void *method_callback_ud = nullptr;
  83. void *prop_callback_ud = nullptr;
  84. MethodNotifyCallback method_callback = nullptr;
  85. PropertyNotifyCallback property_callback = nullptr;
  86. int committing = 0;
  87. protected:
  88. static void _bind_methods();
  89. public:
  90. void create_action(const String &p_name = "", MergeMode p_mode = MERGE_DISABLE);
  91. void add_do_methodp(Object *p_object, const StringName &p_method, const Variant **p_args, int p_argcount);
  92. void add_undo_methodp(Object *p_object, const StringName &p_method, const Variant **p_args, int p_argcount);
  93. template <typename... VarArgs>
  94. void add_do_method(Object *p_object, const StringName &p_method, VarArgs... p_args) {
  95. Variant args[sizeof...(p_args) + 1] = { p_args..., Variant() }; // +1 makes sure zero sized arrays are also supported.
  96. const Variant *argptrs[sizeof...(p_args) + 1];
  97. for (uint32_t i = 0; i < sizeof...(p_args); i++) {
  98. argptrs[i] = &args[i];
  99. }
  100. add_do_methodp(p_object, p_method, sizeof...(p_args) == 0 ? nullptr : (const Variant **)argptrs, sizeof...(p_args));
  101. }
  102. template <typename... VarArgs>
  103. void add_undo_method(Object *p_object, const StringName &p_method, VarArgs... p_args) {
  104. Variant args[sizeof...(p_args) + 1] = { p_args..., Variant() }; // +1 makes sure zero sized arrays are also supported.
  105. const Variant *argptrs[sizeof...(p_args) + 1];
  106. for (uint32_t i = 0; i < sizeof...(p_args); i++) {
  107. argptrs[i] = &args[i];
  108. }
  109. add_undo_methodp(p_object, p_method, sizeof...(p_args) == 0 ? nullptr : (const Variant **)argptrs, sizeof...(p_args));
  110. }
  111. void add_do_property(Object *p_object, const StringName &p_property, const Variant &p_value);
  112. void add_undo_property(Object *p_object, const StringName &p_property, const Variant &p_value);
  113. void add_do_reference(Object *p_object);
  114. void add_undo_reference(Object *p_object);
  115. void start_force_keep_in_merge_ends();
  116. void end_force_keep_in_merge_ends();
  117. bool is_committing_action() const;
  118. void commit_action(bool p_execute = true);
  119. bool redo();
  120. bool undo();
  121. String get_current_action_name() const;
  122. int get_history_count();
  123. int get_current_action();
  124. String get_action_name(int p_id);
  125. void clear_history(bool p_increase_version = true);
  126. bool has_undo() const;
  127. bool has_redo() const;
  128. uint64_t get_version() const;
  129. void set_commit_notify_callback(CommitNotifyCallback p_callback, void *p_ud);
  130. void set_method_notify_callback(MethodNotifyCallback p_method_callback, void *p_ud);
  131. void set_property_notify_callback(PropertyNotifyCallback p_property_callback, void *p_ud);
  132. UndoRedo() {}
  133. ~UndoRedo();
  134. };
  135. VARIANT_ENUM_CAST(UndoRedo::MergeMode);
  136. #endif // UNDO_REDO_H