Debian Codegraph @

editor/editor_undo_redo_manager.cpp

raw symbols SCIP
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 }
393
394 bool EditorUndoRedoManager::is_history_unsaved(int p_id) {
395 History &history = get_or_create_history(p_id);
396 if (history.saved_version == UNSAVED_VERSION) {
397 return true;
398 }
399
400 int version_difference = history.undo_redo->get_version() - history.saved_version;
401 if (version_difference > 0) {
402 List<Action>::Element *E = history.undo_stack.back();
403 for (int i = 0; i < version_difference; i++) {
404 ERR_FAIL_NULL_V_MSG(E, false, "Inconsistent undo history.");
405 if (E->get().mark_unsaved) {
406 return true;
407 }
408 E = E->prev();
409 }
410 } else if (version_difference < 0) {
411 List<Action>::Element *E = history.redo_stack.back();
412 for (int i = 0; i > version_difference; i--) {
413 ERR_FAIL_NULL_V_MSG(E, false, "Inconsistent redo history.");
414 if (E->get().mark_unsaved) {
415 return true;
416 }
417 E = E->prev();
418 }
419 }
420 return false;
421 }
422
423 bool EditorUndoRedoManager::has_undo() {
424 for (const KeyValue<int, History> &E : history_map) {
425 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()) {
426 return true;
427 }
428 }
429 return false;
430 }
431
432 bool EditorUndoRedoManager::has_redo() {
433 for (const KeyValue<int, History> &E : history_map) {
434 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()) {
435 return true;
436 }
437 }
438 return false;
439 }
440
441 bool EditorUndoRedoManager::has_history(int p_idx) const {
442 return history_map.has(p_idx);
443 }
444
445 void EditorUndoRedoManager::clear_history(int p_idx, bool p_increase_version) {
446 if (p_idx != INVALID_HISTORY) {
447 History &history = get_or_create_history(p_idx);
448 history.undo_redo->clear_history(p_increase_version);
449 history.undo_stack.clear();
450 history.redo_stack.clear();
451
452 if (p_increase_version) {
453 history.saved_version = UNSAVED_VERSION;
454 } else {
455 set_history_as_saved(p_idx);
456 }
457 emit_signal(SNAME("history_changed"));
458 return;
459 }
460
461 for (KeyValue<int, History> &E : history_map) {
462 if (E.key == REMOTE_HISTORY) {
463 continue;
464 }
465 E.value.undo_redo->clear_history(p_increase_version);
466 E.value.undo_stack.clear();
467 E.value.redo_stack.clear();
468 set_history_as_saved(E.key);
469 }
470 emit_signal(SNAME("history_changed"));
471 }
472
473 String EditorUndoRedoManager::get_current_action_name() {
474 if (has_undo()) {
475 History *selected_history = _get_newest_undo();
476 if (selected_history) {
477 return selected_history->undo_redo->get_current_action_name();
478 }
479 }
480 return "";
481 }
482
483 int EditorUndoRedoManager::get_current_action_history_id() {
484 if (has_undo()) {
485 History *selected_history = _get_newest_undo();
486 if (selected_history) {
487 return selected_history->id;
488 }
489 }
490 return INVALID_HISTORY;
491 }
492
493 void EditorUndoRedoManager::discard_history(int p_idx, bool p_erase_from_map) {
494 ERR_FAIL_COND(!history_map.has(p_idx));
495 History &history = history_map[p_idx];
496
497 if (history.undo_redo) {
498 memdelete(history.undo_redo);
499 history.undo_redo = nullptr;
500 }
501
502 if (p_erase_from_map) {
503 history_map.erase(p_idx);
504 }
505 }
506
507 EditorUndoRedoManager::History *EditorUndoRedoManager::_get_newest_undo() {
508 History *selected_history = nullptr;
509 double global_timestamp = 0;
510
511 // Pick the history with greatest last action timestamp (either global or current scene).
512 {
513 History &history = get_or_create_history(GLOBAL_HISTORY);
514 if (!history.undo_stack.is_empty()) {
515 selected_history = &history;
516 global_timestamp = history.undo_stack.back()->get().timestamp;
517 }
518 }
519
520 {
521 History &history = get_or_create_history(REMOTE_HISTORY);
522 if (!history.undo_stack.is_empty() && history.undo_stack.back()->get().timestamp > global_timestamp) {
523 selected_history = &history;
524 global_timestamp = history.undo_stack.back()->get().timestamp;
525 }
526 }
527
528 {
529 History &history = get_or_create_history(EditorNode::get_editor_data().get_current_edited_scene_history_id());
530 if (!history.undo_stack.is_empty() && history.undo_stack.back()->get().timestamp > global_timestamp) {
531 selected_history = &history;
532 }
533 }
534
535 return selected_history;
536 }
537
538 void EditorUndoRedoManager::_bind_methods() {
539 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));
540 ClassDB::bind_method(D_METHOD("commit_action", "execute"), &EditorUndoRedoManager::commit_action, DEFVAL(true));
541 ClassDB::bind_method(D_METHOD("is_committing_action"), &EditorUndoRedoManager::is_committing_action);
542 ClassDB::bind_method(D_METHOD("force_fixed_history"), &EditorUndoRedoManager::force_fixed_history);
543
544 {
545 MethodInfo mi;
546 mi.name = "add_do_method";
547 mi.arguments.push_back(PropertyInfo(Variant::OBJECT, "object"));
548 mi.arguments.push_back(PropertyInfo(Variant::STRING_NAME, "method"));
549
550 ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "add_do_method", &EditorUndoRedoManager::_add_do_method, mi, varray(), false);
551 }
552
553 {
554 MethodInfo mi;
555 mi.name = "add_undo_method";
556 mi.arguments.push_back(PropertyInfo(Variant::OBJECT, "object"));
557 mi.arguments.push_back(PropertyInfo(Variant::STRING_NAME, "method"));
558
559 ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "add_undo_method", &EditorUndoRedoManager::_add_undo_method, mi, varray(), false);
560 }
561
562 ClassDB::bind_method(D_METHOD("add_do_property", "object", "property", "value"), &EditorUndoRedoManager::add_do_property);
563 ClassDB::bind_method(D_METHOD("add_undo_property", "object", "property", "value"), &EditorUndoRedoManager::add_undo_property);
564 ClassDB::bind_method(D_METHOD("add_do_reference", "object"), &EditorUndoRedoManager::add_do_reference);
565 ClassDB::bind_method(D_METHOD("add_undo_reference", "object"), &EditorUndoRedoManager::add_undo_reference);
566
567 ClassDB::bind_method(D_METHOD("get_object_history_id", "object"), &EditorUndoRedoManager::get_history_id_for_object);
568 ClassDB::bind_method(D_METHOD("get_history_undo_redo", "id"), &EditorUndoRedoManager::get_history_undo_redo);
569 ClassDB::bind_method(D_METHOD("clear_history", "id", "increase_version"), &EditorUndoRedoManager::clear_history, DEFVAL(INVALID_HISTORY), DEFVAL(true));
570
571 ADD_SIGNAL(MethodInfo("history_changed"));
572 ADD_SIGNAL(MethodInfo("version_changed"));
573
574 BIND_ENUM_CONSTANT(GLOBAL_HISTORY);
575 BIND_ENUM_CONSTANT(REMOTE_HISTORY);
576 BIND_ENUM_CONSTANT(INVALID_HISTORY);
577 }
578
579 EditorUndoRedoManager *EditorUndoRedoManager::get_singleton() {
580 return singleton;
581 }
582
583 EditorUndoRedoManager::EditorUndoRedoManager() {
584 if (!singleton) {
585 singleton = this;
586 }
587 }
588
589 EditorUndoRedoManager::~EditorUndoRedoManager() {
590 for (const KeyValue<int, History> &E : history_map) {
591 discard_history(E.key, false);
592 }
593 }