editor_native_shader_source_visualizer.cpp 8.6 KB

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