Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/editor/shader/editor_native_shader_source_visualizer.cpp
9903 views
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_tab_alignment(TabBar::ALIGNMENT_CENTER);
86
versions->set_v_size_flags(Control::SIZE_EXPAND_FILL);
87
versions->set_h_size_flags(Control::SIZE_EXPAND_FILL);
88
for (int i = 0; i < nsc.versions.size(); i++) {
89
TabContainer *vtab = memnew(TabContainer);
90
vtab->set_name("Version " + itos(i));
91
vtab->set_tab_alignment(TabBar::ALIGNMENT_CENTER);
92
vtab->set_v_size_flags(Control::SIZE_EXPAND_FILL);
93
vtab->set_h_size_flags(Control::SIZE_EXPAND_FILL);
94
versions->add_child(vtab);
95
for (int j = 0; j < nsc.versions[i].stages.size(); j++) {
96
CodeEdit *code_edit = memnew(CodeEdit);
97
code_edit->set_editable(false);
98
code_edit->set_syntax_highlighter(syntax_highlighter);
99
code_edit->add_theme_font_override(SceneStringName(font), get_theme_font("source", EditorStringName(EditorFonts)));
100
code_edit->add_theme_font_size_override(SceneStringName(font_size), get_theme_font_size("source_size", EditorStringName(EditorFonts)));
101
code_edit->add_theme_constant_override("line_spacing", EDITOR_GET("text_editor/appearance/whitespace/line_spacing"));
102
103
// Appearance: Caret
104
code_edit->set_caret_type((TextEdit::CaretType)EDITOR_GET("text_editor/appearance/caret/type").operator int());
105
code_edit->set_caret_blink_enabled(EDITOR_GET("text_editor/appearance/caret/caret_blink"));
106
code_edit->set_caret_blink_interval(EDITOR_GET("text_editor/appearance/caret/caret_blink_interval"));
107
code_edit->set_highlight_current_line(EDITOR_GET("text_editor/appearance/caret/highlight_current_line"));
108
code_edit->set_highlight_all_occurrences(EDITOR_GET("text_editor/appearance/caret/highlight_all_occurrences"));
109
110
// Appearance: Gutters
111
code_edit->set_draw_line_numbers(EDITOR_GET("text_editor/appearance/gutters/show_line_numbers"));
112
code_edit->set_line_numbers_zero_padded(EDITOR_GET("text_editor/appearance/gutters/line_numbers_zero_padded"));
113
114
// Appearance: Minimap
115
code_edit->set_draw_minimap(EDITOR_GET("text_editor/appearance/minimap/show_minimap"));
116
code_edit->set_minimap_width((int)EDITOR_GET("text_editor/appearance/minimap/minimap_width") * EDSCALE);
117
118
// Appearance: Lines
119
code_edit->set_line_folding_enabled(EDITOR_GET("text_editor/appearance/lines/code_folding"));
120
code_edit->set_draw_fold_gutter(EDITOR_GET("text_editor/appearance/lines/code_folding"));
121
code_edit->set_line_wrapping_mode((TextEdit::LineWrappingMode)EDITOR_GET("text_editor/appearance/lines/word_wrap").operator int());
122
code_edit->set_autowrap_mode((TextServer::AutowrapMode)EDITOR_GET("text_editor/appearance/lines/autowrap_mode").operator int());
123
124
// Appearance: Whitespace
125
code_edit->set_draw_tabs(EDITOR_GET("text_editor/appearance/whitespace/draw_tabs"));
126
code_edit->set_draw_spaces(EDITOR_GET("text_editor/appearance/whitespace/draw_spaces"));
127
code_edit->add_theme_constant_override("line_spacing", EDITOR_GET("text_editor/appearance/whitespace/line_spacing"));
128
129
// Behavior: Navigation
130
code_edit->set_scroll_past_end_of_file_enabled(EDITOR_GET("text_editor/behavior/navigation/scroll_past_end_of_file"));
131
code_edit->set_smooth_scroll_enabled(EDITOR_GET("text_editor/behavior/navigation/smooth_scrolling"));
132
code_edit->set_v_scroll_speed(EDITOR_GET("text_editor/behavior/navigation/v_scroll_speed"));
133
code_edit->set_drag_and_drop_selection_enabled(EDITOR_GET("text_editor/behavior/navigation/drag_and_drop_selection"));
134
135
// Behavior: Indent
136
code_edit->set_indent_size(EDITOR_GET("text_editor/behavior/indent/size"));
137
code_edit->set_auto_indent_enabled(EDITOR_GET("text_editor/behavior/indent/auto_indent"));
138
code_edit->set_indent_wrapped_lines(EDITOR_GET("text_editor/behavior/indent/indent_wrapped_lines"));
139
140
code_edit->set_name(nsc.versions[i].stages[j].name);
141
code_edit->set_text(nsc.versions[i].stages[j].code);
142
code_edit->set_v_size_flags(Control::SIZE_EXPAND_FILL);
143
code_edit->set_h_size_flags(Control::SIZE_EXPAND_FILL);
144
vtab->add_child(code_edit);
145
}
146
}
147
add_child(versions);
148
popup_centered_ratio();
149
}
150
151
void EditorNativeShaderSourceVisualizer::_bind_methods() {
152
ClassDB::bind_method("_inspect_shader", &EditorNativeShaderSourceVisualizer::_inspect_shader);
153
}
154
155
EditorNativeShaderSourceVisualizer::EditorNativeShaderSourceVisualizer() {
156
syntax_highlighter.instantiate();
157
158
add_to_group("_native_shader_source_visualizer");
159
set_title(TTR("Native Shader Source Inspector"));
160
}
161
162