Debian Codegraph @

core/object/undo_redo.cpp

raw symbols SCIP
1 /**************************************************************************/
2 /* undo_redo.cpp */
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 #include "undo_redo.h"
32
33 #include "core/io/resource.h"
34 #include "core/os/os.h"
35 #include "core/templates/local_vector.h"
36 #include "core/variant/callable_bind.h"
37
38 void UndoRedo::Operation::delete_reference() {
39 if (type != Operation::TYPE_REFERENCE) {
40 return;
41 }
42 if (ref.is_valid()) {
43 ref.unref();
44 } else {
45 Object *obj = ObjectDB::get_instance(object);
46 if (obj) {
47 memdelete(obj);
48 }
49 }
50 }
51
52 void UndoRedo::discard_redo() {
53 if (current_action == actions.size() - 1) {
54 return;
55 }
56
57 for (int i = current_action + 1; i < actions.size(); i++) {
58 for (Operation &E : actions.write[i].do_ops) {
59 E.delete_reference();
60 }
61 //ERASE do data
62 }
63
64 actions.resize(current_action + 1);
65 }
66
67 bool UndoRedo::_redo(bool p_execute) {
68 ERR_FAIL_COND_V(action_level > 0, false);
69
70 if ((current_action + 1) >= actions.size()) {
71 return false; //nothing to redo
72 }
73
74 current_action++;
75
76 List<Operation>::Element *start_doops_element = actions.write[current_action].do_ops.front();
77 while (merge_total > 0 && start_doops_element) {
78 start_doops_element = start_doops_element->next();
79 merge_total--;
80 }
81
82 _process_operation_list(start_doops_element, p_execute);
83 version++;
84 emit_signal(SNAME("version_changed"));
85
86 return true;
87 }
88
89 void UndoRedo::create_action(const String &p_name, MergeMode p_mode, bool p_backward_undo_ops) {
90 uint64_t ticks = OS::get_singleton()->get_ticks_msec();
91
92 if (action_level == 0) {
93 discard_redo();
94
95 // Check if the merge operation is valid
96 if (p_mode != MERGE_DISABLE && actions.size() && actions[actions.size() - 1].name == p_name && actions[actions.size() - 1].backward_undo_ops == p_backward_undo_ops && actions[actions.size() - 1].last_tick + 800 > ticks) {
97 current_action = actions.size() - 2;
98
99 if (p_mode == MERGE_ENDS) {
100 // Clear all do ops from last action if they are not forced kept
101 LocalVector<List<Operation>::Element *> to_remove;
102 for (List<Operation>::Element *E = actions.write[current_action + 1].do_ops.front(); E; E = E->next()) {
103 if (!E->get().force_keep_in_merge_ends) {
104 to_remove.push_back(E);
105 }
106 }
107
108 for (List<Operation>::Element *E : to_remove) {
109 // Delete all object references
110 E->get().delete_reference();
111 E->erase();
112 }
113 }
114
115 if (p_mode == MERGE_ALL) {
116 merge_total = actions.write[current_action + 1].do_ops.size();
117 } else {
118 merge_total = 0;
119 }
120
121 actions.write[actions.size() - 1].last_tick = ticks;
122
123 // Revert reverse from previous commit.
124 if (actions[actions.size() - 1].backward_undo_ops) {
125 actions.write[actions.size() - 1].undo_ops.reverse();
126 }
127
128 merge_mode = p_mode;
129 merging = true;
130 } else {
131 Action new_action;
132 new_action.name = p_name;
133 new_action.last_tick = ticks;
134 new_action.backward_undo_ops = p_backward_undo_ops;
135 actions.push_back(new_action);
136
137 merge_mode = MERGE_DISABLE;
138 merge_total = 0;
139 }
140 }
141
142 action_level++;
143
144 force_keep_in_merge_ends = false;
145 }
146
147 void UndoRedo::add_do_method(const Callable &p_callable) {
148 ERR_FAIL_COND(!p_callable.is_valid());
149 ERR_FAIL_COND(action_level <= 0);
150 ERR_FAIL_COND((current_action + 1) >= actions.size());
151
152 ObjectID object_id = p_callable.get_object_id();
153 Object *object = ObjectDB::get_instance(object_id);
154 ERR_FAIL_COND(object_id.is_valid() && object == nullptr);
155
156 Operation do_op;
157 do_op.callable = p_callable;
158 do_op.object = object_id;
159 if (Object::cast_to<RefCounted>(object)) {
160 do_op.ref = Ref<RefCounted>(Object::cast_to<RefCounted>(object));
161 }
162 do_op.type = Operation::TYPE_METHOD;
163 do_op.name = p_callable.get_method();
164 if (do_op.name == StringName()) {
165 // There's no `get_method()` for custom callables, so use `operator String()` instead.
166 do_op.name = static_cast<String>(p_callable);
167 }
168
169 actions.write[current_action + 1].do_ops.push_back(do_op);
170 }
171
172 void UndoRedo::add_undo_method(const Callable &p_callable) {
173 ERR_FAIL_COND(!p_callable.is_valid());
174 ERR_FAIL_COND(action_level <= 0);
175 ERR_FAIL_COND((current_action + 1) >= actions.size());
176
177 // No undo if the merge mode is MERGE_ENDS
178 if (!force_keep_in_merge_ends && merge_mode == MERGE_ENDS) {
179 return;
180 }
181
182 ObjectID object_id = p_callable.get_object_id();
183 Object *object = ObjectDB::get_instance(object_id);
184 ERR_FAIL_COND(object_id.is_valid() && object == nullptr);
185
186 Operation undo_op;
187 undo_op.callable = p_callable;
188 undo_op.object = object_id;
189 if (Object::cast_to<RefCounted>(object)) {
190 undo_op.ref = Ref<RefCounted>(Object::cast_to<RefCounted>(object));
191 }
192 undo_op.type = Operation::TYPE_METHOD;
193 undo_op.force_keep_in_merge_ends = force_keep_in_merge_ends;
194 undo_op.name = p_callable.get_method();
195 if (undo_op.name == StringName()) {
196 // There's no `get_method()` for custom callables, so use `operator String()` instead.
197 undo_op.name = static_cast<String>(p_callable);
198 }
199
200 actions.write[current_action + 1].undo_ops.push_back(undo_op);
201 }
202
203 void UndoRedo::add_do_property(Object *p_object, const StringName &p_property, const Variant &p_value) {
204 ERR_FAIL_NULL(p_object);
205 ERR_FAIL_COND(action_level <= 0);
206 ERR_FAIL_COND((current_action + 1) >= actions.size());
207 Operation do_op;
208 do_op.object = p_object->get_instance_id();
209 if (Object::cast_to<RefCounted>(p_object)) {
210 do_op.ref = Ref<RefCounted>(Object::cast_to<RefCounted>(p_object));
211 }
212
213 do_op.type = Operation::TYPE_PROPERTY;
214 do_op.name = p_property;
215 do_op.value = p_value;
216 actions.write[current_action + 1].do_ops.push_back(do_op);
217 }
218
219 void UndoRedo::add_undo_property(Object *p_object, const StringName &p_property, const Variant &p_value) {
220 ERR_FAIL_NULL(p_object);
221 ERR_FAIL_COND(action_level <= 0);
222 ERR_FAIL_COND((current_action + 1) >= actions.size());
223
224 // No undo if the merge mode is MERGE_ENDS
225 if (!force_keep_in_merge_ends && merge_mode == MERGE_ENDS) {
226 return;
227 }
228
229 Operation undo_op;
230 undo_op.object = p_object->get_instance_id();
231 if (Object::cast_to<RefCounted>(p_object)) {
232 undo_op.ref = Ref<RefCounted>(Object::cast_to<RefCounted>(p_object));
233 }
234
235 undo_op.type = Operation::TYPE_PROPERTY;
236 undo_op.force_keep_in_merge_ends = force_keep_in_merge_ends;
237 undo_op.name = p_property;
238 undo_op.value = p_value;
239 actions.write[current_action + 1].undo_ops.push_back(undo_op);
240 }
241
242 void UndoRedo::add_do_reference(Object *p_object) {
243 ERR_FAIL_NULL(p_object);
244 ERR_FAIL_COND(action_level <= 0);
245 ERR_FAIL_COND((current_action + 1) >= actions.size());
246 Operation do_op;
247 do_op.object = p_object->get_instance_id();
248 if (Object::cast_to<RefCounted>(p_object)) {
249 do_op.ref = Ref<RefCounted>(Object::cast_to<RefCounted>(p_object));
250 }
251
252 do_op.type = Operation::TYPE_REFERENCE;
253 actions.write[current_action + 1].do_ops.push_back(do_op);
254 }
255
256 void UndoRedo::add_undo_reference(Object *p_object) {
257 ERR_FAIL_NULL(p_object);
258 ERR_FAIL_COND(action_level <= 0);
259 ERR_FAIL_COND((current_action + 1) >= actions.size());
260
261 // No undo if the merge mode is MERGE_ENDS
262 if (!force_keep_in_merge_ends && merge_mode == MERGE_ENDS) {
263 return;
264 }
265
266 Operation undo_op;
267 undo_op.object = p_object->get_instance_id();
268 if (Object::cast_to<RefCounted>(p_object)) {
269 undo_op.ref = Ref<RefCounted>(Object::cast_to<RefCounted>(p_object));
270 }
271
272 undo_op.type = Operation::TYPE_REFERENCE;
273 undo_op.force_keep_in_merge_ends = force_keep_in_merge_ends;
274 actions.write[current_action + 1].undo_ops.push_back(undo_op);
275 }
276
277 void UndoRedo::start_force_keep_in_merge_ends() {
278 ERR_FAIL_COND(action_level <= 0);
279 ERR_FAIL_COND((current_action + 1) >= actions.size());
280
281 force_keep_in_merge_ends = true;
282 }
283
284 void UndoRedo::end_force_keep_in_merge_ends() {
285 ERR_FAIL_COND(action_level <= 0);
286 ERR_FAIL_COND((current_action + 1) >= actions.size());
287
288 force_keep_in_merge_ends = false;
289 }
290
291 void UndoRedo::_pop_history_tail() {
292 discard_redo();
293
294 if (!actions.size()) {
295 return;
296 }
297
298 for (Operation &E : actions.write[0].undo_ops) {
299 E.delete_reference();
300 }
301
302 actions.remove_at(0);
303 if (current_action >= 0) {
304 current_action--;
305 }
306 }
307
308 bool UndoRedo::is_committing_action() const {
309 return committing > 0;
310 }
311
312 void UndoRedo::commit_action(bool p_execute) {
313 ERR_FAIL_COND(action_level <= 0);
314 action_level--;
315 if (action_level > 0) {
316 return; //still nested
317 }
318
319 bool add_message = !merging;
320
321 if (merging) {
322 version--;
323 merging = false;
324 }
325
326 if (actions[actions.size() - 1].backward_undo_ops) {
327 actions.write[actions.size() - 1].undo_ops.reverse();
328 }
329
330 committing++;
331 _redo(p_execute); // perform action
332 committing--;
333
334 if (max_steps > 0) {
335 // Clear early steps.
336
337 while (actions.size() > max_steps) {
338 _pop_history_tail();
339 }
340 }
341
342 if (add_message && callback && actions.size() > 0) {
343 callback(callback_ud, actions[actions.size() - 1].name);
344 }
345 }
346
347 void UndoRedo::_process_operation_list(List<Operation>::Element *E, bool p_execute) {
348 const int PREALLOCATE_ARGS_COUNT = 16;
349
350 LocalVector<const Variant *> args;
351 args.reserve(PREALLOCATE_ARGS_COUNT);
352
353 for (; E; E = E->next()) {
354 Operation &op = E->get();
355
356 Object *obj = ObjectDB::get_instance(op.object);
357 if (!obj) { //may have been deleted and this is fine
358 continue;
359 }
360
361 switch (op.type) {
362 case Operation::TYPE_METHOD: {
363 if (p_execute) {
364 Callable::CallError ce;
365 Variant ret;
366 op.callable.callp(nullptr, 0, ret, ce);
367 if (ce.error != Callable::CallError::CALL_OK) {
368 ERR_PRINT(vformat("Error calling UndoRedo method operation '%s': %s.", String(op.name), Variant::get_call_error_text(obj, op.name, nullptr, 0, ce)));
369 }
370 #ifdef TOOLS_ENABLED
371 Resource *res = Object::cast_to<Resource>(obj);
372 if (res) {
373 res->set_edited(true);
374 }
375 #endif
376 }
377
378 if (method_callback) {
379 Vector<Variant> binds;
380 if (op.callable.is_custom()) {
381 CallableCustomBind *ccb = dynamic_cast<CallableCustomBind *>(op.callable.get_custom());
382 if (ccb) {
383 binds = ccb->get_binds();
384 }
385 }
386
387 if (binds.is_empty()) {
388 method_callback(method_callback_ud, obj, op.name, nullptr, 0);
389 } else {
390 args.clear();
391
392 for (int i = 0; i < binds.size(); i++) {
393 args.push_back(&binds[i]);
394 }
395
396 method_callback(method_callback_ud, obj, op.name, args.ptr(), binds.size());
397 }
398 }
399 } break;
400 case Operation::TYPE_PROPERTY: {
401 if (p_execute) {
402 obj->set(op.name, op.value);
403 #ifdef TOOLS_ENABLED
404 Resource *res = Object::cast_to<Resource>(obj);
405 if (res) {
406 res->set_edited(true);
407 }
408 #endif
409 }
410
411 if (property_callback) {
412 property_callback(prop_callback_ud, obj, op.name, op.value);
413 }
414 } break;
415 case Operation::TYPE_REFERENCE: {
416 //do nothing
417 } break;
418 }
419 }
420 }
421
422 bool UndoRedo::redo() {
423 return _redo(true);
424 }
425
426 bool UndoRedo::undo() {
427 ERR_FAIL_COND_V(action_level > 0, false);
428 if (current_action < 0) {
429 return false; //nothing to redo
430 }
431 _process_operation_list(actions.write[current_action].undo_ops.front(), true);
432 current_action--;
433 version--;
434 emit_signal(SNAME("version_changed"));
435
436 return true;
437 }
438
439 int UndoRedo::get_history_count() {
440 ERR_FAIL_COND_V(action_level > 0, -1);
441
442 return actions.size();
443 }
444
445 int UndoRedo::get_current_action() {
446 ERR_FAIL_COND_V(action_level > 0, -1);
447
448 return current_action;
449 }
450
451 String UndoRedo::get_action_name(int p_id) {
452 ERR_FAIL_INDEX_V(p_id, actions.size(), "");
453
454 return actions[p_id].name;
455 }
456
457 void UndoRedo::clear_history(bool p_increase_version) {
458 ERR_FAIL_COND(action_level > 0);
459 discard_redo();
460
461 while (actions.size()) {
462 _pop_history_tail();
463 }
464
465 if (p_increase_version) {
466 version++;
467 emit_signal(SNAME("version_changed"));
468 }
469 }
470
471 String UndoRedo::get_current_action_name() const {
472 ERR_FAIL_COND_V(action_level > 0, "");
473 if (current_action < 0) {
474 return "";
475 }
476 return actions[current_action].name;
477 }
478
479 int UndoRedo::get_action_level() const {
480 return action_level;
481 }
482
483 bool UndoRedo::has_undo() const {
484 return current_action >= 0;
485 }
486
487 bool UndoRedo::has_redo() const {
488 return (current_action + 1) < actions.size();
489 }
490
491 bool UndoRedo::is_merging() const {
492 return merging;
493 }
494
495 uint64_t UndoRedo::get_version() const {
496 return version;
497 }
498
499 void UndoRedo::set_max_steps(int p_max_steps) {
500 max_steps = p_max_steps;
501 }
502
503 int UndoRedo::get_max_steps() const {
504 return max_steps;
505 }
506
507 void UndoRedo::set_commit_notify_callback(CommitNotifyCallback p_callback, void *p_ud) {
508 callback = p_callback;
509 callback_ud = p_ud;
510 }
511
512 void UndoRedo::set_method_notify_callback(MethodNotifyCallback p_method_callback, void *p_ud) {
513 method_callback = p_method_callback;
514 method_callback_ud = p_ud;
515 }
516
517 void UndoRedo::set_property_notify_callback(PropertyNotifyCallback p_property_callback, void *p_ud) {
518 property_callback = p_property_callback;
519 prop_callback_ud = p_ud;
520 }
521
522 UndoRedo::~UndoRedo() {
523 clear_history();
524 }
525
526 void UndoRedo::_bind_methods() {
527 ClassDB::bind_method(D_METHOD("create_action", "name", "merge_mode", "backward_undo_ops"), &UndoRedo::create_action, DEFVAL(MERGE_DISABLE), DEFVAL(false));
528 ClassDB::bind_method(D_METHOD("commit_action", "execute"), &UndoRedo::commit_action, DEFVAL(true));
529 ClassDB::bind_method(D_METHOD("is_committing_action"), &UndoRedo::is_committing_action);
530
531 ClassDB::bind_method(D_METHOD("add_do_method", "callable"), &UndoRedo::add_do_method);
532 ClassDB::bind_method(D_METHOD("add_undo_method", "callable"), &UndoRedo::add_undo_method);
533 ClassDB::bind_method(D_METHOD("add_do_property", "object", "property", "value"), &UndoRedo::add_do_property);
534 ClassDB::bind_method(D_METHOD("add_undo_property", "object", "property", "value"), &UndoRedo::add_undo_property);
535 ClassDB::bind_method(D_METHOD("add_do_reference", "object"), &UndoRedo::add_do_reference);
536 ClassDB::bind_method(D_METHOD("add_undo_reference", "object"), &UndoRedo::add_undo_reference);
537
538 ClassDB::bind_method(D_METHOD("start_force_keep_in_merge_ends"), &UndoRedo::start_force_keep_in_merge_ends);
539 ClassDB::bind_method(D_METHOD("end_force_keep_in_merge_ends"), &UndoRedo::end_force_keep_in_merge_ends);
540
541 ClassDB::bind_method(D_METHOD("get_history_count"), &UndoRedo::get_history_count);
542 ClassDB::bind_method(D_METHOD("get_current_action"), &UndoRedo::get_current_action);
543 ClassDB::bind_method(D_METHOD("get_action_name", "id"), &UndoRedo::get_action_name);
544 ClassDB::bind_method(D_METHOD("clear_history", "increase_version"), &UndoRedo::clear_history, DEFVAL(true));
545
546 ClassDB::bind_method(D_METHOD("get_current_action_name"), &UndoRedo::get_current_action_name);
547
548 ClassDB::bind_method(D_METHOD("has_undo"), &UndoRedo::has_undo);
549 ClassDB::bind_method(D_METHOD("has_redo"), &UndoRedo::has_redo);
550 ClassDB::bind_method(D_METHOD("get_version"), &UndoRedo::get_version);
551 ClassDB::bind_method(D_METHOD("set_max_steps", "max_steps"), &UndoRedo::set_max_steps);
552 ClassDB::bind_method(D_METHOD("get_max_steps"), &UndoRedo::get_max_steps);
553 ClassDB::bind_method(D_METHOD("redo"), &UndoRedo::redo);
554 ClassDB::bind_method(D_METHOD("undo"), &UndoRedo::undo);
555
556 ADD_PROPERTY(PropertyInfo(Variant::INT, "max_steps", PROPERTY_HINT_RANGE, "0,50,1,or_greater"), "set_max_steps", "get_max_steps");
557
558 ADD_SIGNAL(MethodInfo("version_changed"));
559
560 BIND_ENUM_CONSTANT(MERGE_DISABLE);
561 BIND_ENUM_CONSTANT(MERGE_ENDS);
562 BIND_ENUM_CONSTANT(MERGE_ALL);
563 }