diff options
Diffstat (limited to 'editor/plugins/shader_editor_plugin.cpp')
-rw-r--r-- | editor/plugins/shader_editor_plugin.cpp | 122 |
1 files changed, 117 insertions, 5 deletions
diff --git a/editor/plugins/shader_editor_plugin.cpp b/editor/plugins/shader_editor_plugin.cpp index 5cb014e5c7..e7d2d7a11f 100644 --- a/editor/plugins/shader_editor_plugin.cpp +++ b/editor/plugins/shader_editor_plugin.cpp @@ -30,6 +30,7 @@ #include "shader_editor_plugin.h" +#include "editor/editor_command_palette.h" #include "editor/editor_node.h" #include "editor/editor_scale.h" #include "editor/editor_undo_redo_manager.h" @@ -38,6 +39,7 @@ #include "editor/plugins/text_shader_editor.h" #include "editor/plugins/visual_shader_editor_plugin.h" #include "editor/shader_create_dialog.h" +#include "editor/window_wrapper.h" #include "scene/gui/item_list.h" #include "scene/gui/texture_rect.h" @@ -171,7 +173,7 @@ bool ShaderEditorPlugin::handles(Object *p_object) const { void ShaderEditorPlugin::make_visible(bool p_visible) { if (p_visible) { - EditorNode::get_singleton()->make_bottom_panel_item_visible(main_split); + EditorNode::get_singleton()->make_bottom_panel_item_visible(window_wrapper); } } @@ -196,6 +198,88 @@ VisualShaderEditor *ShaderEditorPlugin::get_visual_shader_editor(const Ref<Shade return nullptr; } +void ShaderEditorPlugin::set_window_layout(Ref<ConfigFile> p_layout) { + if (EDITOR_GET("interface/multi_window/restore_windows_on_load") && window_wrapper->is_window_available() && p_layout->has_section_key("ShaderEditor", "window_rect")) { + window_wrapper->restore_window_from_saved_position( + p_layout->get_value("ShaderEditor", "window_rect", Rect2i()), + p_layout->get_value("ShaderEditor", "window_screen", -1), + p_layout->get_value("ShaderEditor", "window_screen_rect", Rect2i())); + } else { + window_wrapper->set_window_enabled(false); + } + + if (!bool(EDITOR_GET("editors/shader_editor/behavior/files/restore_shaders_on_load"))) { + return; + } + if (!p_layout->has_section("ShaderEditor")) { + return; + } + if (!p_layout->has_section_key("ShaderEditor", "open_shaders") || + !p_layout->has_section_key("ShaderEditor", "selected_shader")) { + return; + } + + Array shaders = p_layout->get_value("ShaderEditor", "open_shaders"); + int selected_shader_idx = 0; + String selected_shader = p_layout->get_value("ShaderEditor", "selected_shader"); + for (int i = 0; i < shaders.size(); i++) { + String path = shaders[i]; + Ref<Resource> res = ResourceLoader::load(path); + if (res.is_valid()) { + edit(res.ptr()); + } + if (selected_shader == path) { + selected_shader_idx = i; + } + } + + if (p_layout->has_section_key("ShaderEditor", "split_offset")) { + main_split->set_split_offset(p_layout->get_value("ShaderEditor", "split_offset")); + } + + _update_shader_list(); + _shader_selected(selected_shader_idx); +} + +void ShaderEditorPlugin::get_window_layout(Ref<ConfigFile> p_layout) { + if (window_wrapper->get_window_enabled()) { + p_layout->set_value("ShaderEditor", "window_rect", window_wrapper->get_window_rect()); + int screen = window_wrapper->get_window_screen(); + p_layout->set_value("ShaderEditor", "window_screen", screen); + p_layout->set_value("ShaderEditor", "window_screen_rect", DisplayServer::get_singleton()->screen_get_usable_rect(screen)); + + } else { + if (p_layout->has_section_key("ShaderEditor", "window_rect")) { + p_layout->erase_section_key("ShaderEditor", "window_rect"); + } + if (p_layout->has_section_key("ShaderEditor", "window_screen")) { + p_layout->erase_section_key("ShaderEditor", "window_screen"); + } + if (p_layout->has_section_key("ShaderEditor", "window_screen_rect")) { + p_layout->erase_section_key("ShaderEditor", "window_screen_rect"); + } + } + + Array shaders; + String selected_shader; + for (int i = 0; i < shader_tabs->get_tab_count(); i++) { + EditedShader edited_shader = edited_shaders[i]; + if (edited_shader.shader_editor || edited_shader.visual_shader_editor) { + shaders.push_back(edited_shader.shader->get_path()); + + TextShaderEditor *shader_editor = Object::cast_to<TextShaderEditor>(shader_tabs->get_current_tab_control()); + VisualShaderEditor *visual_shader_editor = Object::cast_to<VisualShaderEditor>(shader_tabs->get_current_tab_control()); + + if ((shader_editor && edited_shader.shader_editor == shader_editor) || (visual_shader_editor && edited_shader.visual_shader_editor == visual_shader_editor)) { + selected_shader = edited_shader.shader->get_path(); + } + } + } + p_layout->set_value("ShaderEditor", "open_shaders", shaders); + p_layout->set_value("ShaderEditor", "split_offset", main_split->get_split_offset()); + p_layout->set_value("ShaderEditor", "selected_shader", selected_shader); +} + void ShaderEditorPlugin::save_external_data() { for (EditedShader &edited_shader : edited_shaders) { if (edited_shader.shader_editor) { @@ -214,6 +298,10 @@ void ShaderEditorPlugin::apply_changes() { } void ShaderEditorPlugin::_shader_selected(int p_index) { + if (p_index >= (int)edited_shaders.size()) { + return; + } + if (edited_shaders[p_index].shader_editor) { edited_shaders[p_index].shader_editor->validate_script(); } @@ -450,6 +538,10 @@ void ShaderEditorPlugin::drop_data_fw(const Point2 &p_point, const Variant &p_da } } +void ShaderEditorPlugin::_window_changed(bool p_visible) { + make_floating->set_visible(!p_visible); +} + void ShaderEditorPlugin::_notification(int p_what) { switch (p_what) { case NOTIFICATION_READY: { @@ -459,12 +551,18 @@ void ShaderEditorPlugin::_notification(int p_what) { } ShaderEditorPlugin::ShaderEditorPlugin() { + window_wrapper = memnew(WindowWrapper); + window_wrapper->set_window_title(TTR("Shader Editor - Godot Engine")); + window_wrapper->set_margins_enabled(true); + main_split = memnew(HSplitContainer); + Ref<Shortcut> make_floating_shortcut = ED_SHORTCUT_AND_COMMAND("shader_editor/make_floating", TTR("Make Floating")); + window_wrapper->set_wrapped_control(main_split, make_floating_shortcut); VBoxContainer *vb = memnew(VBoxContainer); - HBoxContainer *file_hb = memnew(HBoxContainer); - vb->add_child(file_hb); + HBoxContainer *menu_hb = memnew(HBoxContainer); + vb->add_child(menu_hb); file_menu = memnew(MenuButton); file_menu->set_text(TTR("File")); file_menu->get_popup()->add_item(TTR("New Shader"), FILE_NEW); @@ -479,12 +577,26 @@ ShaderEditorPlugin::ShaderEditorPlugin() { file_menu->get_popup()->add_separator(); file_menu->get_popup()->add_item(TTR("Close File"), FILE_CLOSE); file_menu->get_popup()->connect("id_pressed", callable_mp(this, &ShaderEditorPlugin::_menu_item_pressed)); - file_hb->add_child(file_menu); + menu_hb->add_child(file_menu); for (int i = FILE_SAVE; i < FILE_MAX; i++) { file_menu->get_popup()->set_item_disabled(file_menu->get_popup()->get_item_index(i), true); } + if (window_wrapper->is_window_available()) { + Control *padding = memnew(Control); + padding->set_h_size_flags(Control::SIZE_EXPAND_FILL); + menu_hb->add_child(padding); + + make_floating = memnew(ScreenSelect); + make_floating->set_flat(true); + make_floating->set_tooltip_text(TTR("Make the shader editor floating.")); + make_floating->connect("request_open_in_screen", callable_mp(window_wrapper, &WindowWrapper::enable_window_on_screen).bind(true)); + + menu_hb->add_child(make_floating); + window_wrapper->connect("window_visibility_changed", callable_mp(this, &ShaderEditorPlugin::_window_changed)); + } + shader_list = memnew(ItemList); shader_list->set_v_size_flags(Control::SIZE_EXPAND_FILL); vb->add_child(shader_list); @@ -503,7 +615,7 @@ ShaderEditorPlugin::ShaderEditorPlugin() { empty.instantiate(); shader_tabs->add_theme_style_override("panel", empty); - button = EditorNode::get_singleton()->add_bottom_panel_item(TTR("Shader Editor"), main_split); + button = EditorNode::get_singleton()->add_bottom_panel_item(TTR("Shader Editor"), window_wrapper); // Defer connect because Editor class is not in the binding system yet. EditorNode::get_singleton()->call_deferred("connect", "resource_saved", callable_mp(this, &ShaderEditorPlugin::_resource_saved), CONNECT_DEFERRED); |