Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/editor/editor_undo_redo_manager.cpp
9821 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
history.redo_stack.clear();
257
258
if (history.undo_redo->get_action_level() > 0) {
259
// Nested action.
260
is_committing = false;
261
return;
262
}
263
264
if (!merging) {
265
history.undo_stack.push_back(pending_action);
266
}
267
268
if (history.id != GLOBAL_HISTORY) {
269
// Clear global redo, to avoid unexpected actions when redoing.
270
History &global = get_or_create_history(GLOBAL_HISTORY);
271
global.redo_stack.clear();
272
global.undo_redo->discard_redo();
273
} else {
274
// On global actions, clear redo of all scenes instead.
275
for (KeyValue<int, History> &E : history_map) {
276
if (E.key == GLOBAL_HISTORY) {
277
continue;
278
}
279
E.value.redo_stack.clear();
280
E.value.undo_redo->discard_redo();
281
}
282
}
283
284
pending_action = Action();
285
is_committing = false;
286
emit_signal(SNAME("history_changed"));
287
}
288
289
bool EditorUndoRedoManager::is_committing_action() const {
290
return is_committing;
291
}
292
293
bool EditorUndoRedoManager::undo() {
294
if (!has_undo()) {
295
return false;
296
}
297
298
History *selected_history = _get_newest_undo();
299
if (selected_history) {
300
return undo_history(selected_history->id);
301
}
302
return false;
303
}
304
305
bool EditorUndoRedoManager::undo_history(int p_id) {
306
ERR_FAIL_COND_V(p_id == INVALID_HISTORY, false);
307
History &history = get_or_create_history(p_id);
308
309
Action action = history.undo_stack.back()->get();
310
history.undo_stack.pop_back();
311
history.redo_stack.push_back(action);
312
313
bool success = history.undo_redo->undo();
314
if (success) {
315
emit_signal(SNAME("version_changed"));
316
}
317
return success;
318
}
319
320
bool EditorUndoRedoManager::redo() {
321
if (!has_redo()) {
322
return false;
323
}
324
325
int selected_history = INVALID_HISTORY;
326
double global_timestamp = Math::INF;
327
328
// Pick the history with lowest last action timestamp (either global or current scene).
329
{
330
History &history = get_or_create_history(GLOBAL_HISTORY);
331
if (!history.redo_stack.is_empty()) {
332
selected_history = history.id;
333
global_timestamp = history.redo_stack.back()->get().timestamp;
334
}
335
}
336
337
{
338
History &history = get_or_create_history(REMOTE_HISTORY);
339
if (!history.redo_stack.is_empty() && history.redo_stack.back()->get().timestamp < global_timestamp) {
340
selected_history = history.id;
341
global_timestamp = history.redo_stack.back()->get().timestamp;
342
}
343
}
344
345
{
346
History &history = get_or_create_history(EditorNode::get_editor_data().get_current_edited_scene_history_id());
347
if (!history.redo_stack.is_empty() && history.redo_stack.back()->get().timestamp < global_timestamp) {
348
selected_history = history.id;
349
}
350
}
351
352
if (selected_history != INVALID_HISTORY) {
353
return redo_history(selected_history);
354
}
355
return false;
356
}
357
358
bool EditorUndoRedoManager::redo_history(int p_id) {
359
ERR_FAIL_COND_V(p_id == INVALID_HISTORY, false);
360
History &history = get_or_create_history(p_id);
361
362
Action action = history.redo_stack.back()->get();
363
history.redo_stack.pop_back();
364
history.undo_stack.push_back(action);
365
366
bool success = history.undo_redo->redo();
367
if (success) {
368
emit_signal(SNAME("version_changed"));
369
}
370
return success;
371
}
372
373
void EditorUndoRedoManager::set_history_as_saved(int p_id) {
374
History &history = get_or_create_history(p_id);
375
history.saved_version = history.undo_redo->get_version();
376
}
377
378
void EditorUndoRedoManager::set_history_as_unsaved(int p_id) {
379
History &history = get_or_create_history(p_id);
380
history.saved_version = 0;
381
}
382
383
bool EditorUndoRedoManager::is_history_unsaved(int p_id) {
384
History &history = get_or_create_history(p_id);
385
if (history.saved_version == 0) {
386
return true;
387
}
388
389
int version_difference = history.undo_redo->get_version() - history.saved_version;
390
if (version_difference > 0) {
391
List<Action>::Element *E = history.undo_stack.back();
392
for (int i = 0; i < version_difference; i++) {
393
ERR_FAIL_NULL_V_MSG(E, false, "Inconsistent undo history.");
394
if (E->get().mark_unsaved) {
395
return true;
396
}
397
E = E->prev();
398
}
399
} else if (version_difference < 0) {
400
List<Action>::Element *E = history.redo_stack.back();
401
for (int i = 0; i > version_difference; i--) {
402
ERR_FAIL_NULL_V_MSG(E, false, "Inconsistent redo history.");
403
if (E->get().mark_unsaved) {
404
return true;
405
}
406
E = E->prev();
407
}
408
}
409
return false;
410
}
411
412
bool EditorUndoRedoManager::has_undo() {
413
for (const KeyValue<int, History> &E : history_map) {
414
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()) {
415
return true;
416
}
417
}
418
return false;
419
}
420
421
bool EditorUndoRedoManager::has_redo() {
422
for (const KeyValue<int, History> &E : history_map) {
423
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()) {
424
return true;
425
}
426
}
427
return false;
428
}
429
430
bool EditorUndoRedoManager::has_history(int p_idx) const {
431
return history_map.has(p_idx);
432
}
433
434
void EditorUndoRedoManager::clear_history(int p_idx, bool p_increase_version) {
435
if (p_idx != INVALID_HISTORY) {
436
History &history = get_or_create_history(p_idx);
437
history.undo_redo->clear_history(p_increase_version);
438
history.undo_stack.clear();
439
history.redo_stack.clear();
440
441
if (!p_increase_version) {
442
set_history_as_saved(p_idx);
443
}
444
emit_signal(SNAME("history_changed"));
445
return;
446
}
447
448
for (const KeyValue<int, History> &E : history_map) {
449
E.value.undo_redo->clear_history(p_increase_version);
450
set_history_as_saved(E.key);
451
}
452
emit_signal(SNAME("history_changed"));
453
}
454
455
String EditorUndoRedoManager::get_current_action_name() {
456
if (has_undo()) {
457
History *selected_history = _get_newest_undo();
458
if (selected_history) {
459
return selected_history->undo_redo->get_current_action_name();
460
}
461
}
462
return "";
463
}
464
465
int EditorUndoRedoManager::get_current_action_history_id() {
466
if (has_undo()) {
467
History *selected_history = _get_newest_undo();
468
if (selected_history) {
469
return selected_history->id;
470
}
471
}
472
return INVALID_HISTORY;
473
}
474
475
void EditorUndoRedoManager::discard_history(int p_idx, bool p_erase_from_map) {
476
ERR_FAIL_COND(!history_map.has(p_idx));
477
History &history = history_map[p_idx];
478
479
if (history.undo_redo) {
480
memdelete(history.undo_redo);
481
history.undo_redo = nullptr;
482
}
483
484
if (p_erase_from_map) {
485
history_map.erase(p_idx);
486
}
487
}
488
489
EditorUndoRedoManager::History *EditorUndoRedoManager::_get_newest_undo() {
490
History *selected_history = nullptr;
491
double global_timestamp = 0;
492
493
// Pick the history with greatest last action timestamp (either global or current scene).
494
{
495
History &history = get_or_create_history(GLOBAL_HISTORY);
496
if (!history.undo_stack.is_empty()) {
497
selected_history = &history;
498
global_timestamp = history.undo_stack.back()->get().timestamp;
499
}
500
}
501
502
{
503
History &history = get_or_create_history(REMOTE_HISTORY);
504
if (!history.undo_stack.is_empty() && history.undo_stack.back()->get().timestamp > global_timestamp) {
505
selected_history = &history;
506
global_timestamp = history.undo_stack.back()->get().timestamp;
507
}
508
}
509
510
{
511
History &history = get_or_create_history(EditorNode::get_editor_data().get_current_edited_scene_history_id());
512
if (!history.undo_stack.is_empty() && history.undo_stack.back()->get().timestamp > global_timestamp) {
513
selected_history = &history;
514
}
515
}
516
517
return selected_history;
518
}
519
520
void EditorUndoRedoManager::_bind_methods() {
521
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));
522
ClassDB::bind_method(D_METHOD("commit_action", "execute"), &EditorUndoRedoManager::commit_action, DEFVAL(true));
523
ClassDB::bind_method(D_METHOD("is_committing_action"), &EditorUndoRedoManager::is_committing_action);
524
ClassDB::bind_method(D_METHOD("force_fixed_history"), &EditorUndoRedoManager::force_fixed_history);
525
526
{
527
MethodInfo mi;
528
mi.name = "add_do_method";
529
mi.arguments.push_back(PropertyInfo(Variant::OBJECT, "object"));
530
mi.arguments.push_back(PropertyInfo(Variant::STRING_NAME, "method"));
531
532
ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "add_do_method", &EditorUndoRedoManager::_add_do_method, mi, varray(), false);
533
}
534
535
{
536
MethodInfo mi;
537
mi.name = "add_undo_method";
538
mi.arguments.push_back(PropertyInfo(Variant::OBJECT, "object"));
539
mi.arguments.push_back(PropertyInfo(Variant::STRING_NAME, "method"));
540
541
ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "add_undo_method", &EditorUndoRedoManager::_add_undo_method, mi, varray(), false);
542
}
543
544
ClassDB::bind_method(D_METHOD("add_do_property", "object", "property", "value"), &EditorUndoRedoManager::add_do_property);
545
ClassDB::bind_method(D_METHOD("add_undo_property", "object", "property", "value"), &EditorUndoRedoManager::add_undo_property);
546
ClassDB::bind_method(D_METHOD("add_do_reference", "object"), &EditorUndoRedoManager::add_do_reference);
547
ClassDB::bind_method(D_METHOD("add_undo_reference", "object"), &EditorUndoRedoManager::add_undo_reference);
548
549
ClassDB::bind_method(D_METHOD("get_object_history_id", "object"), &EditorUndoRedoManager::get_history_id_for_object);
550
ClassDB::bind_method(D_METHOD("get_history_undo_redo", "id"), &EditorUndoRedoManager::get_history_undo_redo);
551
ClassDB::bind_method(D_METHOD("clear_history", "id", "increase_version"), &EditorUndoRedoManager::clear_history, DEFVAL(INVALID_HISTORY), DEFVAL(true));
552
553
ADD_SIGNAL(MethodInfo("history_changed"));
554
ADD_SIGNAL(MethodInfo("version_changed"));
555
556
BIND_ENUM_CONSTANT(GLOBAL_HISTORY);
557
BIND_ENUM_CONSTANT(REMOTE_HISTORY);
558
BIND_ENUM_CONSTANT(INVALID_HISTORY);
559
}
560
561
EditorUndoRedoManager *EditorUndoRedoManager::get_singleton() {
562
return singleton;
563
}
564
565
EditorUndoRedoManager::EditorUndoRedoManager() {
566
if (!singleton) {
567
singleton = this;
568
}
569
}
570
571
EditorUndoRedoManager::~EditorUndoRedoManager() {
572
for (const KeyValue<int, History> &E : history_map) {
573
discard_history(E.key, false);
574
}
575
}
576
577