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