Skip to content

Commit ad7a2d1

Browse files
committed
Expose EditorUndoRedoManager's clear_history()
1 parent fd7239c commit ad7a2d1

11 files changed

+47
-18
lines changed

doc/classes/EditorInterface.xml

+6
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,12 @@
117117
[b]Note:[/b] When creating custom editor UI, prefer accessing theme items directly from your GUI nodes using the [code]get_theme_*[/code] methods.
118118
</description>
119119
</method>
120+
<method name="get_editor_undo_redo" qualifiers="const">
121+
<return type="EditorUndoRedoManager" />
122+
<description>
123+
Returns the editor's [EditorUndoRedoManager].
124+
</description>
125+
</method>
120126
<method name="get_editor_viewport_2d" qualifiers="const">
121127
<return type="SubViewport" />
122128
<description>

doc/classes/EditorUndoRedoManager.xml

+15
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,21 @@
6868
Register a reference for "undo" that will be erased if the "undo" history is lost. This is useful mostly for nodes removed with the "do" call (not the "undo" call!).
6969
</description>
7070
</method>
71+
<method name="clear_history">
72+
<return type="void" />
73+
<param index="0" name="id" type="int" default="-99" />
74+
<param index="1" name="increase_version" type="bool" default="true" />
75+
<description>
76+
Clears the given undo history. You can clear history for a specific scene, global history, or for all scenes at once if [param id] is [constant INVALID_HISTORY].
77+
If [param increase_version] is [code]true[/code], the undo history version will be increased, marking it as unsaved. Useful for operations that modify the scene, but don't support undo.
78+
[codeblock]
79+
var scene_root = EditorInterface.get_edited_scene_root()
80+
var undo_redo = EditorInterface.get_editor_undo_redo()
81+
undo_redo.clear_history(undo_redo.get_object_history_id(scene_root))
82+
[/codeblock]
83+
[b]Note:[/b] If you want to mark an edited scene as unsaved without clearing its history, use [method EditorInterface.mark_scene_as_unsaved] instead.
84+
</description>
85+
</method>
7186
<method name="commit_action">
7287
<return type="void" />
7388
<param index="0" name="execute" type="bool" default="true" />

editor/animation_track_editor.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -6887,8 +6887,8 @@ void AnimationTrackEditor::_edit_menu_pressed(int p_option) {
68876887
_redraw_tracks();
68886888
_update_key_edit();
68896889
EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
6890-
undo_redo->clear_history(true, undo_redo->get_history_id_for_object(animation.ptr()));
6891-
undo_redo->clear_history(true, undo_redo->get_history_id_for_object(this));
6890+
undo_redo->clear_history(undo_redo->get_history_id_for_object(animation.ptr()));
6891+
undo_redo->clear_history(undo_redo->get_history_id_for_object(this));
68926892

68936893
} break;
68946894
case EDIT_CLEAN_UP_ANIMATION: {
@@ -7030,8 +7030,8 @@ void AnimationTrackEditor::_cleanup_animation(Ref<Animation> p_animation) {
70307030
}
70317031

70327032
EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
7033-
undo_redo->clear_history(true, undo_redo->get_history_id_for_object(animation.ptr()));
7034-
undo_redo->clear_history(true, undo_redo->get_history_id_for_object(this));
7033+
undo_redo->clear_history(undo_redo->get_history_id_for_object(animation.ptr()));
7034+
undo_redo->clear_history(undo_redo->get_history_id_for_object(this));
70357035
_update_tracks();
70367036
}
70377037

editor/debugger/editor_debugger_node.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ void EditorDebuggerNode::stop(bool p_force) {
303303
});
304304
_break_state_changed();
305305
breakpoints.clear();
306-
EditorUndoRedoManager::get_singleton()->clear_history(false, EditorUndoRedoManager::REMOTE_HISTORY);
306+
EditorUndoRedoManager::get_singleton()->clear_history(EditorUndoRedoManager::REMOTE_HISTORY, false);
307307
set_process(false);
308308
}
309309

editor/editor_audio_buses.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -1262,7 +1262,7 @@ void EditorAudioBuses::_load_default_layout() {
12621262
file->set_text(String(TTR("Layout:")) + " " + layout_path.get_file());
12631263
AudioServer::get_singleton()->set_bus_layout(state);
12641264
_rebuild_buses();
1265-
EditorUndoRedoManager::get_singleton()->clear_history(true, EditorUndoRedoManager::GLOBAL_HISTORY);
1265+
EditorUndoRedoManager::get_singleton()->clear_history(EditorUndoRedoManager::GLOBAL_HISTORY);
12661266
callable_mp(this, &EditorAudioBuses::_select_layout).call_deferred();
12671267
}
12681268

@@ -1278,7 +1278,7 @@ void EditorAudioBuses::_file_dialog_callback(const String &p_string) {
12781278
file->set_text(String(TTR("Layout:")) + " " + p_string.get_file());
12791279
AudioServer::get_singleton()->set_bus_layout(state);
12801280
_rebuild_buses();
1281-
EditorUndoRedoManager::get_singleton()->clear_history(true, EditorUndoRedoManager::GLOBAL_HISTORY);
1281+
EditorUndoRedoManager::get_singleton()->clear_history(EditorUndoRedoManager::GLOBAL_HISTORY);
12821282
callable_mp(this, &EditorAudioBuses::_select_layout).call_deferred();
12831283

12841284
} else if (file_dialog->get_file_mode() == EditorFileDialog::FILE_MODE_SAVE_FILE) {
@@ -1298,7 +1298,7 @@ void EditorAudioBuses::_file_dialog_callback(const String &p_string) {
12981298
edited_path = p_string;
12991299
file->set_text(String(TTR("Layout:")) + " " + p_string.get_file());
13001300
_rebuild_buses();
1301-
EditorUndoRedoManager::get_singleton()->clear_history(true, EditorUndoRedoManager::GLOBAL_HISTORY);
1301+
EditorUndoRedoManager::get_singleton()->clear_history(EditorUndoRedoManager::GLOBAL_HISTORY);
13021302
callable_mp(this, &EditorAudioBuses::_select_layout).call_deferred();
13031303
}
13041304
}
@@ -1397,7 +1397,7 @@ void EditorAudioBuses::open_layout(const String &p_path) {
13971397
file->set_text(p_path.get_file());
13981398
AudioServer::get_singleton()->set_bus_layout(state);
13991399
_rebuild_buses();
1400-
EditorUndoRedoManager::get_singleton()->clear_history(true, EditorUndoRedoManager::GLOBAL_HISTORY);
1400+
EditorUndoRedoManager::get_singleton()->clear_history(EditorUndoRedoManager::GLOBAL_HISTORY);
14011401
callable_mp(this, &EditorAudioBuses::_select_layout).call_deferred();
14021402
}
14031403

editor/editor_interface.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,10 @@ Ref<EditorSettings> EditorInterface::get_editor_settings() const {
8686
return EditorSettings::get_singleton();
8787
}
8888

89+
EditorUndoRedoManager *EditorInterface::get_editor_undo_redo() const {
90+
return EditorUndoRedoManager::get_singleton();
91+
}
92+
8993
TypedArray<Texture2D> EditorInterface::_make_mesh_previews(const TypedArray<Mesh> &p_meshes, int p_preview_size) {
9094
Vector<Ref<Mesh>> meshes;
9195

@@ -525,6 +529,7 @@ void EditorInterface::_bind_methods() {
525529
ClassDB::bind_method(D_METHOD("get_resource_previewer"), &EditorInterface::get_resource_previewer);
526530
ClassDB::bind_method(D_METHOD("get_selection"), &EditorInterface::get_selection);
527531
ClassDB::bind_method(D_METHOD("get_editor_settings"), &EditorInterface::get_editor_settings);
532+
ClassDB::bind_method(D_METHOD("get_editor_undo_redo"), &EditorInterface::get_editor_undo_redo);
528533

529534
ClassDB::bind_method(D_METHOD("make_mesh_previews", "meshes", "preview_size"), &EditorInterface::_make_mesh_previews);
530535

editor/editor_interface.h

+2
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ class EditorPlugin;
4545
class EditorResourcePreview;
4646
class EditorSelection;
4747
class EditorSettings;
48+
class EditorUndoRedoManager;
4849
class FileSystemDock;
4950
class Mesh;
5051
class Node;
@@ -93,6 +94,7 @@ class EditorInterface : public Object {
9394
EditorResourcePreview *get_resource_previewer() const;
9495
EditorSelection *get_selection() const;
9596
Ref<EditorSettings> get_editor_settings() const;
97+
EditorUndoRedoManager *get_editor_undo_redo() const;
9698

9799
Vector<Ref<Texture2D>> make_mesh_previews(const Vector<Ref<Mesh>> &p_meshes, Vector<Transform3D> *p_transforms, int p_preview_size);
98100

editor/editor_node.cpp

+6-6
Original file line numberDiff line numberDiff line change
@@ -2961,7 +2961,7 @@ void EditorNode::_menu_option_confirm(int p_option, bool p_confirmed) {
29612961
ERR_PRINT("Failed to load scene");
29622962
}
29632963
editor_data.move_edited_scene_to_index(cur_idx);
2964-
EditorUndoRedoManager::get_singleton()->clear_history(false, editor_data.get_current_edited_scene_history_id());
2964+
EditorUndoRedoManager::get_singleton()->clear_history(editor_data.get_current_edited_scene_history_id(), false);
29652965
scene_tabs->set_current_tab(cur_idx);
29662966

29672967
} break;
@@ -3963,7 +3963,7 @@ void EditorNode::_set_current_scene_nocheck(int p_idx) {
39633963
editor_folding.load_scene_folding(editor_data.get_edited_scene_root(p_idx), editor_data.get_scene_path(p_idx));
39643964
}
39653965

3966-
EditorUndoRedoManager::get_singleton()->clear_history(false, editor_data.get_scene_history_id(p_idx));
3966+
EditorUndoRedoManager::get_singleton()->clear_history(editor_data.get_scene_history_id(p_idx), false);
39673967
}
39683968

39693969
Dictionary state = editor_data.restore_edited_scene_state(editor_selection, &editor_history);
@@ -4073,7 +4073,7 @@ Error EditorNode::load_scene(const String &p_scene, bool p_ignore_broken_deps, b
40734073
_set_current_scene(idx);
40744074
}
40754075
} else {
4076-
EditorUndoRedoManager::get_singleton()->clear_history(false, editor_data.get_current_edited_scene_history_id());
4076+
EditorUndoRedoManager::get_singleton()->clear_history(editor_data.get_current_edited_scene_history_id(), false);
40774077
}
40784078

40794079
dependency_errors.clear();
@@ -5928,7 +5928,7 @@ void EditorNode::reload_scene(const String &p_path) {
59285928
bool is_unsaved = EditorUndoRedoManager::get_singleton()->is_history_unsaved(current_history_id);
59295929

59305930
// Scene is not open, so at it might be instantiated. We'll refresh the whole scene later.
5931-
EditorUndoRedoManager::get_singleton()->clear_history(false, current_history_id);
5931+
EditorUndoRedoManager::get_singleton()->clear_history(current_history_id, false);
59325932
if (is_unsaved) {
59335933
EditorUndoRedoManager::get_singleton()->set_history_as_unsaved(current_history_id);
59345934
}
@@ -5947,7 +5947,7 @@ void EditorNode::reload_scene(const String &p_path) {
59475947

59485948
// Adjust index so tab is back a the previous position.
59495949
editor_data.move_edited_scene_to_index(scene_idx);
5950-
EditorUndoRedoManager::get_singleton()->clear_history(false, editor_data.get_scene_history_id(scene_idx));
5950+
EditorUndoRedoManager::get_singleton()->clear_history(editor_data.get_scene_history_id(scene_idx), false);
59515951

59525952
// Recover the tab.
59535953
scene_tabs->set_current_tab(current_tab);
@@ -6092,7 +6092,7 @@ void EditorNode::reload_instances_with_path_in_edited_scenes() {
60926092
bool is_unsaved = EditorUndoRedoManager::get_singleton()->is_history_unsaved(current_history_id);
60936093

60946094
// Clear the history for this affected tab.
6095-
EditorUndoRedoManager::get_singleton()->clear_history(false, current_history_id);
6095+
EditorUndoRedoManager::get_singleton()->clear_history(current_history_id, false);
60966096

60976097
// Update the version
60986098
editor_data.is_scene_changed(current_scene_idx);

editor/editor_undo_redo_manager.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,7 @@ bool EditorUndoRedoManager::has_history(int p_idx) const {
390390
return history_map.has(p_idx);
391391
}
392392

393-
void EditorUndoRedoManager::clear_history(bool p_increase_version, int p_idx) {
393+
void EditorUndoRedoManager::clear_history(int p_idx, bool p_increase_version) {
394394
if (p_idx != INVALID_HISTORY) {
395395
History &history = get_or_create_history(p_idx);
396396
history.undo_redo->clear_history(p_increase_version);
@@ -507,6 +507,7 @@ void EditorUndoRedoManager::_bind_methods() {
507507

508508
ClassDB::bind_method(D_METHOD("get_object_history_id", "object"), &EditorUndoRedoManager::get_history_id_for_object);
509509
ClassDB::bind_method(D_METHOD("get_history_undo_redo", "id"), &EditorUndoRedoManager::get_history_undo_redo);
510+
ClassDB::bind_method(D_METHOD("clear_history", "id", "increase_version"), &EditorUndoRedoManager::clear_history, DEFVAL(INVALID_HISTORY), DEFVAL(true));
510511

511512
ADD_SIGNAL(MethodInfo("history_changed"));
512513
ADD_SIGNAL(MethodInfo("version_changed"));

editor/editor_undo_redo_manager.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ class EditorUndoRedoManager : public Object {
125125
bool undo_history(int p_id);
126126
bool redo();
127127
bool redo_history(int p_id);
128-
void clear_history(bool p_increase_version = true, int p_idx = INVALID_HISTORY);
128+
void clear_history(int p_idx = INVALID_HISTORY, bool p_increase_version = true);
129129

130130
void set_history_as_saved(int p_idx);
131131
void set_history_as_unsaved(int p_idx);

editor/inspector_dock.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ void InspectorDock::_menu_option_confirm(int p_option, bool p_confirmed) {
190190
}
191191

192192
int history_id = EditorUndoRedoManager::get_singleton()->get_history_id_for_object(current);
193-
EditorUndoRedoManager::get_singleton()->clear_history(true, history_id);
193+
EditorUndoRedoManager::get_singleton()->clear_history(history_id);
194194

195195
EditorNode::get_singleton()->edit_item(current, inspector);
196196
}

0 commit comments

Comments
 (0)