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
33#include "core/io/resource.h"
34#include "core/os/os.h"
35#include "core/templates/local_vector.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
42EditorUndoRedoManager *EditorUndoRedoManager::singleton = nullptr;
43
44EditorUndoRedoManager::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
57UndoRedo *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
62int EditorUndoRedoManager::get_history_id_for_object(Object *p_object) const {
63 int history_id = INVALID_HISTORY;
64
65 if (Object::cast_to<EditorDebuggerRemoteObject>(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
106EditorUndoRedoManager::History &EditorUndoRedoManager::get_history_for_object(Object *p_object) {
107 int history_id = get_history_id_for_object(p_object);
108 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));
109
110 History &history = get_or_create_history(history_id);
111 if (pending_action.history_id == INVALID_HISTORY) {
112 pending_action.history_id = history_id;
113 history.undo_redo->create_action(pending_action.action_name, pending_action.merge_mode, pending_action.backward_undo_ops);
114 }
115
116 return history;
117}
118
119void EditorUndoRedoManager::create_action_for_history(const String &p_name, int p_history_id, UndoRedo::MergeMode p_mode, bool p_backward_undo_ops) {
120 if (pending_action.history_id != INVALID_HISTORY) {
121 // Nested action.
122 p_history_id = pending_action.history_id;
123 } else {
124 pending_action.action_name = p_name;
125 pending_action.timestamp = OS::get_singleton()->get_unix_time();
126 pending_action.merge_mode = p_mode;
127 pending_action.backward_undo_ops = p_backward_undo_ops;
128 }
129
130 if (p_history_id != INVALID_HISTORY) {
131 pending_action.history_id = p_history_id;
132 History &history = get_or_create_history(p_history_id);
133 history.undo_redo->create_action(pending_action.action_name, pending_action.merge_mode, pending_action.backward_undo_ops);
134 }
135}
136
137void EditorUndoRedoManager::create_action(const String &p_name, UndoRedo::MergeMode p_mode, Object *p_custom_context, bool p_backward_undo_ops) {
138 create_action_for_history(p_name, INVALID_HISTORY, p_mode, p_backward_undo_ops);
139
140 if (p_custom_context) {
141 // This assigns history to pending action.
142 get_history_for_object(p_custom_context);
143 }
144}
145
146void EditorUndoRedoManager::add_do_methodp(Object *p_object, const StringName &p_method, const Variant **p_args, int p_argcount) {
147 UndoRedo *undo_redo = get_history_for_object(p_object).undo_redo;
148 undo_redo->add_do_method(Callable(p_object, p_method).bindp(p_args, p_argcount));
149}
150
151void EditorUndoRedoManager::add_undo_methodp(Object *p_object, const StringName &p_method, const Variant **p_args, int p_argcount) {
152 UndoRedo *undo_redo = get_history_for_object(p_object).undo_redo;
153 undo_redo->add_undo_method(Callable(p_object, p_method).bindp(p_args, p_argcount));
154}
155
156void EditorUndoRedoManager::_add_do_method(const Variant **p_args, int p_argcount, Callable::CallError &r_error) {
157 if (p_argcount < 2) {
158 r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
159 r_error.argument = 0;
160 return;
161 }
162
163 if (p_args[0]->get_type() != Variant::OBJECT) {
164 r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
165 r_error.argument = 0;
166 r_error.expected = Variant::OBJECT;
167 return;
168 }
169
170 if (p_args[1]->get_type() != Variant::STRING_NAME && p_args[1]->get_type() != Variant::STRING) {
171 r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
172 r_error.argument = 1;
173 r_error.expected = Variant::STRING_NAME;
174 return;
175 }
176
177 r_error.error = Callable::CallError::CALL_OK;
178
179 Object *object = *p_args[0];
180 StringName method = *p_args[1];
181
182 add_do_methodp(object, method, p_args + 2, p_argcount - 2);
183}
184
185void EditorUndoRedoManager::_add_undo_method(const Variant **p_args, int p_argcount, Callable::CallError &r_error) {
186 if (p_argcount < 2) {
187 r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
188 r_error.argument = 0;
189 return;
190 }
191
192 if (p_args[0]->get_type() != Variant::OBJECT) {
193 r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
194 r_error.argument = 0;
195 r_error.expected = Variant::OBJECT;
196 return;
197 }
198
199 if (p_args[1]->get_type() != Variant::STRING_NAME && p_args[1]->get_type() != Variant::STRING) {
200 r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
201 r_error.argument = 1;
202 r_error.expected = Variant::STRING_NAME;
203 return;
204 }
205
206 r_error.error = Callable::CallError::CALL_OK;
207
208 Object *object = *p_args[0];
209 StringName method = *p_args[1];
210
211 add_undo_methodp(object, method, p_args + 2, p_argcount - 2);
212}
213
214void EditorUndoRedoManager::add_do_property(Object *p_object, const StringName &p_property, const Variant &p_value) {
215 UndoRedo *undo_redo = get_history_for_object(p_object).undo_redo;
216 undo_redo->add_do_property(p_object, p_property, p_value);
217}
218
219void EditorUndoRedoManager::add_undo_property(Object *p_object, const StringName &p_property, const Variant &p_value) {
220 UndoRedo *undo_redo = get_history_for_object(p_object).undo_redo;
221 undo_redo->add_undo_property(p_object, p_property, p_value);
222}
223
224void EditorUndoRedoManager::add_do_reference(Object *p_object) {
225 UndoRedo *undo_redo = get_history_for_object(p_object).undo_redo;
226 undo_redo->add_do_reference(p_object);
227}
228
229void EditorUndoRedoManager::add_undo_reference(Object *p_object) {
230 UndoRedo *undo_redo = get_history_for_object(p_object).undo_redo;
231 undo_redo->add_undo_reference(p_object);
232}
233
234void EditorUndoRedoManager::commit_action(bool p_execute) {
235 if (pending_action.history_id == INVALID_HISTORY) {
236 return; // Empty action, do nothing.
237 }
238
239 is_committing = true;
240
241 History &history = get_or_create_history(pending_action.history_id);
242 history.undo_redo->commit_action(p_execute);
243 history.redo_stack.clear();
244
245 if (history.undo_redo->get_action_level() > 0) {
246 // Nested action.
247 is_committing = false;
248 return;
249 }
250
251 if (!history.undo_stack.is_empty()) {
252 // Discard action if it should be merged (UndoRedo handles merging internally).
253 switch (pending_action.merge_mode) {
254 case UndoRedo::MERGE_DISABLE:
255 break; // Nothing to do here.
256 case UndoRedo::MERGE_ENDS: {
257 if (history.undo_stack.size() < 2) {
258 break;
259 }
260
261 const Action &prev_action = history.undo_stack.back()->get();
262 const Action &pre_prev_action = history.undo_stack.back()->prev()->get();
263 if (pending_action.merge_mode == prev_action.merge_mode && pending_action.merge_mode == pre_prev_action.merge_mode &&
264 pending_action.action_name == prev_action.action_name && pending_action.action_name == pre_prev_action.action_name) {
265 pending_action = Action();
266 is_committing = false;
267 emit_signal(SNAME("history_changed"));
268 return;
269 }
270 } break;
271 case UndoRedo::MERGE_ALL: {
272 const Action &prev_action = history.undo_stack.back()->get();
273 if (pending_action.merge_mode == prev_action.merge_mode && pending_action.action_name == prev_action.action_name) {
274 pending_action = Action();
275 is_committing = false;
276 emit_signal(SNAME("history_changed"));
277 return;
278 }
279 } break;
280 }
281 }
282
283 history.undo_stack.push_back(pending_action);
284 pending_action = Action();
285 is_committing = false;
286 emit_signal(SNAME("history_changed"));
287}
288
289bool EditorUndoRedoManager::is_committing_action() const {
290 return is_committing;
291}
292
293bool 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
305bool 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
320bool EditorUndoRedoManager::redo() {
321 if (!has_redo()) {
322 return false;
323 }
324
325 int selected_history = INVALID_HISTORY;
326 double global_timestamp = INFINITY;
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
358bool 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
373void 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
378void EditorUndoRedoManager::set_history_as_unsaved(int p_id) {
379 History &history = get_or_create_history(p_id);
380 history.saved_version = -1;
381}
382
383bool EditorUndoRedoManager::is_history_unsaved(int p_id) {
384 History &history = get_or_create_history(p_id);
385 return history.undo_redo->get_version() != history.saved_version;
386}
387
388bool EditorUndoRedoManager::has_undo() {
389 for (const KeyValue<int, History> &E : history_map) {
390 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()) {
391 return true;
392 }
393 }
394 return false;
395}
396
397bool EditorUndoRedoManager::has_redo() {
398 for (const KeyValue<int, History> &E : history_map) {
399 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()) {
400 return true;
401 }
402 }
403 return false;
404}
405
406void EditorUndoRedoManager::clear_history(bool p_increase_version, int p_idx) {
407 if (p_idx != INVALID_HISTORY) {
408 History &history = get_or_create_history(p_idx);
409 history.undo_redo->clear_history(p_increase_version);
410 history.undo_stack.clear();
411 history.redo_stack.clear();
412
413 if (!p_increase_version) {
414 set_history_as_saved(p_idx);
415 }
416 emit_signal(SNAME("history_changed"));
417 return;
418 }
419
420 for (const KeyValue<int, History> &E : history_map) {
421 E.value.undo_redo->clear_history(p_increase_version);
422 set_history_as_saved(E.key);
423 }
424 emit_signal(SNAME("history_changed"));
425}
426
427String EditorUndoRedoManager::get_current_action_name() {
428 if (has_undo()) {
429 History *selected_history = _get_newest_undo();
430 if (selected_history) {
431 return selected_history->undo_redo->get_current_action_name();
432 }
433 }
434 return "";
435}
436
437int EditorUndoRedoManager::get_current_action_history_id() {
438 if (has_undo()) {
439 History *selected_history = _get_newest_undo();
440 if (selected_history) {
441 return selected_history->id;
442 }
443 }
444 return INVALID_HISTORY;
445}
446
447void EditorUndoRedoManager::discard_history(int p_idx, bool p_erase_from_map) {
448 ERR_FAIL_COND(!history_map.has(p_idx));
449 History &history = history_map[p_idx];
450
451 if (history.undo_redo) {
452 memdelete(history.undo_redo);
453 history.undo_redo = nullptr;
454 }
455
456 if (p_erase_from_map) {
457 history_map.erase(p_idx);
458 }
459}
460
461EditorUndoRedoManager::History *EditorUndoRedoManager::_get_newest_undo() {
462 History *selected_history = nullptr;
463 double global_timestamp = 0;
464
465 // Pick the history with greatest last action timestamp (either global or current scene).
466 {
467 History &history = get_or_create_history(GLOBAL_HISTORY);
468 if (!history.undo_stack.is_empty()) {
469 selected_history = &history;
470 global_timestamp = history.undo_stack.back()->get().timestamp;
471 }
472 }
473
474 {
475 History &history = get_or_create_history(REMOTE_HISTORY);
476 if (!history.undo_stack.is_empty() && history.undo_stack.back()->get().timestamp > global_timestamp) {
477 selected_history = &history;
478 global_timestamp = history.undo_stack.back()->get().timestamp;
479 }
480 }
481
482 {
483 History &history = get_or_create_history(EditorNode::get_editor_data().get_current_edited_scene_history_id());
484 if (!history.undo_stack.is_empty() && history.undo_stack.back()->get().timestamp > global_timestamp) {
485 selected_history = &history;
486 }
487 }
488
489 return selected_history;
490}
491
492void EditorUndoRedoManager::_bind_methods() {
493 ClassDB::bind_method(D_METHOD("create_action", "name", "merge_mode", "custom_context", "backward_undo_ops"), &EditorUndoRedoManager::create_action, DEFVAL(UndoRedo::MERGE_DISABLE), DEFVAL((Object *)nullptr), DEFVAL(false));
494 ClassDB::bind_method(D_METHOD("commit_action", "execute"), &EditorUndoRedoManager::commit_action, DEFVAL(true));
495 ClassDB::bind_method(D_METHOD("is_committing_action"), &EditorUndoRedoManager::is_committing_action);
496
497 {
498 MethodInfo mi;
499 mi.name = "add_do_method";
500 mi.arguments.push_back(PropertyInfo(Variant::OBJECT, "object"));
501 mi.arguments.push_back(PropertyInfo(Variant::STRING_NAME, "method"));
502
503 ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "add_do_method", &EditorUndoRedoManager::_add_do_method, mi, varray(), false);
504 }
505
506 {
507 MethodInfo mi;
508 mi.name = "add_undo_method";
509 mi.arguments.push_back(PropertyInfo(Variant::OBJECT, "object"));
510 mi.arguments.push_back(PropertyInfo(Variant::STRING_NAME, "method"));
511
512 ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "add_undo_method", &EditorUndoRedoManager::_add_undo_method, mi, varray(), false);
513 }
514
515 ClassDB::bind_method(D_METHOD("add_do_property", "object", "property", "value"), &EditorUndoRedoManager::add_do_property);
516 ClassDB::bind_method(D_METHOD("add_undo_property", "object", "property", "value"), &EditorUndoRedoManager::add_undo_property);
517 ClassDB::bind_method(D_METHOD("add_do_reference", "object"), &EditorUndoRedoManager::add_do_reference);
518 ClassDB::bind_method(D_METHOD("add_undo_reference", "object"), &EditorUndoRedoManager::add_undo_reference);
519
520 ClassDB::bind_method(D_METHOD("get_object_history_id", "object"), &EditorUndoRedoManager::get_history_id_for_object);
521 ClassDB::bind_method(D_METHOD("get_history_undo_redo", "id"), &EditorUndoRedoManager::get_history_undo_redo);
522
523 ADD_SIGNAL(MethodInfo("history_changed"));
524 ADD_SIGNAL(MethodInfo("version_changed"));
525
526 BIND_ENUM_CONSTANT(GLOBAL_HISTORY);
527 BIND_ENUM_CONSTANT(REMOTE_HISTORY);
528 BIND_ENUM_CONSTANT(INVALID_HISTORY);
529}
530
531EditorUndoRedoManager *EditorUndoRedoManager::get_singleton() {
532 return singleton;
533}
534
535EditorUndoRedoManager::EditorUndoRedoManager() {
536 if (!singleton) {
537 singleton = this;
538 }
539}
540
541EditorUndoRedoManager::~EditorUndoRedoManager() {
542 for (const KeyValue<int, History> &E : history_map) {
543 discard_history(E.key, false);
544 }
545}
546