diff options
author | Rémi Verschelde <rverschelde@gmail.com> | 2022-11-02 18:52:42 +0100 |
---|---|---|
committer | Rémi Verschelde <rverschelde@gmail.com> | 2022-11-02 18:52:42 +0100 |
commit | 17497b1eb977b0b717066d5ee2f1534e2b7215b5 (patch) | |
tree | 5c68180c67b309cb5839d4fd2d14742be84c092b | |
parent | d2e1b4b9f0cbd67e38773ad610912ff382cf4d4f (diff) | |
parent | f42cd7f83f82a34b8cbc168de4e9f37bbe674150 (diff) |
Merge pull request #65012 from KoBeWi/Godot_museum
Add history dock
-rw-r--r-- | doc/classes/EditorUndoRedoManager.xml | 12 | ||||
-rw-r--r-- | editor/editor_node.cpp | 9 | ||||
-rw-r--r-- | editor/editor_undo_redo_manager.cpp | 62 | ||||
-rw-r--r-- | editor/editor_undo_redo_manager.h | 4 | ||||
-rw-r--r-- | editor/history_dock.cpp | 251 | ||||
-rw-r--r-- | editor/history_dock.h | 66 |
6 files changed, 387 insertions, 17 deletions
diff --git a/doc/classes/EditorUndoRedoManager.xml b/doc/classes/EditorUndoRedoManager.xml index 1350e4487c..c1e771d653 100644 --- a/doc/classes/EditorUndoRedoManager.xml +++ b/doc/classes/EditorUndoRedoManager.xml @@ -107,6 +107,18 @@ </description> </method> </methods> + <signals> + <signal name="history_changed"> + <description> + Emitted when the list of actions in any history has changed, either when an action is commited or a history is cleared. + </description> + </signal> + <signal name="version_changed"> + <description> + Emitted when the version of any history has changed as a result of undo or redo call. + </description> + </signal> + </signals> <constants> <constant name="GLOBAL_HISTORY" value="0" enum="SpecialHistory"> Global history not associated with any scene, but with external resources etc. diff --git a/editor/editor_node.cpp b/editor/editor_node.cpp index be1f0be279..716e0b454f 100644 --- a/editor/editor_node.cpp +++ b/editor/editor_node.cpp @@ -110,6 +110,7 @@ #include "editor/export/export_template_manager.h" #include "editor/export/project_export.h" #include "editor/filesystem_dock.h" +#include "editor/history_dock.h" #include "editor/import/audio_stream_import_settings.h" #include "editor/import/dynamic_font_import_settings.h" #include "editor/import/editor_import_collada.h" @@ -3697,6 +3698,7 @@ void EditorNode::_set_main_scene_state(Dictionary p_state, Node *p_for_scene) { EditorDebuggerNode::get_singleton()->update_live_edit_root(); ScriptEditor::get_singleton()->set_scene_root_script(editor_data.get_scene_root_script(editor_data.get_edited_scene())); editor_data.notify_edited_scene_changed(); + emit_signal(SNAME("scene_changed")); } bool EditorNode::is_changing_scene() const { @@ -6040,6 +6042,7 @@ void EditorNode::_bind_methods() { ADD_SIGNAL(MethodInfo("resource_saved", PropertyInfo(Variant::OBJECT, "obj"))); ADD_SIGNAL(MethodInfo("scene_saved", PropertyInfo(Variant::STRING, "path"))); ADD_SIGNAL(MethodInfo("project_settings_changed")); + ADD_SIGNAL(MethodInfo("scene_changed")); } static Node *_resource_get_edited_scene() { @@ -7105,6 +7108,8 @@ EditorNode::EditorNode() { filesystem_dock->connect("display_mode_changed", callable_mp(this, &EditorNode::_save_docks)); get_project_settings()->connect_filesystem_dock_signals(filesystem_dock); + HistoryDock *hd = memnew(HistoryDock); + // Scene: Top left. dock_slot[DOCK_SLOT_LEFT_UR]->add_child(SceneTreeDock::get_singleton()); dock_slot[DOCK_SLOT_LEFT_UR]->set_tab_title(dock_slot[DOCK_SLOT_LEFT_UR]->get_tab_idx_from_control(SceneTreeDock::get_singleton()), TTR("Scene")); @@ -7125,6 +7130,10 @@ EditorNode::EditorNode() { dock_slot[DOCK_SLOT_RIGHT_UL]->add_child(NodeDock::get_singleton()); dock_slot[DOCK_SLOT_RIGHT_UL]->set_tab_title(dock_slot[DOCK_SLOT_RIGHT_UL]->get_tab_idx_from_control(NodeDock::get_singleton()), TTR("Node")); + // History: Full height right, behind Node. + dock_slot[DOCK_SLOT_RIGHT_UL]->add_child(hd); + dock_slot[DOCK_SLOT_RIGHT_UL]->set_tab_title(dock_slot[DOCK_SLOT_RIGHT_UL]->get_tab_idx_from_control(hd), TTR("History")); + // Hide unused dock slots and vsplits. dock_slot[DOCK_SLOT_LEFT_UL]->hide(); dock_slot[DOCK_SLOT_LEFT_BL]->hide(); diff --git a/editor/editor_undo_redo_manager.cpp b/editor/editor_undo_redo_manager.cpp index 064448fd96..09b567fc68 100644 --- a/editor/editor_undo_redo_manager.cpp +++ b/editor/editor_undo_redo_manager.cpp @@ -241,6 +241,7 @@ void EditorUndoRedoManager::commit_action(bool p_execute) { history.undo_stack.push_back(pending_action); pending_action = Action(); is_committing = false; + emit_signal(SNAME("history_changed")); } bool EditorUndoRedoManager::is_committing_action() const { @@ -252,14 +253,14 @@ bool EditorUndoRedoManager::undo() { return false; } - History *selected_history = nullptr; + int selected_history = INVALID_HISTORY; double global_timestamp = 0; // Pick the history with greatest last action timestamp (either global or current scene). { History &history = get_or_create_history(GLOBAL_HISTORY); if (!history.undo_stack.is_empty()) { - selected_history = &history; + selected_history = history.id; global_timestamp = history.undo_stack.back()->get().timestamp; } } @@ -267,32 +268,44 @@ bool EditorUndoRedoManager::undo() { { History &history = get_or_create_history(EditorNode::get_editor_data().get_current_edited_scene_history_id()); if (!history.undo_stack.is_empty() && history.undo_stack.back()->get().timestamp > global_timestamp) { - selected_history = &history; + selected_history = history.id; } } - if (selected_history) { - Action action = selected_history->undo_stack.back()->get(); - selected_history->undo_stack.pop_back(); - selected_history->redo_stack.push_back(action); - return selected_history->undo_redo->undo(); + if (selected_history != INVALID_HISTORY) { + return undo_history(selected_history); } return false; } +bool EditorUndoRedoManager::undo_history(int p_id) { + ERR_FAIL_COND_V(p_id == INVALID_HISTORY, false); + History &history = get_or_create_history(p_id); + + Action action = history.undo_stack.back()->get(); + history.undo_stack.pop_back(); + history.redo_stack.push_back(action); + + bool success = history.undo_redo->undo(); + if (success) { + emit_signal(SNAME("version_changed")); + } + return success; +} + bool EditorUndoRedoManager::redo() { if (!has_redo()) { return false; } - History *selected_history = nullptr; + int selected_history = INVALID_HISTORY; double global_timestamp = INFINITY; // Pick the history with lowest last action timestamp (either global or current scene). { History &history = get_or_create_history(GLOBAL_HISTORY); if (!history.redo_stack.is_empty()) { - selected_history = &history; + selected_history = history.id; global_timestamp = history.redo_stack.back()->get().timestamp; } } @@ -300,19 +313,31 @@ bool EditorUndoRedoManager::redo() { { History &history = get_or_create_history(EditorNode::get_editor_data().get_current_edited_scene_history_id()); if (!history.redo_stack.is_empty() && history.redo_stack.back()->get().timestamp < global_timestamp) { - selected_history = &history; + selected_history = history.id; } } - if (selected_history) { - Action action = selected_history->redo_stack.back()->get(); - selected_history->redo_stack.pop_back(); - selected_history->undo_stack.push_back(action); - return selected_history->undo_redo->redo(); + if (selected_history != INVALID_HISTORY) { + return redo_history(selected_history); } return false; } +bool EditorUndoRedoManager::redo_history(int p_id) { + ERR_FAIL_COND_V(p_id == INVALID_HISTORY, false); + History &history = get_or_create_history(p_id); + + Action action = history.redo_stack.back()->get(); + history.redo_stack.pop_back(); + history.undo_stack.push_back(action); + + bool success = history.undo_redo->redo(); + if (success) { + emit_signal(SNAME("version_changed")); + } + return success; +} + void EditorUndoRedoManager::set_history_as_saved(int p_id) { History &history = get_or_create_history(p_id); history.saved_version = history.undo_redo->get_version(); @@ -352,6 +377,7 @@ void EditorUndoRedoManager::clear_history(bool p_increase_version, int p_idx) { if (!p_increase_version) { set_history_as_saved(p_idx); } + emit_signal(SNAME("history_changed")); return; } @@ -359,6 +385,7 @@ void EditorUndoRedoManager::clear_history(bool p_increase_version, int p_idx) { E.value.undo_redo->clear_history(p_increase_version); set_history_as_saved(E.key); } + emit_signal(SNAME("history_changed")); } String EditorUndoRedoManager::get_current_action_name() { @@ -434,6 +461,9 @@ 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); + ADD_SIGNAL(MethodInfo("history_changed")); + ADD_SIGNAL(MethodInfo("version_changed")); + BIND_ENUM_CONSTANT(GLOBAL_HISTORY); BIND_ENUM_CONSTANT(INVALID_HISTORY); } diff --git a/editor/editor_undo_redo_manager.h b/editor/editor_undo_redo_manager.h index c4d85daa22..60bcd059df 100644 --- a/editor/editor_undo_redo_manager.h +++ b/editor/editor_undo_redo_manager.h @@ -44,7 +44,6 @@ public: INVALID_HISTORY = -99, }; -private: struct Action { int history_id = INVALID_HISTORY; double timestamp = 0; @@ -60,6 +59,7 @@ private: List<Action> redo_stack; }; +private: HashMap<int, History> history_map; Action pending_action; @@ -114,7 +114,9 @@ public: bool is_committing_action() const; bool undo(); + 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 set_history_as_saved(int p_idx); diff --git a/editor/history_dock.cpp b/editor/history_dock.cpp new file mode 100644 index 0000000000..57088a76cb --- /dev/null +++ b/editor/history_dock.cpp @@ -0,0 +1,251 @@ +/*************************************************************************/ +/* history_dock.cpp */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/*************************************************************************/ + +#include "history_dock.h" + +#include "editor/editor_node.h" +#include "editor/editor_undo_redo_manager.h" +#include "scene/gui/check_box.h" +#include "scene/gui/item_list.h" + +struct SortActionsByTimestamp { + bool operator()(const EditorUndoRedoManager::Action &l, const EditorUndoRedoManager::Action &r) const { + return l.timestamp > r.timestamp; + } +}; + +void HistoryDock::on_history_changed() { + if (is_visible_in_tree()) { + refresh_history(); + } else { + need_refresh = true; + } +} + +void HistoryDock::refresh_history() { + action_list->clear(); + bool include_scene = current_scene_checkbox->is_pressed(); + bool include_global = global_history_checkbox->is_pressed(); + + if (!include_scene && !include_global) { + action_list->add_item(TTR("The Beginning")); + return; + } + + const EditorUndoRedoManager::History ¤t_scene_history = ur_manager->get_or_create_history(EditorNode::get_editor_data().get_current_edited_scene_history_id()); + const EditorUndoRedoManager::History &global_history = ur_manager->get_or_create_history(EditorUndoRedoManager::GLOBAL_HISTORY); + + Vector<EditorUndoRedoManager::Action> full_history; + { + int full_size = 0; + if (include_scene) { + full_size += current_scene_history.redo_stack.size() + current_scene_history.undo_stack.size(); + } + if (include_global) { + full_size += global_history.redo_stack.size() + global_history.undo_stack.size(); + } + full_history.resize(full_size); + } + + int i = 0; + if (include_scene) { + for (const EditorUndoRedoManager::Action &E : current_scene_history.redo_stack) { + full_history.write[i] = E; + i++; + } + for (const EditorUndoRedoManager::Action &E : current_scene_history.undo_stack) { + full_history.write[i] = E; + i++; + } + } + if (include_global) { + for (const EditorUndoRedoManager::Action &E : global_history.redo_stack) { + full_history.write[i] = E; + i++; + } + for (const EditorUndoRedoManager::Action &E : global_history.undo_stack) { + full_history.write[i] = E; + i++; + } + } + + full_history.sort_custom<SortActionsByTimestamp>(); + for (const EditorUndoRedoManager::Action &E : full_history) { + action_list->add_item(E.action_name); + if (E.history_id == EditorUndoRedoManager::GLOBAL_HISTORY) { + action_list->set_item_custom_fg_color(-1, get_theme_color(SNAME("accent_color"), SNAME("Editor"))); + } + } + + action_list->add_item(TTR("The Beginning")); + refresh_version(); +} + +void HistoryDock::on_version_changed() { + if (is_visible_in_tree()) { + refresh_version(); + } else { + need_refresh = true; + } +} + +void HistoryDock::refresh_version() { + int idx = 0; + bool include_scene = current_scene_checkbox->is_pressed(); + bool include_global = global_history_checkbox->is_pressed(); + + if (!include_scene && !include_global) { + current_version = idx; + action_list->set_current(idx); + return; + } + + const EditorUndoRedoManager::History ¤t_scene_history = ur_manager->get_or_create_history(EditorNode::get_editor_data().get_current_edited_scene_history_id()); + const EditorUndoRedoManager::History &global_history = ur_manager->get_or_create_history(EditorUndoRedoManager::GLOBAL_HISTORY); + double newest_undo_timestamp = 0; + + if (include_scene && !current_scene_history.undo_stack.is_empty()) { + newest_undo_timestamp = current_scene_history.undo_stack.front()->get().timestamp; + } + + if (include_global && !global_history.undo_stack.is_empty()) { + double global_undo_timestamp = global_history.undo_stack.front()->get().timestamp; + if (global_undo_timestamp > newest_undo_timestamp) { + newest_undo_timestamp = global_undo_timestamp; + } + } + + if (include_scene) { + int skip = 0; + for (const EditorUndoRedoManager::Action &E : current_scene_history.redo_stack) { + if (E.timestamp < newest_undo_timestamp) { + skip++; + } else { + break; + } + } + idx += current_scene_history.redo_stack.size() - skip; + } + + if (include_global) { + int skip = 0; + for (const EditorUndoRedoManager::Action &E : global_history.redo_stack) { + if (E.timestamp < newest_undo_timestamp) { + skip++; + } else { + break; + } + } + idx += global_history.redo_stack.size() - skip; + } + + current_version = idx; + action_list->set_current(idx); +} + +void HistoryDock::seek_history(int p_index) { + bool include_scene = current_scene_checkbox->is_pressed(); + bool include_global = global_history_checkbox->is_pressed(); + + if (!include_scene && !include_global) { + return; + } + int current_scene_id = EditorNode::get_editor_data().get_current_edited_scene_history_id(); + + while (current_version < p_index) { + if (include_scene) { + if (include_global) { + ur_manager->undo(); + } else { + ur_manager->undo_history(current_scene_id); + } + } else { + ur_manager->undo_history(EditorUndoRedoManager::GLOBAL_HISTORY); + } + } + + while (current_version > p_index) { + if (include_scene) { + if (include_global) { + ur_manager->redo(); + } else { + ur_manager->redo_history(current_scene_id); + } + } else { + ur_manager->redo_history(EditorUndoRedoManager::GLOBAL_HISTORY); + } + } +} + +void HistoryDock::_notification(int p_notification) { + switch (p_notification) { + case NOTIFICATION_ENTER_TREE: { + EditorNode::get_singleton()->connect("scene_changed", callable_mp(this, &HistoryDock::on_history_changed)); + } break; + + case NOTIFICATION_VISIBILITY_CHANGED: { + if (is_visible_in_tree() && need_refresh) { + refresh_history(); + } + } break; + } +} + +HistoryDock::HistoryDock() { + ur_manager = EditorNode::get_undo_redo(); + ur_manager->connect("history_changed", callable_mp(this, &HistoryDock::on_history_changed)); + ur_manager->connect("version_changed", callable_mp(this, &HistoryDock::on_version_changed)); + + HBoxContainer *mode_hb = memnew(HBoxContainer); + add_child(mode_hb); + + current_scene_checkbox = memnew(CheckBox); + mode_hb->add_child(current_scene_checkbox); + current_scene_checkbox->set_flat(true); + current_scene_checkbox->set_pressed(true); + current_scene_checkbox->set_text(TTR("Scene")); + current_scene_checkbox->set_h_size_flags(SIZE_EXPAND_FILL); + current_scene_checkbox->set_clip_text(true); + current_scene_checkbox->connect("toggled", callable_mp(this, &HistoryDock::refresh_history).unbind(1)); + + global_history_checkbox = memnew(CheckBox); + mode_hb->add_child(global_history_checkbox); + global_history_checkbox->set_flat(true); + global_history_checkbox->set_pressed(true); + global_history_checkbox->set_text(TTR("Global")); + global_history_checkbox->set_h_size_flags(SIZE_EXPAND_FILL); + global_history_checkbox->set_clip_text(true); + global_history_checkbox->connect("toggled", callable_mp(this, &HistoryDock::refresh_history).unbind(1)); + + action_list = memnew(ItemList); + add_child(action_list); + action_list->set_v_size_flags(Control::SIZE_EXPAND_FILL); + action_list->connect("item_selected", callable_mp(this, &HistoryDock::seek_history)); +} diff --git a/editor/history_dock.h b/editor/history_dock.h new file mode 100644 index 0000000000..9dda8165e6 --- /dev/null +++ b/editor/history_dock.h @@ -0,0 +1,66 @@ +/*************************************************************************/ +/* history_dock.h */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/*************************************************************************/ + +#ifndef HISTORY_DOCK_H +#define HISTORY_DOCK_H + +#include "scene/gui/box_container.h" + +class CheckBox; +class ItemList; +class EditorUndoRedoManager; + +class HistoryDock : public VBoxContainer { + GDCLASS(HistoryDock, VBoxContainer); + + Ref<EditorUndoRedoManager> ur_manager; + ItemList *action_list = nullptr; + + CheckBox *current_scene_checkbox = nullptr; + CheckBox *global_history_checkbox = nullptr; + + bool need_refresh = true; + int current_version = 0; + + void on_history_changed(); + void refresh_history(); + void on_version_changed(); + void refresh_version(); + +protected: + void _notification(int p_notification); + +public: + void seek_history(int p_index); + + HistoryDock(); +}; + +#endif // HISTORY_DOCK_H |