undo_redo.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /**************************************************************************/
  2. /* undo_redo.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/class_db.h"
  32. #include "core/object/ref_counted.h"
  33. class UndoRedo : public Object {
  34. GDCLASS(UndoRedo, Object);
  35. OBJ_SAVE_TYPE(UndoRedo);
  36. public:
  37. enum MergeMode {
  38. MERGE_DISABLE,
  39. MERGE_ENDS,
  40. MERGE_ALL
  41. };
  42. typedef void (*CommitNotifyCallback)(void *p_ud, const String &p_name);
  43. typedef void (*MethodNotifyCallback)(void *p_ud, Object *p_base, const StringName &p_name, const Variant **p_args, int p_argcount);
  44. typedef void (*PropertyNotifyCallback)(void *p_ud, Object *p_base, const StringName &p_property, const Variant &p_value);
  45. private:
  46. struct Operation {
  47. enum Type {
  48. TYPE_METHOD,
  49. TYPE_PROPERTY,
  50. TYPE_REFERENCE
  51. } type;
  52. bool force_keep_in_merge_ends = false;
  53. Ref<RefCounted> ref;
  54. ObjectID object;
  55. StringName name;
  56. Callable callable;
  57. Variant value;
  58. void delete_reference();
  59. };
  60. struct Action {
  61. String name;
  62. List<Operation> do_ops;
  63. List<Operation> undo_ops;
  64. uint64_t last_tick = 0;
  65. bool backward_undo_ops = false;
  66. };
  67. Vector<Action> actions;
  68. int current_action = -1;
  69. bool force_keep_in_merge_ends = false;
  70. int action_level = 0;
  71. int max_steps = 0;
  72. MergeMode merge_mode = MERGE_DISABLE;
  73. bool merging = false;
  74. uint64_t version = 1;
  75. int merge_total = 0;
  76. void _pop_history_tail();
  77. void _process_operation_list(List<Operation>::Element *E, bool p_execute);
  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, bool p_backward_undo_ops = false);
  91. void add_do_method(const Callable &p_callable);
  92. void add_undo_method(const Callable &p_callable);
  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 start_force_keep_in_merge_ends();
  98. void end_force_keep_in_merge_ends();
  99. bool is_committing_action() const;
  100. void commit_action(bool p_execute = true);
  101. bool redo();
  102. bool undo();
  103. String get_current_action_name() const;
  104. int get_action_level() const;
  105. int get_history_count();
  106. int get_current_action();
  107. String get_action_name(int p_id);
  108. void clear_history(bool p_increase_version = true);
  109. void discard_redo();
  110. bool has_undo() const;
  111. bool has_redo() const;
  112. bool is_merging() const;
  113. uint64_t get_version() const;
  114. void set_max_steps(int p_max_steps);
  115. int get_max_steps() const;
  116. void set_commit_notify_callback(CommitNotifyCallback p_callback, void *p_ud);
  117. void set_method_notify_callback(MethodNotifyCallback p_method_callback, void *p_ud);
  118. void set_property_notify_callback(PropertyNotifyCallback p_property_callback, void *p_ud);
  119. ~UndoRedo();
  120. };
  121. VARIANT_ENUM_CAST(UndoRedo::MergeMode);