Path: blob/master/editor/shader/text_shader_editor.cpp
20852 views
/**************************************************************************/1/* text_shader_editor.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 "text_shader_editor.h"3132#include "core/config/project_settings.h"33#include "core/version_generated.gen.h"34#include "editor/editor_node.h"35#include "editor/editor_string_names.h"36#include "editor/file_system/editor_file_system.h"37#include "editor/settings/editor_settings.h"38#include "editor/themes/editor_scale.h"39#include "editor/themes/editor_theme_manager.h"40#include "scene/gui/split_container.h"41#include "servers/rendering/shader_preprocessor.h"42#include "servers/rendering/shader_types.h"4344/*** SHADER SYNTAX HIGHLIGHTER ****/4546Dictionary GDShaderSyntaxHighlighter::_get_line_syntax_highlighting_impl(int p_line) {47Dictionary color_map;4849for (const Point2i ®ion : disabled_branch_regions) {50if (p_line >= region.x && p_line <= region.y) {51// When "color_regions[0].p_start_key.length() > 2",52// disabled_branch_region causes color_region to break.53// This should be seen as a temporary solution.54CodeHighlighter::_get_line_syntax_highlighting_impl(p_line);5556Dictionary highlighter_info;57highlighter_info["color"] = disabled_branch_color;5859color_map[0] = highlighter_info;60return color_map;61}62}6364return CodeHighlighter::_get_line_syntax_highlighting_impl(p_line);65}6667void GDShaderSyntaxHighlighter::add_disabled_branch_region(const Point2i &p_region) {68ERR_FAIL_COND(p_region.x < 0);69ERR_FAIL_COND(p_region.y < 0);7071for (int i = 0; i < disabled_branch_regions.size(); i++) {72ERR_FAIL_COND_MSG(disabled_branch_regions[i].x == p_region.x, "Branch region with a start line '" + itos(p_region.x) + "' already exists.");73}7475Point2i disabled_branch_region;76disabled_branch_region.x = p_region.x;77disabled_branch_region.y = p_region.y;78disabled_branch_regions.push_back(disabled_branch_region);7980clear_highlighting_cache();81}8283void GDShaderSyntaxHighlighter::clear_disabled_branch_regions() {84disabled_branch_regions.clear();85clear_highlighting_cache();86}8788void GDShaderSyntaxHighlighter::set_disabled_branch_color(const Color &p_color) {89disabled_branch_color = p_color;90clear_highlighting_cache();91}9293/*** SHADER SCRIPT EDITOR ****/9495static bool saved_warnings_enabled = false;96static bool saved_treat_warning_as_errors = false;97static HashMap<ShaderWarning::Code, bool> saved_warnings;98static uint32_t saved_warning_flags = 0U;99100void ShaderTextEditor::_notification(int p_what) {101switch (p_what) {102case NOTIFICATION_THEME_CHANGED: {103if (is_visible_in_tree()) {104_load_theme_settings();105if (warnings.size() > 0 && last_compile_result == OK) {106warnings_panel->clear();107_update_warning_panel();108}109}110} break;111}112}113114Ref<Shader> ShaderTextEditor::get_edited_shader() const {115return shader;116}117118Ref<ShaderInclude> ShaderTextEditor::get_edited_shader_include() const {119return shader_inc;120}121122void ShaderTextEditor::set_edited_shader(const Ref<Shader> &p_shader) {123set_edited_shader(p_shader, p_shader->get_code());124}125126void ShaderTextEditor::set_edited_shader(const Ref<Shader> &p_shader, const String &p_code) {127if (shader == p_shader) {128return;129}130if (shader.is_valid()) {131shader->disconnect_changed(callable_mp(this, &ShaderTextEditor::_shader_changed));132}133shader = p_shader;134shader_inc = Ref<ShaderInclude>();135136set_edited_code(p_code);137138if (shader.is_valid()) {139shader->connect_changed(callable_mp(this, &ShaderTextEditor::_shader_changed));140}141}142143void ShaderTextEditor::set_edited_shader_include(const Ref<ShaderInclude> &p_shader_inc) {144set_edited_shader_include(p_shader_inc, p_shader_inc->get_code());145}146147void ShaderTextEditor::_shader_changed() {148// This function is used for dependencies (include changing changes main shader and forces it to revalidate)149if (block_shader_changed) {150return;151}152dependencies_version++;153_validate_script();154}155156void ShaderTextEditor::set_edited_shader_include(const Ref<ShaderInclude> &p_shader_inc, const String &p_code) {157if (shader_inc == p_shader_inc) {158return;159}160if (shader_inc.is_valid()) {161shader_inc->disconnect_changed(callable_mp(this, &ShaderTextEditor::_shader_changed));162}163shader_inc = p_shader_inc;164shader = Ref<Shader>();165166set_edited_code(p_code);167168if (shader_inc.is_valid()) {169shader_inc->connect_changed(callable_mp(this, &ShaderTextEditor::_shader_changed));170}171}172173void ShaderTextEditor::set_edited_code(const String &p_code) {174_load_theme_settings();175176get_text_editor()->set_text(p_code);177get_text_editor()->clear_undo_history();178callable_mp((TextEdit *)get_text_editor(), &TextEdit::set_h_scroll).call_deferred(0);179callable_mp((TextEdit *)get_text_editor(), &TextEdit::set_v_scroll).call_deferred(0);180get_text_editor()->tag_saved_version();181182_validate_script();183_line_col_changed();184}185186void ShaderTextEditor::reload_text() {187ERR_FAIL_COND(shader.is_null() && shader_inc.is_null());188189String code;190if (shader.is_valid()) {191code = shader->get_code();192} else {193code = shader_inc->get_code();194}195196CodeEdit *te = get_text_editor();197int column = te->get_caret_column();198int row = te->get_caret_line();199int h = te->get_h_scroll();200int v = te->get_v_scroll();201202te->set_text(code);203te->set_caret_line(row);204te->set_caret_column(column);205te->set_h_scroll(h);206te->set_v_scroll(v);207208te->tag_saved_version();209210update_line_and_column();211}212213void ShaderTextEditor::set_warnings_panel(RichTextLabel *p_warnings_panel) {214warnings_panel = p_warnings_panel;215}216217void ShaderTextEditor::_load_theme_settings() {218CodeEdit *te = get_text_editor();219Color updated_marked_line_color = EDITOR_GET("text_editor/theme/highlighting/mark_color");220if (updated_marked_line_color != marked_line_color) {221for (int i = 0; i < te->get_line_count(); i++) {222if (te->get_line_background_color(i) == marked_line_color) {223te->set_line_background_color(i, updated_marked_line_color);224}225}226marked_line_color = updated_marked_line_color;227}228229syntax_highlighter->set_number_color(EDITOR_GET("text_editor/theme/highlighting/number_color"));230syntax_highlighter->set_symbol_color(EDITOR_GET("text_editor/theme/highlighting/symbol_color"));231syntax_highlighter->set_function_color(EDITOR_GET("text_editor/theme/highlighting/function_color"));232syntax_highlighter->set_member_variable_color(EDITOR_GET("text_editor/theme/highlighting/member_variable_color"));233234syntax_highlighter->clear_keyword_colors();235236const Color keyword_color = EDITOR_GET("text_editor/theme/highlighting/keyword_color");237const Color control_flow_keyword_color = EDITOR_GET("text_editor/theme/highlighting/control_flow_keyword_color");238239List<String> keywords;240ShaderLanguage::get_keyword_list(&keywords);241242for (const String &E : keywords) {243if (ShaderLanguage::is_control_flow_keyword(E)) {244syntax_highlighter->add_keyword_color(E, control_flow_keyword_color);245} else {246syntax_highlighter->add_keyword_color(E, keyword_color);247}248}249250List<String> pp_keywords;251ShaderPreprocessor::get_keyword_list(&pp_keywords, false);252253for (const String &E : pp_keywords) {254syntax_highlighter->add_keyword_color(E, control_flow_keyword_color);255}256257// Colorize built-ins like `COLOR` differently to make them easier258// to distinguish from keywords at a quick glance.259260List<String> built_ins;261262if (shader_inc.is_valid()) {263for (int i = 0; i < RenderingServer::SHADER_MAX; i++) {264for (const KeyValue<StringName, ShaderLanguage::FunctionInfo> &E : ShaderTypes::get_singleton()->get_functions(RenderingServer::ShaderMode(i))) {265for (const KeyValue<StringName, ShaderLanguage::BuiltInInfo> &F : E.value.built_ins) {266built_ins.push_back(F.key);267}268}269270{271const Vector<ShaderLanguage::ModeInfo> &render_modes = ShaderTypes::get_singleton()->get_modes(RenderingServer::ShaderMode(i));272273for (const ShaderLanguage::ModeInfo &mode_info : render_modes) {274if (!mode_info.options.is_empty()) {275for (const StringName &option : mode_info.options) {276built_ins.push_back(String(mode_info.name) + "_" + String(option));277}278} else {279built_ins.push_back(String(mode_info.name));280}281}282}283284{285const Vector<ShaderLanguage::ModeInfo> &stencil_modes = ShaderTypes::get_singleton()->get_stencil_modes(RenderingServer::ShaderMode(i));286287for (const ShaderLanguage::ModeInfo &mode_info : stencil_modes) {288if (!mode_info.options.is_empty()) {289for (const StringName &option : mode_info.options) {290built_ins.push_back(String(mode_info.name) + "_" + String(option));291}292} else {293built_ins.push_back(String(mode_info.name));294}295}296}297}298} else if (shader.is_valid()) {299for (const KeyValue<StringName, ShaderLanguage::FunctionInfo> &E : ShaderTypes::get_singleton()->get_functions(RenderingServer::ShaderMode(shader->get_mode()))) {300for (const KeyValue<StringName, ShaderLanguage::BuiltInInfo> &F : E.value.built_ins) {301built_ins.push_back(F.key);302}303}304305{306const Vector<ShaderLanguage::ModeInfo> &shader_modes = ShaderTypes::get_singleton()->get_modes(RenderingServer::ShaderMode(shader->get_mode()));307308for (const ShaderLanguage::ModeInfo &mode_info : shader_modes) {309if (!mode_info.options.is_empty()) {310for (const StringName &option : mode_info.options) {311built_ins.push_back(String(mode_info.name) + "_" + String(option));312}313} else {314built_ins.push_back(String(mode_info.name));315}316}317}318319{320const Vector<ShaderLanguage::ModeInfo> &stencil_modes = ShaderTypes::get_singleton()->get_stencil_modes(RenderingServer::ShaderMode(shader->get_mode()));321322for (const ShaderLanguage::ModeInfo &mode_info : stencil_modes) {323if (!mode_info.options.is_empty()) {324for (const StringName &option : mode_info.options) {325built_ins.push_back(String(mode_info.name) + "_" + String(option));326}327} else {328built_ins.push_back(String(mode_info.name));329}330}331}332}333334const Color user_type_color = EDITOR_GET("text_editor/theme/highlighting/user_type_color");335336for (const String &E : built_ins) {337syntax_highlighter->add_keyword_color(E, user_type_color);338}339340// Colorize comments.341const Color comment_color = EDITOR_GET("text_editor/theme/highlighting/comment_color");342syntax_highlighter->clear_color_regions();343syntax_highlighter->add_color_region("/*", "*/", comment_color, false);344syntax_highlighter->add_color_region("//", "", comment_color, true);345346const Color doc_comment_color = EDITOR_GET("text_editor/theme/highlighting/doc_comment_color");347syntax_highlighter->add_color_region("/**", "*/", doc_comment_color, false);348// "/**/" will be treated as the start of the "/**" region, this line is guaranteed to end the color_region.349syntax_highlighter->add_color_region("/**/", "", comment_color, true);350351// Disabled preprocessor branches use translucent text color to be easier to distinguish from comments.352syntax_highlighter->set_disabled_branch_color(Color(EDITOR_GET("text_editor/theme/highlighting/text_color")) * Color(1, 1, 1, 0.5));353354te->clear_comment_delimiters();355te->add_comment_delimiter("/*", "*/", false);356te->add_comment_delimiter("//", "", true);357358if (!te->has_auto_brace_completion_open_key("/*")) {359te->add_auto_brace_completion_pair("/*", "*/");360}361362// Colorize preprocessor include strings.363const Color string_color = EDITOR_GET("text_editor/theme/highlighting/string_color");364syntax_highlighter->add_color_region("\"", "\"", string_color, false);365syntax_highlighter->set_uint_suffix_enabled(true);366}367368void ShaderTextEditor::_check_shader_mode() {369String type = ShaderLanguage::get_shader_type(get_text_editor()->get_text());370371Shader::Mode mode;372373if (type == "canvas_item") {374mode = Shader::MODE_CANVAS_ITEM;375} else if (type == "particles") {376mode = Shader::MODE_PARTICLES;377} else if (type == "sky") {378mode = Shader::MODE_SKY;379} else if (type == "fog") {380mode = Shader::MODE_FOG;381} else if (type == "texture_blit") {382mode = Shader::MODE_TEXTURE_BLIT;383} else {384mode = Shader::MODE_SPATIAL;385}386387if (shader->get_mode() != mode) {388set_block_shader_changed(true);389shader->set_code(get_text_editor()->get_text());390set_block_shader_changed(false);391_load_theme_settings();392}393}394395static ShaderLanguage::DataType _get_global_shader_uniform_type(const StringName &p_variable) {396RS::GlobalShaderParameterType gvt = RS::get_singleton()->global_shader_parameter_get_type(p_variable);397return (ShaderLanguage::DataType)RS::global_shader_uniform_type_get_shader_datatype(gvt);398}399400static String complete_from_path;401402static void _complete_include_paths_search(EditorFileSystemDirectory *p_efsd, List<ScriptLanguage::CodeCompletionOption> *r_options) {403if (!p_efsd) {404return;405}406for (int i = 0; i < p_efsd->get_file_count(); i++) {407if (p_efsd->get_file_type(i) == SNAME("ShaderInclude")) {408String path = p_efsd->get_file_path(i);409if (path.begins_with(complete_from_path)) {410path = path.replace_first(complete_from_path, "");411}412r_options->push_back(ScriptLanguage::CodeCompletionOption(path, ScriptLanguage::CODE_COMPLETION_KIND_FILE_PATH));413}414}415for (int j = 0; j < p_efsd->get_subdir_count(); j++) {416_complete_include_paths_search(p_efsd->get_subdir(j), r_options);417}418}419420static void _complete_include_paths(List<ScriptLanguage::CodeCompletionOption> *r_options) {421_complete_include_paths_search(EditorFileSystem::get_singleton()->get_filesystem(), r_options);422}423424void ShaderTextEditor::_code_complete_script(const String &p_code, List<ScriptLanguage::CodeCompletionOption> *r_options) {425List<ScriptLanguage::CodeCompletionOption> pp_options;426List<ScriptLanguage::CodeCompletionOption> pp_defines;427ShaderPreprocessor preprocessor;428String code;429String resource_path = (shader.is_valid() ? shader->get_path() : shader_inc->get_path());430complete_from_path = resource_path.get_base_dir();431if (!complete_from_path.ends_with("/")) {432complete_from_path += "/";433}434preprocessor.preprocess(p_code, resource_path, code, nullptr, nullptr, nullptr, nullptr, &pp_options, &pp_defines, _complete_include_paths);435complete_from_path = String();436if (pp_options.size()) {437for (const ScriptLanguage::CodeCompletionOption &E : pp_options) {438r_options->push_back(E);439}440return;441}442for (const ScriptLanguage::CodeCompletionOption &E : pp_defines) {443r_options->push_back(E);444}445446ShaderLanguage sl;447String calltip;448ShaderLanguage::ShaderCompileInfo comp_info;449comp_info.global_shader_uniform_type_func = _get_global_shader_uniform_type;450451if (shader.is_null()) {452comp_info.is_include = true;453454sl.complete(code, comp_info, r_options, calltip);455get_text_editor()->set_code_hint(calltip);456return;457}458_check_shader_mode();459comp_info.functions = ShaderTypes::get_singleton()->get_functions(RenderingServer::ShaderMode(shader->get_mode()));460comp_info.render_modes = ShaderTypes::get_singleton()->get_modes(RenderingServer::ShaderMode(shader->get_mode()));461comp_info.stencil_modes = ShaderTypes::get_singleton()->get_stencil_modes(RenderingServer::ShaderMode(shader->get_mode()));462comp_info.shader_types = ShaderTypes::get_singleton()->get_types();463464sl.complete(code, comp_info, r_options, calltip);465get_text_editor()->set_code_hint(calltip);466}467468void ShaderTextEditor::_validate_script() {469emit_signal(CoreStringName(script_changed)); // Ensure to notify that it changed, so it is applied470471String code;472473if (shader.is_valid()) {474_check_shader_mode();475code = shader->get_code();476} else {477code = shader_inc->get_code();478}479480ShaderPreprocessor preprocessor;481String code_pp;482String error_pp;483List<ShaderPreprocessor::FilePosition> err_positions;484List<ShaderPreprocessor::Region> regions;485String filename;486if (shader.is_valid()) {487filename = shader->get_path();488} else if (shader_inc.is_valid()) {489filename = shader_inc->get_path();490}491last_compile_result = preprocessor.preprocess(code, filename, code_pp, &error_pp, &err_positions, ®ions);492493for (int i = 0; i < get_text_editor()->get_line_count(); i++) {494get_text_editor()->set_line_background_color(i, Color(0, 0, 0, 0));495}496497syntax_highlighter->clear_disabled_branch_regions();498for (const ShaderPreprocessor::Region ®ion : regions) {499if (!region.enabled) {500if (filename != region.file) {501continue;502}503syntax_highlighter->add_disabled_branch_region(Point2i(region.from_line, region.to_line));504}505}506507set_error("");508set_error_count(0);509510if (last_compile_result != OK) {511// Preprocessor error.512ERR_FAIL_COND(err_positions.is_empty());513514String err_text;515const int err_line = err_positions.front()->get().line;516if (err_positions.size() == 1) {517// Error in the main file.518const String message = error_pp.replace("[", "[lb]");519520err_text = vformat(TTR("Error at line %d:"), err_line) + " " + message;521} else {522// Error in an included file.523const String inc_file = err_positions.back()->get().file.get_file();524const int inc_line = err_positions.back()->get().line;525const String message = error_pp.replace("[", "[lb]");526527err_text = vformat(TTR("Error at line %d in include %s:%d:"), err_line, inc_file, inc_line) + " " + message;528set_error_count(err_positions.size() - 1);529}530531set_error(err_text);532set_error_pos(err_line - 1, 0);533534for (int i = 0; i < get_text_editor()->get_line_count(); i++) {535get_text_editor()->set_line_background_color(i, Color(0, 0, 0, 0));536}537get_text_editor()->set_line_background_color(err_line - 1, marked_line_color);538539set_warning_count(0);540} else {541ShaderLanguage sl;542543sl.enable_warning_checking(saved_warnings_enabled);544uint32_t flags = saved_warning_flags;545if (shader.is_null()) {546if (flags & ShaderWarning::UNUSED_CONSTANT) {547flags &= ~(ShaderWarning::UNUSED_CONSTANT);548}549if (flags & ShaderWarning::UNUSED_FUNCTION) {550flags &= ~(ShaderWarning::UNUSED_FUNCTION);551}552if (flags & ShaderWarning::UNUSED_STRUCT) {553flags &= ~(ShaderWarning::UNUSED_STRUCT);554}555if (flags & ShaderWarning::UNUSED_UNIFORM) {556flags &= ~(ShaderWarning::UNUSED_UNIFORM);557}558if (flags & ShaderWarning::UNUSED_VARYING) {559flags &= ~(ShaderWarning::UNUSED_VARYING);560}561}562sl.set_warning_flags(flags);563564ShaderLanguage::ShaderCompileInfo comp_info;565comp_info.global_shader_uniform_type_func = _get_global_shader_uniform_type;566567if (shader.is_null()) {568comp_info.is_include = true;569} else {570Shader::Mode mode = shader->get_mode();571comp_info.functions = ShaderTypes::get_singleton()->get_functions(RenderingServer::ShaderMode(mode));572comp_info.render_modes = ShaderTypes::get_singleton()->get_modes(RenderingServer::ShaderMode(mode));573comp_info.stencil_modes = ShaderTypes::get_singleton()->get_stencil_modes(RenderingServer::ShaderMode(mode));574comp_info.shader_types = ShaderTypes::get_singleton()->get_types();575}576577code = code_pp;578//compiler error579last_compile_result = sl.compile(code, comp_info);580581if (last_compile_result != OK) {582Vector<ShaderLanguage::FilePosition> include_positions = sl.get_include_positions();583584String err_text;585int err_line;586if (include_positions.size() > 1) {587// Error in an included file.588err_line = include_positions[0].line;589590const String inc_file = include_positions[include_positions.size() - 1].file;591const int inc_line = include_positions[include_positions.size() - 1].line;592const String message = sl.get_error_text().replace("[", "[lb]");593594err_text = vformat(TTR("Error at line %d in include %s:%d:"), err_line, inc_file, inc_line) + " " + message;595set_error_count(include_positions.size() - 1);596} else {597// Error in the main file.598err_line = sl.get_error_line();599600const String message = sl.get_error_text().replace("[", "[lb]");601602err_text = vformat(TTR("Error at line %d:"), err_line) + " " + message;603set_error_count(0);604}605606set_error(err_text);607set_error_pos(err_line - 1, 0);608609get_text_editor()->set_line_background_color(err_line - 1, marked_line_color);610} else {611set_error("");612}613614if (warnings.size() > 0 || last_compile_result != OK) {615warnings_panel->clear();616}617warnings.clear();618for (List<ShaderWarning>::Element *E = sl.get_warnings_ptr(); E; E = E->next()) {619warnings.push_back(E->get());620}621if (warnings.size() > 0 && last_compile_result == OK) {622warnings.sort_custom<WarningsComparator>();623_update_warning_panel();624} else {625set_warning_count(0);626}627}628629emit_signal(SNAME("script_validated"), last_compile_result == OK); // Notify that validation finished, to update the list of scripts630}631632void ShaderTextEditor::_update_warning_panel() {633int warning_count = 0;634635warnings_panel->push_table(2);636for (const ShaderWarning &w : warnings) {637if (warning_count == 0) {638if (saved_treat_warning_as_errors) {639const String message = (w.get_message() + " " + TTR("Warnings should be fixed to prevent errors.")).replace("[", "[lb]");640const String error_text = vformat(TTR("Error at line %d:"), w.get_line()) + " " + message;641642set_error(error_text);643set_error_pos(w.get_line() - 1, 0);644645get_text_editor()->set_line_background_color(w.get_line() - 1, marked_line_color);646}647}648649warning_count++;650int line = w.get_line();651652// First cell.653warnings_panel->push_cell();654warnings_panel->push_color(warnings_panel->get_theme_color(SNAME("warning_color"), EditorStringName(Editor)));655if (line != -1) {656warnings_panel->push_meta(line - 1);657warnings_panel->add_text(vformat(TTR("Line %d (%s):"), line, w.get_name()));658warnings_panel->pop(); // Meta goto.659} else {660warnings_panel->add_text(w.get_name() + ":");661}662warnings_panel->pop(); // Color.663warnings_panel->pop(); // Cell.664665// Second cell.666warnings_panel->push_cell();667warnings_panel->add_text(w.get_message());668warnings_panel->pop(); // Cell.669}670warnings_panel->pop(); // Table.671672set_warning_count(warning_count);673}674675void ShaderTextEditor::_bind_methods() {676ADD_SIGNAL(MethodInfo("script_validated", PropertyInfo(Variant::BOOL, "valid")));677}678679ShaderTextEditor::ShaderTextEditor() {680syntax_highlighter.instantiate();681get_text_editor()->set_syntax_highlighter(syntax_highlighter);682}683684/*** SCRIPT EDITOR ******/685686void TextShaderEditor::_menu_option(int p_option) {687code_editor->get_text_editor()->apply_ime();688689switch (p_option) {690case EDIT_UNDO: {691code_editor->get_text_editor()->undo();692} break;693case EDIT_REDO: {694code_editor->get_text_editor()->redo();695} break;696case EDIT_CUT: {697code_editor->get_text_editor()->cut();698} break;699case EDIT_COPY: {700code_editor->get_text_editor()->copy();701} break;702case EDIT_PASTE: {703code_editor->get_text_editor()->paste();704} break;705case EDIT_SELECT_ALL: {706code_editor->get_text_editor()->select_all();707} break;708case EDIT_MOVE_LINE_UP: {709code_editor->get_text_editor()->move_lines_up();710} break;711case EDIT_MOVE_LINE_DOWN: {712code_editor->get_text_editor()->move_lines_down();713} break;714case EDIT_INDENT: {715if (shader.is_null() && shader_inc.is_null()) {716return;717}718code_editor->get_text_editor()->indent_lines();719} break;720case EDIT_UNINDENT: {721if (shader.is_null() && shader_inc.is_null()) {722return;723}724code_editor->get_text_editor()->unindent_lines();725} break;726case EDIT_DELETE_LINE: {727code_editor->get_text_editor()->delete_lines();728} break;729case EDIT_DUPLICATE_SELECTION: {730code_editor->get_text_editor()->duplicate_selection();731} break;732case EDIT_DUPLICATE_LINES: {733code_editor->get_text_editor()->duplicate_lines();734} break;735case EDIT_TOGGLE_WORD_WRAP: {736TextEdit::LineWrappingMode wrap = code_editor->get_text_editor()->get_line_wrapping_mode();737code_editor->get_text_editor()->set_line_wrapping_mode(wrap == TextEdit::LINE_WRAPPING_BOUNDARY ? TextEdit::LINE_WRAPPING_NONE : TextEdit::LINE_WRAPPING_BOUNDARY);738} break;739case EDIT_TOGGLE_COMMENT: {740if (shader.is_null() && shader_inc.is_null()) {741return;742}743code_editor->toggle_inline_comment("//");744} break;745case EDIT_COMPLETE: {746code_editor->get_text_editor()->request_code_completion();747} break;748case SEARCH_FIND: {749code_editor->get_find_replace_bar()->popup_search();750} break;751case SEARCH_FIND_NEXT: {752code_editor->get_find_replace_bar()->search_next();753} break;754case SEARCH_FIND_PREV: {755code_editor->get_find_replace_bar()->search_prev();756} break;757case SEARCH_REPLACE: {758code_editor->get_find_replace_bar()->popup_replace();759} break;760case SEARCH_GOTO_LINE: {761goto_line_popup->popup_find_line(code_editor);762} break;763case BOOKMARK_TOGGLE: {764code_editor->toggle_bookmark();765} break;766case BOOKMARK_GOTO_NEXT: {767code_editor->goto_next_bookmark();768} break;769case BOOKMARK_GOTO_PREV: {770code_editor->goto_prev_bookmark();771} break;772case BOOKMARK_REMOVE_ALL: {773code_editor->remove_all_bookmarks();774} break;775case HELP_DOCS: {776OS::get_singleton()->shell_open(vformat("%s/tutorials/shaders/shader_reference/index.html", GODOT_VERSION_DOCS_URL));777} break;778case EDIT_EMOJI_AND_SYMBOL: {779code_editor->get_text_editor()->show_emoji_and_symbol_picker();780} break;781}782if (p_option != SEARCH_FIND && p_option != SEARCH_REPLACE && p_option != SEARCH_GOTO_LINE) {783callable_mp((Control *)code_editor->get_text_editor(), &Control::grab_focus).call_deferred(false);784}785}786787void TextShaderEditor::_prepare_edit_menu() {788const CodeEdit *tx = code_editor->get_text_editor();789PopupMenu *popup = edit_menu->get_popup();790popup->set_item_disabled(popup->get_item_index(EDIT_UNDO), !tx->has_undo());791popup->set_item_disabled(popup->get_item_index(EDIT_REDO), !tx->has_redo());792}793794void TextShaderEditor::_notification(int p_what) {795switch (p_what) {796case EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED: {797if (EditorThemeManager::is_generated_theme_outdated() ||798EditorSettings::get_singleton()->check_changed_settings_in_group("interface/editor") ||799EditorSettings::get_singleton()->check_changed_settings_in_group("text_editor")) {800_apply_editor_settings();801}802} break;803804case NOTIFICATION_THEME_CHANGED: {805site_search->set_button_icon(get_editor_theme_icon(SNAME("ExternalLink")));806} break;807808case NOTIFICATION_APPLICATION_FOCUS_IN: {809_check_for_external_edit();810} break;811}812}813814void TextShaderEditor::_apply_editor_settings() {815code_editor->update_editor_settings();816817trim_trailing_whitespace_on_save = EDITOR_GET("text_editor/behavior/files/trim_trailing_whitespace_on_save");818trim_final_newlines_on_save = EDITOR_GET("text_editor/behavior/files/trim_final_newlines_on_save");819}820821void TextShaderEditor::_show_warnings_panel(bool p_show) {822warnings_panel->set_visible(p_show);823}824825void TextShaderEditor::_warning_clicked(const Variant &p_line) {826if (p_line.get_type() == Variant::INT) {827code_editor->goto_line_centered(p_line.operator int64_t());828}829}830831void TextShaderEditor::_bind_methods() {832ClassDB::bind_method("_show_warnings_panel", &TextShaderEditor::_show_warnings_panel);833ClassDB::bind_method("_warning_clicked", &TextShaderEditor::_warning_clicked);834835ADD_SIGNAL(MethodInfo("validation_changed"));836}837838void TextShaderEditor::ensure_select_current() {839}840841void TextShaderEditor::goto_line_selection(int p_line, int p_begin, int p_end) {842code_editor->goto_line_selection(p_line, p_begin, p_end);843}844845void TextShaderEditor::_project_settings_changed() {846_update_warnings(true);847}848849void TextShaderEditor::_update_warnings(bool p_validate) {850bool changed = false;851852bool warnings_enabled = GLOBAL_GET("debug/shader_language/warnings/enable").booleanize();853if (warnings_enabled != saved_warnings_enabled) {854saved_warnings_enabled = warnings_enabled;855changed = true;856}857858bool treat_warning_as_errors = GLOBAL_GET("debug/shader_language/warnings/treat_warnings_as_errors").booleanize();859if (treat_warning_as_errors != saved_treat_warning_as_errors) {860saved_treat_warning_as_errors = treat_warning_as_errors;861changed = true;862}863864bool update_flags = false;865866for (int i = 0; i < ShaderWarning::WARNING_MAX; i++) {867ShaderWarning::Code code = (ShaderWarning::Code)i;868bool value = GLOBAL_GET("debug/shader_language/warnings/" + ShaderWarning::get_name_from_code(code).to_lower());869870if (saved_warnings[code] != value) {871saved_warnings[code] = value;872update_flags = true;873changed = true;874}875}876877if (update_flags) {878saved_warning_flags = (uint32_t)ShaderWarning::get_flags_from_codemap(saved_warnings);879}880881if (p_validate && changed && code_editor && code_editor->get_edited_shader().is_valid()) {882code_editor->validate_script();883}884}885886void TextShaderEditor::_check_for_external_edit() {887bool use_autoreload = bool(EDITOR_GET("text_editor/behavior/files/auto_reload_scripts_on_external_change"));888889if (shader_inc.is_valid()) {890if (shader_inc->get_last_modified_time() != FileAccess::get_modified_time(shader_inc->get_path())) {891if (use_autoreload) {892_reload_shader_include_from_disk();893} else {894callable_mp((Window *)disk_changed, &Window::popup_centered).call_deferred(Size2i());895}896}897return;898}899900if (shader.is_null() || shader->is_built_in()) {901return;902}903904if (shader->get_last_modified_time() != FileAccess::get_modified_time(shader->get_path())) {905if (use_autoreload) {906_reload_shader_from_disk();907} else {908callable_mp((Window *)disk_changed, &Window::popup_centered).call_deferred(Size2i());909}910}911}912913void TextShaderEditor::_reload_shader_from_disk() {914Ref<Shader> rel_shader = ResourceLoader::load(shader->get_path(), shader->get_class(), ResourceFormatLoader::CACHE_MODE_IGNORE);915ERR_FAIL_COND(rel_shader.is_null());916917code_editor->set_block_shader_changed(true);918shader->set_code(rel_shader->get_code());919code_editor->set_block_shader_changed(false);920shader->set_last_modified_time(rel_shader->get_last_modified_time());921code_editor->reload_text();922}923924void TextShaderEditor::_reload_shader_include_from_disk() {925Ref<ShaderInclude> rel_shader_include = ResourceLoader::load(shader_inc->get_path(), shader_inc->get_class(), ResourceFormatLoader::CACHE_MODE_IGNORE);926ERR_FAIL_COND(rel_shader_include.is_null());927928code_editor->set_block_shader_changed(true);929shader_inc->set_code(rel_shader_include->get_code());930code_editor->set_block_shader_changed(false);931shader_inc->set_last_modified_time(rel_shader_include->get_last_modified_time());932code_editor->reload_text();933}934935void TextShaderEditor::_reload() {936if (shader.is_valid()) {937_reload_shader_from_disk();938} else if (shader_inc.is_valid()) {939_reload_shader_include_from_disk();940}941}942943void TextShaderEditor::edit_shader(const Ref<Shader> &p_shader) {944if (p_shader.is_null() || !p_shader->is_text_shader()) {945return;946}947948if (shader == p_shader) {949return;950}951952shader = p_shader;953shader_inc = Ref<ShaderInclude>();954955code_editor->set_edited_shader(shader);956}957958void TextShaderEditor::edit_shader_include(const Ref<ShaderInclude> &p_shader_inc) {959if (p_shader_inc.is_null()) {960return;961}962963if (shader_inc == p_shader_inc) {964return;965}966967shader_inc = p_shader_inc;968shader = Ref<Shader>();969970code_editor->set_edited_shader_include(p_shader_inc);971}972973void TextShaderEditor::use_menu_bar(MenuButton *p_file_menu) {974p_file_menu->set_switch_on_hover(true);975menu_bar_hbox->add_child(p_file_menu);976menu_bar_hbox->move_child(p_file_menu, 0);977}978979void TextShaderEditor::save_external_data(const String &p_str) {980if (shader.is_null() && shader_inc.is_null()) {981disk_changed->hide();982return;983}984985if (trim_trailing_whitespace_on_save) {986trim_trailing_whitespace();987}988989if (trim_final_newlines_on_save) {990trim_final_newlines();991}992993apply_shaders();994995Ref<Shader> edited_shader = code_editor->get_edited_shader();996if (edited_shader.is_valid()) {997ResourceSaver::save(edited_shader);998}999if (shader.is_valid() && shader != edited_shader) {1000ResourceSaver::save(shader);1001}10021003Ref<ShaderInclude> edited_shader_inc = code_editor->get_edited_shader_include();1004if (edited_shader_inc.is_valid()) {1005ResourceSaver::save(edited_shader_inc);1006}1007if (shader_inc.is_valid() && shader_inc != edited_shader_inc) {1008ResourceSaver::save(shader_inc);1009}1010code_editor->get_text_editor()->tag_saved_version();10111012disk_changed->hide();1013}10141015void TextShaderEditor::trim_trailing_whitespace() {1016code_editor->trim_trailing_whitespace();1017}10181019void TextShaderEditor::trim_final_newlines() {1020code_editor->trim_final_newlines();1021}10221023void TextShaderEditor::set_toggle_list_control(Control *p_toggle_list_control) {1024code_editor->set_toggle_list_control(p_toggle_list_control);1025}10261027void TextShaderEditor::update_toggle_files_button() {1028code_editor->update_toggle_files_button();1029}10301031void TextShaderEditor::validate_script() {1032code_editor->_validate_script();1033}10341035bool TextShaderEditor::is_unsaved() const {1036return code_editor->get_text_editor()->get_saved_version() != code_editor->get_text_editor()->get_version();1037}10381039void TextShaderEditor::tag_saved_version() {1040code_editor->get_text_editor()->tag_saved_version();1041}10421043void TextShaderEditor::apply_shaders() {1044String editor_code = code_editor->get_text_editor()->get_text();1045if (shader.is_valid()) {1046String shader_code = shader->get_code();1047if (shader_code != editor_code || dependencies_version != code_editor->get_dependencies_version()) {1048code_editor->set_block_shader_changed(true);1049shader->set_code(editor_code);1050code_editor->set_block_shader_changed(false);1051shader->set_edited(true);1052}1053}1054if (shader_inc.is_valid()) {1055String shader_inc_code = shader_inc->get_code();1056if (shader_inc_code != editor_code || dependencies_version != code_editor->get_dependencies_version()) {1057code_editor->set_block_shader_changed(true);1058shader_inc->set_code(editor_code);1059code_editor->set_block_shader_changed(false);1060shader_inc->set_edited(true);1061}1062}10631064dependencies_version = code_editor->get_dependencies_version();1065}10661067void TextShaderEditor::_text_edit_gui_input(const Ref<InputEvent> &ev) {1068Ref<InputEventMouseButton> mb = ev;10691070if (mb.is_valid()) {1071if (mb->get_button_index() == MouseButton::RIGHT && mb->is_pressed()) {1072CodeEdit *tx = code_editor->get_text_editor();10731074tx->apply_ime();10751076Point2i pos = tx->get_line_column_at_pos(mb->get_global_position() - tx->get_global_position());1077int row = pos.y;1078int col = pos.x;1079tx->set_move_caret_on_right_click_enabled(EDITOR_GET("text_editor/behavior/navigation/move_caret_on_right_click"));10801081if (tx->is_move_caret_on_right_click_enabled()) {1082tx->remove_secondary_carets();1083if (tx->has_selection()) {1084int from_line = tx->get_selection_from_line();1085int to_line = tx->get_selection_to_line();1086int from_column = tx->get_selection_from_column();1087int to_column = tx->get_selection_to_column();10881089if (row < from_line || row > to_line || (row == from_line && col < from_column) || (row == to_line && col > to_column)) {1090// Right click is outside the selected text1091tx->deselect();1092}1093}1094if (!tx->has_selection()) {1095tx->set_caret_line(row, true, false, -1);1096tx->set_caret_column(col);1097}1098}1099_make_context_menu(tx->has_selection(), get_local_mouse_position());1100}1101}11021103Ref<InputEventKey> k = ev;1104if (k.is_valid() && k->is_pressed() && k->is_action("ui_menu", true)) {1105CodeEdit *tx = code_editor->get_text_editor();1106tx->adjust_viewport_to_caret();1107_make_context_menu(tx->has_selection(), (get_global_transform().inverse() * tx->get_global_transform()).xform(tx->get_caret_draw_pos()));1108context_menu->grab_focus();1109}1110}11111112void TextShaderEditor::_update_bookmark_list() {1113bookmarks_menu->clear();11141115bookmarks_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/toggle_bookmark"), BOOKMARK_TOGGLE);1116bookmarks_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/remove_all_bookmarks"), BOOKMARK_REMOVE_ALL);1117bookmarks_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/goto_next_bookmark"), BOOKMARK_GOTO_NEXT);1118bookmarks_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/goto_previous_bookmark"), BOOKMARK_GOTO_PREV);11191120PackedInt32Array bookmark_list = code_editor->get_text_editor()->get_bookmarked_lines();1121if (bookmark_list.is_empty()) {1122return;1123}11241125bookmarks_menu->add_separator();11261127for (int i = 0; i < bookmark_list.size(); i++) {1128String line = code_editor->get_text_editor()->get_line(bookmark_list[i]).strip_edges();1129// Limit the size of the line if too big.1130if (line.length() > 50) {1131line = line.substr(0, 50);1132}11331134bookmarks_menu->add_item(String::num_int64(bookmark_list[i] + 1) + " - \"" + line + "\"");1135bookmarks_menu->set_item_metadata(-1, bookmark_list[i]);1136}1137}11381139void TextShaderEditor::_bookmark_item_pressed(int p_idx) {1140if (p_idx < 4) { // Any item before the separator.1141_menu_option(bookmarks_menu->get_item_id(p_idx));1142} else {1143code_editor->goto_line(bookmarks_menu->get_item_metadata(p_idx));1144}1145}11461147void TextShaderEditor::_make_context_menu(bool p_selection, Vector2 p_position) {1148context_menu->clear();1149if (DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_EMOJI_AND_SYMBOL_PICKER)) {1150context_menu->add_item(TTR("Emoji & Symbols"), EDIT_EMOJI_AND_SYMBOL);1151context_menu->add_separator();1152}1153if (p_selection) {1154context_menu->add_shortcut(ED_GET_SHORTCUT("ui_cut"), EDIT_CUT);1155context_menu->add_shortcut(ED_GET_SHORTCUT("ui_copy"), EDIT_COPY);1156}11571158context_menu->add_shortcut(ED_GET_SHORTCUT("ui_paste"), EDIT_PASTE);1159context_menu->add_separator();1160context_menu->add_shortcut(ED_GET_SHORTCUT("ui_text_select_all"), EDIT_SELECT_ALL);1161context_menu->add_shortcut(ED_GET_SHORTCUT("ui_undo"), EDIT_UNDO);1162context_menu->add_shortcut(ED_GET_SHORTCUT("ui_redo"), EDIT_REDO);11631164context_menu->add_separator();1165context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/indent"), EDIT_INDENT);1166context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/unindent"), EDIT_UNINDENT);1167context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/toggle_comment"), EDIT_TOGGLE_COMMENT);1168context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/toggle_bookmark"), BOOKMARK_TOGGLE);11691170context_menu->set_item_disabled(context_menu->get_item_index(EDIT_UNDO), !code_editor->get_text_editor()->has_undo());1171context_menu->set_item_disabled(context_menu->get_item_index(EDIT_REDO), !code_editor->get_text_editor()->has_redo());11721173context_menu->set_position(get_screen_position() + p_position);1174context_menu->reset_size();1175context_menu->popup();1176}11771178TextShaderEditor::TextShaderEditor() {1179_update_warnings(false);11801181code_editor = memnew(ShaderTextEditor);11821183code_editor->connect("script_validated", callable_mp(this, &TextShaderEditor::_script_validated));11841185code_editor->set_v_size_flags(SIZE_EXPAND_FILL);1186code_editor->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT);11871188code_editor->connect("show_warnings_panel", callable_mp(this, &TextShaderEditor::_show_warnings_panel));1189code_editor->connect(CoreStringName(script_changed), callable_mp(this, &TextShaderEditor::apply_shaders));1190ProjectSettings::get_singleton()->connect("settings_changed", callable_mp(this, &TextShaderEditor::_project_settings_changed));11911192code_editor->get_text_editor()->set_symbol_lookup_on_click_enabled(true);1193code_editor->get_text_editor()->set_context_menu_enabled(false);1194code_editor->get_text_editor()->set_draw_breakpoints_gutter(false);1195code_editor->get_text_editor()->set_draw_executing_lines_gutter(false);1196code_editor->get_text_editor()->connect(SceneStringName(gui_input), callable_mp(this, &TextShaderEditor::_text_edit_gui_input));11971198code_editor->update_editor_settings();11991200context_menu = memnew(PopupMenu);1201add_child(context_menu);1202context_menu->connect(SceneStringName(id_pressed), callable_mp(this, &TextShaderEditor::_menu_option));12031204VBoxContainer *main_container = memnew(VBoxContainer);1205main_container->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT);1206menu_bar_hbox = memnew(HBoxContainer);12071208edit_menu = memnew(MenuButton);1209edit_menu->set_flat(false);1210edit_menu->set_theme_type_variation("FlatMenuButton");1211edit_menu->set_shortcut_context(this);1212edit_menu->set_text(TTR("Edit"));1213edit_menu->set_switch_on_hover(true);1214edit_menu->connect("about_to_popup", callable_mp(this, &TextShaderEditor::_prepare_edit_menu));12151216edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("ui_undo"), EDIT_UNDO);1217edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("ui_redo"), EDIT_REDO);1218edit_menu->get_popup()->add_separator();1219edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("ui_cut"), EDIT_CUT);1220edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("ui_copy"), EDIT_COPY);1221edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("ui_paste"), EDIT_PASTE);1222edit_menu->get_popup()->add_separator();1223edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("ui_text_select_all"), EDIT_SELECT_ALL);1224edit_menu->get_popup()->add_separator();1225edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/move_up"), EDIT_MOVE_LINE_UP);1226edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/move_down"), EDIT_MOVE_LINE_DOWN);1227edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/indent"), EDIT_INDENT);1228edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/unindent"), EDIT_UNINDENT);1229edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/delete_line"), EDIT_DELETE_LINE);1230edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/toggle_comment"), EDIT_TOGGLE_COMMENT);1231edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/duplicate_selection"), EDIT_DUPLICATE_SELECTION);1232edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/duplicate_lines"), EDIT_DUPLICATE_LINES);1233edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/toggle_word_wrap"), EDIT_TOGGLE_WORD_WRAP);1234edit_menu->get_popup()->add_separator();1235edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("ui_text_completion_query"), EDIT_COMPLETE);1236edit_menu->get_popup()->connect(SceneStringName(id_pressed), callable_mp(this, &TextShaderEditor::_menu_option));12371238search_menu = memnew(MenuButton);1239search_menu->set_flat(false);1240search_menu->set_theme_type_variation("FlatMenuButton");1241search_menu->set_shortcut_context(this);1242search_menu->set_text(TTR("Search"));1243search_menu->set_switch_on_hover(true);12441245search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/find"), SEARCH_FIND);1246search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/find_next"), SEARCH_FIND_NEXT);1247search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/find_previous"), SEARCH_FIND_PREV);1248search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/replace"), SEARCH_REPLACE);1249search_menu->get_popup()->connect(SceneStringName(id_pressed), callable_mp(this, &TextShaderEditor::_menu_option));12501251MenuButton *goto_menu = memnew(MenuButton);1252goto_menu->set_flat(false);1253goto_menu->set_theme_type_variation("FlatMenuButton");1254goto_menu->set_shortcut_context(this);1255goto_menu->set_text(TTR("Go To"));1256goto_menu->set_switch_on_hover(true);1257goto_menu->get_popup()->connect(SceneStringName(id_pressed), callable_mp(this, &TextShaderEditor::_menu_option));12581259goto_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/goto_line"), SEARCH_GOTO_LINE);1260goto_menu->get_popup()->add_separator();12611262bookmarks_menu = memnew(PopupMenu);1263goto_menu->get_popup()->add_submenu_node_item(TTR("Bookmarks"), bookmarks_menu);1264_update_bookmark_list();1265bookmarks_menu->connect("about_to_popup", callable_mp(this, &TextShaderEditor::_update_bookmark_list));1266bookmarks_menu->connect("index_pressed", callable_mp(this, &TextShaderEditor::_bookmark_item_pressed));12671268add_child(main_container);1269main_container->add_child(menu_bar_hbox);1270menu_bar_hbox->add_child(edit_menu);1271menu_bar_hbox->add_child(search_menu);1272menu_bar_hbox->add_child(goto_menu);1273menu_bar_hbox->add_spacer();12741275site_search = memnew(Button);1276site_search->set_theme_type_variation(SceneStringName(FlatButton));1277site_search->connect(SceneStringName(pressed), callable_mp(this, &TextShaderEditor::_menu_option).bind(HELP_DOCS));1278site_search->set_text(TTR("Online Docs"));1279site_search->set_tooltip_text(TTR("Open Godot online documentation."));1280menu_bar_hbox->add_child(site_search);12811282menu_bar_hbox->add_theme_style_override(SceneStringName(panel), EditorNode::get_singleton()->get_editor_theme()->get_stylebox(SNAME("ScriptEditorPanel"), EditorStringName(EditorStyles)));12831284VSplitContainer *editor_box = memnew(VSplitContainer);1285main_container->add_child(editor_box);1286editor_box->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT);1287editor_box->set_v_size_flags(SIZE_EXPAND_FILL);1288editor_box->add_child(code_editor);12891290FindReplaceBar *bar = memnew(FindReplaceBar);1291main_container->add_child(bar);1292bar->hide();1293code_editor->set_find_replace_bar(bar);12941295warnings_panel = memnew(RichTextLabel);1296warnings_panel->set_custom_minimum_size(Size2(0, 100 * EDSCALE));1297warnings_panel->set_h_size_flags(SIZE_EXPAND_FILL);1298warnings_panel->set_meta_underline(true);1299warnings_panel->set_selection_enabled(true);1300warnings_panel->set_context_menu_enabled(true);1301warnings_panel->set_focus_mode(FOCUS_CLICK);1302warnings_panel->hide();1303warnings_panel->connect("meta_clicked", callable_mp(this, &TextShaderEditor::_warning_clicked));1304editor_box->add_child(warnings_panel);1305code_editor->set_warnings_panel(warnings_panel);13061307goto_line_popup = memnew(GotoLinePopup);1308add_child(goto_line_popup);13091310disk_changed = memnew(ConfirmationDialog);13111312VBoxContainer *vbc = memnew(VBoxContainer);1313disk_changed->add_child(vbc);13141315Label *dl = memnew(Label);1316dl->set_focus_mode(FOCUS_ACCESSIBILITY);1317dl->set_text(TTR("This shader has been modified on disk.\nWhat action should be taken?"));1318vbc->add_child(dl);13191320disk_changed->connect(SceneStringName(confirmed), callable_mp(this, &TextShaderEditor::_reload));1321disk_changed->set_ok_button_text(TTR("Reload"));13221323disk_changed->add_button(TTR("Resave"), !DisplayServer::get_singleton()->get_swap_cancel_ok(), "resave");1324disk_changed->connect("custom_action", callable_mp(this, &TextShaderEditor::save_external_data));13251326add_child(disk_changed);13271328_apply_editor_settings();1329code_editor->show_toggle_files_button(); // TODO: Disabled for now, because it doesn't work properly.1330}133113321333