1 /*************************************************************************/
2 /*  shader_editor_plugin.cpp                                             */
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 #include "shader_editor_plugin.h"
31 #include "editor/editor_settings.h"
32 
33 #include "editor/editor_node.h"
34 #include "editor/property_editor.h"
35 #include "io/resource_loader.h"
36 #include "io/resource_saver.h"
37 #include "os/keyboard.h"
38 #include "os/os.h"
39 #include "scene/resources/shader_graph.h"
40 #include "spatial_editor_plugin.h"
41 
42 /*** SETTINGS EDITOR ****/
43 
44 /*** SCRIPT EDITOR ****/
45 
get_edited_shader() const46 Ref<Shader> ShaderTextEditor::get_edited_shader() const {
47 
48 	return shader;
49 }
set_edited_shader(const Ref<Shader> & p_shader,ShaderLanguage::ShaderType p_type)50 void ShaderTextEditor::set_edited_shader(const Ref<Shader> &p_shader, ShaderLanguage::ShaderType p_type) {
51 
52 	shader = p_shader;
53 	type = p_type;
54 
55 	_load_theme_settings();
56 
57 	if (p_type == ShaderLanguage::SHADER_MATERIAL_LIGHT || p_type == ShaderLanguage::SHADER_CANVAS_ITEM_LIGHT)
58 		get_text_edit()->set_text(shader->get_light_code());
59 	else if (p_type == ShaderLanguage::SHADER_MATERIAL_VERTEX || p_type == ShaderLanguage::SHADER_CANVAS_ITEM_VERTEX)
60 		get_text_edit()->set_text(shader->get_vertex_code());
61 	else
62 		get_text_edit()->set_text(shader->get_fragment_code());
63 
64 	_line_col_changed();
65 }
66 
_load_theme_settings()67 void ShaderTextEditor::_load_theme_settings() {
68 
69 	get_text_edit()->clear_colors();
70 
71 	/* keyword color */
72 
73 	get_text_edit()->set_custom_bg_color(EDITOR_DEF("text_editor/background_color", Color(0, 0, 0, 0)));
74 	get_text_edit()->add_color_override("completion_background_color", EDITOR_DEF("text_editor/completion_background_color", Color(0, 0, 0, 0)));
75 	get_text_edit()->add_color_override("completion_selected_color", EDITOR_DEF("text_editor/completion_selected_color", Color::html("434244")));
76 	get_text_edit()->add_color_override("completion_existing_color", EDITOR_DEF("text_editor/completion_existing_color", Color::html("21dfdfdf")));
77 	get_text_edit()->add_color_override("completion_scroll_color", EDITOR_DEF("text_editor/completion_scroll_color", Color::html("ffffff")));
78 	get_text_edit()->add_color_override("completion_font_color", EDITOR_DEF("text_editor/completion_font_color", Color::html("aaaaaa")));
79 	get_text_edit()->add_color_override("font_color", EDITOR_DEF("text_editor/text_color", Color(0, 0, 0)));
80 	get_text_edit()->add_color_override("line_number_color", EDITOR_DEF("text_editor/line_number_color", Color(0, 0, 0)));
81 	get_text_edit()->add_color_override("caret_color", EDITOR_DEF("text_editor/caret_color", Color(0, 0, 0)));
82 	get_text_edit()->add_color_override("caret_background_color", EDITOR_DEF("text_editor/caret_background_color", Color(0, 0, 0)));
83 	get_text_edit()->add_color_override("font_selected_color", EDITOR_DEF("text_editor/text_selected_color", Color(1, 1, 1)));
84 	get_text_edit()->add_color_override("selection_color", EDITOR_DEF("text_editor/selection_color", Color(0.2, 0.2, 1)));
85 	get_text_edit()->add_color_override("brace_mismatch_color", EDITOR_DEF("text_editor/brace_mismatch_color", Color(1, 0.2, 0.2)));
86 	get_text_edit()->add_color_override("current_line_color", EDITOR_DEF("text_editor/current_line_color", Color(0.3, 0.5, 0.8, 0.15)));
87 	get_text_edit()->add_color_override("word_highlighted_color", EDITOR_DEF("text_editor/word_highlighted_color", Color(0.8, 0.9, 0.9, 0.15)));
88 	get_text_edit()->add_color_override("number_color", EDITOR_DEF("text_editor/number_color", Color(0.9, 0.6, 0.0, 2)));
89 	get_text_edit()->add_color_override("function_color", EDITOR_DEF("text_editor/function_color", Color(0.4, 0.6, 0.8)));
90 	get_text_edit()->add_color_override("member_variable_color", EDITOR_DEF("text_editor/member_variable_color", Color(0.9, 0.3, 0.3)));
91 	get_text_edit()->add_color_override("mark_color", EDITOR_DEF("text_editor/mark_color", Color(1.0, 0.4, 0.4, 0.4)));
92 	get_text_edit()->add_color_override("breakpoint_color", EDITOR_DEF("text_editor/breakpoint_color", Color(0.8, 0.8, 0.4, 0.2)));
93 	get_text_edit()->add_color_override("search_result_color", EDITOR_DEF("text_editor/search_result_color", Color(0.05, 0.25, 0.05, 1)));
94 	get_text_edit()->add_color_override("search_result_border_color", EDITOR_DEF("text_editor/search_result_border_color", Color(0.1, 0.45, 0.1, 1)));
95 
96 	Color keyword_color = EDITOR_DEF("text_editor/keyword_color", Color(0.5, 0.0, 0.2));
97 
98 	List<String> keywords;
99 	ShaderLanguage::get_keyword_list(type, &keywords);
100 
101 	for (List<String>::Element *E = keywords.front(); E; E = E->next()) {
102 
103 		get_text_edit()->add_keyword_color(E->get(), keyword_color);
104 	}
105 
106 	//colorize core types
107 	//	Color basetype_color= EDITOR_DEF("text_editor/base_type_color",Color(0.3,0.3,0.0));
108 
109 	//colorize comments
110 	Color comment_color = EDITOR_DEF("text_editor/comment_color", Color::hex(0x797e7eff));
111 
112 	get_text_edit()->add_color_region("/*", "*/", comment_color, false);
113 	get_text_edit()->add_color_region("//", "", comment_color, false);
114 
115 	/*//colorize strings
116 	Color string_color = EDITOR_DEF("text_editor/string_color",Color::hex(0x6b6f00ff));
117 
118 	List<String> strings;
119 	shader->get_shader_mode()->get_string_delimiters(&strings);
120 
121 	for (List<String>::Element *E=strings.front();E;E=E->next()) {
122 
123 		String string = E->get();
124 		String beg = string.get_slice(" ",0);
125 		String end = string.get_slice_count(" ")>1?string.get_slice(" ",1):String();
126 		get_text_edit()->add_color_region(beg,end,string_color,end=="");
127 	}*/
128 
129 	//colorize symbols
130 	Color symbol_color = EDITOR_DEF("text_editor/symbol_color", Color::hex(0x005291ff));
131 	get_text_edit()->set_symbol_color(symbol_color);
132 }
133 
_validate_script()134 void ShaderTextEditor::_validate_script() {
135 
136 	String errortxt;
137 	int line, col;
138 
139 	String code = get_text_edit()->get_text();
140 	//List<StringName> params;
141 	//shader->get_param_list(&params);
142 
143 	Error err = ShaderLanguage::compile(code, type, NULL, NULL, &errortxt, &line, &col);
144 
145 	if (err != OK) {
146 		String error_text = "error(" + itos(line + 1) + "," + itos(col + 1) + "): " + errortxt;
147 		set_error(error_text);
148 		get_text_edit()->set_line_as_marked(line, true);
149 
150 	} else {
151 		for (int i = 0; i < get_text_edit()->get_line_count(); i++)
152 			get_text_edit()->set_line_as_marked(i, false);
153 		set_error("");
154 	}
155 
156 	emit_signal("script_changed");
157 }
158 
_bind_methods()159 void ShaderTextEditor::_bind_methods() {
160 
161 	//ADD_SIGNAL( MethodInfo("script_changed") );
162 }
163 
ShaderTextEditor()164 ShaderTextEditor::ShaderTextEditor() {
165 }
166 
167 /*** SCRIPT EDITOR ******/
168 
_menu_option(int p_option)169 void ShaderEditor::_menu_option(int p_option) {
170 
171 	ShaderTextEditor *current = tab_container->get_current_tab_control()->cast_to<ShaderTextEditor>();
172 	if (!current)
173 		return;
174 
175 	switch (p_option) {
176 		case EDIT_UNDO: {
177 
178 			current->get_text_edit()->undo();
179 		} break;
180 		case EDIT_REDO: {
181 			current->get_text_edit()->redo();
182 
183 		} break;
184 		case EDIT_CUT: {
185 
186 			current->get_text_edit()->cut();
187 		} break;
188 		case EDIT_COPY: {
189 			current->get_text_edit()->copy();
190 
191 		} break;
192 		case EDIT_PASTE: {
193 			current->get_text_edit()->paste();
194 
195 		} break;
196 		case EDIT_SELECT_ALL: {
197 
198 			current->get_text_edit()->select_all();
199 
200 		} break;
201 		case SEARCH_FIND: {
202 
203 			current->get_find_replace_bar()->popup_search();
204 		} break;
205 		case SEARCH_FIND_NEXT: {
206 
207 			current->get_find_replace_bar()->search_next();
208 		} break;
209 		case SEARCH_FIND_PREV: {
210 
211 			current->get_find_replace_bar()->search_prev();
212 		} break;
213 		case SEARCH_REPLACE: {
214 
215 			current->get_find_replace_bar()->popup_replace();
216 		} break;
217 		//		case SEARCH_LOCATE_SYMBOL: {
218 
219 		//		} break;
220 		case SEARCH_GOTO_LINE: {
221 
222 			goto_line_dialog->popup_find_line(current->get_text_edit());
223 		} break;
224 	}
225 }
226 
_tab_changed(int p_which)227 void ShaderEditor::_tab_changed(int p_which) {
228 
229 	ShaderTextEditor *shader_editor = tab_container->get_tab_control(p_which)->cast_to<ShaderTextEditor>();
230 
231 	if (shader_editor && is_inside_tree())
232 		shader_editor->get_text_edit()->grab_focus();
233 
234 	ensure_select_current();
235 }
236 
_notification(int p_what)237 void ShaderEditor::_notification(int p_what) {
238 
239 	if (p_what == NOTIFICATION_ENTER_TREE) {
240 
241 		close->set_normal_texture(get_icon("Close", "EditorIcons"));
242 		close->set_hover_texture(get_icon("CloseHover", "EditorIcons"));
243 		close->set_pressed_texture(get_icon("Close", "EditorIcons"));
244 		close->connect("pressed", this, "_close_callback");
245 	}
246 	if (p_what == NOTIFICATION_DRAW) {
247 
248 		RID ci = get_canvas_item();
249 		Ref<StyleBox> style = get_stylebox("panel", "Panel");
250 		style->draw(ci, Rect2(Point2(), get_size()));
251 	}
252 }
253 
get_state() const254 Dictionary ShaderEditor::get_state() const {
255 #if 0
256 	apply_shaders();
257 
258 	Dictionary state;
259 
260 	Array paths;
261 	int open=-1;
262 
263 	for(int i=0;i<tab_container->get_child_count();i++) {
264 
265 		ShaderTextEditor *ste = tab_container->get_child(i)->cast_to<ShaderTextEditor>();
266 		if (!ste)
267 			continue;
268 
269 
270 		Ref<Shader> shader = ste->get_edited_shader();
271 		if (shader->get_path()!="" && shader->get_path().find("local://")==-1 && shader->get_path().find("::")==-1) {
272 
273 			paths.push_back(shader->get_path());
274 		} else {
275 
276 
277 			const Node *owner = _find_node_with_shader(get_root_node(),shader.get_ref_ptr());
278 			if (owner)
279 				paths.push_back(owner->get_path());
280 
281 		}
282 
283 		if (i==tab_container->get_current_tab())
284 			open=i;
285 	}
286 
287 	if (paths.size())
288 		state["sources"]=paths;
289 	if (open!=-1)
290 		state["current"]=open;
291 
292 
293 	return state;
294 #endif
295 	return Dictionary();
296 }
set_state(const Dictionary & p_state)297 void ShaderEditor::set_state(const Dictionary &p_state) {
298 #if 0
299 	print_line("setting state..");
300 	if (!p_state.has("sources"))
301 		return; //bleh
302 
303 	Array sources = p_state["sources"];
304 	for(int i=0;i<sources.size();i++) {
305 
306 		Variant source=sources[i];
307 
308 		Ref<Shader> shader;
309 
310 		if (source.get_type()==Variant::NODE_PATH) {
311 
312 			print_line("cain find owner at path "+String(source));
313 			Node *owner=get_root_node()->get_node(source);
314 			if (!owner)
315 				continue;
316 
317 			shader = owner->get_shader();
318 		} else if (source.get_type()==Variant::STRING) {
319 
320 			print_line("loading at path "+String(source));
321 			shader = ResourceLoader::load(source,"Shader");
322 		}
323 
324 		print_line("found shader at "+String(source)+"? - "+itos(shader.is_null()));
325 		if (shader.is_null()) //ah well..
326 			continue;
327 
328 		get_scene()->get_root_node()->call("_resource_selected",shader);
329 	}
330 
331 	if (p_state.has("current"))
332 	tab_container->set_current_tab(p_state["current"]);
333 #endif
334 }
clear()335 void ShaderEditor::clear() {
336 }
337 
_params_changed()338 void ShaderEditor::_params_changed() {
339 
340 	fragment_editor->_validate_script();
341 	vertex_editor->_validate_script();
342 	light_editor->_validate_script();
343 }
344 
_editor_settings_changed()345 void ShaderEditor::_editor_settings_changed() {
346 
347 	vertex_editor->update_editor_settings();
348 	fragment_editor->update_editor_settings();
349 	light_editor->update_editor_settings();
350 }
351 
_bind_methods()352 void ShaderEditor::_bind_methods() {
353 
354 	ObjectTypeDB::bind_method("_editor_settings_changed", &ShaderEditor::_editor_settings_changed);
355 	ObjectTypeDB::bind_method("_tab_changed", &ShaderEditor::_tab_changed);
356 	ObjectTypeDB::bind_method("_menu_option", &ShaderEditor::_menu_option);
357 	ObjectTypeDB::bind_method("_params_changed", &ShaderEditor::_params_changed);
358 	ObjectTypeDB::bind_method("_close_callback", &ShaderEditor::_close_callback);
359 	ObjectTypeDB::bind_method("apply_shaders", &ShaderEditor::apply_shaders);
360 	//	ObjectTypeDB::bind_method("_close_current_tab",&ShaderEditor::_close_current_tab);
361 }
362 
ensure_select_current()363 void ShaderEditor::ensure_select_current() {
364 
365 	/*
366 	if (tab_container->get_child_count() && tab_container->get_current_tab()>=0) {
367 
368 		ShaderTextEditor *ste = tab_container->get_child(tab_container->get_current_tab())->cast_to<ShaderTextEditor>();
369 		if (!ste)
370 			return;
371 		Ref<Shader> shader = ste->get_edited_shader();
372 		get_scene()->get_root_node()->call("_resource_selected",shader);
373 	}*/
374 }
375 
edit(const Ref<Shader> & p_shader)376 void ShaderEditor::edit(const Ref<Shader> &p_shader) {
377 
378 	if (p_shader.is_null())
379 		return;
380 
381 	shader = p_shader;
382 
383 	if (shader->get_mode() == Shader::MODE_MATERIAL) {
384 		vertex_editor->set_edited_shader(p_shader, ShaderLanguage::SHADER_MATERIAL_VERTEX);
385 		fragment_editor->set_edited_shader(p_shader, ShaderLanguage::SHADER_MATERIAL_FRAGMENT);
386 		light_editor->set_edited_shader(shader, ShaderLanguage::SHADER_MATERIAL_LIGHT);
387 	} else if (shader->get_mode() == Shader::MODE_CANVAS_ITEM) {
388 
389 		vertex_editor->set_edited_shader(p_shader, ShaderLanguage::SHADER_CANVAS_ITEM_VERTEX);
390 		fragment_editor->set_edited_shader(p_shader, ShaderLanguage::SHADER_CANVAS_ITEM_FRAGMENT);
391 		light_editor->set_edited_shader(shader, ShaderLanguage::SHADER_CANVAS_ITEM_LIGHT);
392 	}
393 
394 	//vertex_editor->set_edited_shader(shader,ShaderLanguage::SHADER_MATERIAL_VERTEX);
395 	// see if already has it
396 }
397 
save_external_data()398 void ShaderEditor::save_external_data() {
399 
400 	if (shader.is_null())
401 		return;
402 	apply_shaders();
403 
404 	if (shader->get_path() != "" && shader->get_path().find("local://") == -1 && shader->get_path().find("::") == -1) {
405 		//external shader, save it
406 		ResourceSaver::save(shader->get_path(), shader);
407 	}
408 }
409 
apply_shaders()410 void ShaderEditor::apply_shaders() {
411 
412 	if (shader.is_valid()) {
413 		shader->set_code(vertex_editor->get_text_edit()->get_text(), fragment_editor->get_text_edit()->get_text(), light_editor->get_text_edit()->get_text(), 0, 0);
414 		shader->set_edited(true);
415 	}
416 }
417 
_close_callback()418 void ShaderEditor::_close_callback() {
419 
420 	hide();
421 }
422 
ShaderEditor()423 ShaderEditor::ShaderEditor() {
424 
425 	tab_container = memnew(TabContainer);
426 	add_child(tab_container);
427 	tab_container->set_area_as_parent_rect();
428 	tab_container->set_begin(Point2(0, 0));
429 	//tab_container->set_begin(Point2(0,0));
430 
431 	close = memnew(TextureButton);
432 	close->set_anchor_and_margin(MARGIN_LEFT, ANCHOR_END, 20);
433 	close->set_anchor_and_margin(MARGIN_RIGHT, ANCHOR_END, 4);
434 	close->set_anchor_and_margin(MARGIN_TOP, ANCHOR_BEGIN, 2);
435 	add_child(close);
436 
437 	edit_menu = memnew(MenuButton);
438 	add_child(edit_menu);
439 	edit_menu->set_pos(Point2(5, -1));
440 	edit_menu->set_text(TTR("Edit"));
441 	edit_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/undo", TTR("Undo"), KEY_MASK_CMD | KEY_Z), EDIT_UNDO);
442 	edit_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/redo", TTR("Redo"), KEY_MASK_CMD | KEY_Y), EDIT_REDO);
443 	edit_menu->get_popup()->add_separator();
444 	edit_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/cut", TTR("Cut"), KEY_MASK_CMD | KEY_X), EDIT_CUT);
445 	edit_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/copy", TTR("Copy"), KEY_MASK_CMD | KEY_C), EDIT_COPY);
446 	edit_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/paste", TTR("Paste"), KEY_MASK_CMD | KEY_V), EDIT_PASTE);
447 	edit_menu->get_popup()->add_separator();
448 	edit_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/select_all", TTR("Select All"), KEY_MASK_CMD | KEY_A), EDIT_SELECT_ALL);
449 	edit_menu->get_popup()->connect("item_pressed", this, "_menu_option");
450 
451 	search_menu = memnew(MenuButton);
452 	add_child(search_menu);
453 	search_menu->set_pos(Point2(38, -1));
454 	search_menu->set_text(TTR("Search"));
455 	search_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/find", TTR("Find.."), KEY_MASK_CMD | KEY_F), SEARCH_FIND);
456 	search_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/find_next", TTR("Find Next"), KEY_F3), SEARCH_FIND_NEXT);
457 	search_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/find_previous", TTR("Find Previous"), KEY_MASK_SHIFT | KEY_F3), SEARCH_FIND_PREV);
458 	search_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/replace", TTR("Replace.."), KEY_MASK_CMD | KEY_R), SEARCH_REPLACE);
459 	search_menu->get_popup()->add_separator();
460 	//	search_menu->get_popup()->add_item("Locate Symbol..",SEARCH_LOCATE_SYMBOL,KEY_MASK_CMD|KEY_K);
461 	search_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/goto_line", TTR("Goto Line.."), KEY_MASK_CMD | KEY_L), SEARCH_GOTO_LINE);
462 	search_menu->get_popup()->connect("item_pressed", this, "_menu_option");
463 
464 	tab_container->connect("tab_changed", this, "_tab_changed");
465 
466 	erase_tab_confirm = memnew(ConfirmationDialog);
467 	add_child(erase_tab_confirm);
468 	erase_tab_confirm->connect("confirmed", this, "_close_current_tab");
469 
470 	goto_line_dialog = memnew(GotoLineDialog);
471 	add_child(goto_line_dialog);
472 
473 	vertex_editor = memnew(ShaderTextEditor);
474 	tab_container->add_child(vertex_editor);
475 	vertex_editor->set_name(TTR("Vertex"));
476 
477 	fragment_editor = memnew(ShaderTextEditor);
478 	tab_container->add_child(fragment_editor);
479 	fragment_editor->set_name(TTR("Fragment"));
480 
481 	light_editor = memnew(ShaderTextEditor);
482 	tab_container->add_child(light_editor);
483 	light_editor->set_name(TTR("Lighting"));
484 
485 	tab_container->set_current_tab(1);
486 
487 	vertex_editor->connect("script_changed", this, "apply_shaders");
488 	fragment_editor->connect("script_changed", this, "apply_shaders");
489 	light_editor->connect("script_changed", this, "apply_shaders");
490 	EditorSettings::get_singleton()->connect("settings_changed", this, "_editor_settings_changed");
491 
492 	_editor_settings_changed();
493 }
494 
edit(Object * p_object)495 void ShaderEditorPlugin::edit(Object *p_object) {
496 
497 	Shader *s = p_object->cast_to<Shader>();
498 	if (!s || s->cast_to<ShaderGraph>()) {
499 		shader_editor->hide(); //Dont edit ShaderGraph
500 		return;
501 	}
502 
503 	if (_2d && s->get_mode() == Shader::MODE_CANVAS_ITEM)
504 		shader_editor->edit(s);
505 	else if (!_2d && s->get_mode() == Shader::MODE_MATERIAL)
506 		shader_editor->edit(s);
507 }
508 
handles(Object * p_object) const509 bool ShaderEditorPlugin::handles(Object *p_object) const {
510 
511 	bool handles = true;
512 	Shader *shader = p_object->cast_to<Shader>();
513 	if (!shader || shader->cast_to<ShaderGraph>()) // Dont handle ShaderGraph's
514 		handles = false;
515 	if (handles && _2d)
516 		handles = shader->get_mode() == Shader::MODE_CANVAS_ITEM;
517 	else if (handles && !_2d)
518 		return shader->get_mode() == Shader::MODE_MATERIAL;
519 
520 	if (!handles)
521 		shader_editor->hide();
522 	return handles;
523 }
524 
make_visible(bool p_visible)525 void ShaderEditorPlugin::make_visible(bool p_visible) {
526 
527 	if (p_visible) {
528 		shader_editor->show();
529 	} else {
530 		shader_editor->apply_shaders();
531 	}
532 }
533 
selected_notify()534 void ShaderEditorPlugin::selected_notify() {
535 
536 	shader_editor->ensure_select_current();
537 }
538 
get_state() const539 Dictionary ShaderEditorPlugin::get_state() const {
540 
541 	return shader_editor->get_state();
542 }
543 
set_state(const Dictionary & p_state)544 void ShaderEditorPlugin::set_state(const Dictionary &p_state) {
545 
546 	shader_editor->set_state(p_state);
547 }
clear()548 void ShaderEditorPlugin::clear() {
549 
550 	shader_editor->clear();
551 }
552 
save_external_data()553 void ShaderEditorPlugin::save_external_data() {
554 
555 	shader_editor->save_external_data();
556 }
557 
apply_changes()558 void ShaderEditorPlugin::apply_changes() {
559 
560 	shader_editor->apply_shaders();
561 }
562 
ShaderEditorPlugin(EditorNode * p_node,bool p_2d)563 ShaderEditorPlugin::ShaderEditorPlugin(EditorNode *p_node, bool p_2d) {
564 
565 	editor = p_node;
566 	shader_editor = memnew(ShaderEditor);
567 	_2d = p_2d;
568 	if (p_2d)
569 		add_control_to_container(CONTAINER_CANVAS_EDITOR_BOTTOM, shader_editor);
570 	else
571 		add_control_to_container(CONTAINER_SPATIAL_EDITOR_BOTTOM, shader_editor);
572 	//	editor->get_viewport()->add_child(shader_editor);
573 	//	shader_editor->set_area_as_parent_rect();
574 
575 	shader_editor->hide();
576 }
577 
~ShaderEditorPlugin()578 ShaderEditorPlugin::~ShaderEditorPlugin() {
579 }
580