text_shader_editor.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /**************************************************************************/
  2. /* text_shader_editor.h */
  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. #ifndef TEXT_SHADER_EDITOR_H
  31. #define TEXT_SHADER_EDITOR_H
  32. #include "editor/code_editor.h"
  33. #include "editor/plugins/shader/shader_editor.h"
  34. #include "scene/gui/margin_container.h"
  35. #include "scene/gui/menu_button.h"
  36. #include "scene/gui/rich_text_label.h"
  37. #include "servers/rendering/shader_warnings.h"
  38. class GDShaderSyntaxHighlighter : public CodeHighlighter {
  39. GDCLASS(GDShaderSyntaxHighlighter, CodeHighlighter)
  40. private:
  41. Vector<Point2i> disabled_branch_regions;
  42. Color disabled_branch_color;
  43. public:
  44. virtual Dictionary _get_line_syntax_highlighting_impl(int p_line) override;
  45. void add_disabled_branch_region(const Point2i &p_region);
  46. void clear_disabled_branch_regions();
  47. void set_disabled_branch_color(const Color &p_color);
  48. };
  49. class ShaderTextEditor : public CodeTextEditor {
  50. GDCLASS(ShaderTextEditor, CodeTextEditor);
  51. Color marked_line_color = Color(1, 1, 1);
  52. struct WarningsComparator {
  53. _ALWAYS_INLINE_ bool operator()(const ShaderWarning &p_a, const ShaderWarning &p_b) const { return (p_a.get_line() < p_b.get_line()); }
  54. };
  55. Ref<GDShaderSyntaxHighlighter> syntax_highlighter;
  56. RichTextLabel *warnings_panel = nullptr;
  57. Ref<Shader> shader;
  58. Ref<ShaderInclude> shader_inc;
  59. List<ShaderWarning> warnings;
  60. Error last_compile_result = Error::OK;
  61. void _check_shader_mode();
  62. void _update_warning_panel();
  63. bool block_shader_changed = false;
  64. void _shader_changed();
  65. uint32_t dependencies_version = 0; // Incremented if deps changed
  66. protected:
  67. void _notification(int p_what);
  68. static void _bind_methods();
  69. virtual void _load_theme_settings() override;
  70. virtual void _code_complete_script(const String &p_code, List<ScriptLanguage::CodeCompletionOption> *r_options) override;
  71. public:
  72. void set_block_shader_changed(bool p_block) { block_shader_changed = p_block; }
  73. uint32_t get_dependencies_version() const { return dependencies_version; }
  74. virtual void _validate_script() override;
  75. void reload_text();
  76. void set_warnings_panel(RichTextLabel *p_warnings_panel);
  77. Ref<Shader> get_edited_shader() const;
  78. Ref<ShaderInclude> get_edited_shader_include() const;
  79. void set_edited_shader(const Ref<Shader> &p_shader);
  80. void set_edited_shader(const Ref<Shader> &p_shader, const String &p_code);
  81. void set_edited_shader_include(const Ref<ShaderInclude> &p_include);
  82. void set_edited_shader_include(const Ref<ShaderInclude> &p_include, const String &p_code);
  83. void set_edited_code(const String &p_code);
  84. ShaderTextEditor();
  85. };
  86. class TextShaderEditor : public ShaderEditor {
  87. GDCLASS(TextShaderEditor, ShaderEditor);
  88. enum {
  89. EDIT_UNDO,
  90. EDIT_REDO,
  91. EDIT_CUT,
  92. EDIT_COPY,
  93. EDIT_PASTE,
  94. EDIT_SELECT_ALL,
  95. EDIT_MOVE_LINE_UP,
  96. EDIT_MOVE_LINE_DOWN,
  97. EDIT_INDENT,
  98. EDIT_UNINDENT,
  99. EDIT_DELETE_LINE,
  100. EDIT_DUPLICATE_SELECTION,
  101. EDIT_DUPLICATE_LINES,
  102. EDIT_TOGGLE_WORD_WRAP,
  103. EDIT_TOGGLE_COMMENT,
  104. EDIT_COMPLETE,
  105. SEARCH_FIND,
  106. SEARCH_FIND_NEXT,
  107. SEARCH_FIND_PREV,
  108. SEARCH_REPLACE,
  109. SEARCH_GOTO_LINE,
  110. BOOKMARK_TOGGLE,
  111. BOOKMARK_GOTO_NEXT,
  112. BOOKMARK_GOTO_PREV,
  113. BOOKMARK_REMOVE_ALL,
  114. HELP_DOCS,
  115. };
  116. MenuButton *edit_menu = nullptr;
  117. MenuButton *search_menu = nullptr;
  118. PopupMenu *bookmarks_menu = nullptr;
  119. MenuButton *help_menu = nullptr;
  120. PopupMenu *context_menu = nullptr;
  121. RichTextLabel *warnings_panel = nullptr;
  122. uint64_t idle = 0;
  123. GotoLineDialog *goto_line_dialog = nullptr;
  124. ConfirmationDialog *erase_tab_confirm = nullptr;
  125. ConfirmationDialog *disk_changed = nullptr;
  126. ShaderTextEditor *code_editor = nullptr;
  127. bool compilation_success = true;
  128. void _menu_option(int p_option);
  129. void _prepare_edit_menu();
  130. mutable Ref<Shader> shader;
  131. mutable Ref<ShaderInclude> shader_inc;
  132. void _editor_settings_changed();
  133. void _apply_editor_settings();
  134. void _project_settings_changed();
  135. void _check_for_external_edit();
  136. void _reload_shader_from_disk();
  137. void _reload_shader_include_from_disk();
  138. void _reload();
  139. void _show_warnings_panel(bool p_show);
  140. void _warning_clicked(const Variant &p_line);
  141. void _update_warnings(bool p_validate);
  142. void _script_validated(bool p_valid) {
  143. compilation_success = p_valid;
  144. emit_signal(SNAME("validation_changed"));
  145. }
  146. uint32_t dependencies_version = 0xFFFFFFFF;
  147. bool trim_trailing_whitespace_on_save;
  148. bool trim_final_newlines_on_save;
  149. protected:
  150. void _notification(int p_what);
  151. static void _bind_methods();
  152. void _make_context_menu(bool p_selection, Vector2 p_position);
  153. void _text_edit_gui_input(const Ref<InputEvent> &p_ev);
  154. void _update_bookmark_list();
  155. void _bookmark_item_pressed(int p_idx);
  156. public:
  157. virtual void edit_shader(const Ref<Shader> &p_shader) override;
  158. virtual void edit_shader_include(const Ref<ShaderInclude> &p_shader_inc) override;
  159. virtual void apply_shaders() override;
  160. virtual bool is_unsaved() const override;
  161. virtual void save_external_data(const String &p_str = "") override;
  162. virtual void validate_script() override;
  163. bool was_compilation_successful() const { return compilation_success; }
  164. bool get_trim_trailing_whitespace_on_save() const { return trim_trailing_whitespace_on_save; }
  165. bool get_trim_final_newlines_on_save() const { return trim_final_newlines_on_save; }
  166. void ensure_select_current();
  167. void goto_line_selection(int p_line, int p_begin, int p_end);
  168. void trim_trailing_whitespace();
  169. void trim_final_newlines();
  170. void tag_saved_version();
  171. ShaderTextEditor *get_code_editor() { return code_editor; }
  172. virtual Size2 get_minimum_size() const override { return Size2(0, 200); }
  173. TextShaderEditor();
  174. };
  175. #endif // TEXT_SHADER_EDITOR_H