| 1 | /**************************************************************************/ |
| 2 | /* text_shader_editor.cpp */ |
| 3 | /**************************************************************************/ |
| 4 | /* This file is part of: */ |
| 5 | /* GODOT ENGINE */ |
| 6 | /* https://godotengine.org */ |
| 7 | /**************************************************************************/ |
| 8 | /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ |
| 9 | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ |
| 10 | /* */ |
| 11 | /* Permission is hereby granted, free of charge, to any person obtaining */ |
| 12 | /* a copy of this software and associated documentation files (the */ |
| 13 | /* "Software"), to deal in the Software without restriction, including */ |
| 14 | /* without limitation the rights to use, copy, modify, merge, publish, */ |
| 15 | /* distribute, sublicense, and/or sell copies of the Software, and to */ |
| 16 | /* permit persons to whom the Software is furnished to do so, subject to */ |
| 17 | /* the following conditions: */ |
| 18 | /* */ |
| 19 | /* The above copyright notice and this permission notice shall be */ |
| 20 | /* included in all copies or substantial portions of the Software. */ |
| 21 | /* */ |
| 22 | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ |
| 23 | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ |
| 24 | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ |
| 25 | /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ |
| 26 | /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ |
| 27 | /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ |
| 28 | /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ |
| 29 | /**************************************************************************/ |
| 30 | |
| 31 | #include "text_shader_editor.h" |
| 32 | |
| 33 | #include "core/config/project_settings.h" |
| 34 | #include "core/version_generated.gen.h" |
| 35 | #include "editor/editor_node.h" |
| 36 | #include "editor/editor_string_names.h" |
| 37 | #include "editor/file_system/editor_file_system.h" |
| 38 | #include "editor/settings/editor_settings.h" |
| 39 | #include "editor/themes/editor_scale.h" |
| 40 | #include "editor/themes/editor_theme_manager.h" |
| 41 | #include "scene/gui/split_container.h" |
| 42 | #include "servers/rendering/shader_preprocessor.h" |
| 43 | #include "servers/rendering/shader_types.h" |
| 44 | |
| 45 | /*** SHADER SYNTAX HIGHLIGHTER ****/ |
| 46 | |
| 47 | Dictionary GDShaderSyntaxHighlighter::_get_line_syntax_highlighting_impl(int p_line) { |
| 48 | Dictionary color_map; |
| 49 | |
| 50 | for (const Point2i ®ion : disabled_branch_regions) { |
| 51 | if (p_line >= region.x && p_line <= region.y) { |
| 52 | // When "color_regions[0].p_start_key.length() > 2", |
| 53 | // disabled_branch_region causes color_region to break. |
| 54 | // This should be seen as a temporary solution. |
| 55 | CodeHighlighter::_get_line_syntax_highlighting_impl(p_line); |
| 56 | |
| 57 | Dictionary highlighter_info; |
| 58 | highlighter_info["color"] = disabled_branch_color; |
| 59 | |
| 60 | color_map[0] = highlighter_info; |
| 61 | return color_map; |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | return CodeHighlighter::_get_line_syntax_highlighting_impl(p_line); |
| 66 | } |
| 67 | |
| 68 | void GDShaderSyntaxHighlighter::add_disabled_branch_region(const Point2i &p_region) { |
| 69 | ERR_FAIL_COND(p_region.x < 0); |
| 70 | ERR_FAIL_COND(p_region.y < 0); |
| 71 | |
| 72 | for (int i = 0; i < disabled_branch_regions.size(); i++) { |
| 73 | ERR_FAIL_COND_MSG(disabled_branch_regions[i].x == p_region.x, "Branch region with a start line '" + itos(p_region.x) + "' already exists."); |
| 74 | } |
| 75 | |
| 76 | Point2i disabled_branch_region; |
| 77 | disabled_branch_region.x = p_region.x; |
| 78 | disabled_branch_region.y = p_region.y; |
| 79 | disabled_branch_regions.push_back(disabled_branch_region); |
| 80 | |
| 81 | clear_highlighting_cache(); |
| 82 | } |
| 83 | |
| 84 | void GDShaderSyntaxHighlighter::clear_disabled_branch_regions() { |
| 85 | disabled_branch_regions.clear(); |
| 86 | clear_highlighting_cache(); |
| 87 | } |
| 88 | |
| 89 | void GDShaderSyntaxHighlighter::set_disabled_branch_color(const Color &p_color) { |
| 90 | disabled_branch_color = p_color; |
| 91 | clear_highlighting_cache(); |
| 92 | } |
| 93 | |
| 94 | /*** SHADER SCRIPT EDITOR ****/ |
| 95 | |
| 96 | static bool saved_warnings_enabled = false; |
| 97 | static bool saved_treat_warning_as_errors = false; |
| 98 | static HashMap<ShaderWarning::Code, bool> saved_warnings; |
| 99 | static uint32_t saved_warning_flags = 0U; |
| 100 | |
| 101 | void ShaderTextEditor::_notification(int p_what) { |
| 102 | switch (p_what) { |
| 103 | case NOTIFICATION_THEME_CHANGED: { |
| 104 | if (is_visible_in_tree()) { |
| 105 | _load_theme_settings(); |
| 106 | if (warnings.size() > 0 && last_compile_result == OK) { |
| 107 | warnings_panel->clear(); |
| 108 | _update_warning_panel(); |
| 109 | } |
| 110 | } |
| 111 | } break; |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | Ref<Shader> ShaderTextEditor::get_edited_shader() const { |
| 116 | return shader; |
| 117 | } |
| 118 | |
| 119 | Ref<ShaderInclude> ShaderTextEditor::get_edited_shader_include() const { |
| 120 | return shader_inc; |
| 121 | } |
| 122 | |
| 123 | void ShaderTextEditor::set_edited_shader(const Ref<Shader> &p_shader) { |
| 124 | set_edited_shader(p_shader, p_shader->get_code()); |
| 125 | } |
| 126 | |
| 127 | void ShaderTextEditor::set_edited_shader(const Ref<Shader> &p_shader, const String &p_code) { |
| 128 | if (shader == p_shader) { |
| 129 | return; |
| 130 | } |
| 131 | if (shader.is_valid()) { |
| 132 | shader->disconnect_changed(callable_mp(this, &ShaderTextEditor::_shader_changed)); |
| 133 | } |
| 134 | shader = p_shader; |
| 135 | shader_inc = Ref<ShaderInclude>(); |
| 136 | |
| 137 | set_edited_code(p_code); |
| 138 | |
| 139 | if (shader.is_valid()) { |
| 140 | shader->connect_changed(callable_mp(this, &ShaderTextEditor::_shader_changed)); |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | void ShaderTextEditor::set_edited_shader_include(const Ref<ShaderInclude> &p_shader_inc) { |
| 145 | set_edited_shader_include(p_shader_inc, p_shader_inc->get_code()); |
| 146 | } |
| 147 | |
| 148 | void ShaderTextEditor::_shader_changed() { |
| 149 | // This function is used for dependencies (include changing changes main shader and forces it to revalidate) |
| 150 | if (block_shader_changed) { |
| 151 | return; |
| 152 | } |
| 153 | dependencies_version++; |
| 154 | _validate_script(); |
| 155 | } |
| 156 | |
| 157 | void ShaderTextEditor::set_edited_shader_include(const Ref<ShaderInclude> &p_shader_inc, const String &p_code) { |
| 158 | if (shader_inc == p_shader_inc) { |
| 159 | return; |
| 160 | } |
| 161 | if (shader_inc.is_valid()) { |
| 162 | shader_inc->disconnect_changed(callable_mp(this, &ShaderTextEditor::_shader_changed)); |
| 163 | } |
| 164 | shader_inc = p_shader_inc; |
| 165 | shader = Ref<Shader>(); |
| 166 | |
| 167 | set_edited_code(p_code); |
| 168 | |
| 169 | if (shader_inc.is_valid()) { |
| 170 | shader_inc->connect_changed(callable_mp(this, &ShaderTextEditor::_shader_changed)); |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | void ShaderTextEditor::set_edited_code(const String &p_code) { |
| 175 | _load_theme_settings(); |
| 176 | |
| 177 | get_text_editor()->set_text(p_code); |
| 178 | get_text_editor()->clear_undo_history(); |
| 179 | callable_mp((TextEdit *)get_text_editor(), &TextEdit::set_h_scroll).call_deferred(0); |
| 180 | callable_mp((TextEdit *)get_text_editor(), &TextEdit::set_v_scroll).call_deferred(0); |
| 181 | get_text_editor()->tag_saved_version(); |
| 182 | |
| 183 | _validate_script(); |
| 184 | _line_col_changed(); |
| 185 | } |
| 186 | |
| 187 | void ShaderTextEditor::reload_text() { |
| 188 | ERR_FAIL_COND(shader.is_null() && shader_inc.is_null()); |
| 189 | |
| 190 | String code; |
| 191 | if (shader.is_valid()) { |
| 192 | code = shader->get_code(); |
| 193 | } else { |
| 194 | code = shader_inc->get_code(); |
| 195 | } |
| 196 | |
| 197 | CodeEdit *te = get_text_editor(); |
| 198 | int column = te->get_caret_column(); |
| 199 | int row = te->get_caret_line(); |
| 200 | int h = te->get_h_scroll(); |
| 201 | int v = te->get_v_scroll(); |
| 202 | |
| 203 | te->set_text(code); |
| 204 | te->set_caret_line(row); |
| 205 | te->set_caret_column(column); |
| 206 | te->set_h_scroll(h); |
| 207 | te->set_v_scroll(v); |
| 208 | |
| 209 | te->tag_saved_version(); |
| 210 | |
| 211 | update_line_and_column(); |
| 212 | } |
| 213 | |
| 214 | void ShaderTextEditor::set_warnings_panel(RichTextLabel *p_warnings_panel) { |
| 215 | warnings_panel = p_warnings_panel; |
| 216 | } |
| 217 | |
| 218 | void ShaderTextEditor::_load_theme_settings() { |
| 219 | CodeEdit *te = get_text_editor(); |
| 220 | Color updated_marked_line_color = EDITOR_GET("text_editor/theme/highlighting/mark_color"); |
| 221 | if (updated_marked_line_color != marked_line_color) { |
| 222 | for (int i = 0; i < te->get_line_count(); i++) { |
| 223 | if (te->get_line_background_color(i) == marked_line_color) { |
| 224 | te->set_line_background_color(i, updated_marked_line_color); |
| 225 | } |
| 226 | } |
| 227 | marked_line_color = updated_marked_line_color; |
| 228 | } |
| 229 | |
| 230 | syntax_highlighter->set_number_color(EDITOR_GET("text_editor/theme/highlighting/number_color")); |
| 231 | syntax_highlighter->set_symbol_color(EDITOR_GET("text_editor/theme/highlighting/symbol_color")); |
| 232 | syntax_highlighter->set_function_color(EDITOR_GET("text_editor/theme/highlighting/function_color")); |
| 233 | syntax_highlighter->set_member_variable_color(EDITOR_GET("text_editor/theme/highlighting/member_variable_color")); |
| 234 | |
| 235 | syntax_highlighter->clear_keyword_colors(); |
| 236 | |
| 237 | const Color keyword_color = EDITOR_GET("text_editor/theme/highlighting/keyword_color"); |
| 238 | const Color control_flow_keyword_color = EDITOR_GET("text_editor/theme/highlighting/control_flow_keyword_color"); |
| 239 | |
| 240 | List<String> keywords; |
| 241 | ShaderLanguage::get_keyword_list(&keywords); |
| 242 | |
| 243 | for (const String &E : keywords) { |
| 244 | if (ShaderLanguage::is_control_flow_keyword(E)) { |
| 245 | syntax_highlighter->add_keyword_color(E, control_flow_keyword_color); |
| 246 | } else { |
| 247 | syntax_highlighter->add_keyword_color(E, keyword_color); |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | List<String> pp_keywords; |
| 252 | ShaderPreprocessor::get_keyword_list(&pp_keywords, false); |
| 253 | |
| 254 | for (const String &E : pp_keywords) { |
| 255 | syntax_highlighter->add_keyword_color(E, control_flow_keyword_color); |
| 256 | } |
| 257 | |
| 258 | // Colorize built-ins like `COLOR` differently to make them easier |
| 259 | // to distinguish from keywords at a quick glance. |
| 260 | |
| 261 | List<String> built_ins; |
| 262 | |
| 263 | if (shader_inc.is_valid()) { |
| 264 | for (int i = 0; i < RenderingServer::SHADER_MAX; i++) { |
| 265 | for (const KeyValue<StringName, ShaderLanguage::FunctionInfo> &E : ShaderTypes::get_singleton()->get_functions(RenderingServer::ShaderMode(i))) { |
| 266 | for (const KeyValue<StringName, ShaderLanguage::BuiltInInfo> &F : E.value.built_ins) { |
| 267 | built_ins.push_back(F.key); |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | { |
| 272 | const Vector<ShaderLanguage::ModeInfo> &render_modes = ShaderTypes::get_singleton()->get_modes(RenderingServer::ShaderMode(i)); |
| 273 | |
| 274 | for (const ShaderLanguage::ModeInfo &mode_info : render_modes) { |
| 275 | if (!mode_info.options.is_empty()) { |
| 276 | for (const StringName &option : mode_info.options) { |
| 277 | built_ins.push_back(String(mode_info.name) + "_" + String(option)); |
| 278 | } |
| 279 | } else { |
| 280 | built_ins.push_back(String(mode_info.name)); |
| 281 | } |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | { |
| 286 | const Vector<ShaderLanguage::ModeInfo> &stencil_modes = ShaderTypes::get_singleton()->get_stencil_modes(RenderingServer::ShaderMode(i)); |
| 287 | |
| 288 | for (const ShaderLanguage::ModeInfo &mode_info : stencil_modes) { |
| 289 | if (!mode_info.options.is_empty()) { |
| 290 | for (const StringName &option : mode_info.options) { |
| 291 | built_ins.push_back(String(mode_info.name) + "_" + String(option)); |
| 292 | } |
| 293 | } else { |
| 294 | built_ins.push_back(String(mode_info.name)); |
| 295 | } |
| 296 | } |
| 297 | } |
| 298 | } |
| 299 | } else if (shader.is_valid()) { |
| 300 | for (const KeyValue<StringName, ShaderLanguage::FunctionInfo> &E : ShaderTypes::get_singleton()->get_functions(RenderingServer::ShaderMode(shader->get_mode()))) { |
| 301 | for (const KeyValue<StringName, ShaderLanguage::BuiltInInfo> &F : E.value.built_ins) { |
| 302 | built_ins.push_back(F.key); |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | { |
| 307 | const Vector<ShaderLanguage::ModeInfo> &shader_modes = ShaderTypes::get_singleton()->get_modes(RenderingServer::ShaderMode(shader->get_mode())); |
| 308 | |
| 309 | for (const ShaderLanguage::ModeInfo &mode_info : shader_modes) { |
| 310 | if (!mode_info.options.is_empty()) { |
| 311 | for (const StringName &option : mode_info.options) { |
| 312 | built_ins.push_back(String(mode_info.name) + "_" + String(option)); |
| 313 | } |
| 314 | } else { |
| 315 | built_ins.push_back(String(mode_info.name)); |
| 316 | } |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | { |
| 321 | const Vector<ShaderLanguage::ModeInfo> &stencil_modes = ShaderTypes::get_singleton()->get_stencil_modes(RenderingServer::ShaderMode(shader->get_mode())); |
| 322 | |
| 323 | for (const ShaderLanguage::ModeInfo &mode_info : stencil_modes) { |
| 324 | if (!mode_info.options.is_empty()) { |
| 325 | for (const StringName &option : mode_info.options) { |
| 326 | built_ins.push_back(String(mode_info.name) + "_" + String(option)); |
| 327 | } |
| 328 | } else { |
| 329 | built_ins.push_back(String(mode_info.name)); |
| 330 | } |
| 331 | } |
| 332 | } |
| 333 | } |
| 334 | |
| 335 | const Color user_type_color = EDITOR_GET("text_editor/theme/highlighting/user_type_color"); |
| 336 | |
| 337 | for (const String &E : built_ins) { |
| 338 | syntax_highlighter->add_keyword_color(E, user_type_color); |
| 339 | } |
| 340 | |
| 341 | // Colorize comments. |
| 342 | const Color comment_color = EDITOR_GET("text_editor/theme/highlighting/comment_color"); |
| 343 | syntax_highlighter->clear_color_regions(); |
| 344 | syntax_highlighter->add_color_region("/*", "*/", comment_color, false); |
| 345 | syntax_highlighter->add_color_region("//", "", comment_color, true); |
| 346 | |
| 347 | const Color doc_comment_color = EDITOR_GET("text_editor/theme/highlighting/doc_comment_color"); |
| 348 | syntax_highlighter->add_color_region("/**", "*/", doc_comment_color, false); |
| 349 | // "/**/" will be treated as the start of the "/**" region, this line is guaranteed to end the color_region. |
| 350 | syntax_highlighter->add_color_region("/**/", "", comment_color, true); |
| 351 | |
| 352 | // Disabled preprocessor branches use translucent text color to be easier to distinguish from comments. |
| 353 | syntax_highlighter->set_disabled_branch_color(Color(EDITOR_GET("text_editor/theme/highlighting/text_color")) * Color(1, 1, 1, 0.5)); |
| 354 | |
| 355 | te->clear_comment_delimiters(); |
| 356 | te->add_comment_delimiter("/*", "*/", false); |
| 357 | te->add_comment_delimiter("//", "", true); |
| 358 | |
| 359 | if (!te->has_auto_brace_completion_open_key("/*")) { |
| 360 | te->add_auto_brace_completion_pair("/*", "*/"); |
| 361 | } |
| 362 | |
| 363 | // Colorize preprocessor include strings. |
| 364 | const Color string_color = EDITOR_GET("text_editor/theme/highlighting/string_color"); |
| 365 | syntax_highlighter->add_color_region("\"", "\"", string_color, false); |
| 366 | syntax_highlighter->set_uint_suffix_enabled(true); |
| 367 | } |
| 368 | |
| 369 | void ShaderTextEditor::_check_shader_mode() { |
| 370 | String type = ShaderLanguage::get_shader_type(get_text_editor()->get_text()); |
| 371 | |
| 372 | Shader::Mode mode; |
| 373 | |
| 374 | if (type == "canvas_item") { |
| 375 | mode = Shader::MODE_CANVAS_ITEM; |
| 376 | } else if (type == "particles") { |
| 377 | mode = Shader::MODE_PARTICLES; |
| 378 | } else if (type == "sky") { |
| 379 | mode = Shader::MODE_SKY; |
| 380 | } else if (type == "fog") { |
| 381 | mode = Shader::MODE_FOG; |
| 382 | } else { |
| 383 | mode = Shader::MODE_SPATIAL; |
| 384 | } |
| 385 | |
| 386 | if (shader->get_mode() != mode) { |
| 387 | set_block_shader_changed(true); |
| 388 | shader->set_code(get_text_editor()->get_text()); |
| 389 | set_block_shader_changed(false); |
| 390 | _load_theme_settings(); |
| 391 | } |
| 392 | } |
| 393 | |
| 394 | static ShaderLanguage::DataType _get_global_shader_uniform_type(const StringName &p_variable) { |
| 395 | RS::GlobalShaderParameterType gvt = RS::get_singleton()->global_shader_parameter_get_type(p_variable); |
| 396 | return (ShaderLanguage::DataType)RS::global_shader_uniform_type_get_shader_datatype(gvt); |
| 397 | } |
| 398 | |
| 399 | static String complete_from_path; |
| 400 | |
| 401 | static void _complete_include_paths_search(EditorFileSystemDirectory *p_efsd, List<ScriptLanguage::CodeCompletionOption> *r_options) { |
| 402 | if (!p_efsd) { |
| 403 | return; |
| 404 | } |
| 405 | for (int i = 0; i < p_efsd->get_file_count(); i++) { |
| 406 | if (p_efsd->get_file_type(i) == SNAME("ShaderInclude")) { |
| 407 | String path = p_efsd->get_file_path(i); |
| 408 | if (path.begins_with(complete_from_path)) { |
| 409 | path = path.replace_first(complete_from_path, ""); |
| 410 | } |
| 411 | r_options->push_back(ScriptLanguage::CodeCompletionOption(path, ScriptLanguage::CODE_COMPLETION_KIND_FILE_PATH)); |
| 412 | } |
| 413 | } |
| 414 | for (int j = 0; j < p_efsd->get_subdir_count(); j++) { |
| 415 | _complete_include_paths_search(p_efsd->get_subdir(j), r_options); |
| 416 | } |
| 417 | } |
| 418 | |
| 419 | static void _complete_include_paths(List<ScriptLanguage::CodeCompletionOption> *r_options) { |
| 420 | _complete_include_paths_search(EditorFileSystem::get_singleton()->get_filesystem(), r_options); |
| 421 | } |
| 422 | |
| 423 | void ShaderTextEditor::_code_complete_script(const String &p_code, List<ScriptLanguage::CodeCompletionOption> *r_options) { |
| 424 | List<ScriptLanguage::CodeCompletionOption> pp_options; |
| 425 | List<ScriptLanguage::CodeCompletionOption> pp_defines; |
| 426 | ShaderPreprocessor preprocessor; |
| 427 | String code; |
| 428 | String resource_path = (shader.is_valid() ? shader->get_path() : shader_inc->get_path()); |
| 429 | complete_from_path = resource_path.get_base_dir(); |
| 430 | if (!complete_from_path.ends_with("/")) { |
| 431 | complete_from_path += "/"; |
| 432 | } |
| 433 | preprocessor.preprocess(p_code, resource_path, code, nullptr, nullptr, nullptr, nullptr, &pp_options, &pp_defines, _complete_include_paths); |
| 434 | complete_from_path = String(); |
| 435 | if (pp_options.size()) { |
| 436 | for (const ScriptLanguage::CodeCompletionOption &E : pp_options) { |
| 437 | r_options->push_back(E); |
| 438 | } |
| 439 | return; |
| 440 | } |
| 441 | |
| 442 | ShaderLanguage sl; |
| 443 | String calltip; |
| 444 | ShaderLanguage::ShaderCompileInfo comp_info; |
| 445 | comp_info.global_shader_uniform_type_func = _get_global_shader_uniform_type; |
| 446 | |
| 447 | if (shader.is_null()) { |
| 448 | comp_info.is_include = true; |
| 449 | |
| 450 | sl.complete(code, comp_info, r_options, calltip); |
| 451 | if (sl.get_completion_type() == ShaderLanguage::COMPLETION_IDENTIFIER) { |
| 452 | for (const ScriptLanguage::CodeCompletionOption &E : pp_defines) { |
| 453 | r_options->push_back(E); |
| 454 | } |
| 455 | } |
| 456 | |
| 457 | get_text_editor()->set_code_hint(calltip); |
| 458 | return; |
| 459 | } |
| 460 | _check_shader_mode(); |
| 461 | comp_info.functions = ShaderTypes::get_singleton()->get_functions(RenderingServer::ShaderMode(shader->get_mode())); |
| 462 | comp_info.render_modes = ShaderTypes::get_singleton()->get_modes(RenderingServer::ShaderMode(shader->get_mode())); |
| 463 | comp_info.stencil_modes = ShaderTypes::get_singleton()->get_stencil_modes(RenderingServer::ShaderMode(shader->get_mode())); |
| 464 | comp_info.shader_types = ShaderTypes::get_singleton()->get_types(); |
| 465 | |
| 466 | sl.complete(code, comp_info, r_options, calltip); |
| 467 | if (sl.get_completion_type() == ShaderLanguage::COMPLETION_IDENTIFIER) { |
| 468 | for (const ScriptLanguage::CodeCompletionOption &E : pp_defines) { |
| 469 | r_options->push_back(E); |
| 470 | } |
| 471 | } |
| 472 | |
| 473 | get_text_editor()->set_code_hint(calltip); |
| 474 | } |
| 475 | |
| 476 | void ShaderTextEditor::_validate_script() { |
| 477 | emit_signal(CoreStringName(script_changed)); // Ensure to notify that it changed, so it is applied |
| 478 | |
| 479 | String code; |
| 480 | |
| 481 | if (shader.is_valid()) { |
| 482 | _check_shader_mode(); |
| 483 | code = shader->get_code(); |
| 484 | } else { |
| 485 | code = shader_inc->get_code(); |
| 486 | } |
| 487 | |
| 488 | ShaderPreprocessor preprocessor; |
| 489 | String code_pp; |
| 490 | String error_pp; |
| 491 | List<ShaderPreprocessor::FilePosition> err_positions; |
| 492 | List<ShaderPreprocessor::Region> regions; |
| 493 | String filename; |
| 494 | if (shader.is_valid()) { |
| 495 | filename = shader->get_path(); |
| 496 | } else if (shader_inc.is_valid()) { |
| 497 | filename = shader_inc->get_path(); |
| 498 | } |
| 499 | last_compile_result = preprocessor.preprocess(code, filename, code_pp, &error_pp, &err_positions, ®ions); |
| 500 | |
| 501 | for (int i = 0; i < get_text_editor()->get_line_count(); i++) { |
| 502 | get_text_editor()->set_line_background_color(i, Color(0, 0, 0, 0)); |
| 503 | } |
| 504 | |
| 505 | syntax_highlighter->clear_disabled_branch_regions(); |
| 506 | for (const ShaderPreprocessor::Region ®ion : regions) { |
| 507 | if (!region.enabled) { |
| 508 | if (filename != region.file) { |
| 509 | continue; |
| 510 | } |
| 511 | syntax_highlighter->add_disabled_branch_region(Point2i(region.from_line, region.to_line)); |
| 512 | } |
| 513 | } |
| 514 | |
| 515 | set_error(""); |
| 516 | set_error_count(0); |
| 517 | |
| 518 | if (last_compile_result != OK) { |
| 519 | // Preprocessor error. |
| 520 | ERR_FAIL_COND(err_positions.is_empty()); |
| 521 | |
| 522 | String err_text; |
| 523 | const int err_line = err_positions.front()->get().line; |
| 524 | if (err_positions.size() == 1) { |
| 525 | // Error in the main file. |
| 526 | const String message = error_pp.replace("[", "[lb]"); |
| 527 | |
| 528 | err_text = vformat(TTR("Error at line %d:"), err_line) + " " + message; |
| 529 | } else { |
| 530 | // Error in an included file. |
| 531 | const String inc_file = err_positions.back()->get().file.get_file(); |
| 532 | const int inc_line = err_positions.back()->get().line; |
| 533 | const String message = error_pp.replace("[", "[lb]"); |
| 534 | |
| 535 | err_text = vformat(TTR("Error at line %d in include %s:%d:"), err_line, inc_file, inc_line) + " " + message; |
| 536 | set_error_count(err_positions.size() - 1); |
| 537 | } |
| 538 | |
| 539 | set_error(err_text); |
| 540 | set_error_pos(err_line - 1, 0); |
| 541 | |
| 542 | for (int i = 0; i < get_text_editor()->get_line_count(); i++) { |
| 543 | get_text_editor()->set_line_background_color(i, Color(0, 0, 0, 0)); |
| 544 | } |
| 545 | get_text_editor()->set_line_background_color(err_line - 1, marked_line_color); |
| 546 | |
| 547 | set_warning_count(0); |
| 548 | } else { |
| 549 | ShaderLanguage sl; |
| 550 | |
| 551 | sl.enable_warning_checking(saved_warnings_enabled); |
| 552 | uint32_t flags = saved_warning_flags; |
| 553 | if (shader.is_null()) { |
| 554 | if (flags & ShaderWarning::UNUSED_CONSTANT) { |
| 555 | flags &= ~(ShaderWarning::UNUSED_CONSTANT); |
| 556 | } |
| 557 | if (flags & ShaderWarning::UNUSED_FUNCTION) { |
| 558 | flags &= ~(ShaderWarning::UNUSED_FUNCTION); |
| 559 | } |
| 560 | if (flags & ShaderWarning::UNUSED_STRUCT) { |
| 561 | flags &= ~(ShaderWarning::UNUSED_STRUCT); |
| 562 | } |
| 563 | if (flags & ShaderWarning::UNUSED_UNIFORM) { |
| 564 | flags &= ~(ShaderWarning::UNUSED_UNIFORM); |
| 565 | } |
| 566 | if (flags & ShaderWarning::UNUSED_VARYING) { |
| 567 | flags &= ~(ShaderWarning::UNUSED_VARYING); |
| 568 | } |
| 569 | } |
| 570 | sl.set_warning_flags(flags); |
| 571 | |
| 572 | ShaderLanguage::ShaderCompileInfo comp_info; |
| 573 | comp_info.global_shader_uniform_type_func = _get_global_shader_uniform_type; |
| 574 | |
| 575 | if (shader.is_null()) { |
| 576 | comp_info.is_include = true; |
| 577 | } else { |
| 578 | Shader::Mode mode = shader->get_mode(); |
| 579 | comp_info.functions = ShaderTypes::get_singleton()->get_functions(RenderingServer::ShaderMode(mode)); |
| 580 | comp_info.render_modes = ShaderTypes::get_singleton()->get_modes(RenderingServer::ShaderMode(mode)); |
| 581 | comp_info.stencil_modes = ShaderTypes::get_singleton()->get_stencil_modes(RenderingServer::ShaderMode(mode)); |
| 582 | comp_info.shader_types = ShaderTypes::get_singleton()->get_types(); |
| 583 | } |
| 584 | |
| 585 | code = code_pp; |
| 586 | //compiler error |
| 587 | last_compile_result = sl.compile(code, comp_info); |
| 588 | |
| 589 | if (last_compile_result != OK) { |
| 590 | Vector<ShaderLanguage::FilePosition> include_positions = sl.get_include_positions(); |
| 591 | |
| 592 | String err_text; |
| 593 | int err_line; |
| 594 | if (include_positions.size() > 1) { |
| 595 | // Error in an included file. |
| 596 | err_line = include_positions[0].line; |
| 597 | |
| 598 | const String inc_file = include_positions[include_positions.size() - 1].file; |
| 599 | const int inc_line = include_positions[include_positions.size() - 1].line; |
| 600 | const String message = sl.get_error_text().replace("[", "[lb]"); |
| 601 | |
| 602 | err_text = vformat(TTR("Error at line %d in include %s:%d:"), err_line, inc_file, inc_line) + " " + message; |
| 603 | set_error_count(include_positions.size() - 1); |
| 604 | } else { |
| 605 | // Error in the main file. |
| 606 | err_line = sl.get_error_line(); |
| 607 | |
| 608 | const String message = sl.get_error_text().replace("[", "[lb]"); |
| 609 | |
| 610 | err_text = vformat(TTR("Error at line %d:"), err_line) + " " + message; |
| 611 | set_error_count(0); |
| 612 | } |
| 613 | |
| 614 | set_error(err_text); |
| 615 | set_error_pos(err_line - 1, 0); |
| 616 | |
| 617 | get_text_editor()->set_line_background_color(err_line - 1, marked_line_color); |
| 618 | } else { |
| 619 | set_error(""); |
| 620 | } |
| 621 | |
| 622 | if (warnings.size() > 0 || last_compile_result != OK) { |
| 623 | warnings_panel->clear(); |
| 624 | } |
| 625 | warnings.clear(); |
| 626 | for (List<ShaderWarning>::Element *E = sl.get_warnings_ptr(); E; E = E->next()) { |
| 627 | warnings.push_back(E->get()); |
| 628 | } |
| 629 | if (warnings.size() > 0 && last_compile_result == OK) { |
| 630 | warnings.sort_custom<WarningsComparator>(); |
| 631 | _update_warning_panel(); |
| 632 | } else { |
| 633 | set_warning_count(0); |
| 634 | } |
| 635 | } |
| 636 | |
| 637 | emit_signal(SNAME("script_validated"), last_compile_result == OK); // Notify that validation finished, to update the list of scripts |
| 638 | } |
| 639 | |
| 640 | void ShaderTextEditor::_update_warning_panel() { |
| 641 | int warning_count = 0; |
| 642 | |
| 643 | warnings_panel->push_table(2); |
| 644 | for (const ShaderWarning &w : warnings) { |
| 645 | if (warning_count == 0) { |
| 646 | if (saved_treat_warning_as_errors) { |
| 647 | const String message = (w.get_message() + " " + TTR("Warnings should be fixed to prevent errors.")).replace("[", "[lb]"); |
| 648 | const String error_text = vformat(TTR("Error at line %d:"), w.get_line()) + " " + message; |
| 649 | |
| 650 | set_error(error_text); |
| 651 | set_error_pos(w.get_line() - 1, 0); |
| 652 | |
| 653 | get_text_editor()->set_line_background_color(w.get_line() - 1, marked_line_color); |
| 654 | } |
| 655 | } |
| 656 | |
| 657 | warning_count++; |
| 658 | int line = w.get_line(); |
| 659 | |
| 660 | // First cell. |
| 661 | warnings_panel->push_cell(); |
| 662 | warnings_panel->push_color(warnings_panel->get_theme_color(SNAME("warning_color"), EditorStringName(Editor))); |
| 663 | if (line != -1) { |
| 664 | warnings_panel->push_meta(line - 1); |
| 665 | warnings_panel->add_text(vformat(TTR("Line %d (%s):"), line, w.get_name())); |
| 666 | warnings_panel->pop(); // Meta goto. |
| 667 | } else { |
| 668 | warnings_panel->add_text(w.get_name() + ":"); |
| 669 | } |
| 670 | warnings_panel->pop(); // Color. |
| 671 | warnings_panel->pop(); // Cell. |
| 672 | |
| 673 | // Second cell. |
| 674 | warnings_panel->push_cell(); |
| 675 | warnings_panel->add_text(w.get_message()); |
| 676 | warnings_panel->pop(); // Cell. |
| 677 | } |
| 678 | warnings_panel->pop(); // Table. |
| 679 | |
| 680 | set_warning_count(warning_count); |
| 681 | } |
| 682 | |
| 683 | void ShaderTextEditor::_bind_methods() { |
| 684 | ADD_SIGNAL(MethodInfo("script_validated", PropertyInfo(Variant::BOOL, "valid"))); |
| 685 | } |
| 686 | |
| 687 | ShaderTextEditor::ShaderTextEditor() { |
| 688 | syntax_highlighter.instantiate(); |
| 689 | get_text_editor()->set_syntax_highlighter(syntax_highlighter); |
| 690 | } |
| 691 | |
| 692 | /*** SCRIPT EDITOR ******/ |
| 693 | |
| 694 | void TextShaderEditor::_menu_option(int p_option) { |
| 695 | code_editor->get_text_editor()->apply_ime(); |
| 696 | |
| 697 | switch (p_option) { |
| 698 | case EDIT_UNDO: { |
| 699 | code_editor->get_text_editor()->undo(); |
| 700 | } break; |
| 701 | case EDIT_REDO: { |
| 702 | code_editor->get_text_editor()->redo(); |
| 703 | } break; |
| 704 | case EDIT_CUT: { |
| 705 | code_editor->get_text_editor()->cut(); |
| 706 | } break; |
| 707 | case EDIT_COPY: { |
| 708 | code_editor->get_text_editor()->copy(); |
| 709 | } break; |
| 710 | case EDIT_PASTE: { |
| 711 | code_editor->get_text_editor()->paste(); |
| 712 | } break; |
| 713 | case EDIT_SELECT_ALL: { |
| 714 | code_editor->get_text_editor()->select_all(); |
| 715 | } break; |
| 716 | case EDIT_MOVE_LINE_UP: { |
| 717 | code_editor->get_text_editor()->move_lines_up(); |
| 718 | } break; |
| 719 | case EDIT_MOVE_LINE_DOWN: { |
| 720 | code_editor->get_text_editor()->move_lines_down(); |
| 721 | } break; |
| 722 | case EDIT_INDENT: { |
| 723 | if (shader.is_null() && shader_inc.is_null()) { |
| 724 | return; |
| 725 | } |
| 726 | code_editor->get_text_editor()->indent_lines(); |
| 727 | } break; |
| 728 | case EDIT_UNINDENT: { |
| 729 | if (shader.is_null() && shader_inc.is_null()) { |
| 730 | return; |
| 731 | } |
| 732 | code_editor->get_text_editor()->unindent_lines(); |
| 733 | } break; |
| 734 | case EDIT_DELETE_LINE: { |
| 735 | code_editor->get_text_editor()->delete_lines(); |
| 736 | } break; |
| 737 | case EDIT_DUPLICATE_SELECTION: { |
| 738 | code_editor->get_text_editor()->duplicate_selection(); |
| 739 | } break; |
| 740 | case EDIT_DUPLICATE_LINES: { |
| 741 | code_editor->get_text_editor()->duplicate_lines(); |
| 742 | } break; |
| 743 | case EDIT_TOGGLE_WORD_WRAP: { |
| 744 | TextEdit::LineWrappingMode wrap = code_editor->get_text_editor()->get_line_wrapping_mode(); |
| 745 | code_editor->get_text_editor()->set_line_wrapping_mode(wrap == TextEdit::LINE_WRAPPING_BOUNDARY ? TextEdit::LINE_WRAPPING_NONE : TextEdit::LINE_WRAPPING_BOUNDARY); |
| 746 | } break; |
| 747 | case EDIT_TOGGLE_COMMENT: { |
| 748 | if (shader.is_null() && shader_inc.is_null()) { |
| 749 | return; |
| 750 | } |
| 751 | code_editor->toggle_inline_comment("//"); |
| 752 | } break; |
| 753 | case EDIT_COMPLETE: { |
| 754 | code_editor->get_text_editor()->request_code_completion(); |
| 755 | } break; |
| 756 | case SEARCH_FIND: { |
| 757 | code_editor->get_find_replace_bar()->popup_search(); |
| 758 | } break; |
| 759 | case SEARCH_FIND_NEXT: { |
| 760 | code_editor->get_find_replace_bar()->search_next(); |
| 761 | } break; |
| 762 | case SEARCH_FIND_PREV: { |
| 763 | code_editor->get_find_replace_bar()->search_prev(); |
| 764 | } break; |
| 765 | case SEARCH_REPLACE: { |
| 766 | code_editor->get_find_replace_bar()->popup_replace(); |
| 767 | } break; |
| 768 | case SEARCH_GOTO_LINE: { |
| 769 | goto_line_popup->popup_find_line(code_editor); |
| 770 | } break; |
| 771 | case BOOKMARK_TOGGLE: { |
| 772 | code_editor->toggle_bookmark(); |
| 773 | } break; |
| 774 | case BOOKMARK_GOTO_NEXT: { |
| 775 | code_editor->goto_next_bookmark(); |
| 776 | } break; |
| 777 | case BOOKMARK_GOTO_PREV: { |
| 778 | code_editor->goto_prev_bookmark(); |
| 779 | } break; |
| 780 | case BOOKMARK_REMOVE_ALL: { |
| 781 | code_editor->remove_all_bookmarks(); |
| 782 | } break; |
| 783 | case HELP_DOCS: { |
| 784 | OS::get_singleton()->shell_open(vformat("%s/tutorials/shaders/shader_reference/index.html", GODOT_VERSION_DOCS_URL)); |
| 785 | } break; |
| 786 | case EDIT_EMOJI_AND_SYMBOL: { |
| 787 | code_editor->get_text_editor()->show_emoji_and_symbol_picker(); |
| 788 | } break; |
| 789 | } |
| 790 | if (p_option != SEARCH_FIND && p_option != SEARCH_REPLACE && p_option != SEARCH_GOTO_LINE) { |
| 791 | callable_mp((Control *)code_editor->get_text_editor(), &Control::grab_focus).call_deferred(false); |
| 792 | } |
| 793 | } |
| 794 | |
| 795 | void TextShaderEditor::_prepare_edit_menu() { |
| 796 | const CodeEdit *tx = code_editor->get_text_editor(); |
| 797 | PopupMenu *popup = edit_menu->get_popup(); |
| 798 | popup->set_item_disabled(popup->get_item_index(EDIT_UNDO), !tx->has_undo()); |
| 799 | popup->set_item_disabled(popup->get_item_index(EDIT_REDO), !tx->has_redo()); |
| 800 | } |
| 801 | |
| 802 | void TextShaderEditor::_notification(int p_what) { |
| 803 | switch (p_what) { |
| 804 | case EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED: { |
| 805 | if (EditorThemeManager::is_generated_theme_outdated() || |
| 806 | EditorSettings::get_singleton()->check_changed_settings_in_group("interface/editor") || |
| 807 | EditorSettings::get_singleton()->check_changed_settings_in_group("text_editor")) { |
| 808 | _apply_editor_settings(); |
| 809 | } |
| 810 | } break; |
| 811 | |
| 812 | case NOTIFICATION_THEME_CHANGED: { |
| 813 | site_search->set_button_icon(get_editor_theme_icon(SNAME("ExternalLink"))); |
| 814 | } break; |
| 815 | |
| 816 | case NOTIFICATION_APPLICATION_FOCUS_IN: { |
| 817 | _check_for_external_edit(); |
| 818 | } break; |
| 819 | } |
| 820 | } |
| 821 | |
| 822 | void TextShaderEditor::_apply_editor_settings() { |
| 823 | code_editor->update_editor_settings(); |
| 824 | |
| 825 | trim_trailing_whitespace_on_save = EDITOR_GET("text_editor/behavior/files/trim_trailing_whitespace_on_save"); |
| 826 | trim_final_newlines_on_save = EDITOR_GET("text_editor/behavior/files/trim_final_newlines_on_save"); |
| 827 | } |
| 828 | |
| 829 | void TextShaderEditor::_show_warnings_panel(bool p_show) { |
| 830 | warnings_panel->set_visible(p_show); |
| 831 | } |
| 832 | |
| 833 | void TextShaderEditor::_warning_clicked(const Variant &p_line) { |
| 834 | if (p_line.get_type() == Variant::INT) { |
| 835 | code_editor->goto_line_centered(p_line.operator int64_t()); |
| 836 | } |
| 837 | } |
| 838 | |
| 839 | void TextShaderEditor::_bind_methods() { |
| 840 | ClassDB::bind_method("_show_warnings_panel", &TextShaderEditor::_show_warnings_panel); |
| 841 | ClassDB::bind_method("_warning_clicked", &TextShaderEditor::_warning_clicked); |
| 842 | |
| 843 | ADD_SIGNAL(MethodInfo("validation_changed")); |
| 844 | } |
| 845 | |
| 846 | void TextShaderEditor::ensure_select_current() { |
| 847 | } |
| 848 | |
| 849 | void TextShaderEditor::goto_line_selection(int p_line, int p_begin, int p_end) { |
| 850 | code_editor->goto_line_selection(p_line, p_begin, p_end); |
| 851 | } |
| 852 | |
| 853 | void TextShaderEditor::_project_settings_changed() { |
| 854 | _update_warnings(true); |
| 855 | } |
| 856 | |
| 857 | void TextShaderEditor::_update_warnings(bool p_validate) { |
| 858 | bool changed = false; |
| 859 | |
| 860 | bool warnings_enabled = GLOBAL_GET("debug/shader_language/warnings/enable").booleanize(); |
| 861 | if (warnings_enabled != saved_warnings_enabled) { |
| 862 | saved_warnings_enabled = warnings_enabled; |
| 863 | changed = true; |
| 864 | } |
| 865 | |
| 866 | bool treat_warning_as_errors = GLOBAL_GET("debug/shader_language/warnings/treat_warnings_as_errors").booleanize(); |
| 867 | if (treat_warning_as_errors != saved_treat_warning_as_errors) { |
| 868 | saved_treat_warning_as_errors = treat_warning_as_errors; |
| 869 | changed = true; |
| 870 | } |
| 871 | |
| 872 | bool update_flags = false; |
| 873 | |
| 874 | for (int i = 0; i < ShaderWarning::WARNING_MAX; i++) { |
| 875 | ShaderWarning::Code code = (ShaderWarning::Code)i; |
| 876 | bool value = GLOBAL_GET("debug/shader_language/warnings/" + ShaderWarning::get_name_from_code(code).to_lower()); |
| 877 | |
| 878 | if (saved_warnings[code] != value) { |
| 879 | saved_warnings[code] = value; |
| 880 | update_flags = true; |
| 881 | changed = true; |
| 882 | } |
| 883 | } |
| 884 | |
| 885 | if (update_flags) { |
| 886 | saved_warning_flags = (uint32_t)ShaderWarning::get_flags_from_codemap(saved_warnings); |
| 887 | } |
| 888 | |
| 889 | if (p_validate && changed && code_editor && code_editor->get_edited_shader().is_valid()) { |
| 890 | code_editor->validate_script(); |
| 891 | } |
| 892 | } |
| 893 | |
| 894 | void TextShaderEditor::_check_for_external_edit() { |
| 895 | bool use_autoreload = bool(EDITOR_GET("text_editor/behavior/files/auto_reload_scripts_on_external_change")); |
| 896 | |
| 897 | if (shader_inc.is_valid()) { |
| 898 | if (shader_inc->get_last_modified_time() != FileAccess::get_modified_time(shader_inc->get_path())) { |
| 899 | if (use_autoreload) { |
| 900 | _reload_shader_include_from_disk(); |
| 901 | } else { |
| 902 | callable_mp((Window *)disk_changed, &Window::popup_centered).call_deferred(Size2i()); |
| 903 | } |
| 904 | } |
| 905 | return; |
| 906 | } |
| 907 | |
| 908 | if (shader.is_null() || shader->is_built_in()) { |
| 909 | return; |
| 910 | } |
| 911 | |
| 912 | if (shader->get_last_modified_time() != FileAccess::get_modified_time(shader->get_path())) { |
| 913 | if (use_autoreload) { |
| 914 | _reload_shader_from_disk(); |
| 915 | } else { |
| 916 | callable_mp((Window *)disk_changed, &Window::popup_centered).call_deferred(Size2i()); |
| 917 | } |
| 918 | } |
| 919 | } |
| 920 | |
| 921 | void TextShaderEditor::_reload_shader_from_disk() { |
| 922 | Ref<Shader> rel_shader = ResourceLoader::load(shader->get_path(), shader->get_class(), ResourceFormatLoader::CACHE_MODE_IGNORE); |
| 923 | ERR_FAIL_COND(rel_shader.is_null()); |
| 924 | |
| 925 | code_editor->set_block_shader_changed(true); |
| 926 | shader->set_code(rel_shader->get_code()); |
| 927 | code_editor->set_block_shader_changed(false); |
| 928 | shader->set_last_modified_time(rel_shader->get_last_modified_time()); |
| 929 | code_editor->reload_text(); |
| 930 | } |
| 931 | |
| 932 | void TextShaderEditor::_reload_shader_include_from_disk() { |
| 933 | Ref<ShaderInclude> rel_shader_include = ResourceLoader::load(shader_inc->get_path(), shader_inc->get_class(), ResourceFormatLoader::CACHE_MODE_IGNORE); |
| 934 | ERR_FAIL_COND(rel_shader_include.is_null()); |
| 935 | |
| 936 | code_editor->set_block_shader_changed(true); |
| 937 | shader_inc->set_code(rel_shader_include->get_code()); |
| 938 | code_editor->set_block_shader_changed(false); |
| 939 | shader_inc->set_last_modified_time(rel_shader_include->get_last_modified_time()); |
| 940 | code_editor->reload_text(); |
| 941 | } |
| 942 | |
| 943 | void TextShaderEditor::_reload() { |
| 944 | if (shader.is_valid()) { |
| 945 | _reload_shader_from_disk(); |
| 946 | } else if (shader_inc.is_valid()) { |
| 947 | _reload_shader_include_from_disk(); |
| 948 | } |
| 949 | } |
| 950 | |
| 951 | void TextShaderEditor::edit_shader(const Ref<Shader> &p_shader) { |
| 952 | if (p_shader.is_null() || !p_shader->is_text_shader()) { |
| 953 | return; |
| 954 | } |
| 955 | |
| 956 | if (shader == p_shader) { |
| 957 | return; |
| 958 | } |
| 959 | |
| 960 | shader = p_shader; |
| 961 | shader_inc = Ref<ShaderInclude>(); |
| 962 | |
| 963 | code_editor->set_edited_shader(shader); |
| 964 | } |
| 965 | |
| 966 | void TextShaderEditor::edit_shader_include(const Ref<ShaderInclude> &p_shader_inc) { |
| 967 | if (p_shader_inc.is_null()) { |
| 968 | return; |
| 969 | } |
| 970 | |
| 971 | if (shader_inc == p_shader_inc) { |
| 972 | return; |
| 973 | } |
| 974 | |
| 975 | shader_inc = p_shader_inc; |
| 976 | shader = Ref<Shader>(); |
| 977 | |
| 978 | code_editor->set_edited_shader_include(p_shader_inc); |
| 979 | } |
| 980 | |
| 981 | void TextShaderEditor::use_menu_bar(MenuButton *p_file_menu) { |
| 982 | p_file_menu->set_switch_on_hover(true); |
| 983 | menu_bar_hbox->add_child(p_file_menu); |
| 984 | menu_bar_hbox->move_child(p_file_menu, 0); |
| 985 | } |
| 986 | |
| 987 | void TextShaderEditor::save_external_data(const String &p_str) { |
| 988 | if (shader.is_null() && shader_inc.is_null()) { |
| 989 | disk_changed->hide(); |
| 990 | return; |
| 991 | } |
| 992 | |
| 993 | if (trim_trailing_whitespace_on_save) { |
| 994 | trim_trailing_whitespace(); |
| 995 | } |
| 996 | |
| 997 | if (trim_final_newlines_on_save) { |
| 998 | trim_final_newlines(); |
| 999 | } |
| 1000 | |
| 1001 | apply_shaders(); |
| 1002 | |
| 1003 | Ref<Shader> edited_shader = code_editor->get_edited_shader(); |
| 1004 | if (edited_shader.is_valid()) { |
| 1005 | ResourceSaver::save(edited_shader); |
| 1006 | } |
| 1007 | if (shader.is_valid() && shader != edited_shader) { |
| 1008 | ResourceSaver::save(shader); |
| 1009 | } |
| 1010 | |
| 1011 | Ref<ShaderInclude> edited_shader_inc = code_editor->get_edited_shader_include(); |
| 1012 | if (edited_shader_inc.is_valid()) { |
| 1013 | ResourceSaver::save(edited_shader_inc); |
| 1014 | } |
| 1015 | if (shader_inc.is_valid() && shader_inc != edited_shader_inc) { |
| 1016 | ResourceSaver::save(shader_inc); |
| 1017 | } |
| 1018 | code_editor->get_text_editor()->tag_saved_version(); |
| 1019 | |
| 1020 | disk_changed->hide(); |
| 1021 | } |
| 1022 | |
| 1023 | void TextShaderEditor::trim_trailing_whitespace() { |
| 1024 | code_editor->trim_trailing_whitespace(); |
| 1025 | } |
| 1026 | |
| 1027 | void TextShaderEditor::trim_final_newlines() { |
| 1028 | code_editor->trim_final_newlines(); |
| 1029 | } |
| 1030 | |
| 1031 | void TextShaderEditor::set_toggle_list_control(Control *p_toggle_list_control) { |
| 1032 | code_editor->set_toggle_list_control(p_toggle_list_control); |
| 1033 | } |
| 1034 | |
| 1035 | void TextShaderEditor::update_toggle_files_button() { |
| 1036 | code_editor->update_toggle_files_button(); |
| 1037 | } |
| 1038 | |
| 1039 | void TextShaderEditor::validate_script() { |
| 1040 | code_editor->_validate_script(); |
| 1041 | } |
| 1042 | |
| 1043 | bool TextShaderEditor::is_unsaved() const { |
| 1044 | return code_editor->get_text_editor()->get_saved_version() != code_editor->get_text_editor()->get_version(); |
| 1045 | } |
| 1046 | |
| 1047 | void TextShaderEditor::tag_saved_version() { |
| 1048 | code_editor->get_text_editor()->tag_saved_version(); |
| 1049 | } |
| 1050 | |
| 1051 | void TextShaderEditor::apply_shaders() { |
| 1052 | String editor_code = code_editor->get_text_editor()->get_text(); |
| 1053 | if (shader.is_valid()) { |
| 1054 | String shader_code = shader->get_code(); |
| 1055 | if (shader_code != editor_code || dependencies_version != code_editor->get_dependencies_version()) { |
| 1056 | code_editor->set_block_shader_changed(true); |
| 1057 | shader->set_code(editor_code); |
| 1058 | code_editor->set_block_shader_changed(false); |
| 1059 | shader->set_edited(true); |
| 1060 | } |
| 1061 | } |
| 1062 | if (shader_inc.is_valid()) { |
| 1063 | String shader_inc_code = shader_inc->get_code(); |
| 1064 | if (shader_inc_code != editor_code || dependencies_version != code_editor->get_dependencies_version()) { |
| 1065 | code_editor->set_block_shader_changed(true); |
| 1066 | shader_inc->set_code(editor_code); |
| 1067 | code_editor->set_block_shader_changed(false); |
| 1068 | shader_inc->set_edited(true); |
| 1069 | } |
| 1070 | } |
| 1071 | |
| 1072 | dependencies_version = code_editor->get_dependencies_version(); |
| 1073 | } |
| 1074 | |
| 1075 | void TextShaderEditor::_text_edit_gui_input(const Ref<InputEvent> &ev) { |
| 1076 | Ref<InputEventMouseButton> mb = ev; |
| 1077 | |
| 1078 | if (mb.is_valid()) { |
| 1079 | if (mb->get_button_index() == MouseButton::RIGHT && mb->is_pressed()) { |
| 1080 | CodeEdit *tx = code_editor->get_text_editor(); |
| 1081 | |
| 1082 | tx->apply_ime(); |
| 1083 | |
| 1084 | Point2i pos = tx->get_line_column_at_pos(mb->get_global_position() - tx->get_global_position()); |
| 1085 | int row = pos.y; |
| 1086 | int col = pos.x; |
| 1087 | tx->set_move_caret_on_right_click_enabled(EDITOR_GET("text_editor/behavior/navigation/move_caret_on_right_click")); |
| 1088 | |
| 1089 | if (tx->is_move_caret_on_right_click_enabled()) { |
| 1090 | tx->remove_secondary_carets(); |
| 1091 | if (tx->has_selection()) { |
| 1092 | int from_line = tx->get_selection_from_line(); |
| 1093 | int to_line = tx->get_selection_to_line(); |
| 1094 | int from_column = tx->get_selection_from_column(); |
| 1095 | int to_column = tx->get_selection_to_column(); |
| 1096 | |
| 1097 | if (row < from_line || row > to_line || (row == from_line && col < from_column) || (row == to_line && col > to_column)) { |
| 1098 | // Right click is outside the selected text |
| 1099 | tx->deselect(); |
| 1100 | } |
| 1101 | } |
| 1102 | if (!tx->has_selection()) { |
| 1103 | tx->set_caret_line(row, true, false, -1); |
| 1104 | tx->set_caret_column(col); |
| 1105 | } |
| 1106 | } |
| 1107 | _make_context_menu(tx->has_selection(), get_local_mouse_position()); |
| 1108 | } |
| 1109 | } |
| 1110 | |
| 1111 | Ref<InputEventKey> k = ev; |
| 1112 | if (k.is_valid() && k->is_pressed() && k->is_action("ui_menu", true)) { |
| 1113 | CodeEdit *tx = code_editor->get_text_editor(); |
| 1114 | tx->adjust_viewport_to_caret(); |
| 1115 | _make_context_menu(tx->has_selection(), (get_global_transform().inverse() * tx->get_global_transform()).xform(tx->get_caret_draw_pos())); |
| 1116 | context_menu->grab_focus(); |
| 1117 | } |
| 1118 | } |
| 1119 | |
| 1120 | void TextShaderEditor::_update_bookmark_list() { |
| 1121 | bookmarks_menu->clear(); |
| 1122 | |
| 1123 | bookmarks_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/toggle_bookmark"), BOOKMARK_TOGGLE); |
| 1124 | bookmarks_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/remove_all_bookmarks"), BOOKMARK_REMOVE_ALL); |
| 1125 | bookmarks_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/goto_next_bookmark"), BOOKMARK_GOTO_NEXT); |
| 1126 | bookmarks_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/goto_previous_bookmark"), BOOKMARK_GOTO_PREV); |
| 1127 | |
| 1128 | PackedInt32Array bookmark_list = code_editor->get_text_editor()->get_bookmarked_lines(); |
| 1129 | if (bookmark_list.is_empty()) { |
| 1130 | return; |
| 1131 | } |
| 1132 | |
| 1133 | bookmarks_menu->add_separator(); |
| 1134 | |
| 1135 | for (int i = 0; i < bookmark_list.size(); i++) { |
| 1136 | String line = code_editor->get_text_editor()->get_line(bookmark_list[i]).strip_edges(); |
| 1137 | // Limit the size of the line if too big. |
| 1138 | if (line.length() > 50) { |
| 1139 | line = line.substr(0, 50); |
| 1140 | } |
| 1141 | |
| 1142 | bookmarks_menu->add_item(String::num_int64(bookmark_list[i] + 1) + " - \"" + line + "\""); |
| 1143 | bookmarks_menu->set_item_metadata(-1, bookmark_list[i]); |
| 1144 | } |
| 1145 | } |
| 1146 | |
| 1147 | void TextShaderEditor::_bookmark_item_pressed(int p_idx) { |
| 1148 | if (p_idx < 4) { // Any item before the separator. |
| 1149 | _menu_option(bookmarks_menu->get_item_id(p_idx)); |
| 1150 | } else { |
| 1151 | code_editor->goto_line(bookmarks_menu->get_item_metadata(p_idx)); |
| 1152 | } |
| 1153 | } |
| 1154 | |
| 1155 | void TextShaderEditor::_make_context_menu(bool p_selection, Vector2 p_position) { |
| 1156 | context_menu->clear(); |
| 1157 | if (DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_EMOJI_AND_SYMBOL_PICKER)) { |
| 1158 | context_menu->add_item(TTR("Emoji & Symbols"), EDIT_EMOJI_AND_SYMBOL); |
| 1159 | context_menu->add_separator(); |
| 1160 | } |
| 1161 | if (p_selection) { |
| 1162 | context_menu->add_shortcut(ED_GET_SHORTCUT("ui_cut"), EDIT_CUT); |
| 1163 | context_menu->add_shortcut(ED_GET_SHORTCUT("ui_copy"), EDIT_COPY); |
| 1164 | } |
| 1165 | |
| 1166 | context_menu->add_shortcut(ED_GET_SHORTCUT("ui_paste"), EDIT_PASTE); |
| 1167 | context_menu->add_separator(); |
| 1168 | context_menu->add_shortcut(ED_GET_SHORTCUT("ui_text_select_all"), EDIT_SELECT_ALL); |
| 1169 | context_menu->add_shortcut(ED_GET_SHORTCUT("ui_undo"), EDIT_UNDO); |
| 1170 | context_menu->add_shortcut(ED_GET_SHORTCUT("ui_redo"), EDIT_REDO); |
| 1171 | |
| 1172 | context_menu->add_separator(); |
| 1173 | context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/indent"), EDIT_INDENT); |
| 1174 | context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/unindent"), EDIT_UNINDENT); |
| 1175 | context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/toggle_comment"), EDIT_TOGGLE_COMMENT); |
| 1176 | context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/toggle_bookmark"), BOOKMARK_TOGGLE); |
| 1177 | |
| 1178 | context_menu->set_item_disabled(context_menu->get_item_index(EDIT_UNDO), !code_editor->get_text_editor()->has_undo()); |
| 1179 | context_menu->set_item_disabled(context_menu->get_item_index(EDIT_REDO), !code_editor->get_text_editor()->has_redo()); |
| 1180 | |
| 1181 | context_menu->set_position(get_screen_position() + p_position); |
| 1182 | context_menu->reset_size(); |
| 1183 | context_menu->popup(); |
| 1184 | } |
| 1185 | |
| 1186 | TextShaderEditor::TextShaderEditor() { |
| 1187 | _update_warnings(false); |
| 1188 | |
| 1189 | code_editor = memnew(ShaderTextEditor); |
| 1190 | |
| 1191 | code_editor->connect("script_validated", callable_mp(this, &TextShaderEditor::_script_validated)); |
| 1192 | |
| 1193 | code_editor->set_v_size_flags(SIZE_EXPAND_FILL); |
| 1194 | code_editor->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT); |
| 1195 | |
| 1196 | code_editor->connect("show_warnings_panel", callable_mp(this, &TextShaderEditor::_show_warnings_panel)); |
| 1197 | code_editor->connect(CoreStringName(script_changed), callable_mp(this, &TextShaderEditor::apply_shaders)); |
| 1198 | ProjectSettings::get_singleton()->connect("settings_changed", callable_mp(this, &TextShaderEditor::_project_settings_changed)); |
| 1199 | |
| 1200 | code_editor->get_text_editor()->set_symbol_lookup_on_click_enabled(true); |
| 1201 | code_editor->get_text_editor()->set_context_menu_enabled(false); |
| 1202 | code_editor->get_text_editor()->set_draw_breakpoints_gutter(false); |
| 1203 | code_editor->get_text_editor()->set_draw_executing_lines_gutter(false); |
| 1204 | code_editor->get_text_editor()->connect(SceneStringName(gui_input), callable_mp(this, &TextShaderEditor::_text_edit_gui_input)); |
| 1205 | |
| 1206 | code_editor->update_editor_settings(); |
| 1207 | |
| 1208 | context_menu = memnew(PopupMenu); |
| 1209 | add_child(context_menu); |
| 1210 | context_menu->connect(SceneStringName(id_pressed), callable_mp(this, &TextShaderEditor::_menu_option)); |
| 1211 | |
| 1212 | VBoxContainer *main_container = memnew(VBoxContainer); |
| 1213 | main_container->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT); |
| 1214 | menu_bar_hbox = memnew(HBoxContainer); |
| 1215 | |
| 1216 | edit_menu = memnew(MenuButton); |
| 1217 | edit_menu->set_flat(false); |
| 1218 | edit_menu->set_theme_type_variation("FlatMenuButton"); |
| 1219 | edit_menu->set_shortcut_context(this); |
| 1220 | edit_menu->set_text(TTR("Edit")); |
| 1221 | edit_menu->set_switch_on_hover(true); |
| 1222 | edit_menu->connect("about_to_popup", callable_mp(this, &TextShaderEditor::_prepare_edit_menu)); |
| 1223 | |
| 1224 | edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("ui_undo"), EDIT_UNDO); |
| 1225 | edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("ui_redo"), EDIT_REDO); |
| 1226 | edit_menu->get_popup()->add_separator(); |
| 1227 | edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("ui_cut"), EDIT_CUT); |
| 1228 | edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("ui_copy"), EDIT_COPY); |
| 1229 | edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("ui_paste"), EDIT_PASTE); |
| 1230 | edit_menu->get_popup()->add_separator(); |
| 1231 | edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("ui_text_select_all"), EDIT_SELECT_ALL); |
| 1232 | edit_menu->get_popup()->add_separator(); |
| 1233 | edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/move_up"), EDIT_MOVE_LINE_UP); |
| 1234 | edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/move_down"), EDIT_MOVE_LINE_DOWN); |
| 1235 | edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/indent"), EDIT_INDENT); |
| 1236 | edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/unindent"), EDIT_UNINDENT); |
| 1237 | edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/delete_line"), EDIT_DELETE_LINE); |
| 1238 | edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/toggle_comment"), EDIT_TOGGLE_COMMENT); |
| 1239 | edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/duplicate_selection"), EDIT_DUPLICATE_SELECTION); |
| 1240 | edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/duplicate_lines"), EDIT_DUPLICATE_LINES); |
| 1241 | edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/toggle_word_wrap"), EDIT_TOGGLE_WORD_WRAP); |
| 1242 | edit_menu->get_popup()->add_separator(); |
| 1243 | edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("ui_text_completion_query"), EDIT_COMPLETE); |
| 1244 | edit_menu->get_popup()->connect(SceneStringName(id_pressed), callable_mp(this, &TextShaderEditor::_menu_option)); |
| 1245 | |
| 1246 | search_menu = memnew(MenuButton); |
| 1247 | search_menu->set_flat(false); |
| 1248 | search_menu->set_theme_type_variation("FlatMenuButton"); |
| 1249 | search_menu->set_shortcut_context(this); |
| 1250 | search_menu->set_text(TTR("Search")); |
| 1251 | search_menu->set_switch_on_hover(true); |
| 1252 | |
| 1253 | search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/find"), SEARCH_FIND); |
| 1254 | search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/find_next"), SEARCH_FIND_NEXT); |
| 1255 | search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/find_previous"), SEARCH_FIND_PREV); |
| 1256 | search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/replace"), SEARCH_REPLACE); |
| 1257 | search_menu->get_popup()->connect(SceneStringName(id_pressed), callable_mp(this, &TextShaderEditor::_menu_option)); |
| 1258 | |
| 1259 | MenuButton *goto_menu = memnew(MenuButton); |
| 1260 | goto_menu->set_flat(false); |
| 1261 | goto_menu->set_theme_type_variation("FlatMenuButton"); |
| 1262 | goto_menu->set_shortcut_context(this); |
| 1263 | goto_menu->set_text(TTR("Go To")); |
| 1264 | goto_menu->set_switch_on_hover(true); |
| 1265 | goto_menu->get_popup()->connect(SceneStringName(id_pressed), callable_mp(this, &TextShaderEditor::_menu_option)); |
| 1266 | |
| 1267 | goto_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/goto_line"), SEARCH_GOTO_LINE); |
| 1268 | goto_menu->get_popup()->add_separator(); |
| 1269 | |
| 1270 | bookmarks_menu = memnew(PopupMenu); |
| 1271 | goto_menu->get_popup()->add_submenu_node_item(TTR("Bookmarks"), bookmarks_menu); |
| 1272 | _update_bookmark_list(); |
| 1273 | bookmarks_menu->connect("about_to_popup", callable_mp(this, &TextShaderEditor::_update_bookmark_list)); |
| 1274 | bookmarks_menu->connect("index_pressed", callable_mp(this, &TextShaderEditor::_bookmark_item_pressed)); |
| 1275 | |
| 1276 | add_child(main_container); |
| 1277 | main_container->add_child(menu_bar_hbox); |
| 1278 | menu_bar_hbox->add_child(edit_menu); |
| 1279 | menu_bar_hbox->add_child(search_menu); |
| 1280 | menu_bar_hbox->add_child(goto_menu); |
| 1281 | menu_bar_hbox->add_spacer(); |
| 1282 | |
| 1283 | site_search = memnew(Button); |
| 1284 | site_search->set_theme_type_variation(SceneStringName(FlatButton)); |
| 1285 | site_search->connect(SceneStringName(pressed), callable_mp(this, &TextShaderEditor::_menu_option).bind(HELP_DOCS)); |
| 1286 | site_search->set_text(TTR("Online Docs")); |
| 1287 | site_search->set_tooltip_text(TTR("Open Godot online documentation.")); |
| 1288 | menu_bar_hbox->add_child(site_search); |
| 1289 | |
| 1290 | menu_bar_hbox->add_theme_style_override(SceneStringName(panel), EditorNode::get_singleton()->get_editor_theme()->get_stylebox(SNAME("ScriptEditorPanel"), EditorStringName(EditorStyles))); |
| 1291 | |
| 1292 | VSplitContainer *editor_box = memnew(VSplitContainer); |
| 1293 | main_container->add_child(editor_box); |
| 1294 | editor_box->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT); |
| 1295 | editor_box->set_v_size_flags(SIZE_EXPAND_FILL); |
| 1296 | editor_box->add_child(code_editor); |
| 1297 | |
| 1298 | FindReplaceBar *bar = memnew(FindReplaceBar); |
| 1299 | main_container->add_child(bar); |
| 1300 | bar->hide(); |
| 1301 | code_editor->set_find_replace_bar(bar); |
| 1302 | |
| 1303 | warnings_panel = memnew(RichTextLabel); |
| 1304 | warnings_panel->set_custom_minimum_size(Size2(0, 100 * EDSCALE)); |
| 1305 | warnings_panel->set_h_size_flags(SIZE_EXPAND_FILL); |
| 1306 | warnings_panel->set_meta_underline(true); |
| 1307 | warnings_panel->set_selection_enabled(true); |
| 1308 | warnings_panel->set_context_menu_enabled(true); |
| 1309 | warnings_panel->set_focus_mode(FOCUS_CLICK); |
| 1310 | warnings_panel->hide(); |
| 1311 | warnings_panel->connect("meta_clicked", callable_mp(this, &TextShaderEditor::_warning_clicked)); |
| 1312 | editor_box->add_child(warnings_panel); |
| 1313 | code_editor->set_warnings_panel(warnings_panel); |
| 1314 | |
| 1315 | goto_line_popup = memnew(GotoLinePopup); |
| 1316 | add_child(goto_line_popup); |
| 1317 | |
| 1318 | disk_changed = memnew(ConfirmationDialog); |
| 1319 | |
| 1320 | VBoxContainer *vbc = memnew(VBoxContainer); |
| 1321 | disk_changed->add_child(vbc); |
| 1322 | |
| 1323 | Label *dl = memnew(Label); |
| 1324 | dl->set_focus_mode(FOCUS_ACCESSIBILITY); |
| 1325 | dl->set_text(TTR("This shader has been modified on disk.\nWhat action should be taken?")); |
| 1326 | vbc->add_child(dl); |
| 1327 | |
| 1328 | disk_changed->connect(SceneStringName(confirmed), callable_mp(this, &TextShaderEditor::_reload)); |
| 1329 | disk_changed->set_ok_button_text(TTR("Reload")); |
| 1330 | |
| 1331 | disk_changed->add_button(TTR("Resave"), !DisplayServer::get_singleton()->get_swap_cancel_ok(), "resave"); |
| 1332 | disk_changed->connect("custom_action", callable_mp(this, &TextShaderEditor::save_external_data)); |
| 1333 | |
| 1334 | add_child(disk_changed); |
| 1335 | |
| 1336 | _apply_editor_settings(); |
| 1337 | code_editor->show_toggle_files_button(); // TODO: Disabled for now, because it doesn't work properly. |
| 1338 | } |