| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | /*************************************************************************/ | 
					
						
							|  |  |  | /*  shader_editor_plugin.cpp                                             */ | 
					
						
							|  |  |  | /*************************************************************************/ | 
					
						
							|  |  |  | /*                       This file is part of:                           */ | 
					
						
							|  |  |  | /*                           GODOT ENGINE                                */ | 
					
						
							| 
									
										
										
										
											2017-08-27 14:16:55 +02:00
										 |  |  | /*                      https://godotengine.org                          */ | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | /*************************************************************************/ | 
					
						
							| 
									
										
										
										
											2018-01-01 14:40:08 +01:00
										 |  |  | /* Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur.                 */ | 
					
						
							|  |  |  | /* Copyright (c) 2014-2018 Godot Engine contributors (cf. AUTHORS.md)    */ | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | /*                                                                       */ | 
					
						
							|  |  |  | /* Permission is hereby granted, free of charge, to any person obtaining */ | 
					
						
							|  |  |  | /* a copy of this software and associated documentation files (the       */ | 
					
						
							|  |  |  | /* "Software"), to deal in the Software without restriction, including   */ | 
					
						
							|  |  |  | /* without limitation the rights to use, copy, modify, merge, publish,   */ | 
					
						
							|  |  |  | /* distribute, sublicense, and/or sell copies of the Software, and to    */ | 
					
						
							|  |  |  | /* permit persons to whom the Software is furnished to do so, subject to */ | 
					
						
							|  |  |  | /* the following conditions:                                             */ | 
					
						
							|  |  |  | /*                                                                       */ | 
					
						
							|  |  |  | /* The above copyright notice and this permission notice shall be        */ | 
					
						
							|  |  |  | /* included in all copies or substantial portions of the Software.       */ | 
					
						
							|  |  |  | /*                                                                       */ | 
					
						
							|  |  |  | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,       */ | 
					
						
							|  |  |  | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF    */ | 
					
						
							|  |  |  | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ | 
					
						
							|  |  |  | /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY  */ | 
					
						
							|  |  |  | /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,  */ | 
					
						
							|  |  |  | /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE     */ | 
					
						
							|  |  |  | /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.                */ | 
					
						
							|  |  |  | /*************************************************************************/ | 
					
						
							| 
									
										
										
										
											2018-01-05 00:50:27 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | #include "shader_editor_plugin.h"
 | 
					
						
							| 
									
										
										
										
											2016-03-09 00:00:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-26 17:46:49 +02:00
										 |  |  | #include "core/io/resource_loader.h"
 | 
					
						
							|  |  |  | #include "core/io/resource_saver.h"
 | 
					
						
							|  |  |  | #include "core/os/keyboard.h"
 | 
					
						
							|  |  |  | #include "core/os/os.h"
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | #include "editor/editor_node.h"
 | 
					
						
							| 
									
										
										
										
											2017-03-05 14:21:25 +01:00
										 |  |  | #include "editor/editor_settings.h"
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | #include "editor/property_editor.h"
 | 
					
						
							|  |  |  | #include "scene/resources/shader_graph.h"
 | 
					
						
							| 
									
										
										
										
											2016-10-07 11:31:18 -03:00
										 |  |  | #include "servers/visual/shader_types.h"
 | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-26 17:46:49 +02:00
										 |  |  | /*** SHADER SCRIPT EDITOR ****/ | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | Ref<Shader> ShaderTextEditor::get_edited_shader() const { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return shader; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | void ShaderTextEditor::set_edited_shader(const Ref<Shader> &p_shader) { | 
					
						
							| 
									
										
										
										
											2016-10-07 11:31:18 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	shader = p_shader; | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	_load_theme_settings(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-07 11:31:18 -03:00
										 |  |  | 	get_text_edit()->set_text(p_shader->get_code()); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	_line_col_changed(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void ShaderTextEditor::_load_theme_settings() { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	get_text_edit()->clear_colors(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-27 04:41:08 +09:00
										 |  |  | 	Color background_color = EDITOR_GET("text_editor/highlighting/background_color"); | 
					
						
							|  |  |  | 	Color completion_background_color = EDITOR_GET("text_editor/highlighting/completion_background_color"); | 
					
						
							|  |  |  | 	Color completion_selected_color = EDITOR_GET("text_editor/highlighting/completion_selected_color"); | 
					
						
							|  |  |  | 	Color completion_existing_color = EDITOR_GET("text_editor/highlighting/completion_existing_color"); | 
					
						
							|  |  |  | 	Color completion_scroll_color = EDITOR_GET("text_editor/highlighting/completion_scroll_color"); | 
					
						
							|  |  |  | 	Color completion_font_color = EDITOR_GET("text_editor/highlighting/completion_font_color"); | 
					
						
							|  |  |  | 	Color text_color = EDITOR_GET("text_editor/highlighting/text_color"); | 
					
						
							|  |  |  | 	Color line_number_color = EDITOR_GET("text_editor/highlighting/line_number_color"); | 
					
						
							|  |  |  | 	Color caret_color = EDITOR_GET("text_editor/highlighting/caret_color"); | 
					
						
							|  |  |  | 	Color caret_background_color = EDITOR_GET("text_editor/highlighting/caret_background_color"); | 
					
						
							|  |  |  | 	Color text_selected_color = EDITOR_GET("text_editor/highlighting/text_selected_color"); | 
					
						
							|  |  |  | 	Color selection_color = EDITOR_GET("text_editor/highlighting/selection_color"); | 
					
						
							|  |  |  | 	Color brace_mismatch_color = EDITOR_GET("text_editor/highlighting/brace_mismatch_color"); | 
					
						
							|  |  |  | 	Color current_line_color = EDITOR_GET("text_editor/highlighting/current_line_color"); | 
					
						
							|  |  |  | 	Color line_length_guideline_color = EDITOR_GET("text_editor/highlighting/line_length_guideline_color"); | 
					
						
							|  |  |  | 	Color word_highlighted_color = EDITOR_GET("text_editor/highlighting/word_highlighted_color"); | 
					
						
							|  |  |  | 	Color number_color = EDITOR_GET("text_editor/highlighting/number_color"); | 
					
						
							|  |  |  | 	Color function_color = EDITOR_GET("text_editor/highlighting/function_color"); | 
					
						
							|  |  |  | 	Color member_variable_color = EDITOR_GET("text_editor/highlighting/member_variable_color"); | 
					
						
							|  |  |  | 	Color mark_color = EDITOR_GET("text_editor/highlighting/mark_color"); | 
					
						
							|  |  |  | 	Color breakpoint_color = EDITOR_GET("text_editor/highlighting/breakpoint_color"); | 
					
						
							|  |  |  | 	Color code_folding_color = EDITOR_GET("text_editor/highlighting/code_folding_color"); | 
					
						
							|  |  |  | 	Color search_result_color = EDITOR_GET("text_editor/highlighting/search_result_color"); | 
					
						
							|  |  |  | 	Color search_result_border_color = EDITOR_GET("text_editor/highlighting/search_result_border_color"); | 
					
						
							|  |  |  | 	Color symbol_color = EDITOR_GET("text_editor/highlighting/symbol_color"); | 
					
						
							|  |  |  | 	Color keyword_color = EDITOR_GET("text_editor/highlighting/keyword_color"); | 
					
						
							|  |  |  | 	Color basetype_color = EDITOR_GET("text_editor/highlighting/base_type_color"); | 
					
						
							|  |  |  | 	Color type_color = EDITOR_GET("text_editor/highlighting/engine_type_color"); | 
					
						
							|  |  |  | 	Color comment_color = EDITOR_GET("text_editor/highlighting/comment_color"); | 
					
						
							|  |  |  | 	Color string_color = EDITOR_GET("text_editor/highlighting/string_color"); | 
					
						
							| 
									
										
										
										
											2017-09-14 20:02:05 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	get_text_edit()->add_color_override("background_color", background_color); | 
					
						
							|  |  |  | 	get_text_edit()->add_color_override("completion_background_color", completion_background_color); | 
					
						
							|  |  |  | 	get_text_edit()->add_color_override("completion_selected_color", completion_selected_color); | 
					
						
							|  |  |  | 	get_text_edit()->add_color_override("completion_existing_color", completion_existing_color); | 
					
						
							|  |  |  | 	get_text_edit()->add_color_override("completion_scroll_color", completion_scroll_color); | 
					
						
							|  |  |  | 	get_text_edit()->add_color_override("completion_font_color", completion_font_color); | 
					
						
							|  |  |  | 	get_text_edit()->add_color_override("font_color", text_color); | 
					
						
							|  |  |  | 	get_text_edit()->add_color_override("line_number_color", line_number_color); | 
					
						
							|  |  |  | 	get_text_edit()->add_color_override("caret_color", caret_color); | 
					
						
							|  |  |  | 	get_text_edit()->add_color_override("caret_background_color", caret_background_color); | 
					
						
							|  |  |  | 	get_text_edit()->add_color_override("font_selected_color", text_selected_color); | 
					
						
							|  |  |  | 	get_text_edit()->add_color_override("selection_color", selection_color); | 
					
						
							|  |  |  | 	get_text_edit()->add_color_override("brace_mismatch_color", brace_mismatch_color); | 
					
						
							|  |  |  | 	get_text_edit()->add_color_override("current_line_color", current_line_color); | 
					
						
							|  |  |  | 	get_text_edit()->add_color_override("line_length_guideline_color", line_length_guideline_color); | 
					
						
							|  |  |  | 	get_text_edit()->add_color_override("word_highlighted_color", word_highlighted_color); | 
					
						
							|  |  |  | 	get_text_edit()->add_color_override("number_color", number_color); | 
					
						
							|  |  |  | 	get_text_edit()->add_color_override("function_color", function_color); | 
					
						
							|  |  |  | 	get_text_edit()->add_color_override("member_variable_color", member_variable_color); | 
					
						
							|  |  |  | 	get_text_edit()->add_color_override("mark_color", mark_color); | 
					
						
							|  |  |  | 	get_text_edit()->add_color_override("breakpoint_color", breakpoint_color); | 
					
						
							| 
									
										
										
										
											2017-12-04 21:48:20 +00:00
										 |  |  | 	get_text_edit()->add_color_override("code_folding_color", code_folding_color); | 
					
						
							| 
									
										
										
										
											2017-09-14 20:02:05 -05:00
										 |  |  | 	get_text_edit()->add_color_override("search_result_color", search_result_color); | 
					
						
							|  |  |  | 	get_text_edit()->add_color_override("search_result_border_color", search_result_border_color); | 
					
						
							|  |  |  | 	get_text_edit()->add_color_override("symbol_color", symbol_color); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	List<String> keywords; | 
					
						
							| 
									
										
										
										
											2016-10-07 11:31:18 -03:00
										 |  |  | 	ShaderLanguage::get_keyword_list(&keywords); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (shader.is_valid()) { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-29 10:14:07 -03:00
										 |  |  | 		for (const Map<StringName, ShaderLanguage::FunctionInfo>::Element *E = ShaderTypes::get_singleton()->get_functions(VisualServer::ShaderMode(shader->get_mode())).front(); E; E = E->next()) { | 
					
						
							| 
									
										
										
										
											2016-10-07 11:31:18 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-15 18:23:16 -03:00
										 |  |  | 			for (const Map<StringName, ShaderLanguage::BuiltInInfo>::Element *F = E->get().built_ins.front(); F; F = F->next()) { | 
					
						
							| 
									
										
										
										
											2016-10-07 11:31:18 -03:00
										 |  |  | 				keywords.push_back(F->key()); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-14 18:15:42 -03:00
										 |  |  | 		for (int i = 0; i < ShaderTypes::get_singleton()->get_modes(VisualServer::ShaderMode(shader->get_mode())).size(); i++) { | 
					
						
							| 
									
										
										
										
											2016-10-07 11:31:18 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-14 18:15:42 -03:00
										 |  |  | 			keywords.push_back(ShaderTypes::get_singleton()->get_modes(VisualServer::ShaderMode(shader->get_mode()))[i]); | 
					
						
							| 
									
										
										
										
											2016-10-07 11:31:18 -03:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	for (List<String>::Element *E = keywords.front(); E; E = E->next()) { | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 		get_text_edit()->add_keyword_color(E->get(), keyword_color); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	//colorize core types
 | 
					
						
							| 
									
										
										
										
											2017-01-14 12:26:56 +01:00
										 |  |  | 	//Color basetype_color= EDITOR_DEF("text_editor/base_type_color",Color(0.3,0.3,0.0));
 | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	//colorize comments
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	get_text_edit()->add_color_region("/*", "*/", comment_color, false); | 
					
						
							|  |  |  | 	get_text_edit()->add_color_region("//", "", comment_color, false); | 
					
						
							| 
									
										
										
										
											2016-07-06 19:04:21 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	/*//colorize strings
 | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	Color string_color = EDITOR_DEF("text_editor/string_color",Color::hex(0x6b6f00ff)); | 
					
						
							| 
									
										
										
										
											2016-07-06 19:04:21 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	List<String> strings; | 
					
						
							|  |  |  | 	shader->get_shader_mode()->get_string_delimiters(&strings); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for (List<String>::Element *E=strings.front();E;E=E->next()) { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		String string = E->get(); | 
					
						
							|  |  |  | 		String beg = string.get_slice(" ",0); | 
					
						
							|  |  |  | 		String end = string.get_slice_count(" ")>1?string.get_slice(" ",1):String(); | 
					
						
							|  |  |  | 		get_text_edit()->add_color_region(beg,end,string_color,end==""); | 
					
						
							|  |  |  | 	}*/ | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-06 23:36:37 -03:00
										 |  |  | void ShaderTextEditor::_check_shader_mode() { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	String type = ShaderLanguage::get_shader_type(get_text_edit()->get_text()); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	print_line("type is: " + type); | 
					
						
							|  |  |  | 	Shader::Mode mode; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (type == "canvas_item") { | 
					
						
							|  |  |  | 		mode = Shader::MODE_CANVAS_ITEM; | 
					
						
							|  |  |  | 	} else if (type == "particles") { | 
					
						
							|  |  |  | 		mode = Shader::MODE_PARTICLES; | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		mode = Shader::MODE_SPATIAL; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (shader->get_mode() != mode) { | 
					
						
							|  |  |  | 		shader->set_code(get_text_edit()->get_text()); | 
					
						
							|  |  |  | 		_load_theme_settings(); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | void ShaderTextEditor::_code_complete_script(const String &p_code, List<String> *r_options) { | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-06 23:36:37 -03:00
										 |  |  | 	_check_shader_mode(); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-07 11:31:18 -03:00
										 |  |  | 	ShaderLanguage sl; | 
					
						
							|  |  |  | 	String calltip; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-06 23:36:37 -03:00
										 |  |  | 	Error err = sl.complete(p_code, ShaderTypes::get_singleton()->get_functions(VisualServer::ShaderMode(shader->get_mode())), ShaderTypes::get_singleton()->get_modes(VisualServer::ShaderMode(shader->get_mode())), ShaderTypes::get_singleton()->get_types(), r_options, calltip); | 
					
						
							| 
									
										
										
										
											2017-09-02 22:32:31 +02:00
										 |  |  | 	if (err != OK) | 
					
						
							|  |  |  | 		ERR_PRINT("Shaderlang complete failed"); | 
					
						
							| 
									
										
										
										
											2016-10-07 11:31:18 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	if (calltip != "") { | 
					
						
							| 
									
										
										
										
											2016-10-07 11:31:18 -03:00
										 |  |  | 		get_text_edit()->set_code_hint(calltip); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-07 11:31:18 -03:00
										 |  |  | void ShaderTextEditor::_validate_script() { | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-06 23:36:37 -03:00
										 |  |  | 	_check_shader_mode(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	String code = get_text_edit()->get_text(); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	//List<StringName> params;
 | 
					
						
							|  |  |  | 	//shader->get_param_list(¶ms);
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-07 11:31:18 -03:00
										 |  |  | 	ShaderLanguage sl; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-06 23:36:37 -03:00
										 |  |  | 	Error err = sl.compile(code, ShaderTypes::get_singleton()->get_functions(VisualServer::ShaderMode(shader->get_mode())), ShaderTypes::get_singleton()->get_modes(VisualServer::ShaderMode(shader->get_mode())), ShaderTypes::get_singleton()->get_types()); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	if (err != OK) { | 
					
						
							|  |  |  | 		String error_text = "error(" + itos(sl.get_error_line()) + "): " + sl.get_error_text(); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 		set_error(error_text); | 
					
						
							| 
									
										
										
										
											2017-11-10 23:07:41 -05:00
										 |  |  | 		for (int i = 0; i < get_text_edit()->get_line_count(); i++) | 
					
						
							|  |  |  | 			get_text_edit()->set_line_as_marked(i, false); | 
					
						
							| 
									
										
										
										
											2017-06-21 21:09:41 +01:00
										 |  |  | 		get_text_edit()->set_line_as_marked(sl.get_error_line() - 1, true); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	} else { | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 		for (int i = 0; i < get_text_edit()->get_line_count(); i++) | 
					
						
							|  |  |  | 			get_text_edit()->set_line_as_marked(i, false); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 		set_error(""); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	emit_signal("script_changed"); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void ShaderTextEditor::_bind_methods() { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ShaderTextEditor::ShaderTextEditor() { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*** SCRIPT EDITOR ******/ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void ShaderEditor::_menu_option(int p_option) { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	switch (p_option) { | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 		case EDIT_UNDO: { | 
					
						
							| 
									
										
										
										
											2017-11-10 23:07:41 -05:00
										 |  |  | 			shader_editor->get_text_edit()->undo(); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 		} break; | 
					
						
							|  |  |  | 		case EDIT_REDO: { | 
					
						
							| 
									
										
										
										
											2017-11-10 23:07:41 -05:00
										 |  |  | 			shader_editor->get_text_edit()->redo(); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 		} break; | 
					
						
							|  |  |  | 		case EDIT_CUT: { | 
					
						
							| 
									
										
										
										
											2017-11-10 23:07:41 -05:00
										 |  |  | 			shader_editor->get_text_edit()->cut(); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 		} break; | 
					
						
							|  |  |  | 		case EDIT_COPY: { | 
					
						
							| 
									
										
										
										
											2017-11-10 23:07:41 -05:00
										 |  |  | 			shader_editor->get_text_edit()->copy(); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 		} break; | 
					
						
							|  |  |  | 		case EDIT_PASTE: { | 
					
						
							| 
									
										
										
										
											2017-11-10 23:07:41 -05:00
										 |  |  | 			shader_editor->get_text_edit()->paste(); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 		} break; | 
					
						
							|  |  |  | 		case EDIT_SELECT_ALL: { | 
					
						
							| 
									
										
										
										
											2017-11-10 23:07:41 -05:00
										 |  |  | 			shader_editor->get_text_edit()->select_all(); | 
					
						
							|  |  |  | 		} break; | 
					
						
							|  |  |  | 		case EDIT_MOVE_LINE_UP: { | 
					
						
							| 
									
										
										
										
											2018-05-25 23:49:35 +01:00
										 |  |  | 			shader_editor->move_lines_up(); | 
					
						
							| 
									
										
										
										
											2017-11-10 23:07:41 -05:00
										 |  |  | 		} break; | 
					
						
							|  |  |  | 		case EDIT_MOVE_LINE_DOWN: { | 
					
						
							| 
									
										
										
										
											2018-05-25 23:49:35 +01:00
										 |  |  | 			shader_editor->move_lines_down(); | 
					
						
							| 
									
										
										
										
											2017-11-10 23:07:41 -05:00
										 |  |  | 		} break; | 
					
						
							|  |  |  | 		case EDIT_INDENT_LEFT: { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			TextEdit *tx = shader_editor->get_text_edit(); | 
					
						
							|  |  |  | 			if (shader.is_null()) | 
					
						
							|  |  |  | 				return; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-14 10:10:53 +01:00
										 |  |  | 			tx->indent_left(); | 
					
						
							| 
									
										
										
										
											2017-11-10 23:07:41 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		} break; | 
					
						
							|  |  |  | 		case EDIT_INDENT_RIGHT: { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			TextEdit *tx = shader_editor->get_text_edit(); | 
					
						
							|  |  |  | 			if (shader.is_null()) | 
					
						
							|  |  |  | 				return; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-14 10:10:53 +01:00
										 |  |  | 			tx->indent_right(); | 
					
						
							| 
									
										
										
										
											2017-11-10 23:07:41 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		} break; | 
					
						
							|  |  |  | 		case EDIT_DELETE_LINE: { | 
					
						
							| 
									
										
										
										
											2018-05-25 23:49:35 +01:00
										 |  |  | 			shader_editor->delete_lines(); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 		} break; | 
					
						
							| 
									
										
										
										
											2017-11-10 23:07:41 -05:00
										 |  |  | 		case EDIT_CLONE_DOWN: { | 
					
						
							| 
									
										
										
										
											2018-05-25 23:49:35 +01:00
										 |  |  | 			shader_editor->code_lines_down(); | 
					
						
							| 
									
										
										
										
											2017-11-10 23:07:41 -05:00
										 |  |  | 		} break; | 
					
						
							|  |  |  | 		case EDIT_TOGGLE_COMMENT: { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			TextEdit *tx = shader_editor->get_text_edit(); | 
					
						
							|  |  |  | 			if (shader.is_null()) | 
					
						
							|  |  |  | 				return; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			tx->begin_complex_operation(); | 
					
						
							|  |  |  | 			if (tx->is_selection_active()) { | 
					
						
							|  |  |  | 				int begin = tx->get_selection_from_line(); | 
					
						
							|  |  |  | 				int end = tx->get_selection_to_line(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				// End of selection ends on the first column of the last line, ignore it.
 | 
					
						
							|  |  |  | 				if (tx->get_selection_to_column() == 0) | 
					
						
							|  |  |  | 					end -= 1; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				// Check if all lines in the selected block are commented
 | 
					
						
							|  |  |  | 				bool is_commented = true; | 
					
						
							|  |  |  | 				for (int i = begin; i <= end; i++) { | 
					
						
							|  |  |  | 					if (!tx->get_line(i).begins_with("//")) { | 
					
						
							|  |  |  | 						is_commented = false; | 
					
						
							|  |  |  | 						break; | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				for (int i = begin; i <= end; i++) { | 
					
						
							|  |  |  | 					String line_text = tx->get_line(i); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 					if (line_text.strip_edges().empty()) { | 
					
						
							|  |  |  | 						line_text = "//"; | 
					
						
							|  |  |  | 					} else { | 
					
						
							|  |  |  | 						if (is_commented) { | 
					
						
							|  |  |  | 							line_text = line_text.substr(2, line_text.length()); | 
					
						
							|  |  |  | 						} else { | 
					
						
							|  |  |  | 							line_text = "//" + line_text; | 
					
						
							|  |  |  | 						} | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 					tx->set_line(i, line_text); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			} else { | 
					
						
							|  |  |  | 				int begin = tx->cursor_get_line(); | 
					
						
							|  |  |  | 				String line_text = tx->get_line(begin); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				if (line_text.begins_with("//")) | 
					
						
							|  |  |  | 					line_text = line_text.substr(2, line_text.length()); | 
					
						
							|  |  |  | 				else | 
					
						
							|  |  |  | 					line_text = "//" + line_text; | 
					
						
							|  |  |  | 				tx->set_line(begin, line_text); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			tx->end_complex_operation(); | 
					
						
							|  |  |  | 			tx->update(); | 
					
						
							|  |  |  | 			//tx->deselect();
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		} break; | 
					
						
							|  |  |  | 		case EDIT_COMPLETE: { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			shader_editor->get_text_edit()->query_code_comple(); | 
					
						
							|  |  |  | 		} break; | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 		case SEARCH_FIND: { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-10 23:07:41 -05:00
										 |  |  | 			shader_editor->get_find_replace_bar()->popup_search(); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 		} break; | 
					
						
							|  |  |  | 		case SEARCH_FIND_NEXT: { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-10 23:07:41 -05:00
										 |  |  | 			shader_editor->get_find_replace_bar()->search_next(); | 
					
						
							| 
									
										
										
										
											2016-05-28 18:25:45 +02:00
										 |  |  | 		} break; | 
					
						
							|  |  |  | 		case SEARCH_FIND_PREV: { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-10 23:07:41 -05:00
										 |  |  | 			shader_editor->get_find_replace_bar()->search_prev(); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 		} break; | 
					
						
							|  |  |  | 		case SEARCH_REPLACE: { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-10 23:07:41 -05:00
										 |  |  | 			shader_editor->get_find_replace_bar()->popup_replace(); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 		} break; | 
					
						
							|  |  |  | 		case SEARCH_GOTO_LINE: { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-10 23:07:41 -05:00
										 |  |  | 			goto_line_dialog->popup_find_line(shader_editor->get_text_edit()); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 		} break; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-11-10 23:07:41 -05:00
										 |  |  | 	if (p_option != SEARCH_FIND && p_option != SEARCH_REPLACE && p_option != SEARCH_GOTO_LINE) { | 
					
						
							|  |  |  | 		shader_editor->get_text_edit()->call_deferred("grab_focus"); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void ShaderEditor::_notification(int p_what) { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	if (p_what == NOTIFICATION_ENTER_TREE) { | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	if (p_what == NOTIFICATION_DRAW) { | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void ShaderEditor::_params_changed() { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-07 11:31:18 -03:00
										 |  |  | 	shader_editor->_validate_script(); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-29 12:39:04 +01:00
										 |  |  | void ShaderEditor::_editor_settings_changed() { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	shader_editor->get_text_edit()->set_auto_brace_completion(EditorSettings::get_singleton()->get("text_editor/completion/auto_brace_complete")); | 
					
						
							|  |  |  | 	shader_editor->get_text_edit()->set_scroll_pass_end_of_file(EditorSettings::get_singleton()->get("text_editor/cursor/scroll_past_end_of_file")); | 
					
						
							| 
									
										
										
										
											2017-04-17 14:24:30 +01:00
										 |  |  | 	shader_editor->get_text_edit()->set_indent_size(EditorSettings::get_singleton()->get("text_editor/indent/size")); | 
					
						
							| 
									
										
										
										
											2017-04-26 12:14:03 +01:00
										 |  |  | 	shader_editor->get_text_edit()->set_indent_using_spaces(EditorSettings::get_singleton()->get("text_editor/indent/type")); | 
					
						
							| 
									
										
										
										
											2017-08-31 19:35:15 +01:00
										 |  |  | 	shader_editor->get_text_edit()->set_auto_indent(EditorSettings::get_singleton()->get("text_editor/indent/auto_indent")); | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	shader_editor->get_text_edit()->set_draw_tabs(EditorSettings::get_singleton()->get("text_editor/indent/draw_tabs")); | 
					
						
							|  |  |  | 	shader_editor->get_text_edit()->set_show_line_numbers(EditorSettings::get_singleton()->get("text_editor/line_numbers/show_line_numbers")); | 
					
						
							|  |  |  | 	shader_editor->get_text_edit()->set_syntax_coloring(EditorSettings::get_singleton()->get("text_editor/highlighting/syntax_highlighting")); | 
					
						
							|  |  |  | 	shader_editor->get_text_edit()->set_highlight_all_occurrences(EditorSettings::get_singleton()->get("text_editor/highlighting/highlight_all_occurrences")); | 
					
						
							| 
									
										
										
										
											2017-10-22 13:38:00 +01:00
										 |  |  | 	shader_editor->get_text_edit()->set_highlight_current_line(EditorSettings::get_singleton()->get("text_editor/highlighting/highlight_current_line")); | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	shader_editor->get_text_edit()->cursor_set_blink_enabled(EditorSettings::get_singleton()->get("text_editor/cursor/caret_blink")); | 
					
						
							|  |  |  | 	shader_editor->get_text_edit()->cursor_set_blink_speed(EditorSettings::get_singleton()->get("text_editor/cursor/caret_blink_speed")); | 
					
						
							|  |  |  | 	shader_editor->get_text_edit()->add_constant_override("line_spacing", EditorSettings::get_singleton()->get("text_editor/theme/line_spacing")); | 
					
						
							|  |  |  | 	shader_editor->get_text_edit()->cursor_set_block_mode(EditorSettings::get_singleton()->get("text_editor/cursor/block_caret")); | 
					
						
							| 
									
										
										
										
											2017-09-03 13:31:07 +01:00
										 |  |  | 	shader_editor->get_text_edit()->set_smooth_scroll_enabled(EditorSettings::get_singleton()->get("text_editor/open_scripts/smooth_scrolling")); | 
					
						
							|  |  |  | 	shader_editor->get_text_edit()->set_v_scroll_speed(EditorSettings::get_singleton()->get("text_editor/open_scripts/v_scroll_speed")); | 
					
						
							| 
									
										
										
										
											2016-04-29 12:39:04 +01:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | void ShaderEditor::_bind_methods() { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	ClassDB::bind_method("_editor_settings_changed", &ShaderEditor::_editor_settings_changed); | 
					
						
							| 
									
										
										
										
											2017-11-10 23:07:41 -05:00
										 |  |  | 	ClassDB::bind_method("_text_edit_gui_input", &ShaderEditor::_text_edit_gui_input); | 
					
						
							| 
									
										
										
										
											2016-10-07 11:31:18 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	ClassDB::bind_method("_menu_option", &ShaderEditor::_menu_option); | 
					
						
							|  |  |  | 	ClassDB::bind_method("_params_changed", &ShaderEditor::_params_changed); | 
					
						
							|  |  |  | 	ClassDB::bind_method("apply_shaders", &ShaderEditor::apply_shaders); | 
					
						
							| 
									
										
										
										
											2017-01-14 12:26:56 +01:00
										 |  |  | 	//ClassDB::bind_method("_close_current_tab",&ShaderEditor::_close_current_tab);
 | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void ShaderEditor::ensure_select_current() { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	/*
 | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	if (tab_container->get_child_count() && tab_container->get_current_tab()>=0) { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-24 22:58:51 +02:00
										 |  |  | 		ShaderTextEditor *ste = Object::cast_to<ShaderTextEditor>(tab_container->get_child(tab_container->get_current_tab())); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 		if (!ste) | 
					
						
							|  |  |  | 			return; | 
					
						
							|  |  |  | 		Ref<Shader> shader = ste->get_edited_shader(); | 
					
						
							|  |  |  | 		get_scene()->get_root_node()->call("_resource_selected",shader); | 
					
						
							|  |  |  | 	}*/ | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | void ShaderEditor::edit(const Ref<Shader> &p_shader) { | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if (p_shader.is_null()) | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	shader = p_shader; | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-07 11:31:18 -03:00
										 |  |  | 	shader_editor->set_edited_shader(p_shader); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-21 00:57:37 -03:00
										 |  |  | 	//vertex_editor->set_edited_shader(shader,ShaderLanguage::SHADER_MATERIAL_VERTEX);
 | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	// see if already has it
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void ShaderEditor::save_external_data() { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (shader.is_null()) | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 	apply_shaders(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	if (shader->get_path() != "" && shader->get_path().find("local://") == -1 && shader->get_path().find("::") == -1) { | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 		//external shader, save it
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 		ResourceSaver::save(shader->get_path(), shader); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | void ShaderEditor::apply_shaders() { | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-08-14 10:31:38 -03:00
										 |  |  | 	if (shader.is_valid()) { | 
					
						
							| 
									
										
										
										
											2016-10-07 11:31:18 -03:00
										 |  |  | 		shader->set_code(shader_editor->get_text_edit()->get_text()); | 
					
						
							| 
									
										
										
										
											2014-08-14 10:31:38 -03:00
										 |  |  | 		shader->set_edited(true); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-10 23:07:41 -05:00
										 |  |  | void ShaderEditor::_text_edit_gui_input(const Ref<InputEvent> &ev) { | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-10 23:07:41 -05:00
										 |  |  | 	Ref<InputEventMouseButton> mb = ev; | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-10 23:07:41 -05:00
										 |  |  | 	if (mb.is_valid()) { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-20 02:36:47 +01:00
										 |  |  | 		if (mb->get_button_index() == BUTTON_RIGHT) { | 
					
						
							| 
									
										
										
										
											2017-11-10 23:07:41 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			int col, row; | 
					
						
							|  |  |  | 			TextEdit *tx = shader_editor->get_text_edit(); | 
					
						
							|  |  |  | 			tx->_get_mouse_pos(mb->get_global_position() - tx->get_global_position(), row, col); | 
					
						
							|  |  |  | 			Vector2 mpos = mb->get_global_position() - tx->get_global_position(); | 
					
						
							| 
									
										
										
										
											2017-12-20 02:36:47 +01:00
										 |  |  | 			tx->set_right_click_moves_caret(EditorSettings::get_singleton()->get("text_editor/cursor/right_click_moves_caret")); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			if (tx->is_right_click_moving_caret()) { | 
					
						
							|  |  |  | 				if (tx->is_selection_active()) { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 					int from_line = tx->get_selection_from_line(); | 
					
						
							|  |  |  | 					int to_line = tx->get_selection_to_line(); | 
					
						
							|  |  |  | 					int from_column = tx->get_selection_from_column(); | 
					
						
							|  |  |  | 					int to_column = tx->get_selection_to_column(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 					if (row < from_line || row > to_line || (row == from_line && col < from_column) || (row == to_line && col > to_column)) { | 
					
						
							|  |  |  | 						// Right click is outside the seleted text
 | 
					
						
							|  |  |  | 						tx->deselect(); | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				if (!tx->is_selection_active()) { | 
					
						
							|  |  |  | 					tx->cursor_set_line(row, true, false); | 
					
						
							|  |  |  | 					tx->cursor_set_column(col); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			if (!mb->is_pressed()) { | 
					
						
							|  |  |  | 				_make_context_menu(tx->is_selection_active()); | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2017-11-10 23:07:41 -05:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void ShaderEditor::_make_context_menu(bool p_selection) { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	context_menu->clear(); | 
					
						
							|  |  |  | 	if (p_selection) { | 
					
						
							|  |  |  | 		context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/cut"), EDIT_CUT); | 
					
						
							|  |  |  | 		context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/copy"), EDIT_COPY); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/paste"), EDIT_PASTE); | 
					
						
							|  |  |  | 	context_menu->add_separator(); | 
					
						
							|  |  |  | 	context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/select_all"), EDIT_SELECT_ALL); | 
					
						
							|  |  |  | 	context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/undo"), EDIT_UNDO); | 
					
						
							|  |  |  | 	context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/redo"), EDIT_REDO); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	context_menu->add_separator(); | 
					
						
							|  |  |  | 	context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/indent_left"), EDIT_INDENT_LEFT); | 
					
						
							|  |  |  | 	context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/indent_right"), EDIT_INDENT_RIGHT); | 
					
						
							|  |  |  | 	context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/toggle_comment"), EDIT_TOGGLE_COMMENT); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	context_menu->set_position(get_global_transform().xform(get_local_mouse_position())); | 
					
						
							|  |  |  | 	context_menu->set_size(Vector2(1, 1)); | 
					
						
							|  |  |  | 	context_menu->popup(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ShaderEditor::ShaderEditor(EditorNode *p_node) { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	shader_editor = memnew(ShaderTextEditor); | 
					
						
							|  |  |  | 	shader_editor->set_v_size_flags(SIZE_EXPAND_FILL); | 
					
						
							|  |  |  | 	shader_editor->add_constant_override("separation", 0); | 
					
						
							|  |  |  | 	shader_editor->set_anchors_and_margins_preset(Control::PRESET_WIDE); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	shader_editor->connect("script_changed", this, "apply_shaders"); | 
					
						
							|  |  |  | 	EditorSettings::get_singleton()->connect("settings_changed", this, "_editor_settings_changed"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	shader_editor->get_text_edit()->set_callhint_settings( | 
					
						
							|  |  |  | 			EditorSettings::get_singleton()->get("text_editor/completion/put_callhint_tooltip_below_current_line"), | 
					
						
							|  |  |  | 			EditorSettings::get_singleton()->get("text_editor/completion/callhint_tooltip_offset")); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	shader_editor->get_text_edit()->set_select_identifiers_on_hover(true); | 
					
						
							|  |  |  | 	shader_editor->get_text_edit()->set_context_menu_enabled(false); | 
					
						
							|  |  |  | 	shader_editor->get_text_edit()->connect("gui_input", this, "_text_edit_gui_input"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	shader_editor->update_editor_settings(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	context_menu = memnew(PopupMenu); | 
					
						
							|  |  |  | 	add_child(context_menu); | 
					
						
							|  |  |  | 	context_menu->connect("id_pressed", this, "_menu_option"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	VBoxContainer *main_container = memnew(VBoxContainer); | 
					
						
							|  |  |  | 	HBoxContainer *hbc = memnew(HBoxContainer); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	edit_menu = memnew(MenuButton); | 
					
						
							| 
									
										
										
										
											2017-11-10 23:07:41 -05:00
										 |  |  | 	//edit_menu->set_position(Point2(5, -1));
 | 
					
						
							| 
									
										
										
										
											2016-05-03 22:25:37 -03:00
										 |  |  | 	edit_menu->set_text(TTR("Edit")); | 
					
						
							| 
									
										
										
										
											2017-11-10 23:07:41 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/undo"), EDIT_UNDO); | 
					
						
							|  |  |  | 	edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/redo"), EDIT_REDO); | 
					
						
							|  |  |  | 	edit_menu->get_popup()->add_separator(); | 
					
						
							|  |  |  | 	edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/cut"), EDIT_CUT); | 
					
						
							|  |  |  | 	edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/copy"), EDIT_COPY); | 
					
						
							|  |  |  | 	edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/paste"), EDIT_PASTE); | 
					
						
							|  |  |  | 	edit_menu->get_popup()->add_separator(); | 
					
						
							|  |  |  | 	edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/select_all"), EDIT_SELECT_ALL); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	edit_menu->get_popup()->add_separator(); | 
					
						
							| 
									
										
										
										
											2017-11-10 23:07:41 -05:00
										 |  |  | 	edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/move_up"), EDIT_MOVE_LINE_UP); | 
					
						
							|  |  |  | 	edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/move_down"), EDIT_MOVE_LINE_DOWN); | 
					
						
							|  |  |  | 	edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/indent_left"), EDIT_INDENT_LEFT); | 
					
						
							|  |  |  | 	edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/indent_right"), EDIT_INDENT_RIGHT); | 
					
						
							|  |  |  | 	edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/delete_line"), EDIT_DELETE_LINE); | 
					
						
							|  |  |  | 	edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/toggle_comment"), EDIT_TOGGLE_COMMENT); | 
					
						
							|  |  |  | 	edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/clone_down"), EDIT_CLONE_DOWN); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	edit_menu->get_popup()->add_separator(); | 
					
						
							| 
									
										
										
										
											2017-11-10 23:07:41 -05:00
										 |  |  | 	edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/complete_symbol"), EDIT_COMPLETE); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	edit_menu->get_popup()->connect("id_pressed", this, "_menu_option"); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	search_menu = memnew(MenuButton); | 
					
						
							| 
									
										
										
										
											2017-11-10 23:07:41 -05:00
										 |  |  | 	//search_menu->set_position(Point2(38, -1));
 | 
					
						
							| 
									
										
										
										
											2016-05-03 22:25:37 -03:00
										 |  |  | 	search_menu->set_text(TTR("Search")); | 
					
						
							| 
									
										
										
										
											2017-11-10 23:07:41 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/find"), SEARCH_FIND); | 
					
						
							|  |  |  | 	search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/find_next"), SEARCH_FIND_NEXT); | 
					
						
							|  |  |  | 	search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/find_previous"), SEARCH_FIND_PREV); | 
					
						
							|  |  |  | 	search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/replace"), SEARCH_REPLACE); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	search_menu->get_popup()->add_separator(); | 
					
						
							| 
									
										
										
										
											2017-11-10 23:07:41 -05:00
										 |  |  | 	search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/goto_line"), SEARCH_GOTO_LINE); | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	search_menu->get_popup()->connect("id_pressed", this, "_menu_option"); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-10 23:07:41 -05:00
										 |  |  | 	add_child(main_container); | 
					
						
							|  |  |  | 	main_container->add_child(hbc); | 
					
						
							|  |  |  | 	hbc->add_child(search_menu); | 
					
						
							|  |  |  | 	hbc->add_child(edit_menu); | 
					
						
							|  |  |  | 	hbc->add_style_override("panel", p_node->get_gui_base()->get_stylebox("ScriptEditorPanel", "EditorStyles")); | 
					
						
							|  |  |  | 	main_container->add_child(shader_editor); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	goto_line_dialog = memnew(GotoLineDialog); | 
					
						
							|  |  |  | 	add_child(goto_line_dialog); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-29 12:39:04 +01:00
										 |  |  | 	_editor_settings_changed(); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void ShaderEditorPlugin::edit(Object *p_object) { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-24 22:58:51 +02:00
										 |  |  | 	Shader *s = Object::cast_to<Shader>(p_object); | 
					
						
							| 
									
										
										
										
											2016-10-07 11:31:18 -03:00
										 |  |  | 	shader_editor->edit(s); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | bool ShaderEditorPlugin::handles(Object *p_object) const { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-24 22:58:51 +02:00
										 |  |  | 	Shader *shader = Object::cast_to<Shader>(p_object); | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	return shader != NULL; | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void ShaderEditorPlugin::make_visible(bool p_visible) { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (p_visible) { | 
					
						
							| 
									
										
										
										
											2016-10-07 11:31:18 -03:00
										 |  |  | 		button->show(); | 
					
						
							|  |  |  | 		editor->make_bottom_panel_item_visible(shader_editor); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	} else { | 
					
						
							| 
									
										
										
										
											2016-10-07 11:31:18 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		button->hide(); | 
					
						
							| 
									
										
										
										
											2017-01-13 10:45:50 -03:00
										 |  |  | 		if (shader_editor->is_visible_in_tree()) | 
					
						
							| 
									
										
										
										
											2016-10-07 11:31:18 -03:00
										 |  |  | 			editor->hide_bottom_panel(); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 		shader_editor->apply_shaders(); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void ShaderEditorPlugin::selected_notify() { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	shader_editor->ensure_select_current(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void ShaderEditorPlugin::save_external_data() { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	shader_editor->save_external_data(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void ShaderEditorPlugin::apply_changes() { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	shader_editor->apply_shaders(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-07 11:31:18 -03:00
										 |  |  | ShaderEditorPlugin::ShaderEditorPlugin(EditorNode *p_node) { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	editor = p_node; | 
					
						
							| 
									
										
										
										
											2017-11-10 23:07:41 -05:00
										 |  |  | 	shader_editor = memnew(ShaderEditor(p_node)); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	shader_editor->set_custom_minimum_size(Size2(0, 300)); | 
					
						
							| 
									
										
										
										
											2017-06-12 19:23:37 -03:00
										 |  |  | 	button = editor->add_bottom_panel_item(TTR("Shader"), shader_editor); | 
					
						
							|  |  |  | 	button->hide(); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ShaderEditorPlugin::~ShaderEditorPlugin() { | 
					
						
							|  |  |  | } |