Path: blob/master/editor/shader/shader_editor_plugin.cpp
9898 views
/**************************************************************************/1/* shader_editor_plugin.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "shader_editor_plugin.h"3132#include "editor/docks/filesystem_dock.h"33#include "editor/docks/inspector_dock.h"34#include "editor/editor_node.h"35#include "editor/editor_string_names.h"36#include "editor/editor_undo_redo_manager.h"37#include "editor/gui/editor_bottom_panel.h"38#include "editor/gui/window_wrapper.h"39#include "editor/settings/editor_command_palette.h"40#include "editor/shader/shader_create_dialog.h"41#include "editor/shader/text_shader_editor.h"42#include "editor/shader/visual_shader_editor_plugin.h"43#include "editor/themes/editor_scale.h"44#include "scene/gui/item_list.h"45#include "scene/gui/tab_container.h"46#include "scene/gui/texture_rect.h"4748Ref<Resource> ShaderEditorPlugin::_get_current_shader() {49int index = shader_tabs->get_current_tab();50ERR_FAIL_INDEX_V(index, shader_tabs->get_tab_count(), Ref<Resource>());51if (edited_shaders[index].shader.is_valid()) {52return edited_shaders[index].shader;53} else {54return edited_shaders[index].shader_inc;55}56}5758void ShaderEditorPlugin::_update_shader_list() {59shader_list->clear();60for (EditedShader &edited_shader : edited_shaders) {61Ref<Resource> shader = edited_shader.shader;62if (shader.is_null()) {63shader = edited_shader.shader_inc;64}6566String path = shader->get_path();67String text = path.get_file();68if (text.is_empty()) {69// This appears for newly created built-in shaders before saving the scene.70text = TTR("[unsaved]");71} else if (shader->is_built_in()) {72const String &shader_name = shader->get_name();73if (!shader_name.is_empty()) {74text = vformat("%s (%s)", shader_name, text.get_slice("::", 0));75}76}7778// When shader is deleted in filesystem dock, need this to correctly close shader editor.79edited_shader.path = path;8081bool unsaved = false;82if (edited_shader.shader_editor) {83unsaved = edited_shader.shader_editor->is_unsaved();84}85// TODO: Handle visual shaders too.8687if (unsaved) {88text += "(*)";89}9091String _class = shader->get_class();92if (!shader_list->has_theme_icon(_class, EditorStringName(EditorIcons))) {93_class = "TextFile";94}95Ref<Texture2D> icon = shader_list->get_editor_theme_icon(_class);9697shader_list->add_item(text, icon);98shader_list->set_item_tooltip(-1, path);99edited_shader.name = text;100}101102if (shader_tabs->get_tab_count()) {103shader_list->select(shader_tabs->get_current_tab());104}105106_set_file_specific_items_disabled(edited_shaders.is_empty());107108_update_shader_list_status();109}110111void ShaderEditorPlugin::_update_shader_list_status() {112for (int i = 0; i < shader_list->get_item_count(); i++) {113TextShaderEditor *se = Object::cast_to<TextShaderEditor>(shader_tabs->get_tab_control(i));114if (se) {115if (se->was_compilation_successful()) {116shader_list->set_item_tag_icon(i, Ref<Texture2D>());117} else {118shader_list->set_item_tag_icon(i, shader_list->get_editor_theme_icon(SNAME("Error")));119}120}121}122}123124void ShaderEditorPlugin::_move_shader_tab(int p_from, int p_to) {125if (p_from == p_to) {126return;127}128EditedShader es = edited_shaders[p_from];129edited_shaders.remove_at(p_from);130edited_shaders.insert(p_to, es);131shader_tabs->move_child(shader_tabs->get_tab_control(p_from), p_to);132_update_shader_list();133}134135void ShaderEditorPlugin::edit(Object *p_object) {136if (!p_object) {137return;138}139140EditedShader es;141142ShaderInclude *si = Object::cast_to<ShaderInclude>(p_object);143if (si != nullptr) {144for (uint32_t i = 0; i < edited_shaders.size(); i++) {145if (edited_shaders[i].shader_inc.ptr() == si) {146shader_tabs->set_current_tab(i);147shader_list->select(i);148_switch_to_editor(edited_shaders[i].shader_editor);149return;150}151}152es.shader_inc = Ref<ShaderInclude>(si);153TextShaderEditor *text_shader = memnew(TextShaderEditor);154text_shader->get_code_editor()->set_toggle_list_control(shader_list);155es.shader_editor = text_shader;156es.shader_editor->edit_shader_include(si);157shader_tabs->add_child(es.shader_editor);158} else {159Shader *s = Object::cast_to<Shader>(p_object);160for (uint32_t i = 0; i < edited_shaders.size(); i++) {161if (edited_shaders[i].shader.ptr() == s) {162shader_tabs->set_current_tab(i);163shader_list->select(i);164_switch_to_editor(edited_shaders[i].shader_editor);165return;166}167}168es.shader = Ref<Shader>(s);169Ref<VisualShader> vs = es.shader;170if (vs.is_valid()) {171VisualShaderEditor *vs_editor = memnew(VisualShaderEditor);172vs_editor->set_toggle_list_control(shader_list);173es.shader_editor = vs_editor;174} else {175TextShaderEditor *text_shader = memnew(TextShaderEditor);176text_shader->get_code_editor()->set_toggle_list_control(shader_list);177es.shader_editor = text_shader;178}179shader_tabs->add_child(es.shader_editor);180es.shader_editor->edit_shader(es.shader);181}182183TextShaderEditor *text_shader_editor = Object::cast_to<TextShaderEditor>(es.shader_editor);184if (text_shader_editor) {185text_shader_editor->connect("validation_changed", callable_mp(this, &ShaderEditorPlugin::_update_shader_list));186CodeTextEditor *cte = text_shader_editor->get_code_editor();187if (cte) {188cte->set_zoom_factor(text_shader_zoom_factor);189cte->connect("zoomed", callable_mp(this, &ShaderEditorPlugin::_set_text_shader_zoom_factor));190cte->connect(SceneStringName(visibility_changed), callable_mp(this, &ShaderEditorPlugin::_update_shader_editor_zoom_factor).bind(cte));191}192}193194shader_tabs->set_current_tab(shader_tabs->get_tab_count() - 1);195edited_shaders.push_back(es);196_update_shader_list();197_switch_to_editor(es.shader_editor);198}199200bool ShaderEditorPlugin::handles(Object *p_object) const {201return Object::cast_to<Shader>(p_object) != nullptr || Object::cast_to<ShaderInclude>(p_object) != nullptr;202}203204void ShaderEditorPlugin::make_visible(bool p_visible) {205if (p_visible) {206EditorNode::get_bottom_panel()->make_item_visible(window_wrapper);207}208}209210void ShaderEditorPlugin::selected_notify() {211}212213ShaderEditor *ShaderEditorPlugin::get_shader_editor(const Ref<Shader> &p_for_shader) {214for (EditedShader &edited_shader : edited_shaders) {215if (edited_shader.shader == p_for_shader) {216return edited_shader.shader_editor;217}218}219return nullptr;220}221222void ShaderEditorPlugin::set_window_layout(Ref<ConfigFile> p_layout) {223if (EDITOR_GET("interface/multi_window/restore_windows_on_load") && window_wrapper->is_window_available() && p_layout->has_section_key("ShaderEditor", "window_rect")) {224window_wrapper->restore_window_from_saved_position(225p_layout->get_value("ShaderEditor", "window_rect", Rect2i()),226p_layout->get_value("ShaderEditor", "window_screen", -1),227p_layout->get_value("ShaderEditor", "window_screen_rect", Rect2i()));228} else {229window_wrapper->set_window_enabled(false);230}231232if (!bool(EDITOR_GET("editors/shader_editor/behavior/files/restore_shaders_on_load"))) {233return;234}235if (!p_layout->has_section("ShaderEditor")) {236return;237}238if (!p_layout->has_section_key("ShaderEditor", "open_shaders") ||239!p_layout->has_section_key("ShaderEditor", "selected_shader")) {240return;241}242243Array shaders = p_layout->get_value("ShaderEditor", "open_shaders");244int selected_shader_idx = 0;245String selected_shader = p_layout->get_value("ShaderEditor", "selected_shader");246for (int i = 0; i < shaders.size(); i++) {247String path = shaders[i];248Ref<Resource> res = ResourceLoader::load(path);249if (res.is_valid()) {250edit(res.ptr());251}252if (selected_shader == path) {253selected_shader_idx = i;254}255}256257if (p_layout->has_section_key("ShaderEditor", "split_offset")) {258files_split->set_split_offset(p_layout->get_value("ShaderEditor", "split_offset"));259}260261_update_shader_list();262_shader_selected(selected_shader_idx, false);263264_set_text_shader_zoom_factor(p_layout->get_value("ShaderEditor", "text_shader_zoom_factor", 1.0f));265}266267void ShaderEditorPlugin::get_window_layout(Ref<ConfigFile> p_layout) {268if (window_wrapper->get_window_enabled()) {269p_layout->set_value("ShaderEditor", "window_rect", window_wrapper->get_window_rect());270int screen = window_wrapper->get_window_screen();271p_layout->set_value("ShaderEditor", "window_screen", screen);272p_layout->set_value("ShaderEditor", "window_screen_rect", DisplayServer::get_singleton()->screen_get_usable_rect(screen));273274} else {275if (p_layout->has_section_key("ShaderEditor", "window_rect")) {276p_layout->erase_section_key("ShaderEditor", "window_rect");277}278if (p_layout->has_section_key("ShaderEditor", "window_screen")) {279p_layout->erase_section_key("ShaderEditor", "window_screen");280}281if (p_layout->has_section_key("ShaderEditor", "window_screen_rect")) {282p_layout->erase_section_key("ShaderEditor", "window_screen_rect");283}284}285286Array shaders;287String selected_shader;288for (int i = 0; i < shader_tabs->get_tab_count(); i++) {289EditedShader edited_shader = edited_shaders[i];290if (edited_shader.shader_editor) {291String shader_path;292if (edited_shader.shader.is_valid()) {293shader_path = edited_shader.shader->get_path();294} else {295DEV_ASSERT(edited_shader.shader_inc.is_valid());296shader_path = edited_shader.shader_inc->get_path();297}298shaders.push_back(shader_path);299300ShaderEditor *shader_editor = Object::cast_to<ShaderEditor>(shader_tabs->get_current_tab_control());301302if (shader_editor && edited_shader.shader_editor == shader_editor) {303selected_shader = shader_path;304}305}306}307p_layout->set_value("ShaderEditor", "open_shaders", shaders);308p_layout->set_value("ShaderEditor", "split_offset", files_split->get_split_offset());309p_layout->set_value("ShaderEditor", "selected_shader", selected_shader);310p_layout->set_value("ShaderEditor", "text_shader_zoom_factor", text_shader_zoom_factor);311}312313String ShaderEditorPlugin::get_unsaved_status(const String &p_for_scene) const {314// TODO: This should also include visual shaders and shader includes, but save_external_data() doesn't seem to save them...315PackedStringArray unsaved_shaders;316for (uint32_t i = 0; i < edited_shaders.size(); i++) {317if (edited_shaders[i].shader_editor) {318if (edited_shaders[i].shader_editor->is_unsaved()) {319if (unsaved_shaders.is_empty()) {320unsaved_shaders.append(TTR("Save changes to the following shaders(s) before quitting?"));321}322unsaved_shaders.append(edited_shaders[i].name.trim_suffix("(*)"));323}324}325}326327if (!p_for_scene.is_empty()) {328PackedStringArray unsaved_built_in_shaders;329330const String scene_file = p_for_scene.get_file();331for (const String &E : unsaved_shaders) {332if (!E.is_resource_file() && E.contains(scene_file)) {333if (unsaved_built_in_shaders.is_empty()) {334unsaved_built_in_shaders.append(TTR("There are unsaved changes in the following built-in shaders(s):"));335}336unsaved_built_in_shaders.append(E);337}338}339340if (!unsaved_built_in_shaders.is_empty()) {341return String("\n").join(unsaved_built_in_shaders);342}343return String();344}345346return String("\n").join(unsaved_shaders);347}348349void ShaderEditorPlugin::save_external_data() {350for (EditedShader &edited_shader : edited_shaders) {351if (edited_shader.shader_editor && edited_shader.shader_editor->is_unsaved()) {352edited_shader.shader_editor->save_external_data();353}354}355_update_shader_list();356}357358void ShaderEditorPlugin::apply_changes() {359for (EditedShader &edited_shader : edited_shaders) {360if (edited_shader.shader_editor) {361edited_shader.shader_editor->apply_shaders();362}363}364}365366void ShaderEditorPlugin::_shader_selected(int p_index, bool p_push_item) {367if (p_index >= (int)edited_shaders.size()) {368return;369}370371if (edited_shaders[p_index].shader_editor) {372_switch_to_editor(edited_shaders[p_index].shader_editor);373edited_shaders[p_index].shader_editor->validate_script();374}375376shader_tabs->set_current_tab(p_index);377shader_list->select(p_index);378379if (p_push_item) {380// Avoid `Shader` being edited when editing `ShaderInclude` due to inspector refreshing.381if (edited_shaders[p_index].shader.is_valid()) {382EditorNode::get_singleton()->push_item_no_inspector(edited_shaders[p_index].shader.ptr());383} else {384EditorNode::get_singleton()->push_item_no_inspector(edited_shaders[p_index].shader_inc.ptr());385}386}387}388389void ShaderEditorPlugin::_shader_list_clicked(int p_item, Vector2 p_local_mouse_pos, MouseButton p_mouse_button_index) {390if (p_mouse_button_index == MouseButton::MIDDLE) {391_close_shader(p_item);392}393if (p_mouse_button_index == MouseButton::RIGHT) {394_make_script_list_context_menu();395}396}397398void ShaderEditorPlugin::_setup_popup_menu(PopupMenuType p_type, PopupMenu *p_menu) {399if (p_type == FILE) {400p_menu->add_shortcut(ED_SHORTCUT("shader_editor/new", TTRC("New Shader..."), KeyModifierMask::CMD_OR_CTRL | Key::N), FILE_MENU_NEW);401p_menu->add_shortcut(ED_SHORTCUT("shader_editor/new_include", TTRC("New Shader Include..."), KeyModifierMask::CMD_OR_CTRL | KeyModifierMask::SHIFT | Key::N), FILE_MENU_NEW_INCLUDE);402p_menu->add_separator();403p_menu->add_shortcut(ED_SHORTCUT("shader_editor/open", TTRC("Load Shader File..."), KeyModifierMask::CMD_OR_CTRL | Key::O), FILE_MENU_OPEN);404p_menu->add_shortcut(ED_SHORTCUT("shader_editor/open_include", TTRC("Load Shader Include File..."), KeyModifierMask::CMD_OR_CTRL | KeyModifierMask::SHIFT | Key::O), FILE_MENU_OPEN_INCLUDE);405}406407if (p_type == FILE || p_type == CONTEXT_VALID_ITEM) {408p_menu->add_shortcut(ED_SHORTCUT("shader_editor/save", TTRC("Save File"), KeyModifierMask::ALT | KeyModifierMask::CMD_OR_CTRL | Key::S), FILE_MENU_SAVE);409p_menu->add_shortcut(ED_SHORTCUT("shader_editor/save_as", TTRC("Save File As...")), FILE_MENU_SAVE_AS);410}411412if (p_type == FILE) {413p_menu->add_separator();414p_menu->add_item(TTR("Open File in Inspector"), FILE_MENU_INSPECT);415p_menu->add_item(TTR("Inspect Native Shader Code..."), FILE_MENU_INSPECT_NATIVE_SHADER_CODE);416p_menu->add_separator();417p_menu->add_shortcut(ED_SHORTCUT("shader_editor/close_file", TTRC("Close File"), KeyModifierMask::CMD_OR_CTRL | Key::W), FILE_MENU_CLOSE);418p_menu->add_separator();419p_menu->add_shortcut(ED_GET_SHORTCUT("script_editor/toggle_files_panel"), FILE_MENU_TOGGLE_FILES_PANEL);420} else {421p_menu->add_shortcut(ED_SHORTCUT("shader_editor/close_file", TTRC("Close File"), KeyModifierMask::CMD_OR_CTRL | Key::W), FILE_MENU_CLOSE);422p_menu->add_item(TTR("Close All"), FILE_MENU_CLOSE_ALL);423p_menu->add_item(TTR("Close Other Tabs"), FILE_MENU_CLOSE_OTHER_TABS);424if (p_type == CONTEXT_VALID_ITEM) {425p_menu->add_separator();426p_menu->add_item(TTR("Copy Script Path"), FILE_MENU_COPY_PATH);427p_menu->add_item(TTR("Show in FileSystem"), FILE_MENU_SHOW_IN_FILE_SYSTEM);428}429}430}431432void ShaderEditorPlugin::_make_script_list_context_menu() {433context_menu->clear();434435int selected = shader_tabs->get_current_tab();436if (selected < 0 || selected >= shader_tabs->get_tab_count()) {437return;438}439440Control *control = shader_tabs->get_tab_control(selected);441bool is_valid_editor_control = Object::cast_to<TextShaderEditor>(control) || Object::cast_to<VisualShaderEditor>(control);442443_setup_popup_menu(is_valid_editor_control ? CONTEXT_VALID_ITEM : CONTEXT, context_menu);444445context_menu->set_item_disabled(context_menu->get_item_index(FILE_MENU_CLOSE_ALL), shader_tabs->get_tab_count() <= 0);446context_menu->set_item_disabled(context_menu->get_item_index(FILE_MENU_CLOSE_OTHER_TABS), shader_tabs->get_tab_count() <= 1);447448context_menu->set_position(files_split->get_screen_position() + files_split->get_local_mouse_position());449context_menu->reset_size();450context_menu->popup();451}452453void ShaderEditorPlugin::_close_shader(int p_index) {454ERR_FAIL_INDEX(p_index, shader_tabs->get_tab_count());455if (file_menu->get_parent() != nullptr) {456file_menu->get_parent()->remove_child(file_menu);457}458if (make_floating->get_parent()) {459make_floating->get_parent()->remove_child(make_floating);460}461ShaderEditor *shader_editor = Object::cast_to<ShaderEditor>(shader_tabs->get_tab_control(p_index));462ERR_FAIL_NULL(shader_editor);463464memdelete(shader_editor);465edited_shaders.remove_at(p_index);466_update_shader_list();467EditorUndoRedoManager::get_singleton()->clear_history(); // To prevent undo on deleted graphs.468469if (shader_tabs->get_tab_count() == 0) {470shader_list->show(); // Make sure the panel is visible, because it can't be toggled without open shaders.471} else {472_switch_to_editor(edited_shaders[shader_tabs->get_current_tab()].shader_editor);473}474}475476void ShaderEditorPlugin::_close_builtin_shaders_from_scene(const String &p_scene) {477for (uint32_t i = 0; i < edited_shaders.size();) {478Ref<Shader> &shader = edited_shaders[i].shader;479if (shader.is_valid()) {480if (shader->is_built_in() && shader->get_path().begins_with(p_scene)) {481_close_shader(i);482continue;483}484}485Ref<ShaderInclude> &include = edited_shaders[i].shader_inc;486if (include.is_valid()) {487if (include->is_built_in() && include->get_path().begins_with(p_scene)) {488_close_shader(i);489continue;490}491}492i++;493}494}495496void ShaderEditorPlugin::_resource_saved(Object *obj) {497// May have been renamed on save.498for (EditedShader &edited_shader : edited_shaders) {499if (edited_shader.shader.ptr() == obj || edited_shader.shader_inc.ptr() == obj) {500_update_shader_list();501return;502}503}504}505506void ShaderEditorPlugin::_menu_item_pressed(int p_index) {507switch (p_index) {508case FILE_MENU_NEW: {509String base_path = FileSystemDock::get_singleton()->get_current_path().get_base_dir();510shader_create_dialog->config(base_path.path_join("new_shader"), false, false, 0);511shader_create_dialog->popup_centered();512} break;513case FILE_MENU_NEW_INCLUDE: {514String base_path = FileSystemDock::get_singleton()->get_current_path().get_base_dir();515shader_create_dialog->config(base_path.path_join("new_shader"), false, false, 2);516shader_create_dialog->popup_centered();517} break;518case FILE_MENU_OPEN: {519InspectorDock::get_singleton()->open_resource("Shader");520} break;521case FILE_MENU_OPEN_INCLUDE: {522InspectorDock::get_singleton()->open_resource("ShaderInclude");523} break;524case FILE_MENU_SAVE: {525int index = shader_tabs->get_current_tab();526ERR_FAIL_INDEX(index, shader_tabs->get_tab_count());527TextShaderEditor *editor = Object::cast_to<TextShaderEditor>(edited_shaders[index].shader_editor);528if (editor) {529if (editor->get_trim_trailing_whitespace_on_save()) {530editor->trim_trailing_whitespace();531}532533if (editor->get_trim_final_newlines_on_save()) {534editor->trim_final_newlines();535}536}537if (edited_shaders[index].shader.is_valid()) {538EditorNode::get_singleton()->save_resource(edited_shaders[index].shader);539} else {540EditorNode::get_singleton()->save_resource(edited_shaders[index].shader_inc);541}542if (editor) {543editor->tag_saved_version();544}545} break;546case FILE_MENU_SAVE_AS: {547int index = shader_tabs->get_current_tab();548ERR_FAIL_INDEX(index, shader_tabs->get_tab_count());549TextShaderEditor *editor = Object::cast_to<TextShaderEditor>(edited_shaders[index].shader_editor);550if (editor) {551if (editor->get_trim_trailing_whitespace_on_save()) {552editor->trim_trailing_whitespace();553}554555if (editor->get_trim_final_newlines_on_save()) {556editor->trim_final_newlines();557}558}559String path;560if (edited_shaders[index].shader.is_valid()) {561path = edited_shaders[index].shader->get_path();562if (!path.is_resource_file()) {563path = "";564}565EditorNode::get_singleton()->save_resource_as(edited_shaders[index].shader, path);566} else {567path = edited_shaders[index].shader_inc->get_path();568if (!path.is_resource_file()) {569path = "";570}571EditorNode::get_singleton()->save_resource_as(edited_shaders[index].shader_inc, path);572}573if (editor) {574editor->tag_saved_version();575}576} break;577case FILE_MENU_INSPECT: {578int index = shader_tabs->get_current_tab();579ERR_FAIL_INDEX(index, shader_tabs->get_tab_count());580if (edited_shaders[index].shader.is_valid()) {581EditorNode::get_singleton()->push_item(edited_shaders[index].shader.ptr());582} else {583EditorNode::get_singleton()->push_item(edited_shaders[index].shader_inc.ptr());584}585} break;586case FILE_MENU_INSPECT_NATIVE_SHADER_CODE: {587int index = shader_tabs->get_current_tab();588if (edited_shaders[index].shader.is_valid()) {589edited_shaders[index].shader->inspect_native_shader_code();590}591} break;592case FILE_MENU_CLOSE: {593_close_shader(shader_tabs->get_current_tab());594} break;595case FILE_MENU_CLOSE_ALL: {596while (shader_tabs->get_tab_count() > 0) {597_close_shader(0);598}599} break;600case FILE_MENU_CLOSE_OTHER_TABS: {601int index = shader_tabs->get_current_tab();602for (int i = 0; i < index; i++) {603_close_shader(0);604}605while (shader_tabs->get_tab_count() > 1) {606_close_shader(1);607}608} break;609case FILE_MENU_SHOW_IN_FILE_SYSTEM: {610Ref<Resource> shader = _get_current_shader();611String path = shader->get_path();612if (!path.is_empty()) {613FileSystemDock::get_singleton()->navigate_to_path(path);614}615} break;616case FILE_MENU_COPY_PATH: {617Ref<Resource> shader = _get_current_shader();618DisplayServer::get_singleton()->clipboard_set(shader->get_path());619} break;620case FILE_MENU_TOGGLE_FILES_PANEL: {621shader_list->set_visible(!shader_list->is_visible());622623int index = shader_tabs->get_current_tab();624if (index != -1) {625ERR_FAIL_INDEX(index, (int)edited_shaders.size());626TextShaderEditor *editor = Object::cast_to<TextShaderEditor>(edited_shaders[index].shader_editor);627if (editor) {628editor->get_code_editor()->update_toggle_files_button();629} else {630VisualShaderEditor *vs_editor = Object::cast_to<VisualShaderEditor>(edited_shaders[index].shader_editor);631if (vs_editor) {632vs_editor->update_toggle_files_button();633}634}635}636} break;637}638}639640void ShaderEditorPlugin::_shader_created(Ref<Shader> p_shader) {641EditorNode::get_singleton()->push_item(p_shader.ptr());642}643644void ShaderEditorPlugin::_shader_include_created(Ref<ShaderInclude> p_shader_inc) {645EditorNode::get_singleton()->push_item(p_shader_inc.ptr());646}647648Variant ShaderEditorPlugin::get_drag_data_fw(const Point2 &p_point, Control *p_from) {649if (shader_list->get_item_count() == 0) {650return Variant();651}652653int idx = 0;654if (p_point == Vector2(Math::INF, Math::INF)) {655if (shader_list->is_anything_selected()) {656idx = shader_list->get_selected_items()[0];657}658} else {659idx = shader_list->get_item_at_position(p_point);660}661if (idx < 0) {662return Variant();663}664665HBoxContainer *drag_preview = memnew(HBoxContainer);666String preview_name = shader_list->get_item_text(idx);667Ref<Texture2D> preview_icon = shader_list->get_item_icon(idx);668669if (preview_icon.is_valid()) {670TextureRect *tf = memnew(TextureRect);671tf->set_texture(preview_icon);672tf->set_stretch_mode(TextureRect::STRETCH_KEEP_CENTERED);673drag_preview->add_child(tf);674}675Label *label = memnew(Label(preview_name));676label->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED); // Don't translate script names.677drag_preview->add_child(label);678files_split->set_drag_preview(drag_preview);679680Dictionary drag_data;681drag_data["type"] = "shader_list_element";682drag_data["shader_list_element"] = idx;683684return drag_data;685}686687bool ShaderEditorPlugin::can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const {688Dictionary d = p_data;689if (!d.has("type")) {690return false;691}692693if (String(d["type"]) == "shader_list_element") {694return true;695}696697if (String(d["type"]) == "files") {698Vector<String> files = d["files"];699700if (files.is_empty()) {701return false;702}703704for (int i = 0; i < files.size(); i++) {705const String &file = files[i];706if (ResourceLoader::exists(file, "Shader")) {707Ref<Shader> shader = ResourceLoader::load(file);708if (shader.is_valid()) {709return true;710}711}712if (ResourceLoader::exists(file, "ShaderInclude")) {713Ref<ShaderInclude> sinclude = ResourceLoader::load(file);714if (sinclude.is_valid()) {715return true;716}717}718}719return false;720}721722return false;723}724725void ShaderEditorPlugin::drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) {726if (!can_drop_data_fw(p_point, p_data, p_from)) {727return;728}729730Dictionary d = p_data;731if (!d.has("type")) {732return;733}734735if (String(d["type"]) == "shader_list_element") {736int idx = d["shader_list_element"];737int new_idx = 0;738if (p_point == Vector2(Math::INF, Math::INF)) {739if (shader_list->is_anything_selected()) {740new_idx = shader_list->get_selected_items()[0];741}742} else {743new_idx = shader_list->get_item_at_position(p_point);744}745_move_shader_tab(idx, new_idx);746return;747}748749if (String(d["type"]) == "files") {750Vector<String> files = d["files"];751752for (int i = 0; i < files.size(); i++) {753const String &file = files[i];754Ref<Resource> res;755if (ResourceLoader::exists(file, "Shader") || ResourceLoader::exists(file, "ShaderInclude")) {756res = ResourceLoader::load(file);757}758if (res.is_valid()) {759edit(res.ptr());760}761}762}763}764765void ShaderEditorPlugin::_window_changed(bool p_visible) {766make_floating->set_visible(!p_visible);767}768769void ShaderEditorPlugin::_set_text_shader_zoom_factor(float p_zoom_factor) {770if (text_shader_zoom_factor == p_zoom_factor) {771return;772}773774text_shader_zoom_factor = p_zoom_factor;775}776777void ShaderEditorPlugin::_update_shader_editor_zoom_factor(CodeTextEditor *p_shader_editor) const {778if (p_shader_editor && p_shader_editor->is_visible_in_tree() && text_shader_zoom_factor != p_shader_editor->get_zoom_factor()) {779p_shader_editor->set_zoom_factor(text_shader_zoom_factor);780}781}782783void ShaderEditorPlugin::_switch_to_editor(ShaderEditor *p_editor) {784if (file_menu->get_parent() != nullptr) {785file_menu->get_parent()->remove_child(file_menu);786}787if (make_floating->get_parent()) {788make_floating->get_parent()->remove_child(make_floating);789}790p_editor->use_menu_bar_items(file_menu, make_floating);791}792793void ShaderEditorPlugin::_file_removed(const String &p_removed_file) {794for (uint32_t i = 0; i < edited_shaders.size(); i++) {795if (edited_shaders[i].path == p_removed_file) {796_close_shader(i);797break;798}799}800}801802void ShaderEditorPlugin::_res_saved_callback(const Ref<Resource> &p_res) {803if (p_res.is_null()) {804return;805}806const String &path = p_res->get_path();807808for (EditedShader &edited : edited_shaders) {809Ref<Resource> shader_res = edited.shader;810if (shader_res.is_null()) {811shader_res = edited.shader_inc;812}813ERR_FAIL_COND(shader_res.is_null());814815TextShaderEditor *text_shader_editor = Object::cast_to<TextShaderEditor>(edited.shader_editor);816if (!text_shader_editor || !shader_res->is_built_in()) {817continue;818}819820if (shader_res->get_path().get_slice("::", 0) == path) {821text_shader_editor->tag_saved_version();822_update_shader_list();823}824}825}826827void ShaderEditorPlugin::_set_file_specific_items_disabled(bool p_disabled) {828PopupMenu *file_popup_menu = file_menu->get_popup();829file_popup_menu->set_item_disabled(file_popup_menu->get_item_index(FILE_MENU_SAVE), p_disabled);830file_popup_menu->set_item_disabled(file_popup_menu->get_item_index(FILE_MENU_SAVE_AS), p_disabled);831file_popup_menu->set_item_disabled(file_popup_menu->get_item_index(FILE_MENU_INSPECT), p_disabled);832file_popup_menu->set_item_disabled(file_popup_menu->get_item_index(FILE_MENU_INSPECT_NATIVE_SHADER_CODE), p_disabled);833file_popup_menu->set_item_disabled(file_popup_menu->get_item_index(FILE_MENU_CLOSE), p_disabled);834}835836void ShaderEditorPlugin::_notification(int p_what) {837switch (p_what) {838case NOTIFICATION_READY: {839EditorNode::get_singleton()->connect("resource_saved", callable_mp(this, &ShaderEditorPlugin::_resource_saved), CONNECT_DEFERRED);840EditorNode::get_singleton()->connect("scene_closed", callable_mp(this, &ShaderEditorPlugin::_close_builtin_shaders_from_scene));841FileSystemDock::get_singleton()->connect("file_removed", callable_mp(this, &ShaderEditorPlugin::_file_removed));842EditorNode::get_singleton()->connect("resource_saved", callable_mp(this, &ShaderEditorPlugin::_res_saved_callback));843} break;844}845}846847ShaderEditorPlugin::ShaderEditorPlugin() {848window_wrapper = memnew(WindowWrapper);849window_wrapper->set_window_title(vformat(TTR("%s - Godot Engine"), TTR("Shader Editor")));850window_wrapper->set_margins_enabled(true);851852main_container = memnew(VBoxContainer);853Ref<Shortcut> make_floating_shortcut = ED_SHORTCUT_AND_COMMAND("shader_editor/make_floating", TTRC("Make Floating"));854window_wrapper->set_wrapped_control(main_container, make_floating_shortcut);855856files_split = memnew(HSplitContainer);857files_split->set_split_offset(200 * EDSCALE);858files_split->set_v_size_flags(Control::SIZE_EXPAND_FILL);859860file_menu = memnew(MenuButton);861file_menu->set_flat(false);862file_menu->set_theme_type_variation("FlatMenuButton");863file_menu->set_text(TTR("File"));864file_menu->set_switch_on_hover(true);865file_menu->set_shortcut_context(files_split);866_setup_popup_menu(FILE, file_menu->get_popup());867file_menu->get_popup()->connect(SceneStringName(id_pressed), callable_mp(this, &ShaderEditorPlugin::_menu_item_pressed));868869_set_file_specific_items_disabled(true);870871context_menu = memnew(PopupMenu);872add_child(context_menu);873context_menu->connect(SceneStringName(id_pressed), callable_mp(this, &ShaderEditorPlugin::_menu_item_pressed));874875make_floating = memnew(ScreenSelect);876make_floating->connect("request_open_in_screen", callable_mp(window_wrapper, &WindowWrapper::enable_window_on_screen).bind(true));877if (!make_floating->is_disabled()) {878// Override default ScreenSelect tooltip if multi-window support is available.879make_floating->set_tooltip_text(TTR("Make the shader editor floating.") + "\n" + TTR("Right-click to open the screen selector."));880}881882window_wrapper->connect("window_visibility_changed", callable_mp(this, &ShaderEditorPlugin::_window_changed));883884shader_list = memnew(ItemList);885shader_list->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);886shader_list->set_v_size_flags(Control::SIZE_EXPAND_FILL);887shader_list->set_theme_type_variation("ItemListSecondary");888shader_list->set_custom_minimum_size(Size2(100, 60) * EDSCALE);889files_split->add_child(shader_list);890shader_list->connect(SceneStringName(item_selected), callable_mp(this, &ShaderEditorPlugin::_shader_selected).bind(true));891shader_list->connect("item_clicked", callable_mp(this, &ShaderEditorPlugin::_shader_list_clicked));892shader_list->set_allow_rmb_select(true);893SET_DRAG_FORWARDING_GCD(shader_list, ShaderEditorPlugin);894895main_container->add_child(files_split);896main_container->set_custom_minimum_size(Size2(100, 300) * EDSCALE);897898shader_tabs = memnew(TabContainer);899shader_tabs->set_custom_minimum_size(Size2(460, 300) * EDSCALE);900shader_tabs->set_tabs_visible(false);901shader_tabs->set_h_size_flags(Control::SIZE_EXPAND_FILL);902files_split->add_child(shader_tabs);903Ref<StyleBoxEmpty> empty;904empty.instantiate();905shader_tabs->add_theme_style_override(SceneStringName(panel), empty);906907button = EditorNode::get_bottom_panel()->add_item(TTRC("Shader Editor"), window_wrapper, ED_SHORTCUT_AND_COMMAND("bottom_panels/toggle_shader_editor_bottom_panel", TTRC("Toggle Shader Editor Bottom Panel"), KeyModifierMask::ALT | Key::S));908909shader_create_dialog = memnew(ShaderCreateDialog);910files_split->add_child(shader_create_dialog);911shader_create_dialog->connect("shader_created", callable_mp(this, &ShaderEditorPlugin::_shader_created));912shader_create_dialog->connect("shader_include_created", callable_mp(this, &ShaderEditorPlugin::_shader_include_created));913}914915ShaderEditorPlugin::~ShaderEditorPlugin() {916memdelete(file_menu);917memdelete(make_floating);918}919920921