Path: blob/master/editor/shader/editor_native_shader_source_visualizer.cpp
9903 views
/**************************************************************************/1/* editor_native_shader_source_visualizer.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 "editor_native_shader_source_visualizer.h"3132#include "editor/editor_string_names.h"33#include "editor/settings/editor_settings.h"34#include "editor/themes/editor_scale.h"35#include "scene/gui/code_edit.h"36#include "scene/gui/text_edit.h"37#include "servers/rendering/shader_language.h"3839void EditorNativeShaderSourceVisualizer::_load_theme_settings() {40syntax_highlighter->set_number_color(EDITOR_GET("text_editor/theme/highlighting/number_color"));41syntax_highlighter->set_symbol_color(EDITOR_GET("text_editor/theme/highlighting/symbol_color"));42syntax_highlighter->set_function_color(EDITOR_GET("text_editor/theme/highlighting/function_color"));43syntax_highlighter->set_member_variable_color(EDITOR_GET("text_editor/theme/highlighting/member_variable_color"));4445syntax_highlighter->clear_keyword_colors();4647List<String> keywords;48ShaderLanguage::get_keyword_list(&keywords);49const Color keyword_color = EDITOR_GET("text_editor/theme/highlighting/keyword_color");50const Color control_flow_keyword_color = EDITOR_GET("text_editor/theme/highlighting/control_flow_keyword_color");5152for (const String &keyword : keywords) {53if (ShaderLanguage::is_control_flow_keyword(keyword)) {54syntax_highlighter->add_keyword_color(keyword, control_flow_keyword_color);55} else {56syntax_highlighter->add_keyword_color(keyword, keyword_color);57}58}5960// Colorize comments.61const Color comment_color = EDITOR_GET("text_editor/theme/highlighting/comment_color");62syntax_highlighter->clear_color_regions();63syntax_highlighter->add_color_region("/*", "*/", comment_color, false);64syntax_highlighter->add_color_region("//", "", comment_color, true);6566// Colorize preprocessor statements.67const Color user_type_color = EDITOR_GET("text_editor/theme/highlighting/user_type_color");68syntax_highlighter->add_color_region("#", "", user_type_color, true);6970syntax_highlighter->set_uint_suffix_enabled(true);71}7273void EditorNativeShaderSourceVisualizer::_inspect_shader(RID p_shader) {74if (versions) {75memdelete(versions);76versions = nullptr;77}7879RS::ShaderNativeSourceCode nsc = RS::get_singleton()->shader_get_native_source_code(p_shader);8081_load_theme_settings();8283versions = memnew(TabContainer);84versions->set_tab_alignment(TabBar::ALIGNMENT_CENTER);85versions->set_v_size_flags(Control::SIZE_EXPAND_FILL);86versions->set_h_size_flags(Control::SIZE_EXPAND_FILL);87for (int i = 0; i < nsc.versions.size(); i++) {88TabContainer *vtab = memnew(TabContainer);89vtab->set_name("Version " + itos(i));90vtab->set_tab_alignment(TabBar::ALIGNMENT_CENTER);91vtab->set_v_size_flags(Control::SIZE_EXPAND_FILL);92vtab->set_h_size_flags(Control::SIZE_EXPAND_FILL);93versions->add_child(vtab);94for (int j = 0; j < nsc.versions[i].stages.size(); j++) {95CodeEdit *code_edit = memnew(CodeEdit);96code_edit->set_editable(false);97code_edit->set_syntax_highlighter(syntax_highlighter);98code_edit->add_theme_font_override(SceneStringName(font), get_theme_font("source", EditorStringName(EditorFonts)));99code_edit->add_theme_font_size_override(SceneStringName(font_size), get_theme_font_size("source_size", EditorStringName(EditorFonts)));100code_edit->add_theme_constant_override("line_spacing", EDITOR_GET("text_editor/appearance/whitespace/line_spacing"));101102// Appearance: Caret103code_edit->set_caret_type((TextEdit::CaretType)EDITOR_GET("text_editor/appearance/caret/type").operator int());104code_edit->set_caret_blink_enabled(EDITOR_GET("text_editor/appearance/caret/caret_blink"));105code_edit->set_caret_blink_interval(EDITOR_GET("text_editor/appearance/caret/caret_blink_interval"));106code_edit->set_highlight_current_line(EDITOR_GET("text_editor/appearance/caret/highlight_current_line"));107code_edit->set_highlight_all_occurrences(EDITOR_GET("text_editor/appearance/caret/highlight_all_occurrences"));108109// Appearance: Gutters110code_edit->set_draw_line_numbers(EDITOR_GET("text_editor/appearance/gutters/show_line_numbers"));111code_edit->set_line_numbers_zero_padded(EDITOR_GET("text_editor/appearance/gutters/line_numbers_zero_padded"));112113// Appearance: Minimap114code_edit->set_draw_minimap(EDITOR_GET("text_editor/appearance/minimap/show_minimap"));115code_edit->set_minimap_width((int)EDITOR_GET("text_editor/appearance/minimap/minimap_width") * EDSCALE);116117// Appearance: Lines118code_edit->set_line_folding_enabled(EDITOR_GET("text_editor/appearance/lines/code_folding"));119code_edit->set_draw_fold_gutter(EDITOR_GET("text_editor/appearance/lines/code_folding"));120code_edit->set_line_wrapping_mode((TextEdit::LineWrappingMode)EDITOR_GET("text_editor/appearance/lines/word_wrap").operator int());121code_edit->set_autowrap_mode((TextServer::AutowrapMode)EDITOR_GET("text_editor/appearance/lines/autowrap_mode").operator int());122123// Appearance: Whitespace124code_edit->set_draw_tabs(EDITOR_GET("text_editor/appearance/whitespace/draw_tabs"));125code_edit->set_draw_spaces(EDITOR_GET("text_editor/appearance/whitespace/draw_spaces"));126code_edit->add_theme_constant_override("line_spacing", EDITOR_GET("text_editor/appearance/whitespace/line_spacing"));127128// Behavior: Navigation129code_edit->set_scroll_past_end_of_file_enabled(EDITOR_GET("text_editor/behavior/navigation/scroll_past_end_of_file"));130code_edit->set_smooth_scroll_enabled(EDITOR_GET("text_editor/behavior/navigation/smooth_scrolling"));131code_edit->set_v_scroll_speed(EDITOR_GET("text_editor/behavior/navigation/v_scroll_speed"));132code_edit->set_drag_and_drop_selection_enabled(EDITOR_GET("text_editor/behavior/navigation/drag_and_drop_selection"));133134// Behavior: Indent135code_edit->set_indent_size(EDITOR_GET("text_editor/behavior/indent/size"));136code_edit->set_auto_indent_enabled(EDITOR_GET("text_editor/behavior/indent/auto_indent"));137code_edit->set_indent_wrapped_lines(EDITOR_GET("text_editor/behavior/indent/indent_wrapped_lines"));138139code_edit->set_name(nsc.versions[i].stages[j].name);140code_edit->set_text(nsc.versions[i].stages[j].code);141code_edit->set_v_size_flags(Control::SIZE_EXPAND_FILL);142code_edit->set_h_size_flags(Control::SIZE_EXPAND_FILL);143vtab->add_child(code_edit);144}145}146add_child(versions);147popup_centered_ratio();148}149150void EditorNativeShaderSourceVisualizer::_bind_methods() {151ClassDB::bind_method("_inspect_shader", &EditorNativeShaderSourceVisualizer::_inspect_shader);152}153154EditorNativeShaderSourceVisualizer::EditorNativeShaderSourceVisualizer() {155syntax_highlighter.instantiate();156157add_to_group("_native_shader_source_visualizer");158set_title(TTR("Native Shader Source Inspector"));159}160161162