Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose EditorUndoRedoManager's clear_history() #90130

Merged
merged 1 commit into from
Aug 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions doc/classes/EditorInterface.xml
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,12 @@
[b]Note:[/b] When creating custom editor UI, prefer accessing theme items directly from your GUI nodes using the [code]get_theme_*[/code] methods.
</description>
</method>
<method name="get_editor_undo_redo" qualifiers="const">
<return type="EditorUndoRedoManager" />
<description>
Returns the editor's [EditorUndoRedoManager].
</description>
</method>
<method name="get_editor_viewport_2d" qualifiers="const">
<return type="SubViewport" />
<description>
Expand Down
15 changes: 15 additions & 0 deletions doc/classes/EditorUndoRedoManager.xml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,21 @@
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!).
</description>
</method>
<method name="clear_history">
<return type="void" />
<param index="0" name="id" type="int" default="-99" />
<param index="1" name="increase_version" type="bool" default="true" />
<description>
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].
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.
[codeblock]
var scene_root = EditorInterface.get_edited_scene_root()
var undo_redo = EditorInterface.get_editor_undo_redo()
undo_redo.clear_history(undo_redo.get_object_history_id(scene_root))
[/codeblock]
[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.
</description>
</method>
<method name="commit_action">
<return type="void" />
<param index="0" name="execute" type="bool" default="true" />
Expand Down
8 changes: 4 additions & 4 deletions editor/animation_track_editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6887,8 +6887,8 @@ void AnimationTrackEditor::_edit_menu_pressed(int p_option) {
_redraw_tracks();
_update_key_edit();
EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
undo_redo->clear_history(true, undo_redo->get_history_id_for_object(animation.ptr()));
undo_redo->clear_history(true, undo_redo->get_history_id_for_object(this));
undo_redo->clear_history(undo_redo->get_history_id_for_object(animation.ptr()));
undo_redo->clear_history(undo_redo->get_history_id_for_object(this));

} break;
case EDIT_CLEAN_UP_ANIMATION: {
Expand Down Expand Up @@ -7030,8 +7030,8 @@ void AnimationTrackEditor::_cleanup_animation(Ref<Animation> p_animation) {
}

EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
undo_redo->clear_history(true, undo_redo->get_history_id_for_object(animation.ptr()));
undo_redo->clear_history(true, undo_redo->get_history_id_for_object(this));
undo_redo->clear_history(undo_redo->get_history_id_for_object(animation.ptr()));
undo_redo->clear_history(undo_redo->get_history_id_for_object(this));
_update_tracks();
}

Expand Down
2 changes: 1 addition & 1 deletion editor/debugger/editor_debugger_node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ void EditorDebuggerNode::stop(bool p_force) {
});
_break_state_changed();
breakpoints.clear();
EditorUndoRedoManager::get_singleton()->clear_history(false, EditorUndoRedoManager::REMOTE_HISTORY);
EditorUndoRedoManager::get_singleton()->clear_history(EditorUndoRedoManager::REMOTE_HISTORY, false);
set_process(false);
}

Expand Down
8 changes: 4 additions & 4 deletions editor/editor_audio_buses.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1262,7 +1262,7 @@ void EditorAudioBuses::_load_default_layout() {
file->set_text(String(TTR("Layout:")) + " " + layout_path.get_file());
AudioServer::get_singleton()->set_bus_layout(state);
_rebuild_buses();
EditorUndoRedoManager::get_singleton()->clear_history(true, EditorUndoRedoManager::GLOBAL_HISTORY);
EditorUndoRedoManager::get_singleton()->clear_history(EditorUndoRedoManager::GLOBAL_HISTORY);
callable_mp(this, &EditorAudioBuses::_select_layout).call_deferred();
}

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

} else if (file_dialog->get_file_mode() == EditorFileDialog::FILE_MODE_SAVE_FILE) {
Expand All @@ -1298,7 +1298,7 @@ void EditorAudioBuses::_file_dialog_callback(const String &p_string) {
edited_path = p_string;
file->set_text(String(TTR("Layout:")) + " " + p_string.get_file());
_rebuild_buses();
EditorUndoRedoManager::get_singleton()->clear_history(true, EditorUndoRedoManager::GLOBAL_HISTORY);
EditorUndoRedoManager::get_singleton()->clear_history(EditorUndoRedoManager::GLOBAL_HISTORY);
callable_mp(this, &EditorAudioBuses::_select_layout).call_deferred();
}
}
Expand Down Expand Up @@ -1397,7 +1397,7 @@ void EditorAudioBuses::open_layout(const String &p_path) {
file->set_text(p_path.get_file());
AudioServer::get_singleton()->set_bus_layout(state);
_rebuild_buses();
EditorUndoRedoManager::get_singleton()->clear_history(true, EditorUndoRedoManager::GLOBAL_HISTORY);
EditorUndoRedoManager::get_singleton()->clear_history(EditorUndoRedoManager::GLOBAL_HISTORY);
callable_mp(this, &EditorAudioBuses::_select_layout).call_deferred();
}

Expand Down
5 changes: 5 additions & 0 deletions editor/editor_interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ Ref<EditorSettings> EditorInterface::get_editor_settings() const {
return EditorSettings::get_singleton();
}

EditorUndoRedoManager *EditorInterface::get_editor_undo_redo() const {
return EditorUndoRedoManager::get_singleton();
}

TypedArray<Texture2D> EditorInterface::_make_mesh_previews(const TypedArray<Mesh> &p_meshes, int p_preview_size) {
Vector<Ref<Mesh>> meshes;

Expand Down Expand Up @@ -525,6 +529,7 @@ void EditorInterface::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_resource_previewer"), &EditorInterface::get_resource_previewer);
ClassDB::bind_method(D_METHOD("get_selection"), &EditorInterface::get_selection);
ClassDB::bind_method(D_METHOD("get_editor_settings"), &EditorInterface::get_editor_settings);
ClassDB::bind_method(D_METHOD("get_editor_undo_redo"), &EditorInterface::get_editor_undo_redo);

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

Expand Down
2 changes: 2 additions & 0 deletions editor/editor_interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class EditorPlugin;
class EditorResourcePreview;
class EditorSelection;
class EditorSettings;
class EditorUndoRedoManager;
class FileSystemDock;
class Mesh;
class Node;
Expand Down Expand Up @@ -93,6 +94,7 @@ class EditorInterface : public Object {
EditorResourcePreview *get_resource_previewer() const;
EditorSelection *get_selection() const;
Ref<EditorSettings> get_editor_settings() const;
EditorUndoRedoManager *get_editor_undo_redo() const;

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

Expand Down
12 changes: 6 additions & 6 deletions editor/editor_node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2961,7 +2961,7 @@ void EditorNode::_menu_option_confirm(int p_option, bool p_confirmed) {
ERR_PRINT("Failed to load scene");
}
editor_data.move_edited_scene_to_index(cur_idx);
EditorUndoRedoManager::get_singleton()->clear_history(false, editor_data.get_current_edited_scene_history_id());
EditorUndoRedoManager::get_singleton()->clear_history(editor_data.get_current_edited_scene_history_id(), false);
scene_tabs->set_current_tab(cur_idx);

} break;
Expand Down Expand Up @@ -3963,7 +3963,7 @@ void EditorNode::_set_current_scene_nocheck(int p_idx) {
editor_folding.load_scene_folding(editor_data.get_edited_scene_root(p_idx), editor_data.get_scene_path(p_idx));
}

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

Dictionary state = editor_data.restore_edited_scene_state(editor_selection, &editor_history);
Expand Down Expand Up @@ -4073,7 +4073,7 @@ Error EditorNode::load_scene(const String &p_scene, bool p_ignore_broken_deps, b
_set_current_scene(idx);
}
} else {
EditorUndoRedoManager::get_singleton()->clear_history(false, editor_data.get_current_edited_scene_history_id());
EditorUndoRedoManager::get_singleton()->clear_history(editor_data.get_current_edited_scene_history_id(), false);
}

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

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

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

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

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

// Update the version
editor_data.is_scene_changed(current_scene_idx);
Expand Down
3 changes: 2 additions & 1 deletion editor/editor_undo_redo_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@ bool EditorUndoRedoManager::has_history(int p_idx) const {
return history_map.has(p_idx);
}

void EditorUndoRedoManager::clear_history(bool p_increase_version, int p_idx) {
void EditorUndoRedoManager::clear_history(int p_idx, bool p_increase_version) {
if (p_idx != INVALID_HISTORY) {
History &history = get_or_create_history(p_idx);
history.undo_redo->clear_history(p_increase_version);
Expand Down Expand Up @@ -507,6 +507,7 @@ void EditorUndoRedoManager::_bind_methods() {

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

ADD_SIGNAL(MethodInfo("history_changed"));
ADD_SIGNAL(MethodInfo("version_changed"));
Expand Down
2 changes: 1 addition & 1 deletion editor/editor_undo_redo_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ class EditorUndoRedoManager : public Object {
bool undo_history(int p_id);
bool redo();
bool redo_history(int p_id);
void clear_history(bool p_increase_version = true, int p_idx = INVALID_HISTORY);
void clear_history(int p_idx = INVALID_HISTORY, bool p_increase_version = true);

void set_history_as_saved(int p_idx);
void set_history_as_unsaved(int p_idx);
Expand Down
2 changes: 1 addition & 1 deletion editor/inspector_dock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ void InspectorDock::_menu_option_confirm(int p_option, bool p_confirmed) {
}

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

EditorNode::get_singleton()->edit_item(current, inspector);
}
Expand Down
Loading