diff --git a/doc/classes/EditorPlugin.xml b/doc/classes/EditorPlugin.xml index 5a1037aebe8..fdd3807b698 100644 --- a/doc/classes/EditorPlugin.xml +++ b/doc/classes/EditorPlugin.xml @@ -532,7 +532,7 @@ - + Gets the undo/redo object. Most actions in the editor can be undoable, so use this object to make sure this happens when it's worth it. diff --git a/doc/classes/EditorUndoRedoManager.xml b/doc/classes/EditorUndoRedoManager.xml new file mode 100644 index 00000000000..1350e4487c3 --- /dev/null +++ b/doc/classes/EditorUndoRedoManager.xml @@ -0,0 +1,118 @@ + + + + Manages undo history of scenes opened in the editor. + + + [EditorUndoRedoManager] is a manager for [UndoRedo] objects associated with edited scenes. Each scene has its own undo history and [EditorUndoRedoManager] ensures that each action performed in the editor gets associated with a proper scene. For actions not related to scenes ([ProjectSettings] edits, external resources, etc.), a separate global history is used. + The usage is mostly the same as [UndoRedo]. You create and commit actions and the manager automatically decides under-the-hood what scenes it belongs to. The scene is deduced based on the first operation in an action, using the object from the operation. The rules are as follows: + - If the object is a [Node], use the currently edited scene; + - If the object is a built-in resource, use the scene from its path; + - If the object is external resource or anything else, use global history. + This guessing can sometimes yield false results, so you can provide a custom context object when creating an action. + + + + + + + + + + Register a method that will be called when the action is committed (i.e. the "do" action). + If this is the first operation, the [param object] will be used to deduce target undo history. + + + + + + + + + Register a property value change for "do". + If this is the first operation, the [param object] will be used to deduce target undo history. + + + + + + + Register a reference for "do" that will be erased if the "do" history is lost. This is useful mostly for new nodes created for the "do" call. Do not use for resources. + + + + + + + + Register a method that will be called when the action is undone (i.e. the "undo" action). + If this is the first operation, the [param object] will be used to deduce target undo history. + + + + + + + + + Register a property value change for "undo". + If this is the first operation, the [param object] will be used to deduce target undo history. + + + + + + + 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!). + + + + + + + Commit the action. If [param execute] is true (default), all "do" methods/properties are called/set when this function is called. + + + + + + + + + Create a new action. After this is called, do all your calls to [method add_do_method], [method add_undo_method], [method add_do_property], and [method add_undo_property], then commit the action with [method commit_action]. + The way actions are merged is dictated by the [param merge_mode] argument. See [enum UndoRedo.MergeMode] for details. + If [param custom_context] object is provided, it will be used for deducing target history (instead of using the first operation). + + + + + + + Returns the [UndoRedo] object associated with the given history [param id]. + [param id] above [code]0[/code] are mapped to the opened scene tabs (but it doesn't match their order). [param id] of [code]0[/code] or lower have special meaning (see [enum SpecialHistory]). + Best used with [method get_object_history_id]. This method is only provided in case you need some more advanced methods of [UndoRedo] (but keep in mind that directly operating on the [UndoRedo] object might affect editor's stability). + + + + + + + Returns the history ID deduced from the given [param object]. It can be used with [method get_history_undo_redo]. + + + + + + Returns [code]true[/code] if the [EditorUndoRedoManager] is currently committing the action, i.e. running its "do" method or property change (see [method commit_action]). + + + + + + Global history not associated with any scene, but with external resources etc. + + + Invalid "null" history. It's a special value, not associated with any object. + + + diff --git a/editor/animation_bezier_editor.cpp b/editor/animation_bezier_editor.cpp index e10ed7e9768..11e46152ef5 100644 --- a/editor/animation_bezier_editor.cpp +++ b/editor/animation_bezier_editor.cpp @@ -32,6 +32,7 @@ #include "editor/editor_node.h" #include "editor/editor_scale.h" +#include "editor/editor_undo_redo_manager.h" #include "scene/gui/view_panner.h" #include "scene/resources/text_line.h" @@ -649,7 +650,7 @@ Size2 AnimationBezierTrackEdit::get_minimum_size() const { return Vector2(1, 1); } -void AnimationBezierTrackEdit::set_undo_redo(UndoRedo *p_undo_redo) { +void AnimationBezierTrackEdit::set_undo_redo(Ref p_undo_redo) { undo_redo = p_undo_redo; } diff --git a/editor/animation_bezier_editor.h b/editor/animation_bezier_editor.h index 070a6589ad0..3e94b4fa844 100644 --- a/editor/animation_bezier_editor.h +++ b/editor/animation_bezier_editor.h @@ -34,6 +34,7 @@ #include "animation_track_editor.h" #include "core/templates/rb_set.h" +class EditorUndoRedoManager; class ViewPanner; class AnimationBezierTrackEdit : public Control { @@ -48,7 +49,7 @@ class AnimationBezierTrackEdit : public Control { }; AnimationTimelineEdit *timeline = nullptr; - UndoRedo *undo_redo = nullptr; + Ref undo_redo; Node *root = nullptr; Control *play_position = nullptr; //separate control used to draw so updates for only position changed are much faster float play_position_pos = 0; @@ -180,7 +181,7 @@ public: void set_animation_and_track(const Ref &p_animation, int p_track, bool p_read_only); virtual Size2 get_minimum_size() const override; - void set_undo_redo(UndoRedo *p_undo_redo); + void set_undo_redo(Ref p_undo_redo); void set_timeline(AnimationTimelineEdit *p_timeline); void set_editor(AnimationTrackEditor *p_editor); void set_root(Node *p_root); diff --git a/editor/animation_track_editor.cpp b/editor/animation_track_editor.cpp index 5580d3d5b1d..93666b55f16 100644 --- a/editor/animation_track_editor.cpp +++ b/editor/animation_track_editor.cpp @@ -35,6 +35,7 @@ #include "editor/animation_bezier_editor.h" #include "editor/editor_node.h" #include "editor/editor_scale.h" +#include "editor/editor_undo_redo_manager.h" #include "editor/plugins/animation_player_editor_plugin.h" #include "scene/animation/animation_player.h" #include "scene/gui/separator.h" @@ -680,7 +681,7 @@ public: } } - UndoRedo *undo_redo = nullptr; + Ref undo_redo; Ref animation; int track = -1; float key_ofs = 0; @@ -1374,7 +1375,7 @@ public: bool use_fps = false; - UndoRedo *undo_redo = nullptr; + Ref undo_redo; void notify_change() { notify_property_list_changed(); @@ -1708,7 +1709,7 @@ Size2 AnimationTimelineEdit::get_minimum_size() const { return ms; } -void AnimationTimelineEdit::set_undo_redo(UndoRedo *p_undo_redo) { +void AnimationTimelineEdit::set_undo_redo(Ref p_undo_redo) { undo_redo = p_undo_redo; } @@ -2507,10 +2508,14 @@ Size2 AnimationTrackEdit::get_minimum_size() const { return Vector2(1, max_h + separation); } -void AnimationTrackEdit::set_undo_redo(UndoRedo *p_undo_redo) { +void AnimationTrackEdit::set_undo_redo(Ref p_undo_redo) { undo_redo = p_undo_redo; } +Ref AnimationTrackEdit::get_undo_redo() const { + return undo_redo; +} + void AnimationTrackEdit::set_timeline(AnimationTimelineEdit *p_timeline) { timeline = p_timeline; timeline->set_track_edit(this); @@ -6065,7 +6070,7 @@ void AnimationTrackEditor::_edit_menu_pressed(int p_option) { case EDIT_OPTIMIZE_ANIMATION_CONFIRM: { animation->optimize(optimize_linear_error->get_value(), optimize_angular_error->get_value(), optimize_max_angle->get_value()); _update_tracks(); - undo_redo->clear_history(); + undo_redo->clear_history(true, undo_redo->get_history_for_object(animation.ptr()).id); } break; case EDIT_CLEAN_UP_ANIMATION: { @@ -6133,7 +6138,7 @@ void AnimationTrackEditor::_cleanup_animation(Ref p_animation) { } } - undo_redo->clear_history(); + undo_redo->clear_history(true, undo_redo->get_history_for_object(animation.ptr()).id); _update_tracks(); } @@ -6303,7 +6308,7 @@ void AnimationTrackEditor::_pick_track_filter_input(const Ref &p_ie) } AnimationTrackEditor::AnimationTrackEditor() { - undo_redo = EditorNode::get_singleton()->get_undo_redo(); + undo_redo = EditorNode::get_undo_redo(); main_panel = memnew(PanelContainer); main_panel->set_focus_mode(FOCUS_ALL); // Allow panel to have focus so that shortcuts work as expected. diff --git a/editor/animation_track_editor.h b/editor/animation_track_editor.h index a785f5aec7a..42238d8c725 100644 --- a/editor/animation_track_editor.h +++ b/editor/animation_track_editor.h @@ -78,7 +78,7 @@ class AnimationTimelineEdit : public Range { void _anim_loop_pressed(); void _play_position_draw(); - UndoRedo *undo_redo = nullptr; + Ref undo_redo; Rect2 hsize_rect; bool editing = false; @@ -112,7 +112,7 @@ public: void set_track_edit(AnimationTrackEdit *p_track_edit); void set_zoom(Range *p_zoom); Range *get_zoom() const { return zoom; } - void set_undo_redo(UndoRedo *p_undo_redo); + void set_undo_redo(Ref p_undo_redo); void set_play_position(float p_pos); float get_play_position() const; @@ -153,7 +153,7 @@ class AnimationTrackEdit : public Control { }; AnimationTimelineEdit *timeline = nullptr; - UndoRedo *undo_redo = nullptr; + Ref undo_redo; Popup *path_popup = nullptr; LineEdit *path = nullptr; Node *root = nullptr; @@ -234,12 +234,12 @@ public: Ref get_animation() const; AnimationTimelineEdit *get_timeline() const { return timeline; } AnimationTrackEditor *get_editor() const { return editor; } - UndoRedo *get_undo_redo() const { return undo_redo; } + Ref get_undo_redo() const; NodePath get_path() const; void set_animation_and_track(const Ref &p_animation, int p_track, bool p_read_only); virtual Size2 get_minimum_size() const override; - void set_undo_redo(UndoRedo *p_undo_redo); + void set_undo_redo(Ref p_undo_redo); void set_timeline(AnimationTimelineEdit *p_timeline); void set_editor(AnimationTrackEditor *p_editor); void set_root(Node *p_root); @@ -334,7 +334,7 @@ class AnimationTrackEditor : public VBoxContainer { void _animation_track_remove_request(int p_track, Ref p_from_animation); void _track_grab_focus(int p_track); - UndoRedo *undo_redo = nullptr; + Ref undo_redo; void _update_scroll(double); void _update_step(double p_new_step); diff --git a/editor/animation_track_editor_plugins.cpp b/editor/animation_track_editor_plugins.cpp index cd40b539198..ab64aaa24d6 100644 --- a/editor/animation_track_editor_plugins.cpp +++ b/editor/animation_track_editor_plugins.cpp @@ -33,6 +33,7 @@ #include "editor/audio_stream_preview.h" #include "editor/editor_resource_preview.h" #include "editor/editor_scale.h" +#include "editor/editor_undo_redo_manager.h" #include "scene/2d/animated_sprite_2d.h" #include "scene/2d/sprite_2d.h" #include "scene/3d/sprite_3d.h" diff --git a/editor/array_property_edit.cpp b/editor/array_property_edit.cpp index 58527ee4d19..ab0e801c884 100644 --- a/editor/array_property_edit.cpp +++ b/editor/array_property_edit.cpp @@ -32,6 +32,7 @@ #include "core/io/marshalls.h" #include "editor/editor_node.h" +#include "editor/editor_undo_redo_manager.h" #define ITEMS_PER_PAGE 100 @@ -87,7 +88,7 @@ bool ArrayPropertyEdit::_set(const StringName &p_name, const Variant &p_value) { return true; } - UndoRedo *ur = EditorNode::get_undo_redo(); + Ref &ur = EditorNode::get_undo_redo(); ur->create_action(TTR("Resize Array")); ur->add_do_method(this, "_set_size", newsize); ur->add_undo_method(this, "_set_size", size); @@ -134,7 +135,7 @@ bool ArrayPropertyEdit::_set(const StringName &p_name, const Variant &p_value) { Callable::CallError ce; Variant new_value; Variant::construct(Variant::Type(type), new_value, nullptr, 0, ce); - UndoRedo *ur = EditorNode::get_undo_redo(); + Ref &ur = EditorNode::get_undo_redo(); ur->create_action(TTR("Change Array Value Type")); ur->add_do_method(this, "_set_value", idx, new_value); @@ -150,7 +151,7 @@ bool ArrayPropertyEdit::_set(const StringName &p_name, const Variant &p_value) { Variant arr = get_array(); Variant value = arr.get(idx); - UndoRedo *ur = EditorNode::get_undo_redo(); + Ref &ur = EditorNode::get_undo_redo(); ur->create_action(TTR("Change Array Value")); ur->add_do_method(this, "_set_value", idx, p_value); diff --git a/editor/connections_dialog.cpp b/editor/connections_dialog.cpp index 6fdd9563fb4..6c86b341da0 100644 --- a/editor/connections_dialog.cpp +++ b/editor/connections_dialog.cpp @@ -34,6 +34,7 @@ #include "editor/editor_node.h" #include "editor/editor_scale.h" #include "editor/editor_settings.h" +#include "editor/editor_undo_redo_manager.h" #include "editor/scene_tree_dock.h" #include "plugins/script_editor_plugin.h" @@ -924,6 +925,10 @@ void ConnectionsDock::_bind_methods() { ClassDB::bind_method("update_tree", &ConnectionsDock::update_tree); } +void ConnectionsDock::set_undo_redo(Ref p_undo_redo) { + undo_redo = p_undo_redo; +} + void ConnectionsDock::set_node(Node *p_node) { selected_node = p_node; update_tree(); diff --git a/editor/connections_dialog.h b/editor/connections_dialog.h index d141d1a8802..7e7192019bb 100644 --- a/editor/connections_dialog.h +++ b/editor/connections_dialog.h @@ -31,7 +31,6 @@ #ifndef CONNECTIONS_DIALOG_H #define CONNECTIONS_DIALOG_H -#include "core/object/undo_redo.h" #include "editor/editor_inspector.h" #include "editor/scene_tree_editor.h" #include "scene/gui/button.h" @@ -48,6 +47,7 @@ #include "scene/gui/tree.h" class ConnectDialogBinds; +class EditorUndoRedoManager; class ConnectDialog : public ConfirmationDialog { GDCLASS(ConnectDialog, ConfirmationDialog); @@ -194,7 +194,7 @@ class ConnectionsDock : public VBoxContainer { Button *connect_button = nullptr; PopupMenu *signal_menu = nullptr; PopupMenu *slot_menu = nullptr; - UndoRedo *undo_redo = nullptr; + Ref undo_redo; LineEdit *search_box = nullptr; HashMap> descr_cache; @@ -225,7 +225,7 @@ protected: static void _bind_methods(); public: - void set_undoredo(UndoRedo *p_undo_redo) { undo_redo = p_undo_redo; } + void set_undo_redo(Ref p_undo_redo); void set_node(Node *p_node); void update_tree(); diff --git a/editor/debugger/editor_debugger_node.cpp b/editor/debugger/editor_debugger_node.cpp index 472e53c0e8d..9fd7fa578fd 100644 --- a/editor/debugger/editor_debugger_node.cpp +++ b/editor/debugger/editor_debugger_node.cpp @@ -30,6 +30,7 @@ #include "editor_debugger_node.h" +#include "core/object/undo_redo.h" #include "editor/debugger/editor_debugger_tree.h" #include "editor/debugger/script_editor_debugger.h" #include "editor/editor_log.h" @@ -83,8 +84,6 @@ EditorDebuggerNode::EditorDebuggerNode() { inspect_edited_object_timeout = EDITOR_DEF("debugger/remote_inspect_refresh_interval", 0.2); EditorNode *editor = EditorNode::get_singleton(); - editor->get_undo_redo()->set_method_notify_callback(_method_changeds, this); - editor->get_undo_redo()->set_property_notify_callback(_property_changeds, this); editor->get_pause_button()->connect("pressed", callable_mp(this, &EditorDebuggerNode::_paused)); } @@ -181,6 +180,11 @@ void EditorDebuggerNode::_bind_methods() { ADD_SIGNAL(MethodInfo("breakpoint_toggled", PropertyInfo(Variant::STRING, "path"), PropertyInfo(Variant::INT, "line"), PropertyInfo(Variant::BOOL, "enabled"))); } +void EditorDebuggerNode::register_undo_redo(UndoRedo *p_undo_redo) { + p_undo_redo->set_method_notify_callback(_method_changeds, this); + p_undo_redo->set_property_notify_callback(_property_changeds, this); +} + EditorDebuggerRemoteObject *EditorDebuggerNode::get_inspected_remote_object() { return Object::cast_to(ObjectDB::get_instance(EditorNode::get_singleton()->get_editor_selection_history()->get_current())); } diff --git a/editor/debugger/editor_debugger_node.h b/editor/debugger/editor_debugger_node.h index 4c9ad49ac4c..e79e60b180c 100644 --- a/editor/debugger/editor_debugger_node.h +++ b/editor/debugger/editor_debugger_node.h @@ -41,6 +41,7 @@ class EditorDebuggerRemoteObject; class MenuButton; class ScriptEditorDebugger; class TabContainer; +class UndoRedo; class EditorDebuggerNode : public MarginContainer { GDCLASS(EditorDebuggerNode, MarginContainer); @@ -152,6 +153,7 @@ protected: public: static EditorDebuggerNode *get_singleton() { return singleton; } + void register_undo_redo(UndoRedo *p_undo_redo); ScriptEditorDebugger *get_current_debugger() const; ScriptEditorDebugger *get_default_debugger() const; diff --git a/editor/dictionary_property_edit.cpp b/editor/dictionary_property_edit.cpp index 630265e2686..f16c5402adc 100644 --- a/editor/dictionary_property_edit.cpp +++ b/editor/dictionary_property_edit.cpp @@ -30,6 +30,7 @@ #include "dictionary_property_edit.h" #include "editor/editor_node.h" +#include "editor/editor_undo_redo_manager.h" void DictionaryPropertyEdit::_notif_change() { notify_property_list_changed(); @@ -118,7 +119,7 @@ bool DictionaryPropertyEdit::_set(const StringName &p_name, const Variant &p_val int index = pn.substr(0, slash).to_int(); if (type == "key" && index < keys.size()) { const Variant &key = keys[index]; - UndoRedo *ur = EditorNode::get_undo_redo(); + Ref &ur = EditorNode::get_undo_redo(); ur->create_action(TTR("Change Dictionary Key")); ur->add_do_method(this, "_set_key", key, p_value); @@ -130,7 +131,7 @@ bool DictionaryPropertyEdit::_set(const StringName &p_name, const Variant &p_val const Variant &key = keys[index]; if (dict.has(key)) { Variant value = dict[key]; - UndoRedo *ur = EditorNode::get_undo_redo(); + Ref &ur = EditorNode::get_undo_redo(); ur->create_action(TTR("Change Dictionary Value")); ur->add_do_method(this, "_set_value", key, p_value); diff --git a/editor/editor_audio_buses.cpp b/editor/editor_audio_buses.cpp index b6d7bbc45f7..f1add65b7c0 100644 --- a/editor/editor_audio_buses.cpp +++ b/editor/editor_audio_buses.cpp @@ -38,6 +38,7 @@ #include "editor/editor_node.h" #include "editor/editor_scale.h" #include "editor/editor_settings.h" +#include "editor/editor_undo_redo_manager.h" #include "filesystem_dock.h" #include "scene/resources/font.h" #include "servers/audio_server.h" @@ -280,7 +281,7 @@ void EditorAudioBus::_name_changed(const String &p_new_name) { } updating_bus = true; - UndoRedo *ur = EditorNode::get_undo_redo(); + Ref &ur = EditorNode::get_undo_redo(); StringName current = AudioServer::get_singleton()->get_bus_name(get_index()); ur->create_action(TTR("Rename Audio Bus")); @@ -321,7 +322,7 @@ void EditorAudioBus::_volume_changed(float p_normalized) { slider->set_value(_scaled_db_to_normalized_volume(Math::round(p_db))); } - UndoRedo *ur = EditorNode::get_undo_redo(); + Ref &ur = EditorNode::get_undo_redo(); ur->create_action(TTR("Change Audio Bus Volume"), UndoRedo::MERGE_ENDS); ur->add_do_method(AudioServer::get_singleton(), "set_bus_volume_db", get_index(), p_db); ur->add_undo_method(AudioServer::get_singleton(), "set_bus_volume_db", get_index(), AudioServer::get_singleton()->get_bus_volume_db(get_index())); @@ -415,7 +416,7 @@ void EditorAudioBus::_hide_value_preview() { void EditorAudioBus::_solo_toggled() { updating_bus = true; - UndoRedo *ur = EditorNode::get_undo_redo(); + Ref &ur = EditorNode::get_undo_redo(); ur->create_action(TTR("Toggle Audio Bus Solo")); ur->add_do_method(AudioServer::get_singleton(), "set_bus_solo", get_index(), solo->is_pressed()); ur->add_undo_method(AudioServer::get_singleton(), "set_bus_solo", get_index(), AudioServer::get_singleton()->is_bus_solo(get_index())); @@ -429,7 +430,7 @@ void EditorAudioBus::_solo_toggled() { void EditorAudioBus::_mute_toggled() { updating_bus = true; - UndoRedo *ur = EditorNode::get_undo_redo(); + Ref &ur = EditorNode::get_undo_redo(); ur->create_action(TTR("Toggle Audio Bus Mute")); ur->add_do_method(AudioServer::get_singleton(), "set_bus_mute", get_index(), mute->is_pressed()); ur->add_undo_method(AudioServer::get_singleton(), "set_bus_mute", get_index(), AudioServer::get_singleton()->is_bus_mute(get_index())); @@ -443,7 +444,7 @@ void EditorAudioBus::_mute_toggled() { void EditorAudioBus::_bypass_toggled() { updating_bus = true; - UndoRedo *ur = EditorNode::get_undo_redo(); + Ref &ur = EditorNode::get_undo_redo(); ur->create_action(TTR("Toggle Audio Bus Bypass Effects")); ur->add_do_method(AudioServer::get_singleton(), "set_bus_bypass_effects", get_index(), bypass->is_pressed()); ur->add_undo_method(AudioServer::get_singleton(), "set_bus_bypass_effects", get_index(), AudioServer::get_singleton()->is_bus_bypassing_effects(get_index())); @@ -457,7 +458,7 @@ void EditorAudioBus::_bypass_toggled() { void EditorAudioBus::_send_selected(int p_which) { updating_bus = true; - UndoRedo *ur = EditorNode::get_undo_redo(); + Ref &ur = EditorNode::get_undo_redo(); ur->create_action(TTR("Select Audio Bus Send")); ur->add_do_method(AudioServer::get_singleton(), "set_bus_send", get_index(), send->get_item_text(p_which)); ur->add_undo_method(AudioServer::get_singleton(), "set_bus_send", get_index(), AudioServer::get_singleton()->get_bus_send(get_index())); @@ -507,7 +508,7 @@ void EditorAudioBus::_effect_edited() { int index = effect->get_metadata(0); updating_bus = true; - UndoRedo *ur = EditorNode::get_undo_redo(); + Ref &ur = EditorNode::get_undo_redo(); ur->create_action(TTR("Select Audio Bus Send")); ur->add_do_method(AudioServer::get_singleton(), "set_bus_effect_enabled", get_index(), index, effect->is_checked(0)); ur->add_undo_method(AudioServer::get_singleton(), "set_bus_effect_enabled", get_index(), index, AudioServer::get_singleton()->is_bus_effect_enabled(get_index(), index)); @@ -534,7 +535,7 @@ void EditorAudioBus::_effect_add(int p_which) { afxr->set_name(effect_options->get_item_text(p_which)); - UndoRedo *ur = EditorNode::get_undo_redo(); + Ref &ur = EditorNode::get_undo_redo(); ur->create_action(TTR("Add Audio Bus Effect")); ur->add_do_method(AudioServer::get_singleton(), "add_bus_effect", get_index(), afxr, -1); ur->add_undo_method(AudioServer::get_singleton(), "remove_bus_effect", get_index(), AudioServer::get_singleton()->get_bus_effect_count(get_index())); @@ -688,7 +689,7 @@ void EditorAudioBus::drop_data_fw(const Point2 &p_point, const Variant &p_data, bool enabled = AudioServer::get_singleton()->is_bus_effect_enabled(bus, effect); - UndoRedo *ur = EditorNode::get_undo_redo(); + Ref &ur = EditorNode::get_undo_redo(); ur->create_action(TTR("Move Bus Effect")); ur->add_do_method(AudioServer::get_singleton(), "remove_bus_effect", bus, effect); ur->add_do_method(AudioServer::get_singleton(), "add_bus_effect", get_index(), AudioServer::get_singleton()->get_bus_effect(bus, effect), paste_at); @@ -730,7 +731,7 @@ void EditorAudioBus::_delete_effect_pressed(int p_option) { int index = item->get_metadata(0); - UndoRedo *ur = EditorNode::get_undo_redo(); + Ref &ur = EditorNode::get_undo_redo(); ur->create_action(TTR("Delete Bus Effect")); ur->add_do_method(AudioServer::get_singleton(), "remove_bus_effect", get_index(), index); ur->add_undo_method(AudioServer::get_singleton(), "add_bus_effect", get_index(), AudioServer::get_singleton()->get_bus_effect(get_index(), index), index); @@ -1063,7 +1064,7 @@ void EditorAudioBuses::_notification(int p_what) { } void EditorAudioBuses::_add_bus() { - UndoRedo *ur = EditorNode::get_undo_redo(); + Ref &ur = EditorNode::get_undo_redo(); ur->create_action(TTR("Add Audio Bus")); ur->add_do_method(AudioServer::get_singleton(), "set_bus_count", AudioServer::get_singleton()->get_bus_count() + 1); @@ -1095,7 +1096,7 @@ void EditorAudioBuses::_delete_bus(Object *p_which) { return; } - UndoRedo *ur = EditorNode::get_undo_redo(); + Ref &ur = EditorNode::get_undo_redo(); ur->create_action(TTR("Delete Audio Bus")); ur->add_do_method(AudioServer::get_singleton(), "remove_bus", index); @@ -1117,7 +1118,7 @@ void EditorAudioBuses::_delete_bus(Object *p_which) { void EditorAudioBuses::_duplicate_bus(int p_which) { int add_at_pos = p_which + 1; - UndoRedo *ur = EditorNode::get_undo_redo(); + Ref &ur = EditorNode::get_undo_redo(); ur->create_action(TTR("Duplicate Audio Bus")); ur->add_do_method(AudioServer::get_singleton(), "add_bus", add_at_pos); ur->add_do_method(AudioServer::get_singleton(), "set_bus_name", add_at_pos, AudioServer::get_singleton()->get_bus_name(p_which) + " Copy"); @@ -1140,7 +1141,7 @@ void EditorAudioBuses::_reset_bus_volume(Object *p_which) { EditorAudioBus *bus = Object::cast_to(p_which); int index = bus->get_index(); - UndoRedo *ur = EditorNode::get_undo_redo(); + Ref &ur = EditorNode::get_undo_redo(); ur->create_action(TTR("Reset Bus Volume")); ur->add_do_method(AudioServer::get_singleton(), "set_bus_volume_db", index, 0.f); ur->add_undo_method(AudioServer::get_singleton(), "set_bus_volume_db", index, AudioServer::get_singleton()->get_bus_volume_db(index)); @@ -1160,7 +1161,7 @@ void EditorAudioBuses::_request_drop_end() { } void EditorAudioBuses::_drop_at_index(int p_bus, int p_index) { - UndoRedo *ur = EditorNode::get_undo_redo(); + Ref &ur = EditorNode::get_undo_redo(); ur->create_action(TTR("Move Audio Bus")); ur->add_do_method(AudioServer::get_singleton(), "move_bus", p_bus, p_index); @@ -1219,7 +1220,7 @@ void EditorAudioBuses::_load_default_layout() { file->set_text(String(TTR("Layout:")) + " " + layout_path.get_file()); AudioServer::get_singleton()->set_bus_layout(state); _update_buses(); - EditorNode::get_singleton()->get_undo_redo()->clear_history(); + EditorNode::get_undo_redo()->clear_history(true, EditorUndoRedoManager::GLOBAL_HISTORY); call_deferred(SNAME("_select_layout")); } @@ -1235,7 +1236,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); _update_buses(); - EditorNode::get_singleton()->get_undo_redo()->clear_history(); + EditorNode::get_undo_redo()->clear_history(true, EditorUndoRedoManager::GLOBAL_HISTORY); call_deferred(SNAME("_select_layout")); } else if (file_dialog->get_file_mode() == EditorFileDialog::FILE_MODE_SAVE_FILE) { @@ -1255,7 +1256,7 @@ void EditorAudioBuses::_file_dialog_callback(const String &p_string) { edited_path = p_string; file->set_text(String(TTR("Layout:")) + " " + p_string.get_file()); _update_buses(); - EditorNode::get_singleton()->get_undo_redo()->clear_history(); + EditorNode::get_undo_redo()->clear_history(true, EditorUndoRedoManager::GLOBAL_HISTORY); call_deferred(SNAME("_select_layout")); } } @@ -1354,7 +1355,7 @@ void EditorAudioBuses::open_layout(const String &p_path) { file->set_text(p_path.get_file()); AudioServer::get_singleton()->set_bus_layout(state); _update_buses(); - EditorNode::get_singleton()->get_undo_redo()->clear_history(); + EditorNode::get_undo_redo()->clear_history(true, EditorUndoRedoManager::GLOBAL_HISTORY); call_deferred(SNAME("_select_layout")); } diff --git a/editor/editor_autoload_settings.cpp b/editor/editor_autoload_settings.cpp index ee4955d0a01..120ac5b984a 100644 --- a/editor/editor_autoload_settings.cpp +++ b/editor/editor_autoload_settings.cpp @@ -35,6 +35,7 @@ #include "editor/editor_file_dialog.h" #include "editor/editor_node.h" #include "editor/editor_scale.h" +#include "editor/editor_undo_redo_manager.h" #include "editor/filesystem_dock.h" #include "project_settings_editor.h" #include "scene/main/window.h" @@ -193,7 +194,7 @@ void EditorAutoloadSettings::_autoload_edited() { TreeItem *ti = tree->get_edited(); int column = tree->get_edited_column(); - UndoRedo *undo_redo = EditorNode::get_undo_redo(); + Ref undo_redo = EditorNode::get_undo_redo(); if (column == 0) { String name = ti->get_text(0); @@ -288,7 +289,7 @@ void EditorAutoloadSettings::_autoload_button_pressed(Object *p_item, int p_colu String name = "autoload/" + ti->get_text(0); - UndoRedo *undo_redo = EditorNode::get_undo_redo(); + Ref undo_redo = EditorNode::get_undo_redo(); switch (p_button) { case BUTTON_OPEN: { @@ -713,7 +714,7 @@ void EditorAutoloadSettings::drop_data_fw(const Point2 &p_point, const Variant & orders.sort(); - UndoRedo *undo_redo = EditorNode::get_undo_redo(); + Ref undo_redo = EditorNode::get_undo_redo(); undo_redo->create_action(TTR("Rearrange Autoloads")); @@ -757,7 +758,7 @@ bool EditorAutoloadSettings::autoload_add(const String &p_name, const String &p_ name = "autoload/" + name; - UndoRedo *undo_redo = EditorNode::get_undo_redo(); + Ref undo_redo = EditorNode::get_undo_redo(); undo_redo->create_action(TTR("Add Autoload")); // Singleton autoloads are represented with a leading "*" in their path. @@ -783,7 +784,7 @@ bool EditorAutoloadSettings::autoload_add(const String &p_name, const String &p_ void EditorAutoloadSettings::autoload_remove(const String &p_name) { String name = "autoload/" + p_name; - UndoRedo *undo_redo = EditorNode::get_undo_redo(); + Ref undo_redo = EditorNode::get_undo_redo(); int order = ProjectSettings::get_singleton()->get_order(name); diff --git a/editor/editor_data.cpp b/editor/editor_data.cpp index 2d4945db14c..64bdac1e77d 100644 --- a/editor/editor_data.cpp +++ b/editor/editor_data.cpp @@ -35,6 +35,7 @@ #include "core/io/resource_loader.h" #include "editor/editor_node.h" #include "editor/editor_plugin.h" +#include "editor/editor_undo_redo_manager.h" #include "editor/plugins/script_editor_plugin.h" #include "scene/resources/packed_scene.h" @@ -364,13 +365,13 @@ void EditorData::restore_editor_global_states() { void EditorData::paste_object_params(Object *p_object) { ERR_FAIL_NULL(p_object); - undo_redo.create_action(TTR("Paste Params")); + undo_redo_manager->create_action(TTR("Paste Params")); for (const PropertyData &E : clipboard) { String name = E.name; - undo_redo.add_do_property(p_object, name, E.value); - undo_redo.add_undo_property(p_object, name, p_object->get(name)); + undo_redo_manager->add_do_property(p_object, name, E.value); + undo_redo_manager->add_undo_property(p_object, name, p_object->get(name)); } - undo_redo.commit_action(); + undo_redo_manager->commit_action(); } bool EditorData::call_build() { @@ -383,8 +384,49 @@ bool EditorData::call_build() { return result; } -UndoRedo &EditorData::get_undo_redo() { - return undo_redo; +void EditorData::set_scene_as_saved(int p_idx) { + if (p_idx == -1) { + p_idx = current_edited_scene; + } + ERR_FAIL_INDEX(p_idx, edited_scene.size()); + + get_undo_redo()->set_history_as_saved(edited_scene[p_idx].history_id); +} + +bool EditorData::is_scene_changed(int p_idx) { + if (p_idx == -1) { + p_idx = current_edited_scene; + } + ERR_FAIL_INDEX_V(p_idx, edited_scene.size(), false); + + uint64_t current_scene_version = get_undo_redo()->get_or_create_history(edited_scene[p_idx].history_id).undo_redo->get_version(); + bool is_changed = edited_scene[p_idx].last_checked_version != current_scene_version; + edited_scene.write[p_idx].last_checked_version = current_scene_version; + return is_changed; +} + +int EditorData::get_scene_history_id_from_path(const String &p_path) const { + for (const EditedScene &E : edited_scene) { + if (E.path == p_path) { + return E.history_id; + } + } + return 0; +} + +int EditorData::get_current_edited_scene_history_id() const { + if (current_edited_scene != -1) { + return edited_scene[current_edited_scene].history_id; + } + return 0; +} + +int EditorData::get_scene_history_id(int p_idx) const { + return edited_scene[p_idx].history_id; +} + +Ref &EditorData::get_undo_redo() { + return undo_redo_manager; } void EditorData::add_undo_redo_inspector_hook_callback(Callable p_callable) { @@ -415,12 +457,12 @@ Callable EditorData::get_move_array_element_function(const StringName &p_class) } void EditorData::remove_editor_plugin(EditorPlugin *p_plugin) { - p_plugin->undo_redo = nullptr; + p_plugin->undo_redo = Ref(); editor_plugins.erase(p_plugin); } void EditorData::add_editor_plugin(EditorPlugin *p_plugin) { - p_plugin->undo_redo = &undo_redo; + p_plugin->undo_redo = undo_redo_manager; editor_plugins.push_back(p_plugin); } @@ -505,8 +547,8 @@ int EditorData::add_edited_scene(int p_at_pos) { es.path = String(); es.file_modified_time = 0; es.history_current = -1; - es.version = 0; es.live_edit_root = NodePath(String("/root")); + es.history_id = last_created_scene++; if (p_at_pos == edited_scene.size()) { edited_scene.push_back(es); @@ -547,6 +589,7 @@ void EditorData::remove_scene(int p_idx) { ScriptEditor::get_singleton()->close_builtin_scripts_from_scene(edited_scene[p_idx].path); } + undo_redo_manager->discard_history(edited_scene[p_idx].history_id); edited_scene.remove_at(p_idx); } @@ -679,26 +722,10 @@ Vector EditorData::get_edited_scenes() const { return out_edited_scenes_list; } -void EditorData::set_edited_scene_version(uint64_t version, int p_scene_idx) { - ERR_FAIL_INDEX(current_edited_scene, edited_scene.size()); - if (p_scene_idx < 0) { - edited_scene.write[current_edited_scene].version = version; - } else { - ERR_FAIL_INDEX(p_scene_idx, edited_scene.size()); - edited_scene.write[p_scene_idx].version = version; - } -} - -uint64_t EditorData::get_scene_version(int p_idx) const { - ERR_FAIL_INDEX_V(p_idx, edited_scene.size(), 0); - return edited_scene[p_idx].version; -} - void EditorData::set_scene_modified_time(int p_idx, uint64_t p_time) { if (p_idx == -1) { p_idx = current_edited_scene; } - ERR_FAIL_INDEX(p_idx, edited_scene.size()); edited_scene.write[p_idx].file_modified_time = p_time; @@ -991,6 +1018,7 @@ void EditorData::script_class_load_icon_paths() { EditorData::EditorData() { current_edited_scene = -1; + undo_redo_manager.instantiate(); script_class_load_icon_paths(); } diff --git a/editor/editor_data.h b/editor/editor_data.h index 351c63f4b90..655a62a9aec 100644 --- a/editor/editor_data.h +++ b/editor/editor_data.h @@ -31,12 +31,12 @@ #ifndef EDITOR_DATA_H #define EDITOR_DATA_H -#include "core/object/undo_redo.h" #include "core/templates/list.h" #include "scene/resources/texture.h" class ConfigFile; class EditorPlugin; +class EditorUndoRedoManager; /** * Stores the history of objects which have been selected for editing in the Editor & the Inspector. @@ -118,8 +118,9 @@ public: Vector history_stored; int history_current = 0; Dictionary custom_state; - uint64_t version = 0; NodePath live_edit_root; + int history_id = 0; + uint64_t last_checked_version = 0; }; private: @@ -132,12 +133,13 @@ private: HashMap> custom_types; List clipboard; - UndoRedo undo_redo; + Ref undo_redo_manager; Vector undo_redo_callbacks; HashMap move_element_functions; Vector edited_scene; - int current_edited_scene; + int current_edited_scene = -1; + int last_created_scene = 1; bool _find_updated_instances(Node *p_root, Node *p_node, HashSet &checked_paths); @@ -166,7 +168,7 @@ public: int get_editor_plugin_count() const; EditorPlugin *get_editor_plugin(int p_idx); - UndoRedo &get_undo_redo(); + Ref &get_undo_redo(); void add_undo_redo_inspector_hook_callback(Callable p_callable); // Callbacks should have this signature: void (Object* undo_redo, Object *modified_object, String property, Variant new_value) void remove_undo_redo_inspector_hook_callback(Callable p_callable); const Vector get_undo_redo_inspector_hook_callback(); @@ -200,7 +202,6 @@ public: void set_scene_path(int p_idx, const String &p_path); Ref