Path: blob/master/editor/shader/editor_native_shader_source_visualizer.cpp
20843 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_theme_type_variation("TabContainerInner");85versions->set_tab_alignment(TabBar::ALIGNMENT_CENTER);86versions->set_v_size_flags(Control::SIZE_EXPAND_FILL);87versions->set_h_size_flags(Control::SIZE_EXPAND_FILL);88for (int i = 0; i < nsc.versions.size(); i++) {89TabContainer *vtab = memnew(TabContainer);90vtab->set_theme_type_variation("TabContainerInner");91vtab->set_name("Version " + itos(i));92vtab->set_tab_alignment(TabBar::ALIGNMENT_CENTER);93vtab->set_v_size_flags(Control::SIZE_EXPAND_FILL);94vtab->set_h_size_flags(Control::SIZE_EXPAND_FILL);95versions->add_child(vtab);96for (int j = 0; j < nsc.versions[i].stages.size(); j++) {97CodeEdit *code_edit = memnew(CodeEdit);98code_edit->set_editable(false);99code_edit->set_syntax_highlighter(syntax_highlighter);100code_edit->add_theme_font_override(SceneStringName(font), get_theme_font("source", EditorStringName(EditorFonts)));101code_edit->add_theme_font_size_override(SceneStringName(font_size), get_theme_font_size("source_size", EditorStringName(EditorFonts)));102code_edit->add_theme_constant_override("line_spacing", EDITOR_GET("text_editor/appearance/whitespace/line_spacing"));103104// Appearance: Caret105code_edit->set_caret_type((TextEdit::CaretType)EDITOR_GET("text_editor/appearance/caret/type").operator int());106code_edit->set_caret_blink_enabled(EDITOR_GET("text_editor/appearance/caret/caret_blink"));107code_edit->set_caret_blink_interval(EDITOR_GET("text_editor/appearance/caret/caret_blink_interval"));108code_edit->set_highlight_current_line(EDITOR_GET("text_editor/appearance/caret/highlight_current_line"));109code_edit->set_highlight_all_occurrences(EDITOR_GET("text_editor/appearance/caret/highlight_all_occurrences"));110111// Appearance: Gutters112code_edit->set_draw_line_numbers(EDITOR_GET("text_editor/appearance/gutters/show_line_numbers"));113code_edit->set_line_numbers_zero_padded(EDITOR_GET("text_editor/appearance/gutters/line_numbers_zero_padded"));114115// Appearance: Minimap116code_edit->set_draw_minimap(EDITOR_GET("text_editor/appearance/minimap/show_minimap"));117code_edit->set_minimap_width((int)EDITOR_GET("text_editor/appearance/minimap/minimap_width") * EDSCALE);118119// Appearance: Lines120code_edit->set_line_folding_enabled(EDITOR_GET("text_editor/appearance/lines/code_folding"));121code_edit->set_draw_fold_gutter(EDITOR_GET("text_editor/appearance/lines/code_folding"));122code_edit->set_line_wrapping_mode((TextEdit::LineWrappingMode)EDITOR_GET("text_editor/appearance/lines/word_wrap").operator int());123code_edit->set_autowrap_mode((TextServer::AutowrapMode)EDITOR_GET("text_editor/appearance/lines/autowrap_mode").operator int());124125// Appearance: Whitespace126code_edit->set_draw_tabs(EDITOR_GET("text_editor/appearance/whitespace/draw_tabs"));127code_edit->set_draw_spaces(EDITOR_GET("text_editor/appearance/whitespace/draw_spaces"));128code_edit->add_theme_constant_override("line_spacing", EDITOR_GET("text_editor/appearance/whitespace/line_spacing"));129130// Behavior: Navigation131code_edit->set_scroll_past_end_of_file_enabled(EDITOR_GET("text_editor/behavior/navigation/scroll_past_end_of_file"));132code_edit->set_smooth_scroll_enabled(EDITOR_GET("text_editor/behavior/navigation/smooth_scrolling"));133code_edit->set_v_scroll_speed(EDITOR_GET("text_editor/behavior/navigation/v_scroll_speed"));134code_edit->set_drag_and_drop_selection_enabled(EDITOR_GET("text_editor/behavior/navigation/drag_and_drop_selection"));135136// Behavior: Indent137code_edit->set_indent_size(EDITOR_GET("text_editor/behavior/indent/size"));138code_edit->set_auto_indent_enabled(EDITOR_GET("text_editor/behavior/indent/auto_indent"));139code_edit->set_indent_wrapped_lines(EDITOR_GET("text_editor/behavior/indent/indent_wrapped_lines"));140141code_edit->set_name(nsc.versions[i].stages[j].name);142code_edit->set_text(nsc.versions[i].stages[j].code);143code_edit->set_v_size_flags(Control::SIZE_EXPAND_FILL);144code_edit->set_h_size_flags(Control::SIZE_EXPAND_FILL);145vtab->add_child(code_edit);146}147}148add_child(versions);149popup_centered_ratio();150}151152void EditorNativeShaderSourceVisualizer::_bind_methods() {153ClassDB::bind_method("_inspect_shader", &EditorNativeShaderSourceVisualizer::_inspect_shader);154}155156EditorNativeShaderSourceVisualizer::EditorNativeShaderSourceVisualizer() {157syntax_highlighter.instantiate();158159add_to_group("_native_shader_source_visualizer");160set_title(TTR("Native Shader Source Inspector"));161}162163164