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-2019 Juan Linietsky, Ariel Manzur.                 */
9 /* Copyright (c) 2014-2019 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 
33 #include "editor/code_editor.h"
34 #include "editor/editor_plugin.h"
35 #include "scene/gui/menu_button.h"
36 #include "scene/gui/tab_container.h"
37 #include "scene/gui/text_edit.h"
38 #include "scene/main/timer.h"
39 #include "scene/resources/shader.h"
40 #include "servers/visual/shader_language.h"
41 
42 class ShaderTextEditor : public CodeTextEditor {
43 
44 	OBJ_TYPE(ShaderTextEditor, CodeTextEditor);
45 
46 	Ref<Shader> shader;
47 	ShaderLanguage::ShaderType type;
48 
49 protected:
50 	static void _bind_methods();
51 	virtual void _load_theme_settings();
52 
53 public:
54 	virtual void _validate_script();
55 
56 	Ref<Shader> get_edited_shader() const;
57 	void set_edited_shader(const Ref<Shader> &p_shader, ShaderLanguage::ShaderType p_type);
58 	ShaderTextEditor();
59 };
60 
61 class ShaderEditor : public Control {
62 
63 	OBJ_TYPE(ShaderEditor, Control);
64 
65 	enum {
66 
67 		EDIT_UNDO,
68 		EDIT_REDO,
69 		EDIT_CUT,
70 		EDIT_COPY,
71 		EDIT_PASTE,
72 		EDIT_SELECT_ALL,
73 		SEARCH_FIND,
74 		SEARCH_FIND_NEXT,
75 		SEARCH_FIND_PREV,
76 		SEARCH_REPLACE,
77 		//SEARCH_LOCATE_SYMBOL,
78 		SEARCH_GOTO_LINE,
79 
80 	};
81 
82 	MenuButton *edit_menu;
83 	MenuButton *search_menu;
84 	MenuButton *settings_menu;
85 	uint64_t idle;
86 
87 	TabContainer *tab_container;
88 	GotoLineDialog *goto_line_dialog;
89 	ConfirmationDialog *erase_tab_confirm;
90 
91 	TextureButton *close;
92 
93 	ShaderTextEditor *vertex_editor;
94 	ShaderTextEditor *fragment_editor;
95 	ShaderTextEditor *light_editor;
96 
97 	void _tab_changed(int p_which);
98 	void _menu_option(int p_optin);
99 	void _params_changed();
100 	mutable Ref<Shader> shader;
101 
102 	void _close_callback();
103 
104 	void _editor_settings_changed();
105 
106 protected:
107 	void _notification(int p_what);
108 	static void _bind_methods();
109 
110 public:
111 	void apply_shaders();
112 
113 	void ensure_select_current();
114 	void edit(const Ref<Shader> &p_shader);
115 
116 	Dictionary get_state() const;
117 	void set_state(const Dictionary &p_state);
118 	void clear();
119 
get_minimum_size()120 	virtual Size2 get_minimum_size() const { return Size2(0, 200); }
121 	void save_external_data();
122 
123 	ShaderEditor();
124 };
125 
126 class ShaderEditorPlugin : public EditorPlugin {
127 
128 	OBJ_TYPE(ShaderEditorPlugin, EditorPlugin);
129 
130 	bool _2d;
131 	ShaderEditor *shader_editor;
132 	EditorNode *editor;
133 
134 public:
get_name()135 	virtual String get_name() const { return "Shader"; }
has_main_screen()136 	bool has_main_screen() const { return false; }
137 	virtual void edit(Object *p_node);
138 	virtual bool handles(Object *p_node) const;
139 	virtual void make_visible(bool p_visible);
140 	virtual void selected_notify();
141 
142 	Dictionary get_state() const;
143 	virtual void set_state(const Dictionary &p_state);
144 	virtual void clear();
145 
146 	virtual void save_external_data();
147 	virtual void apply_changes();
148 
149 	ShaderEditorPlugin(EditorNode *p_node, bool p_2d);
150 	~ShaderEditorPlugin();
151 };
152 #endif
153