| 1 | /**************************************************************************/ |
| 2 | /* editor_native_shader_source_visualizer.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 "editor_native_shader_source_visualizer.h" |
| 32 | |
| 33 | #include "editor/editor_string_names.h" |
| 34 | #include "editor/settings/editor_settings.h" |
| 35 | #include "editor/themes/editor_scale.h" |
| 36 | #include "scene/gui/code_edit.h" |
| 37 | #include "scene/gui/text_edit.h" |
| 38 | #include "servers/rendering/shader_language.h" |
| 39 | |
| 40 | void EditorNativeShaderSourceVisualizer::_load_theme_settings() { |
| 41 | syntax_highlighter->set_number_color(EDITOR_GET("text_editor/theme/highlighting/number_color")); |
| 42 | syntax_highlighter->set_symbol_color(EDITOR_GET("text_editor/theme/highlighting/symbol_color")); |
| 43 | syntax_highlighter->set_function_color(EDITOR_GET("text_editor/theme/highlighting/function_color")); |
| 44 | syntax_highlighter->set_member_variable_color(EDITOR_GET("text_editor/theme/highlighting/member_variable_color")); |
| 45 | |
| 46 | syntax_highlighter->clear_keyword_colors(); |
| 47 | |
| 48 | List<String> keywords; |
| 49 | ShaderLanguage::get_keyword_list(&keywords); |
| 50 | const Color keyword_color = EDITOR_GET("text_editor/theme/highlighting/keyword_color"); |
| 51 | const Color control_flow_keyword_color = EDITOR_GET("text_editor/theme/highlighting/control_flow_keyword_color"); |
| 52 | |
| 53 | for (const String &keyword : keywords) { |
| 54 | if (ShaderLanguage::is_control_flow_keyword(keyword)) { |
| 55 | syntax_highlighter->add_keyword_color(keyword, control_flow_keyword_color); |
| 56 | } else { |
| 57 | syntax_highlighter->add_keyword_color(keyword, keyword_color); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | // Colorize comments. |
| 62 | const Color comment_color = EDITOR_GET("text_editor/theme/highlighting/comment_color"); |
| 63 | syntax_highlighter->clear_color_regions(); |
| 64 | syntax_highlighter->add_color_region("/*", "*/", comment_color, false); |
| 65 | syntax_highlighter->add_color_region("//", "", comment_color, true); |
| 66 | |
| 67 | // Colorize preprocessor statements. |
| 68 | const Color user_type_color = EDITOR_GET("text_editor/theme/highlighting/user_type_color"); |
| 69 | syntax_highlighter->add_color_region("#", "", user_type_color, true); |
| 70 | |
| 71 | syntax_highlighter->set_uint_suffix_enabled(true); |
| 72 | } |
| 73 | |
| 74 | void EditorNativeShaderSourceVisualizer::_inspect_shader(RID p_shader) { |
| 75 | if (versions) { |
| 76 | memdelete(versions); |
| 77 | versions = nullptr; |
| 78 | } |
| 79 | |
| 80 | RS::ShaderNativeSourceCode nsc = RS::get_singleton()->shader_get_native_source_code(p_shader); |
| 81 | |
| 82 | _load_theme_settings(); |
| 83 | |
| 84 | versions = memnew(TabContainer); |
| 85 | versions->set_theme_type_variation("TabContainerInner"); |
| 86 | versions->set_tab_alignment(TabBar::ALIGNMENT_CENTER); |
| 87 | versions->set_v_size_flags(Control::SIZE_EXPAND_FILL); |
| 88 | versions->set_h_size_flags(Control::SIZE_EXPAND_FILL); |
| 89 | for (int i = 0; i < nsc.versions.size(); i++) { |
| 90 | TabContainer *vtab = memnew(TabContainer); |
| 91 | vtab->set_theme_type_variation("TabContainerInner"); |
| 92 | vtab->set_name("Version " + itos(i)); |
| 93 | vtab->set_tab_alignment(TabBar::ALIGNMENT_CENTER); |
| 94 | vtab->set_v_size_flags(Control::SIZE_EXPAND_FILL); |
| 95 | vtab->set_h_size_flags(Control::SIZE_EXPAND_FILL); |
| 96 | versions->add_child(vtab); |
| 97 | for (int j = 0; j < nsc.versions[i].stages.size(); j++) { |
| 98 | CodeEdit *code_edit = memnew(CodeEdit); |
| 99 | code_edit->set_editable(false); |
| 100 | code_edit->set_syntax_highlighter(syntax_highlighter); |
| 101 | code_edit->add_theme_font_override(SceneStringName(font), get_theme_font("source", EditorStringName(EditorFonts))); |
| 102 | code_edit->add_theme_font_size_override(SceneStringName(font_size), get_theme_font_size("source_size", EditorStringName(EditorFonts))); |
| 103 | code_edit->add_theme_constant_override("line_spacing", EDITOR_GET("text_editor/appearance/whitespace/line_spacing")); |
| 104 | |
| 105 | // Appearance: Caret |
| 106 | code_edit->set_caret_type((TextEdit::CaretType)EDITOR_GET("text_editor/appearance/caret/type").operator int()); |
| 107 | code_edit->set_caret_blink_enabled(EDITOR_GET("text_editor/appearance/caret/caret_blink")); |
| 108 | code_edit->set_caret_blink_interval(EDITOR_GET("text_editor/appearance/caret/caret_blink_interval")); |
| 109 | code_edit->set_highlight_current_line(EDITOR_GET("text_editor/appearance/caret/highlight_current_line")); |
| 110 | code_edit->set_highlight_all_occurrences(EDITOR_GET("text_editor/appearance/caret/highlight_all_occurrences")); |
| 111 | |
| 112 | // Appearance: Gutters |
| 113 | code_edit->set_draw_line_numbers(EDITOR_GET("text_editor/appearance/gutters/show_line_numbers")); |
| 114 | code_edit->set_line_numbers_zero_padded(EDITOR_GET("text_editor/appearance/gutters/line_numbers_zero_padded")); |
| 115 | |
| 116 | // Appearance: Minimap |
| 117 | code_edit->set_draw_minimap(EDITOR_GET("text_editor/appearance/minimap/show_minimap")); |
| 118 | code_edit->set_minimap_width((int)EDITOR_GET("text_editor/appearance/minimap/minimap_width") * EDSCALE); |
| 119 | |
| 120 | // Appearance: Lines |
| 121 | code_edit->set_line_folding_enabled(EDITOR_GET("text_editor/appearance/lines/code_folding")); |
| 122 | code_edit->set_draw_fold_gutter(EDITOR_GET("text_editor/appearance/lines/code_folding")); |
| 123 | code_edit->set_line_wrapping_mode((TextEdit::LineWrappingMode)EDITOR_GET("text_editor/appearance/lines/word_wrap").operator int()); |
| 124 | code_edit->set_autowrap_mode((TextServer::AutowrapMode)EDITOR_GET("text_editor/appearance/lines/autowrap_mode").operator int()); |
| 125 | |
| 126 | // Appearance: Whitespace |
| 127 | code_edit->set_draw_tabs(EDITOR_GET("text_editor/appearance/whitespace/draw_tabs")); |
| 128 | code_edit->set_draw_spaces(EDITOR_GET("text_editor/appearance/whitespace/draw_spaces")); |
| 129 | code_edit->add_theme_constant_override("line_spacing", EDITOR_GET("text_editor/appearance/whitespace/line_spacing")); |
| 130 | |
| 131 | // Behavior: Navigation |
| 132 | code_edit->set_scroll_past_end_of_file_enabled(EDITOR_GET("text_editor/behavior/navigation/scroll_past_end_of_file")); |
| 133 | code_edit->set_smooth_scroll_enabled(EDITOR_GET("text_editor/behavior/navigation/smooth_scrolling")); |
| 134 | code_edit->set_v_scroll_speed(EDITOR_GET("text_editor/behavior/navigation/v_scroll_speed")); |
| 135 | code_edit->set_drag_and_drop_selection_enabled(EDITOR_GET("text_editor/behavior/navigation/drag_and_drop_selection")); |
| 136 | |
| 137 | // Behavior: Indent |
| 138 | code_edit->set_indent_size(EDITOR_GET("text_editor/behavior/indent/size")); |
| 139 | code_edit->set_auto_indent_enabled(EDITOR_GET("text_editor/behavior/indent/auto_indent")); |
| 140 | code_edit->set_indent_wrapped_lines(EDITOR_GET("text_editor/behavior/indent/indent_wrapped_lines")); |
| 141 | |
| 142 | code_edit->set_name(nsc.versions[i].stages[j].name); |
| 143 | code_edit->set_text(nsc.versions[i].stages[j].code); |
| 144 | code_edit->set_v_size_flags(Control::SIZE_EXPAND_FILL); |
| 145 | code_edit->set_h_size_flags(Control::SIZE_EXPAND_FILL); |
| 146 | vtab->add_child(code_edit); |
| 147 | } |
| 148 | } |
| 149 | add_child(versions); |
| 150 | popup_centered_ratio(); |
| 151 | } |
| 152 | |
| 153 | void EditorNativeShaderSourceVisualizer::_bind_methods() { |
| 154 | ClassDB::bind_method("_inspect_shader", &EditorNativeShaderSourceVisualizer::_inspect_shader); |
| 155 | } |
| 156 | |
| 157 | EditorNativeShaderSourceVisualizer::EditorNativeShaderSourceVisualizer() { |
| 158 | syntax_highlighter.instantiate(); |
| 159 | |
| 160 | add_to_group("_native_shader_source_visualizer"); |
| 161 | set_title(TTR("Native Shader Source Inspector")); |
| 162 | } |