shader_editor_plugin.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662
  1. /*************************************************************************/
  2. /* shader_editor_plugin.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2016 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #include "shader_editor_plugin.h"
  30. #include "tools/editor/editor_settings.h"
  31. #include "spatial_editor_plugin.h"
  32. #include "scene/resources/shader_graph.h"
  33. #include "io/resource_loader.h"
  34. #include "io/resource_saver.h"
  35. #include "os/keyboard.h"
  36. #include "tools/editor/editor_node.h"
  37. #include "tools/editor/property_editor.h"
  38. #include "os/os.h"
  39. /*** SETTINGS EDITOR ****/
  40. /*** SCRIPT EDITOR ****/
  41. Ref<Shader> ShaderTextEditor::get_edited_shader() const {
  42. return shader;
  43. }
  44. void ShaderTextEditor::set_edited_shader(const Ref<Shader>& p_shader,ShaderLanguage::ShaderType p_type) {
  45. shader=p_shader;
  46. type=p_type;
  47. _load_theme_settings();
  48. if (p_type==ShaderLanguage::SHADER_MATERIAL_LIGHT || p_type==ShaderLanguage::SHADER_CANVAS_ITEM_LIGHT)
  49. get_text_edit()->set_text(shader->get_light_code());
  50. else if (p_type==ShaderLanguage::SHADER_MATERIAL_VERTEX || p_type==ShaderLanguage::SHADER_CANVAS_ITEM_VERTEX)
  51. get_text_edit()->set_text(shader->get_vertex_code());
  52. else
  53. get_text_edit()->set_text(shader->get_fragment_code());
  54. _line_col_changed();
  55. }
  56. void ShaderTextEditor::_load_theme_settings() {
  57. get_text_edit()->clear_colors();
  58. /* keyword color */
  59. get_text_edit()->set_custom_bg_color(EDITOR_DEF("text_editor/background_color",Color(0,0,0,0)));
  60. get_text_edit()->add_color_override("completion_background_color", EDITOR_DEF("text_editor/completion_background_color", Color(0,0,0,0)));
  61. get_text_edit()->add_color_override("completion_selected_color", EDITOR_DEF("text_editor/completion_selected_color", Color::html("434244")));
  62. get_text_edit()->add_color_override("completion_existing_color", EDITOR_DEF("text_editor/completion_existing_color", Color::html("21dfdfdf")));
  63. get_text_edit()->add_color_override("completion_scroll_color", EDITOR_DEF("text_editor/completion_scroll_color", Color::html("ffffff")));
  64. get_text_edit()->add_color_override("completion_font_color", EDITOR_DEF("text_editor/completion_font_color", Color::html("aaaaaa")));
  65. get_text_edit()->add_color_override("font_color",EDITOR_DEF("text_editor/text_color",Color(0,0,0)));
  66. get_text_edit()->add_color_override("line_number_color",EDITOR_DEF("text_editor/line_number_color",Color(0,0,0)));
  67. get_text_edit()->add_color_override("caret_color",EDITOR_DEF("text_editor/caret_color",Color(0,0,0)));
  68. get_text_edit()->add_color_override("caret_background_color",EDITOR_DEF("text_editor/caret_background_color",Color(0,0,0)));
  69. get_text_edit()->add_color_override("font_selected_color",EDITOR_DEF("text_editor/text_selected_color",Color(1,1,1)));
  70. get_text_edit()->add_color_override("selection_color",EDITOR_DEF("text_editor/selection_color",Color(0.2,0.2,1)));
  71. get_text_edit()->add_color_override("brace_mismatch_color",EDITOR_DEF("text_editor/brace_mismatch_color",Color(1,0.2,0.2)));
  72. 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)));
  73. 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)));
  74. get_text_edit()->add_color_override("number_color",EDITOR_DEF("text_editor/number_color",Color(0.9,0.6,0.0,2)));
  75. get_text_edit()->add_color_override("function_color",EDITOR_DEF("text_editor/function_color",Color(0.4,0.6,0.8)));
  76. get_text_edit()->add_color_override("member_variable_color",EDITOR_DEF("text_editor/member_variable_color",Color(0.9,0.3,0.3)));
  77. get_text_edit()->add_color_override("mark_color", EDITOR_DEF("text_editor/mark_color", Color(1.0,0.4,0.4,0.4)));
  78. get_text_edit()->add_color_override("breakpoint_color", EDITOR_DEF("text_editor/breakpoint_color", Color(0.8,0.8,0.4,0.2)));
  79. get_text_edit()->add_color_override("search_result_color",EDITOR_DEF("text_editor/search_result_color",Color(0.05,0.25,0.05,1)));
  80. 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)));
  81. Color keyword_color= EDITOR_DEF("text_editor/keyword_color",Color(0.5,0.0,0.2));
  82. List<String> keywords;
  83. ShaderLanguage::get_keyword_list(type,&keywords);
  84. for(List<String>::Element *E=keywords.front();E;E=E->next()) {
  85. get_text_edit()->add_keyword_color(E->get(),keyword_color);
  86. }
  87. //colorize core types
  88. // Color basetype_color= EDITOR_DEF("text_editor/base_type_color",Color(0.3,0.3,0.0));
  89. //colorize comments
  90. Color comment_color = EDITOR_DEF("text_editor/comment_color",Color::hex(0x797e7eff));
  91. get_text_edit()->add_color_region("/*","*/",comment_color,false);
  92. get_text_edit()->add_color_region("//","",comment_color,false);
  93. /*//colorize strings
  94. Color string_color = EDITOR_DEF("text_editor/string_color",Color::hex(0x6b6f00ff));
  95. List<String> strings;
  96. shader->get_shader_mode()->get_string_delimiters(&strings);
  97. for (List<String>::Element *E=strings.front();E;E=E->next()) {
  98. String string = E->get();
  99. String beg = string.get_slice(" ",0);
  100. String end = string.get_slice_count(" ")>1?string.get_slice(" ",1):String();
  101. get_text_edit()->add_color_region(beg,end,string_color,end=="");
  102. }*/
  103. //colorize symbols
  104. Color symbol_color= EDITOR_DEF("text_editor/symbol_color",Color::hex(0x005291ff));
  105. get_text_edit()->set_symbol_color(symbol_color);
  106. }
  107. void ShaderTextEditor::_validate_script() {
  108. String errortxt;
  109. int line,col;
  110. String code=get_text_edit()->get_text();
  111. //List<StringName> params;
  112. //shader->get_param_list(&params);
  113. Error err = ShaderLanguage::compile(code,type,NULL,NULL,&errortxt,&line,&col);
  114. if (err!=OK) {
  115. String error_text="error("+itos(line+1)+","+itos(col)+"): "+errortxt;
  116. set_error(error_text);
  117. get_text_edit()->set_line_as_marked(line,true);
  118. } else {
  119. for(int i=0;i<get_text_edit()->get_line_count();i++)
  120. get_text_edit()->set_line_as_marked(i,false);
  121. set_error("");
  122. }
  123. emit_signal("script_changed");
  124. }
  125. void ShaderTextEditor::_bind_methods() {
  126. //ADD_SIGNAL( MethodInfo("script_changed") );
  127. }
  128. ShaderTextEditor::ShaderTextEditor() {
  129. }
  130. /*** SCRIPT EDITOR ******/
  131. void ShaderEditor::_menu_option(int p_option) {
  132. ShaderTextEditor *current = tab_container->get_current_tab_control()->cast_to<ShaderTextEditor>();
  133. if (!current)
  134. return;
  135. switch(p_option) {
  136. case EDIT_UNDO: {
  137. current->get_text_edit()->undo();
  138. } break;
  139. case EDIT_REDO: {
  140. current->get_text_edit()->redo();
  141. } break;
  142. case EDIT_CUT: {
  143. current->get_text_edit()->cut();
  144. } break;
  145. case EDIT_COPY: {
  146. current->get_text_edit()->copy();
  147. } break;
  148. case EDIT_PASTE: {
  149. current->get_text_edit()->paste();
  150. } break;
  151. case EDIT_SELECT_ALL: {
  152. current->get_text_edit()->select_all();
  153. } break;
  154. case SEARCH_FIND: {
  155. current->get_find_replace_bar()->popup_search();
  156. } break;
  157. case SEARCH_FIND_NEXT: {
  158. current->get_find_replace_bar()->search_next();
  159. } break;
  160. case SEARCH_FIND_PREV: {
  161. current->get_find_replace_bar()->search_prev();
  162. } break;
  163. case SEARCH_REPLACE: {
  164. current->get_find_replace_bar()->popup_replace();
  165. } break;
  166. // case SEARCH_LOCATE_SYMBOL: {
  167. // } break;
  168. case SEARCH_GOTO_LINE: {
  169. goto_line_dialog->popup_find_line(current->get_text_edit());
  170. } break;
  171. }
  172. }
  173. void ShaderEditor::_tab_changed(int p_which) {
  174. ShaderTextEditor *shader_editor = tab_container->get_tab_control(p_which)->cast_to<ShaderTextEditor>();
  175. if (shader_editor && is_inside_tree())
  176. shader_editor->get_text_edit()->grab_focus();
  177. ensure_select_current();
  178. }
  179. void ShaderEditor::_notification(int p_what) {
  180. if (p_what==NOTIFICATION_ENTER_TREE) {
  181. close->set_normal_texture( get_icon("Close","EditorIcons"));
  182. close->set_hover_texture( get_icon("CloseHover","EditorIcons"));
  183. close->set_pressed_texture( get_icon("Close","EditorIcons"));
  184. close->connect("pressed",this,"_close_callback");
  185. }
  186. if (p_what==NOTIFICATION_DRAW) {
  187. RID ci = get_canvas_item();
  188. Ref<StyleBox> style = get_stylebox("panel","Panel");
  189. style->draw( ci, Rect2( Point2(), get_size() ) );
  190. }
  191. }
  192. Dictionary ShaderEditor::get_state() const {
  193. #if 0
  194. apply_shaders();
  195. Dictionary state;
  196. Array paths;
  197. int open=-1;
  198. for(int i=0;i<tab_container->get_child_count();i++) {
  199. ShaderTextEditor *ste = tab_container->get_child(i)->cast_to<ShaderTextEditor>();
  200. if (!ste)
  201. continue;
  202. Ref<Shader> shader = ste->get_edited_shader();
  203. if (shader->get_path()!="" && shader->get_path().find("local://")==-1 && shader->get_path().find("::")==-1) {
  204. paths.push_back(shader->get_path());
  205. } else {
  206. const Node *owner = _find_node_with_shader(get_root_node(),shader.get_ref_ptr());
  207. if (owner)
  208. paths.push_back(owner->get_path());
  209. }
  210. if (i==tab_container->get_current_tab())
  211. open=i;
  212. }
  213. if (paths.size())
  214. state["sources"]=paths;
  215. if (open!=-1)
  216. state["current"]=open;
  217. return state;
  218. #endif
  219. return Dictionary();
  220. }
  221. void ShaderEditor::set_state(const Dictionary& p_state) {
  222. #if 0
  223. print_line("setting state..");
  224. if (!p_state.has("sources"))
  225. return; //bleh
  226. Array sources = p_state["sources"];
  227. for(int i=0;i<sources.size();i++) {
  228. Variant source=sources[i];
  229. Ref<Shader> shader;
  230. if (source.get_type()==Variant::NODE_PATH) {
  231. print_line("cain find owner at path "+String(source));
  232. Node *owner=get_root_node()->get_node(source);
  233. if (!owner)
  234. continue;
  235. shader = owner->get_shader();
  236. } else if (source.get_type()==Variant::STRING) {
  237. print_line("loading at path "+String(source));
  238. shader = ResourceLoader::load(source,"Shader");
  239. }
  240. print_line("found shader at "+String(source)+"? - "+itos(shader.is_null()));
  241. if (shader.is_null()) //ah well..
  242. continue;
  243. get_scene()->get_root_node()->call("_resource_selected",shader);
  244. }
  245. if (p_state.has("current"))
  246. tab_container->set_current_tab(p_state["current"]);
  247. #endif
  248. }
  249. void ShaderEditor::clear() {
  250. }
  251. void ShaderEditor::_params_changed() {
  252. fragment_editor->_validate_script();
  253. vertex_editor->_validate_script();
  254. light_editor->_validate_script();
  255. }
  256. void ShaderEditor::_editor_settings_changed() {
  257. vertex_editor->get_text_edit()->set_auto_brace_completion(EditorSettings::get_singleton()->get("text_editor/auto_brace_complete"));
  258. vertex_editor->get_text_edit()->set_scroll_pass_end_of_file(EditorSettings::get_singleton()->get("text_editor/scroll_past_end_of_file"));
  259. vertex_editor->get_text_edit()->set_tab_size(EditorSettings::get_singleton()->get("text_editor/tab_size"));
  260. vertex_editor->get_text_edit()->set_draw_tabs(EditorSettings::get_singleton()->get("text_editor/draw_tabs"));
  261. vertex_editor->get_text_edit()->set_show_line_numbers(EditorSettings::get_singleton()->get("text_editor/show_line_numbers"));
  262. vertex_editor->get_text_edit()->set_show_line_length_guideline(EditorSettings::get_singleton()->get("text_editor/show_line_length_guideline"));
  263. vertex_editor->get_text_edit()->set_line_length_guideline_column(EditorSettings::get_singleton()->get("text_editor/line_length_guideline_column"));
  264. vertex_editor->get_text_edit()->set_syntax_coloring(EditorSettings::get_singleton()->get("text_editor/syntax_highlighting"));
  265. vertex_editor->get_text_edit()->set_highlight_all_occurrences(EditorSettings::get_singleton()->get("text_editor/highlight_all_occurrences"));
  266. vertex_editor->get_text_edit()->cursor_set_blink_enabled(EditorSettings::get_singleton()->get("text_editor/caret_blink"));
  267. vertex_editor->get_text_edit()->cursor_set_blink_speed(EditorSettings::get_singleton()->get("text_editor/caret_blink_speed"));
  268. vertex_editor->get_text_edit()->add_constant_override("line_spacing", EditorSettings::get_singleton()->get("text_editor/line_spacing"));
  269. vertex_editor->get_text_edit()->cursor_set_block_mode(EditorSettings::get_singleton()->get("text_editor/block_caret"));
  270. fragment_editor->get_text_edit()->set_auto_brace_completion(EditorSettings::get_singleton()->get("text_editor/auto_brace_complete"));
  271. fragment_editor->get_text_edit()->set_scroll_pass_end_of_file(EditorSettings::get_singleton()->get("text_editor/scroll_past_end_of_file"));
  272. fragment_editor->get_text_edit()->set_tab_size(EditorSettings::get_singleton()->get("text_editor/tab_size"));
  273. fragment_editor->get_text_edit()->set_draw_tabs(EditorSettings::get_singleton()->get("text_editor/draw_tabs"));
  274. fragment_editor->get_text_edit()->set_show_line_numbers(EditorSettings::get_singleton()->get("text_editor/show_line_numbers"));
  275. fragment_editor->get_text_edit()->set_show_line_length_guideline(EditorSettings::get_singleton()->get("text_editor/show_line_length_guideline"));
  276. fragment_editor->get_text_edit()->set_line_length_guideline_column(EditorSettings::get_singleton()->get("text_editor/line_length_guideline_column"));
  277. fragment_editor->get_text_edit()->set_syntax_coloring(EditorSettings::get_singleton()->get("text_editor/syntax_highlighting"));
  278. fragment_editor->get_text_edit()->set_highlight_all_occurrences(EditorSettings::get_singleton()->get("text_editor/highlight_all_occurrences"));
  279. fragment_editor->get_text_edit()->cursor_set_blink_enabled(EditorSettings::get_singleton()->get("text_editor/caret_blink"));
  280. fragment_editor->get_text_edit()->cursor_set_blink_speed(EditorSettings::get_singleton()->get("text_editor/caret_blink_speed"));
  281. fragment_editor->get_text_edit()->add_constant_override("line_spacing", EditorSettings::get_singleton()->get("text_editor/line_spacing"));
  282. fragment_editor->get_text_edit()->cursor_set_block_mode(EditorSettings::get_singleton()->get("text_editor/block_caret"));
  283. light_editor->get_text_edit()->set_auto_brace_completion(EditorSettings::get_singleton()->get("text_editor/auto_brace_complete"));
  284. light_editor->get_text_edit()->set_scroll_pass_end_of_file(EditorSettings::get_singleton()->get("text_editor/scroll_past_end_of_file"));
  285. light_editor->get_text_edit()->set_tab_size(EditorSettings::get_singleton()->get("text_editor/tab_size"));
  286. light_editor->get_text_edit()->set_draw_tabs(EditorSettings::get_singleton()->get("text_editor/draw_tabs"));
  287. light_editor->get_text_edit()->set_show_line_numbers(EditorSettings::get_singleton()->get("text_editor/show_line_numbers"));
  288. light_editor->get_text_edit()->set_show_line_length_guideline(EditorSettings::get_singleton()->get("text_editor/show_line_length_guideline"));
  289. light_editor->get_text_edit()->set_line_length_guideline_column(EditorSettings::get_singleton()->get("text_editor/line_length_guideline_column"));
  290. light_editor->get_text_edit()->set_syntax_coloring(EditorSettings::get_singleton()->get("text_editor/syntax_highlighting"));
  291. light_editor->get_text_edit()->set_highlight_all_occurrences(EditorSettings::get_singleton()->get("text_editor/highlight_all_occurrences"));
  292. light_editor->get_text_edit()->cursor_set_blink_enabled(EditorSettings::get_singleton()->get("text_editor/caret_blink"));
  293. light_editor->get_text_edit()->cursor_set_blink_speed(EditorSettings::get_singleton()->get("text_editor/caret_blink_speed"));
  294. light_editor->get_text_edit()->add_constant_override("line_spacing", EditorSettings::get_singleton()->get("text_editor/line_spacing"));
  295. light_editor->get_text_edit()->cursor_set_block_mode(EditorSettings::get_singleton()->get("text_editor/block_caret"));
  296. }
  297. void ShaderEditor::_bind_methods() {
  298. ObjectTypeDB::bind_method("_editor_settings_changed",&ShaderEditor::_editor_settings_changed);
  299. ObjectTypeDB::bind_method("_tab_changed",&ShaderEditor::_tab_changed);
  300. ObjectTypeDB::bind_method("_menu_option",&ShaderEditor::_menu_option);
  301. ObjectTypeDB::bind_method("_params_changed",&ShaderEditor::_params_changed);
  302. ObjectTypeDB::bind_method("_close_callback",&ShaderEditor::_close_callback);
  303. ObjectTypeDB::bind_method("apply_shaders",&ShaderEditor::apply_shaders);
  304. // ObjectTypeDB::bind_method("_close_current_tab",&ShaderEditor::_close_current_tab);
  305. }
  306. void ShaderEditor::ensure_select_current() {
  307. /*
  308. if (tab_container->get_child_count() && tab_container->get_current_tab()>=0) {
  309. ShaderTextEditor *ste = tab_container->get_child(tab_container->get_current_tab())->cast_to<ShaderTextEditor>();
  310. if (!ste)
  311. return;
  312. Ref<Shader> shader = ste->get_edited_shader();
  313. get_scene()->get_root_node()->call("_resource_selected",shader);
  314. }*/
  315. }
  316. void ShaderEditor::edit(const Ref<Shader>& p_shader) {
  317. if (p_shader.is_null())
  318. return;
  319. shader=p_shader;
  320. if (shader->get_mode()==Shader::MODE_MATERIAL) {
  321. vertex_editor->set_edited_shader(p_shader,ShaderLanguage::SHADER_MATERIAL_VERTEX);
  322. fragment_editor->set_edited_shader(p_shader,ShaderLanguage::SHADER_MATERIAL_FRAGMENT);
  323. light_editor->set_edited_shader(shader,ShaderLanguage::SHADER_MATERIAL_LIGHT);
  324. } else if (shader->get_mode()==Shader::MODE_CANVAS_ITEM) {
  325. vertex_editor->set_edited_shader(p_shader,ShaderLanguage::SHADER_CANVAS_ITEM_VERTEX);
  326. fragment_editor->set_edited_shader(p_shader,ShaderLanguage::SHADER_CANVAS_ITEM_FRAGMENT);
  327. light_editor->set_edited_shader(shader,ShaderLanguage::SHADER_CANVAS_ITEM_LIGHT);
  328. }
  329. //vertex_editor->set_edited_shader(shader,ShaderLanguage::SHADER_MATERIAL_VERTEX);
  330. // see if already has it
  331. }
  332. void ShaderEditor::save_external_data() {
  333. if (shader.is_null())
  334. return;
  335. apply_shaders();
  336. if (shader->get_path()!="" && shader->get_path().find("local://")==-1 &&shader->get_path().find("::")==-1) {
  337. //external shader, save it
  338. ResourceSaver::save(shader->get_path(),shader);
  339. }
  340. }
  341. void ShaderEditor::apply_shaders() {
  342. if (shader.is_valid()) {
  343. 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);
  344. shader->set_edited(true);
  345. }
  346. }
  347. void ShaderEditor::_close_callback() {
  348. hide();
  349. }
  350. ShaderEditor::ShaderEditor() {
  351. tab_container = memnew( TabContainer );
  352. add_child(tab_container);
  353. tab_container->set_area_as_parent_rect();
  354. tab_container->set_begin(Point2(0,0));
  355. //tab_container->set_begin(Point2(0,0));
  356. close = memnew( TextureButton );
  357. close->set_anchor_and_margin(MARGIN_LEFT,ANCHOR_END,20);
  358. close->set_anchor_and_margin(MARGIN_RIGHT,ANCHOR_END,4);
  359. close->set_anchor_and_margin(MARGIN_TOP,ANCHOR_BEGIN,2);
  360. add_child(close);
  361. edit_menu = memnew( MenuButton );
  362. add_child(edit_menu);
  363. edit_menu->set_pos(Point2(5,-1));
  364. edit_menu->set_text(TTR("Edit"));
  365. edit_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/undo", TTR("Undo"), KEY_MASK_CMD|KEY_Z), EDIT_UNDO);
  366. edit_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/redo", TTR("Redo"), KEY_MASK_CMD|KEY_Y), EDIT_REDO);
  367. edit_menu->get_popup()->add_separator();
  368. edit_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/cut", TTR("Cut"), KEY_MASK_CMD|KEY_X), EDIT_CUT);
  369. edit_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/copy", TTR("Copy"), KEY_MASK_CMD|KEY_C), EDIT_COPY);
  370. edit_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/paste", TTR("Paste"), KEY_MASK_CMD|KEY_V), EDIT_PASTE);
  371. edit_menu->get_popup()->add_separator();
  372. edit_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/select_all", TTR("Select All"), KEY_MASK_CMD|KEY_A), EDIT_SELECT_ALL);
  373. edit_menu->get_popup()->connect("item_pressed", this,"_menu_option");
  374. search_menu = memnew( MenuButton );
  375. add_child(search_menu);
  376. search_menu->set_pos(Point2(38,-1));
  377. search_menu->set_text(TTR("Search"));
  378. search_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/find", TTR("Find.."), KEY_MASK_CMD|KEY_F), SEARCH_FIND);
  379. search_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/find_next", TTR("Find Next"), KEY_F3), SEARCH_FIND_NEXT);
  380. search_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/find_previous", TTR("Find Previous"), KEY_MASK_SHIFT|KEY_F3), SEARCH_FIND_PREV);
  381. search_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/replace", TTR("Replace.."), KEY_MASK_CMD|KEY_R), SEARCH_REPLACE);
  382. search_menu->get_popup()->add_separator();
  383. // search_menu->get_popup()->add_item("Locate Symbol..",SEARCH_LOCATE_SYMBOL,KEY_MASK_CMD|KEY_K);
  384. search_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/goto_line", TTR("Goto Line.."), KEY_MASK_CMD|KEY_L), SEARCH_GOTO_LINE);
  385. search_menu->get_popup()->connect("item_pressed", this,"_menu_option");
  386. tab_container->connect("tab_changed", this,"_tab_changed");
  387. erase_tab_confirm = memnew( ConfirmationDialog );
  388. add_child(erase_tab_confirm);
  389. erase_tab_confirm->connect("confirmed", this,"_close_current_tab");
  390. goto_line_dialog = memnew(GotoLineDialog);
  391. add_child(goto_line_dialog);
  392. vertex_editor = memnew( ShaderTextEditor );
  393. tab_container->add_child(vertex_editor);
  394. vertex_editor->set_name(TTR("Vertex"));
  395. fragment_editor = memnew( ShaderTextEditor );
  396. tab_container->add_child(fragment_editor);
  397. fragment_editor->set_name(TTR("Fragment"));
  398. light_editor = memnew( ShaderTextEditor );
  399. tab_container->add_child(light_editor);
  400. light_editor->set_name(TTR("Lighting"));
  401. tab_container->set_current_tab(1);
  402. vertex_editor->connect("script_changed", this,"apply_shaders");
  403. fragment_editor->connect("script_changed", this,"apply_shaders");
  404. light_editor->connect("script_changed", this,"apply_shaders");
  405. EditorSettings::get_singleton()->connect("settings_changed",this,"_editor_settings_changed");
  406. _editor_settings_changed();
  407. }
  408. void ShaderEditorPlugin::edit(Object *p_object) {
  409. Shader* s = p_object->cast_to<Shader>();
  410. if (!s || s->cast_to<ShaderGraph>()) {
  411. shader_editor->hide(); //Dont edit ShaderGraph
  412. return;
  413. }
  414. if (_2d && s->get_mode()==Shader::MODE_CANVAS_ITEM)
  415. shader_editor->edit(s);
  416. else if (!_2d && s->get_mode()==Shader::MODE_MATERIAL)
  417. shader_editor->edit(s);
  418. }
  419. bool ShaderEditorPlugin::handles(Object *p_object) const {
  420. bool handles = true;
  421. Shader *shader=p_object->cast_to<Shader>();
  422. if (!shader || shader->cast_to<ShaderGraph>()) // Dont handle ShaderGraph's
  423. handles = false;
  424. if (handles && _2d)
  425. handles = shader->get_mode()==Shader::MODE_CANVAS_ITEM;
  426. else if (handles && !_2d)
  427. return shader->get_mode()==Shader::MODE_MATERIAL;
  428. if (!handles)
  429. shader_editor->hide();
  430. return handles;
  431. }
  432. void ShaderEditorPlugin::make_visible(bool p_visible) {
  433. if (p_visible) {
  434. shader_editor->show();
  435. } else {
  436. shader_editor->apply_shaders();
  437. }
  438. }
  439. void ShaderEditorPlugin::selected_notify() {
  440. shader_editor->ensure_select_current();
  441. }
  442. Dictionary ShaderEditorPlugin::get_state() const {
  443. return shader_editor->get_state();
  444. }
  445. void ShaderEditorPlugin::set_state(const Dictionary& p_state) {
  446. shader_editor->set_state(p_state);
  447. }
  448. void ShaderEditorPlugin::clear() {
  449. shader_editor->clear();
  450. }
  451. void ShaderEditorPlugin::save_external_data() {
  452. shader_editor->save_external_data();
  453. }
  454. void ShaderEditorPlugin::apply_changes() {
  455. shader_editor->apply_shaders();
  456. }
  457. ShaderEditorPlugin::ShaderEditorPlugin(EditorNode *p_node, bool p_2d) {
  458. editor=p_node;
  459. shader_editor = memnew( ShaderEditor );
  460. _2d=p_2d;
  461. if (p_2d)
  462. add_control_to_container(CONTAINER_CANVAS_EDITOR_BOTTOM,shader_editor);
  463. else
  464. add_control_to_container(CONTAINER_SPATIAL_EDITOR_BOTTOM,shader_editor);
  465. // editor->get_viewport()->add_child(shader_editor);
  466. // shader_editor->set_area_as_parent_rect();
  467. shader_editor->hide();
  468. }
  469. ShaderEditorPlugin::~ShaderEditorPlugin() {
  470. }