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