Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/editor/editor_undo_redo_manager.cpp
20843 views
1
/**************************************************************************/
2
/* editor_undo_redo_manager.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 "editor_undo_redo_manager.h"
32
#include "editor_undo_redo_manager.compat.inc"
33
34
#include "core/io/resource.h"
35
#include "core/os/os.h"
36
#include "editor/debugger/editor_debugger_inspector.h"
37
#include "editor/debugger/editor_debugger_node.h"
38
#include "editor/editor_log.h"
39
#include "editor/editor_node.h"
40
#include "scene/main/node.h"
41
42
EditorUndoRedoManager *EditorUndoRedoManager::singleton = nullptr;
43
44
EditorUndoRedoManager::History &EditorUndoRedoManager::get_or_create_history(int p_idx) {
45
if (!history_map.has(p_idx)) {
46
History history;
47
history.undo_redo = memnew(UndoRedo);
48
history.id = p_idx;
49
history_map[p_idx] = history;
50
51
EditorNode::get_singleton()->get_log()->register_undo_redo(history.undo_redo);
52
EditorDebuggerNode::get_singleton()->register_undo_redo(history.undo_redo);
53
}
54
return history_map[p_idx];
55
}
56
57
UndoRedo *EditorUndoRedoManager::get_history_undo_redo(int p_idx) const {
58
ERR_FAIL_COND_V(!history_map.has(p_idx), nullptr);
59
return history_map[p_idx].undo_redo;
60
}
61
62
int EditorUndoRedoManager::get_history_id_for_object(Object *p_object) const {
63
int history_id = INVALID_HISTORY;
64
65
if (Object::cast_to<EditorDebuggerRemoteObjects>(p_object)) {
66
return REMOTE_HISTORY;
67
}
68
69
if (Node *node = Object::cast_to<Node>(p_object)) {
70
Node *edited_scene = EditorNode::get_singleton()->get_edited_scene();
71
72
if (edited_scene && (node == edited_scene || edited_scene->is_ancestor_of(node))) {
73
int idx = EditorNode::get_editor_data().get_current_edited_scene_history_id();
74
if (idx > 0) {
75
history_id = idx;
76
}
77
}
78
}
79
80
if (Resource *res = Object::cast_to<Resource>(p_object)) {
81
if (res->is_built_in()) {
82
if (res->get_path().is_empty()) {
83
int idx = EditorNode::get_editor_data().get_current_edited_scene_history_id();
84
if (idx > 0) {
85
history_id = idx;
86
}
87
} else {
88
int idx = EditorNode::get_editor_data().get_scene_history_id_from_path(res->get_path().get_slice("::", 0));
89
if (idx > 0) {
90
history_id = idx;
91
}
92
}
93
}
94
}
95
96
if (history_id == INVALID_HISTORY) {
97
if (pending_action.history_id != INVALID_HISTORY) {
98
history_id = pending_action.history_id;
99
} else {
100
history_id = GLOBAL_HISTORY;
101
}
102
}
103
return history_id;
104
}
105
106
EditorUndoRedoManager::History &EditorUndoRedoManager::get_history_for_object(Object *p_object) {
107
int history_id;
108
if (!forced_history) {
109
history_id = get_history_id_for_object(p_object);
110
ERR_FAIL_COND_V_MSG(pending_action.history_id != INVALID_HISTORY && history_id != pending_action.history_id, get_or_create_history(pending_action.history_id), vformat("UndoRedo history mismatch: expected %d, got %d.", pending_action.history_id, history_id));
111
} else {
112
history_id = pending_action.history_id;
113
}
114
115
History &history = get_or_create_history(history_id);
116
if (pending_action.history_id == INVALID_HISTORY) {
117
pending_action.history_id = history_id;
118
history.undo_redo->create_action(pending_action.action_name, pending_action.merge_mode, pending_action.backward_undo_ops);
119
}
120
121
return history;
122
}
123
124
void EditorUndoRedoManager::force_fixed_history() {
125
ERR_FAIL_COND_MSG(pending_action.history_id == INVALID_HISTORY, "The current action has no valid history assigned.");
126
forced_history = true;
127
}
128
129
void EditorUndoRedoManager::create_action_for_history(const String &p_name, int p_history_id, UndoRedo::MergeMode p_mode, bool p_backward_undo_ops, bool p_mark_unsaved) {
130
if (pending_action.history_id != INVALID_HISTORY) {
131
// Nested action.
132
p_history_id = pending_action.history_id;
133
} else {
134
pending_action.action_name = p_name;
135
pending_action.timestamp = OS::get_singleton()->get_unix_time();
136
pending_action.merge_mode = p_mode;
137
pending_action.backward_undo_ops = p_backward_undo_ops;
138
pending_action.mark_unsaved = p_mark_unsaved;
139
}
140
141
if (p_history_id != INVALID_HISTORY) {
142
pending_action.history_id = p_history_id;
143
History &history = get_or_create_history(p_history_id);
144
history.undo_redo->create_action(pending_action.action_name, pending_action.merge_mode, pending_action.backward_undo_ops);
145
}
146
}
147
148
void EditorUndoRedoManager::create_action(const String &p_name, UndoRedo::MergeMode p_mode, Object *p_custom_context, bool p_backward_undo_ops, bool p_mark_unsaved) {
149
create_action_for_history(p_name, INVALID_HISTORY, p_mode, p_backward_undo_ops, p_mark_unsaved);
150
151
if (p_custom_context) {
152
// This assigns history to pending action.
153
get_history_for_object(p_custom_context);
154
}
155
}
156
157
void EditorUndoRedoManager::add_do_methodp(Object *p_object, const StringName &p_method, const Variant **p_args, int p_argcount) {
158
UndoRedo *undo_redo = get_history_for_object(p_object).undo_redo;
159
undo_redo->add_do_method(Callable(p_object, p_method).bindp(p_args, p_argcount));
160
}
161
162
void EditorUndoRedoManager::add_undo_methodp(Object *p_object, const StringName &p_method, const Variant **p_args, int p_argcount) {
163
UndoRedo *undo_redo = get_history_for_object(p_object).undo_redo;
164
undo_redo->add_undo_method(Callable(p_object, p_method).bindp(p_args, p_argcount));
165
}
166
167
void EditorUndoRedoManager::_add_do_method(const Variant **p_args, int p_argcount, Callable::CallError &r_error) {
168
if (p_argcount < 2) {
169
r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
170
r_error.expected = 2;
171
return;
172
}
173
174
if (p_args[0]->get_type() != Variant::OBJECT) {
175
r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
176
r_error.argument = 0;
177
r_error.expected = Variant::OBJECT;
178
return;
179
}
180
181
if (!p_args[1]->is_string()) {
182
r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
183
r_error.argument = 1;
184
r_error.expected = Variant::STRING_NAME;
185
return;
186
}
187
188
r_error.error = Callable::CallError::CALL_OK;
189
190
Object *object = *p_args[0];
191
StringName method = *p_args[1];
192
193
add_do_methodp(object, method, p_args + 2, p_argcount - 2);
194
}
195
196
void EditorUndoRedoManager::_add_undo_method(const Variant **p_args, int p_argcount, Callable::CallError &r_error) {
197
if (p_argcount < 2) {
198
r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
199
r_error.expected = 2;
200
return;
201
}
202
203
if (p_args[0]->get_type() != Variant::OBJECT) {
204
r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
205
r_error.argument = 0;
206
r_error.expected = Variant::OBJECT;
207
return;
208
}
209
210
if (!p_args[1]->is_string()) {
211
r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
212
r_error.argument = 1;
213
r_error.expected = Variant::STRING_NAME;
214
return;
215
}
216
217
r_error.error = Callable::CallError::CALL_OK;
218
219
Object *object = *p_args[0];
220
StringName method = *p_args[1];
221
222
add_undo_methodp(object, method, p_args + 2, p_argcount - 2);
223
}
224
225
void EditorUndoRedoManager::add_do_property(Object *p_object, const StringName &p_property, const Variant &p_value) {
226
UndoRedo *undo_redo = get_history_for_object(p_object).undo_redo;
227
undo_redo->add_do_property(p_object, p_property, p_value);
228
}
229
230
void EditorUndoRedoManager::add_undo_property(Object *p_object, const StringName &p_property, const Variant &p_value) {
231
UndoRedo *undo_redo = get_history_for_object(p_object).undo_redo;
232
undo_redo->add_undo_property(p_object, p_property, p_value);
233
}
234
235
void EditorUndoRedoManager::add_do_reference(Object *p_object) {
236
UndoRedo *undo_redo = get_history_for_object(p_object).undo_redo;
237
undo_redo->add_do_reference(p_object);
238
}
239
240
void EditorUndoRedoManager::add_undo_reference(Object *p_object) {
241
UndoRedo *undo_redo = get_history_for_object(p_object).undo_redo;
242
undo_redo->add_undo_reference(p_object);
243
}
244
245
void EditorUndoRedoManager::commit_action(bool p_execute) {
246
if (pending_action.history_id == INVALID_HISTORY) {
247
return; // Empty action, do nothing.
248
}
249
250
forced_history = false;
251
is_committing = true;
252
253
History &history = get_or_create_history(pending_action.history_id);
254
bool merging = history.undo_redo->is_merging();
255
history.undo_redo->commit_action(p_execute);
256
257
if (history.undo_redo->get_action_level() > 0) {
258
// Nested action.
259
is_committing = false;
260
return;
261
}
262
263
history.redo_stack.clear();
264
// If you undo history beyond saved version and modify it, the saved version can no longer be restored.
265
if (history.saved_version >= history.undo_redo->get_version()) {
266
history.saved_version = UNSAVED_VERSION;
267
}
268
269
if (!merging) {
270
history.undo_stack.push_back(pending_action);
271
}
272
273
if (history.id != GLOBAL_HISTORY) {
274
// Clear global redo, to avoid unexpected actions when redoing.
275
History &global = get_or_create_history(GLOBAL_HISTORY);
276
global.redo_stack.clear();
277
global.undo_redo->discard_redo();
278
if (global.saved_version > global.undo_redo->get_version()) {
279
global.saved_version = UNSAVED_VERSION;
280
}
281
} else {
282
// On global actions, clear redo of all scenes instead.
283
for (KeyValue<int, History> &E : history_map) {
284
if (E.key == GLOBAL_HISTORY) {
285
continue;
286
}
287
E.value.redo_stack.clear();
288
E.value.undo_redo->discard_redo();
289
if (E.value.saved_version > E.value.undo_redo->get_version()) {
290
E.value.saved_version = UNSAVED_VERSION;
291
}
292
}
293
}
294
295
pending_action = Action();
296
is_committing = false;
297
emit_signal(SNAME("history_changed"));
298
}
299
300
bool EditorUndoRedoManager::is_committing_action() const {
301
return is_committing;
302
}
303
304
bool EditorUndoRedoManager::undo() {
305
if (!has_undo()) {
306
return false;
307
}
308
309
History *selected_history = _get_newest_undo();
310
if (selected_history) {
311
return undo_history(selected_history->id);
312
}
313
return false;
314
}
315
316
bool EditorUndoRedoManager::undo_history(int p_id) {
317
ERR_FAIL_COND_V(p_id == INVALID_HISTORY, false);
318
History &history = get_or_create_history(p_id);
319
320
Action action = history.undo_stack.back()->get();
321
history.undo_stack.pop_back();
322
history.redo_stack.push_back(action);
323
324
bool success = history.undo_redo->undo();
325
if (success) {
326
emit_signal(SNAME("version_changed"));
327
}
328
return success;
329
}
330
331
bool EditorUndoRedoManager::redo() {
332
if (!has_redo()) {
333
return false;
334
}
335
336
int selected_history = INVALID_HISTORY;
337
double global_timestamp = Math::INF;
338
339
// Pick the history with lowest last action timestamp (either global or current scene).
340
{
341
History &history = get_or_create_history(GLOBAL_HISTORY);
342
if (!history.redo_stack.is_empty()) {
343
selected_history = history.id;
344
global_timestamp = history.redo_stack.back()->get().timestamp;
345
}
346
}
347
348
{
349
History &history = get_or_create_history(REMOTE_HISTORY);
350
if (!history.redo_stack.is_empty() && history.redo_stack.back()->get().timestamp < global_timestamp) {
351
selected_history = history.id;
352
global_timestamp = history.redo_stack.back()->get().timestamp;
353
}
354
}
355
356
{
357
History &history = get_or_create_history(EditorNode::get_editor_data().get_current_edited_scene_history_id());
358
if (!history.redo_stack.is_empty() && history.redo_stack.back()->get().timestamp < global_timestamp) {
359
selected_history = history.id;
360
}
361
}
362
363
if (selected_history != INVALID_HISTORY) {
364
return redo_history(selected_history);
365
}
366
return false;
367
}
368
369
bool EditorUndoRedoManager::redo_history(int p_id) {
370
ERR_FAIL_COND_V(p_id == INVALID_HISTORY, false);
371
History &history = get_or_create_history(p_id);
372
373
Action action = history.redo_stack.back()->get();
374
history.redo_stack.pop_back();
375
history.undo_stack.push_back(action);
376
377
bool success = history.undo_redo->redo();
378
if (success) {
379
emit_signal(SNAME("version_changed"));
380
}
381
return success;
382
}
383
384
void EditorUndoRedoManager::set_history_as_saved(int p_id) {
385
History &history = get_or_create_history(p_id);
386
history.saved_version = history.undo_redo->get_version();
387
}
388
389
void EditorUndoRedoManager::set_history_as_unsaved(int p_id) {
390
History &history = get_or_create_history(p_id);
391
history.saved_version = UNSAVED_VERSION;
392
emit_signal(SNAME("history_changed"));
393
}
394
395
bool EditorUndoRedoManager::is_history_unsaved(int p_id) {
396
History &history = get_or_create_history(p_id);
397
if (history.saved_version == UNSAVED_VERSION) {
398
return true;
399
}
400
401
int version_difference = history.undo_redo->get_version() - history.saved_version;
402
if (version_difference > 0) {
403
List<Action>::Element *E = history.undo_stack.back();
404
for (int i = 0; i < version_difference; i++) {
405
ERR_FAIL_NULL_V_MSG(E, false, "Inconsistent undo history.");
406
if (E->get().mark_unsaved) {
407
return true;
408
}
409
E = E->prev();
410
}
411
} else if (version_difference < 0) {
412
List<Action>::Element *E = history.redo_stack.back();
413
for (int i = 0; i > version_difference; i--) {
414
ERR_FAIL_NULL_V_MSG(E, false, "Inconsistent redo history.");
415
if (E->get().mark_unsaved) {
416
return true;
417
}
418
E = E->prev();
419
}
420
}
421
return false;
422
}
423
424
bool EditorUndoRedoManager::has_undo() {
425
for (const KeyValue<int, History> &E : history_map) {
426
if ((E.key == GLOBAL_HISTORY || E.key == REMOTE_HISTORY || E.key == EditorNode::get_editor_data().get_current_edited_scene_history_id()) && !E.value.undo_stack.is_empty()) {
427
return true;
428
}
429
}
430
return false;
431
}
432
433
bool EditorUndoRedoManager::has_redo() {
434
for (const KeyValue<int, History> &E : history_map) {
435
if ((E.key == GLOBAL_HISTORY || E.key == REMOTE_HISTORY || E.key == EditorNode::get_editor_data().get_current_edited_scene_history_id()) && !E.value.redo_stack.is_empty()) {
436
return true;
437
}
438
}
439
return false;
440
}
441
442
bool EditorUndoRedoManager::has_history(int p_idx) const {
443
return history_map.has(p_idx);
444
}
445
446
void EditorUndoRedoManager::clear_history(int p_idx, bool p_increase_version) {
447
if (p_idx != INVALID_HISTORY) {
448
History &history = get_or_create_history(p_idx);
449
history.undo_redo->clear_history(p_increase_version);
450
history.undo_stack.clear();
451
history.redo_stack.clear();
452
453
if (p_increase_version) {
454
history.saved_version = UNSAVED_VERSION;
455
} else {
456
set_history_as_saved(p_idx);
457
}
458
emit_signal(SNAME("history_changed"));
459
return;
460
}
461
462
for (KeyValue<int, History> &E : history_map) {
463
if (E.key == REMOTE_HISTORY) {
464
continue;
465
}
466
E.value.undo_redo->clear_history(p_increase_version);
467
E.value.undo_stack.clear();
468
E.value.redo_stack.clear();
469
set_history_as_saved(E.key);
470
}
471
emit_signal(SNAME("history_changed"));
472
}
473
474
String EditorUndoRedoManager::get_current_action_name() {
475
if (has_undo()) {
476
History *selected_history = _get_newest_undo();
477
if (selected_history) {
478
return selected_history->undo_redo->get_current_action_name();
479
}
480
}
481
return "";
482
}
483
484
int EditorUndoRedoManager::get_current_action_history_id() {
485
if (has_undo()) {
486
History *selected_history = _get_newest_undo();
487
if (selected_history) {
488
return selected_history->id;
489
}
490
}
491
return INVALID_HISTORY;
492
}
493
494
void EditorUndoRedoManager::discard_history(int p_idx, bool p_erase_from_map) {
495
ERR_FAIL_COND(!history_map.has(p_idx));
496
History &history = history_map[p_idx];
497
498
if (history.undo_redo) {
499
memdelete(history.undo_redo);
500
history.undo_redo = nullptr;
501
}
502
503
if (p_erase_from_map) {
504
history_map.erase(p_idx);
505
}
506
}
507
508
EditorUndoRedoManager::History *EditorUndoRedoManager::_get_newest_undo() {
509
History *selected_history = nullptr;
510
double global_timestamp = 0;
511
512
// Pick the history with greatest last action timestamp (either global or current scene).
513
{
514
History &history = get_or_create_history(GLOBAL_HISTORY);
515
if (!history.undo_stack.is_empty()) {
516
selected_history = &history;
517
global_timestamp = history.undo_stack.back()->get().timestamp;
518
}
519
}
520
521
{
522
History &history = get_or_create_history(REMOTE_HISTORY);
523
if (!history.undo_stack.is_empty() && history.undo_stack.back()->get().timestamp > global_timestamp) {
524
selected_history = &history;
525
global_timestamp = history.undo_stack.back()->get().timestamp;
526
}
527
}
528
529
{
530
History &history = get_or_create_history(EditorNode::get_editor_data().get_current_edited_scene_history_id());
531
if (!history.undo_stack.is_empty() && history.undo_stack.back()->get().timestamp > global_timestamp) {
532
selected_history = &history;
533
}
534
}
535
536
return selected_history;
537
}
538
539
void EditorUndoRedoManager::_bind_methods() {
540
ClassDB::bind_method(D_METHOD("create_action", "name", "merge_mode", "custom_context", "backward_undo_ops", "mark_unsaved"), &EditorUndoRedoManager::create_action, DEFVAL(UndoRedo::MERGE_DISABLE), DEFVAL((Object *)nullptr), DEFVAL(false), DEFVAL(true));
541
ClassDB::bind_method(D_METHOD("commit_action", "execute"), &EditorUndoRedoManager::commit_action, DEFVAL(true));
542
ClassDB::bind_method(D_METHOD("is_committing_action"), &EditorUndoRedoManager::is_committing_action);
543
ClassDB::bind_method(D_METHOD("force_fixed_history"), &EditorUndoRedoManager::force_fixed_history);
544
545
{
546
MethodInfo mi;
547
mi.name = "add_do_method";
548
mi.arguments.push_back(PropertyInfo(Variant::OBJECT, "object"));
549
mi.arguments.push_back(PropertyInfo(Variant::STRING_NAME, "method"));
550
551
ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "add_do_method", &EditorUndoRedoManager::_add_do_method, mi, varray(), false);
552
}
553
554
{
555
MethodInfo mi;
556
mi.name = "add_undo_method";
557
mi.arguments.push_back(PropertyInfo(Variant::OBJECT, "object"));
558
mi.arguments.push_back(PropertyInfo(Variant::STRING_NAME, "method"));
559
560
ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "add_undo_method", &EditorUndoRedoManager::_add_undo_method, mi, varray(), false);
561
}
562
563
ClassDB::bind_method(D_METHOD("add_do_property", "object", "property", "value"), &EditorUndoRedoManager::add_do_property);
564
ClassDB::bind_method(D_METHOD("add_undo_property", "object", "property", "value"), &EditorUndoRedoManager::add_undo_property);
565
ClassDB::bind_method(D_METHOD("add_do_reference", "object"), &EditorUndoRedoManager::add_do_reference);
566
ClassDB::bind_method(D_METHOD("add_undo_reference", "object"), &EditorUndoRedoManager::add_undo_reference);
567
568
ClassDB::bind_method(D_METHOD("get_object_history_id", "object"), &EditorUndoRedoManager::get_history_id_for_object);
569
ClassDB::bind_method(D_METHOD("get_history_undo_redo", "id"), &EditorUndoRedoManager::get_history_undo_redo);
570
ClassDB::bind_method(D_METHOD("clear_history", "id", "increase_version"), &EditorUndoRedoManager::clear_history, DEFVAL(INVALID_HISTORY), DEFVAL(true));
571
572
ADD_SIGNAL(MethodInfo("history_changed"));
573
ADD_SIGNAL(MethodInfo("version_changed"));
574
575
BIND_ENUM_CONSTANT(GLOBAL_HISTORY);
576
BIND_ENUM_CONSTANT(REMOTE_HISTORY);
577
BIND_ENUM_CONSTANT(INVALID_HISTORY);
578
}
579
580
EditorUndoRedoManager *EditorUndoRedoManager::get_singleton() {
581
return singleton;
582
}
583
584
EditorUndoRedoManager::EditorUndoRedoManager() {
585
if (!singleton) {
586
singleton = this;
587
}
588
}
589
590
EditorUndoRedoManager::~EditorUndoRedoManager() {
591
for (const KeyValue<int, History> &E : history_map) {
592
discard_history(E.key, false);
593
}
594
}
595
596