shader_editor_plugin.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. /*************************************************************************/
  2. /* shader_editor_plugin.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  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 SHADER_EDITOR_PLUGIN_H
  31. #define SHADER_EDITOR_PLUGIN_H
  32. #include "editor/code_editor.h"
  33. #include "editor/editor_plugin.h"
  34. #include "scene/gui/menu_button.h"
  35. #include "scene/gui/panel_container.h"
  36. #include "scene/gui/rich_text_label.h"
  37. #include "scene/gui/tab_container.h"
  38. #include "scene/gui/text_edit.h"
  39. #include "scene/main/timer.h"
  40. #include "scene/resources/shader.h"
  41. #include "scene/resources/shader_include.h"
  42. #include "servers/rendering/shader_warnings.h"
  43. class ItemList;
  44. class VisualShaderEditor;
  45. class HSplitContainer;
  46. class ShaderCreateDialog;
  47. class GDShaderSyntaxHighlighter : public CodeHighlighter {
  48. GDCLASS(GDShaderSyntaxHighlighter, CodeHighlighter)
  49. private:
  50. Vector<Point2i> disabled_branch_regions;
  51. Color disabled_branch_color;
  52. public:
  53. virtual Dictionary _get_line_syntax_highlighting_impl(int p_line) override;
  54. void add_disabled_branch_region(const Point2i &p_region);
  55. void clear_disabled_branch_regions();
  56. void set_disabled_branch_color(const Color &p_color);
  57. };
  58. class ShaderTextEditor : public CodeTextEditor {
  59. GDCLASS(ShaderTextEditor, CodeTextEditor);
  60. Color marked_line_color = Color(1, 1, 1);
  61. struct WarningsComparator {
  62. _ALWAYS_INLINE_ bool operator()(const ShaderWarning &p_a, const ShaderWarning &p_b) const { return (p_a.get_line() < p_b.get_line()); }
  63. };
  64. Ref<GDShaderSyntaxHighlighter> syntax_highlighter;
  65. RichTextLabel *warnings_panel = nullptr;
  66. Ref<Shader> shader;
  67. Ref<ShaderInclude> shader_inc;
  68. List<ShaderWarning> warnings;
  69. Error last_compile_result = Error::OK;
  70. void _check_shader_mode();
  71. void _update_warning_panel();
  72. bool block_shader_changed = false;
  73. void _shader_changed();
  74. uint32_t dependencies_version = 0; // Incremented if deps changed
  75. protected:
  76. void _notification(int p_what);
  77. static void _bind_methods();
  78. virtual void _load_theme_settings() override;
  79. virtual void _code_complete_script(const String &p_code, List<ScriptLanguage::CodeCompletionOption> *r_options) override;
  80. public:
  81. void set_block_shader_changed(bool p_block) { block_shader_changed = p_block; }
  82. uint32_t get_dependencies_version() const { return dependencies_version; }
  83. virtual void _validate_script() override;
  84. void reload_text();
  85. void set_warnings_panel(RichTextLabel *p_warnings_panel);
  86. Ref<Shader> get_edited_shader() const;
  87. Ref<ShaderInclude> get_edited_shader_include() const;
  88. void set_edited_shader(const Ref<Shader> &p_shader);
  89. void set_edited_shader(const Ref<Shader> &p_shader, const String &p_code);
  90. void set_edited_shader_include(const Ref<ShaderInclude> &p_include);
  91. void set_edited_shader_include(const Ref<ShaderInclude> &p_include, const String &p_code);
  92. void set_edited_code(const String &p_code);
  93. ShaderTextEditor();
  94. };
  95. class ShaderEditor : public PanelContainer {
  96. GDCLASS(ShaderEditor, PanelContainer);
  97. enum {
  98. EDIT_UNDO,
  99. EDIT_REDO,
  100. EDIT_CUT,
  101. EDIT_COPY,
  102. EDIT_PASTE,
  103. EDIT_SELECT_ALL,
  104. EDIT_MOVE_LINE_UP,
  105. EDIT_MOVE_LINE_DOWN,
  106. EDIT_INDENT_LEFT,
  107. EDIT_INDENT_RIGHT,
  108. EDIT_DELETE_LINE,
  109. EDIT_DUPLICATE_SELECTION,
  110. EDIT_TOGGLE_COMMENT,
  111. EDIT_COMPLETE,
  112. SEARCH_FIND,
  113. SEARCH_FIND_NEXT,
  114. SEARCH_FIND_PREV,
  115. SEARCH_REPLACE,
  116. SEARCH_GOTO_LINE,
  117. BOOKMARK_TOGGLE,
  118. BOOKMARK_GOTO_NEXT,
  119. BOOKMARK_GOTO_PREV,
  120. BOOKMARK_REMOVE_ALL,
  121. HELP_DOCS,
  122. };
  123. MenuButton *edit_menu = nullptr;
  124. MenuButton *search_menu = nullptr;
  125. PopupMenu *bookmarks_menu = nullptr;
  126. MenuButton *help_menu = nullptr;
  127. PopupMenu *context_menu = nullptr;
  128. RichTextLabel *warnings_panel = nullptr;
  129. uint64_t idle = 0;
  130. GotoLineDialog *goto_line_dialog = nullptr;
  131. ConfirmationDialog *erase_tab_confirm = nullptr;
  132. ConfirmationDialog *disk_changed = nullptr;
  133. ShaderTextEditor *shader_editor = nullptr;
  134. bool compilation_success = true;
  135. void _menu_option(int p_option);
  136. mutable Ref<Shader> shader;
  137. mutable Ref<ShaderInclude> shader_inc;
  138. void _editor_settings_changed();
  139. void _project_settings_changed();
  140. void _check_for_external_edit();
  141. void _reload_shader_from_disk();
  142. void _reload_shader_include_from_disk();
  143. void _reload();
  144. void _show_warnings_panel(bool p_show);
  145. void _warning_clicked(Variant p_line);
  146. void _update_warnings(bool p_validate);
  147. void _script_validated(bool p_valid) {
  148. compilation_success = p_valid;
  149. emit_signal(SNAME("validation_changed"));
  150. }
  151. uint32_t dependencies_version = 0xFFFFFFFF;
  152. protected:
  153. void _notification(int p_what);
  154. static void _bind_methods();
  155. void _make_context_menu(bool p_selection, Vector2 p_position);
  156. void _text_edit_gui_input(const Ref<InputEvent> &p_ev);
  157. void _update_bookmark_list();
  158. void _bookmark_item_pressed(int p_idx);
  159. public:
  160. bool was_compilation_successful() const { return compilation_success; }
  161. void apply_shaders();
  162. void ensure_select_current();
  163. void edit(const Ref<Shader> &p_shader);
  164. void edit(const Ref<ShaderInclude> &p_shader_inc);
  165. void goto_line_selection(int p_line, int p_begin, int p_end);
  166. void save_external_data(const String &p_str = "");
  167. void validate_script();
  168. bool is_unsaved() const;
  169. virtual Size2 get_minimum_size() const override { return Size2(0, 200); }
  170. ShaderEditor();
  171. };
  172. class ShaderEditorPlugin : public EditorPlugin {
  173. GDCLASS(ShaderEditorPlugin, EditorPlugin);
  174. struct EditedShader {
  175. Ref<Shader> shader;
  176. Ref<ShaderInclude> shader_inc;
  177. ShaderEditor *shader_editor = nullptr;
  178. VisualShaderEditor *visual_shader_editor = nullptr;
  179. };
  180. LocalVector<EditedShader> edited_shaders;
  181. enum {
  182. FILE_NEW,
  183. FILE_NEW_INCLUDE,
  184. FILE_OPEN,
  185. FILE_OPEN_INCLUDE,
  186. FILE_SAVE,
  187. FILE_SAVE_AS,
  188. FILE_INSPECT,
  189. FILE_CLOSE,
  190. FILE_MAX
  191. };
  192. HSplitContainer *main_split = nullptr;
  193. ItemList *shader_list = nullptr;
  194. TabContainer *shader_tabs = nullptr;
  195. Button *button = nullptr;
  196. MenuButton *file_menu = nullptr;
  197. ShaderCreateDialog *shader_create_dialog = nullptr;
  198. void _update_shader_list();
  199. void _shader_selected(int p_index);
  200. void _shader_list_clicked(int p_item, Vector2 p_local_mouse_pos, MouseButton p_mouse_button_index);
  201. void _menu_item_pressed(int p_index);
  202. void _resource_saved(Object *obj);
  203. void _close_shader(int p_index);
  204. void _shader_created(Ref<Shader> p_shader);
  205. void _shader_include_created(Ref<ShaderInclude> p_shader_inc);
  206. void _update_shader_list_status();
  207. void _move_shader_tab(int p_from, int p_to);
  208. Variant get_drag_data_fw(const Point2 &p_point, Control *p_from);
  209. bool can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const;
  210. void drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from);
  211. protected:
  212. static void _bind_methods();
  213. public:
  214. virtual void edit(Object *p_object) override;
  215. virtual bool handles(Object *p_object) const override;
  216. virtual void make_visible(bool p_visible) override;
  217. virtual void selected_notify() override;
  218. ShaderEditor *get_shader_editor(const Ref<Shader> &p_for_shader);
  219. VisualShaderEditor *get_visual_shader_editor(const Ref<Shader> &p_for_shader);
  220. virtual void save_external_data() override;
  221. virtual void apply_changes() override;
  222. ShaderEditorPlugin();
  223. ~ShaderEditorPlugin();
  224. };
  225. #endif // SHADER_EDITOR_PLUGIN_H