shader_editor_plugin.cpp 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739
  1. /*************************************************************************/
  2. /* shader_editor_plugin.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "shader_editor_plugin.h"
  31. #include "core/io/resource_loader.h"
  32. #include "core/io/resource_saver.h"
  33. #include "core/os/keyboard.h"
  34. #include "core/os/os.h"
  35. #include "editor/editor_node.h"
  36. #include "editor/editor_scale.h"
  37. #include "editor/editor_settings.h"
  38. #include "editor/property_editor.h"
  39. #include "servers/visual/shader_types.h"
  40. /*** SHADER 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) {
  45. if (shader == p_shader) {
  46. return;
  47. }
  48. shader = p_shader;
  49. _load_theme_settings();
  50. get_text_edit()->set_text(p_shader->get_code());
  51. get_text_edit()->clear_undo_history();
  52. get_text_edit()->call_deferred("set_h_scroll", 0);
  53. get_text_edit()->call_deferred("set_v_scroll", 0);
  54. _validate_script();
  55. _line_col_changed();
  56. }
  57. void ShaderTextEditor::reload_text() {
  58. ERR_FAIL_COND(shader.is_null());
  59. TextEdit *te = get_text_edit();
  60. int column = te->cursor_get_column();
  61. int row = te->cursor_get_line();
  62. int h = te->get_h_scroll();
  63. int v = te->get_v_scroll();
  64. te->set_text(shader->get_code());
  65. te->cursor_set_line(row);
  66. te->cursor_set_column(column);
  67. te->set_h_scroll(h);
  68. te->set_v_scroll(v);
  69. te->tag_saved_version();
  70. update_line_and_column();
  71. }
  72. void ShaderTextEditor::_load_theme_settings() {
  73. get_text_edit()->clear_colors();
  74. Color background_color = EDITOR_GET("text_editor/highlighting/background_color");
  75. Color completion_background_color = EDITOR_GET("text_editor/highlighting/completion_background_color");
  76. Color completion_selected_color = EDITOR_GET("text_editor/highlighting/completion_selected_color");
  77. Color completion_existing_color = EDITOR_GET("text_editor/highlighting/completion_existing_color");
  78. Color completion_scroll_color = EDITOR_GET("text_editor/highlighting/completion_scroll_color");
  79. Color completion_font_color = EDITOR_GET("text_editor/highlighting/completion_font_color");
  80. Color text_color = EDITOR_GET("text_editor/highlighting/text_color");
  81. Color line_number_color = EDITOR_GET("text_editor/highlighting/line_number_color");
  82. Color caret_color = EDITOR_GET("text_editor/highlighting/caret_color");
  83. Color caret_background_color = EDITOR_GET("text_editor/highlighting/caret_background_color");
  84. Color text_selected_color = EDITOR_GET("text_editor/highlighting/text_selected_color");
  85. Color selection_color = EDITOR_GET("text_editor/highlighting/selection_color");
  86. Color brace_mismatch_color = EDITOR_GET("text_editor/highlighting/brace_mismatch_color");
  87. Color current_line_color = EDITOR_GET("text_editor/highlighting/current_line_color");
  88. Color line_length_guideline_color = EDITOR_GET("text_editor/highlighting/line_length_guideline_color");
  89. Color word_highlighted_color = EDITOR_GET("text_editor/highlighting/word_highlighted_color");
  90. Color number_color = EDITOR_GET("text_editor/highlighting/number_color");
  91. Color function_color = EDITOR_GET("text_editor/highlighting/function_color");
  92. Color member_variable_color = EDITOR_GET("text_editor/highlighting/member_variable_color");
  93. Color mark_color = EDITOR_GET("text_editor/highlighting/mark_color");
  94. Color bookmark_color = EDITOR_GET("text_editor/highlighting/bookmark_color");
  95. Color breakpoint_color = EDITOR_GET("text_editor/highlighting/breakpoint_color");
  96. Color executing_line_color = EDITOR_GET("text_editor/highlighting/executing_line_color");
  97. Color code_folding_color = EDITOR_GET("text_editor/highlighting/code_folding_color");
  98. Color search_result_color = EDITOR_GET("text_editor/highlighting/search_result_color");
  99. Color search_result_border_color = EDITOR_GET("text_editor/highlighting/search_result_border_color");
  100. Color symbol_color = EDITOR_GET("text_editor/highlighting/symbol_color");
  101. Color keyword_color = EDITOR_GET("text_editor/highlighting/keyword_color");
  102. Color comment_color = EDITOR_GET("text_editor/highlighting/comment_color");
  103. get_text_edit()->add_color_override("background_color", background_color);
  104. get_text_edit()->add_color_override("completion_background_color", completion_background_color);
  105. get_text_edit()->add_color_override("completion_selected_color", completion_selected_color);
  106. get_text_edit()->add_color_override("completion_existing_color", completion_existing_color);
  107. get_text_edit()->add_color_override("completion_scroll_color", completion_scroll_color);
  108. get_text_edit()->add_color_override("completion_font_color", completion_font_color);
  109. get_text_edit()->add_color_override("font_color", text_color);
  110. get_text_edit()->add_color_override("line_number_color", line_number_color);
  111. get_text_edit()->add_color_override("caret_color", caret_color);
  112. get_text_edit()->add_color_override("caret_background_color", caret_background_color);
  113. get_text_edit()->add_color_override("font_color_selected", text_selected_color);
  114. get_text_edit()->add_color_override("selection_color", selection_color);
  115. get_text_edit()->add_color_override("brace_mismatch_color", brace_mismatch_color);
  116. get_text_edit()->add_color_override("current_line_color", current_line_color);
  117. get_text_edit()->add_color_override("line_length_guideline_color", line_length_guideline_color);
  118. get_text_edit()->add_color_override("word_highlighted_color", word_highlighted_color);
  119. get_text_edit()->add_color_override("number_color", number_color);
  120. get_text_edit()->add_color_override("function_color", function_color);
  121. get_text_edit()->add_color_override("member_variable_color", member_variable_color);
  122. get_text_edit()->add_color_override("mark_color", mark_color);
  123. get_text_edit()->add_color_override("bookmark_color", bookmark_color);
  124. get_text_edit()->add_color_override("breakpoint_color", breakpoint_color);
  125. get_text_edit()->add_color_override("executing_line_color", executing_line_color);
  126. get_text_edit()->add_color_override("code_folding_color", code_folding_color);
  127. get_text_edit()->add_color_override("search_result_color", search_result_color);
  128. get_text_edit()->add_color_override("search_result_border_color", search_result_border_color);
  129. get_text_edit()->add_color_override("symbol_color", symbol_color);
  130. List<String> keywords;
  131. ShaderLanguage::get_keyword_list(&keywords);
  132. for (List<String>::Element *E = keywords.front(); E; E = E->next()) {
  133. get_text_edit()->add_keyword_color(E->get(), keyword_color);
  134. }
  135. // Colorize built-ins like `COLOR` differently to make them easier
  136. // to distinguish from keywords at a quick glance.
  137. List<String> built_ins;
  138. if (shader.is_valid()) {
  139. for (const Map<StringName, ShaderLanguage::FunctionInfo>::Element *E = ShaderTypes::get_singleton()->get_functions(VisualServer::ShaderMode(shader->get_mode())).front(); E; E = E->next()) {
  140. for (const Map<StringName, ShaderLanguage::BuiltInInfo>::Element *F = E->get().built_ins.front(); F; F = F->next()) {
  141. built_ins.push_back(F->key());
  142. }
  143. }
  144. for (int i = 0; i < ShaderTypes::get_singleton()->get_modes(VisualServer::ShaderMode(shader->get_mode())).size(); i++) {
  145. built_ins.push_back(ShaderTypes::get_singleton()->get_modes(VisualServer::ShaderMode(shader->get_mode()))[i]);
  146. }
  147. }
  148. for (List<String>::Element *E = built_ins.front(); E; E = E->next()) {
  149. get_text_edit()->add_keyword_color(E->get(), member_variable_color);
  150. }
  151. // Colorize comments.
  152. get_text_edit()->add_color_region("/*", "*/", comment_color, false);
  153. get_text_edit()->add_color_region("//", "", comment_color, false);
  154. }
  155. void ShaderTextEditor::_check_shader_mode() {
  156. String type = ShaderLanguage::get_shader_type(get_text_edit()->get_text());
  157. Shader::Mode mode;
  158. if (type == "canvas_item") {
  159. mode = Shader::MODE_CANVAS_ITEM;
  160. } else if (type == "particles") {
  161. mode = Shader::MODE_PARTICLES;
  162. } else {
  163. mode = Shader::MODE_SPATIAL;
  164. }
  165. if (shader->get_mode() != mode) {
  166. shader->set_code(get_text_edit()->get_text());
  167. _load_theme_settings();
  168. }
  169. }
  170. void ShaderTextEditor::_code_complete_script(const String &p_code, List<ScriptCodeCompletionOption> *r_options) {
  171. _check_shader_mode();
  172. ShaderLanguage sl;
  173. String calltip;
  174. 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);
  175. get_text_edit()->set_code_hint(calltip);
  176. }
  177. void ShaderTextEditor::_validate_script() {
  178. _check_shader_mode();
  179. String code = get_text_edit()->get_text();
  180. //List<StringName> params;
  181. //shader->get_param_list(&params);
  182. ShaderLanguage sl;
  183. 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());
  184. if (err != OK) {
  185. String error_text = "error(" + itos(sl.get_error_line()) + "): " + sl.get_error_text();
  186. set_error(error_text);
  187. set_error_pos(sl.get_error_line() - 1, 0);
  188. for (int i = 0; i < get_text_edit()->get_line_count(); i++) {
  189. get_text_edit()->set_line_as_marked(i, false);
  190. }
  191. get_text_edit()->set_line_as_marked(sl.get_error_line() - 1, true);
  192. } else {
  193. for (int i = 0; i < get_text_edit()->get_line_count(); i++) {
  194. get_text_edit()->set_line_as_marked(i, false);
  195. }
  196. set_error("");
  197. }
  198. emit_signal("script_changed");
  199. }
  200. void ShaderTextEditor::_bind_methods() {
  201. }
  202. ShaderTextEditor::ShaderTextEditor() {
  203. }
  204. /*** SCRIPT EDITOR ******/
  205. void ShaderEditor::_menu_option(int p_option) {
  206. switch (p_option) {
  207. case EDIT_UNDO: {
  208. shader_editor->get_text_edit()->undo();
  209. } break;
  210. case EDIT_REDO: {
  211. shader_editor->get_text_edit()->redo();
  212. } break;
  213. case EDIT_CUT: {
  214. shader_editor->get_text_edit()->cut();
  215. } break;
  216. case EDIT_COPY: {
  217. shader_editor->get_text_edit()->copy();
  218. } break;
  219. case EDIT_PASTE: {
  220. shader_editor->get_text_edit()->paste();
  221. } break;
  222. case EDIT_SELECT_ALL: {
  223. shader_editor->get_text_edit()->select_all();
  224. } break;
  225. case EDIT_MOVE_LINE_UP: {
  226. shader_editor->move_lines_up();
  227. } break;
  228. case EDIT_MOVE_LINE_DOWN: {
  229. shader_editor->move_lines_down();
  230. } break;
  231. case EDIT_INDENT_LEFT: {
  232. if (shader.is_null()) {
  233. return;
  234. }
  235. TextEdit *tx = shader_editor->get_text_edit();
  236. tx->indent_left();
  237. } break;
  238. case EDIT_INDENT_RIGHT: {
  239. if (shader.is_null()) {
  240. return;
  241. }
  242. TextEdit *tx = shader_editor->get_text_edit();
  243. tx->indent_right();
  244. } break;
  245. case EDIT_DELETE_LINE: {
  246. shader_editor->delete_lines();
  247. } break;
  248. case EDIT_CLONE_DOWN: {
  249. shader_editor->clone_lines_down();
  250. } break;
  251. case EDIT_TOGGLE_COMMENT: {
  252. if (shader.is_null()) {
  253. return;
  254. }
  255. shader_editor->toggle_inline_comment("//");
  256. } break;
  257. case EDIT_COMPLETE: {
  258. shader_editor->get_text_edit()->query_code_comple();
  259. } break;
  260. case SEARCH_FIND: {
  261. shader_editor->get_find_replace_bar()->popup_search();
  262. } break;
  263. case SEARCH_FIND_NEXT: {
  264. shader_editor->get_find_replace_bar()->search_next();
  265. } break;
  266. case SEARCH_FIND_PREV: {
  267. shader_editor->get_find_replace_bar()->search_prev();
  268. } break;
  269. case SEARCH_REPLACE: {
  270. shader_editor->get_find_replace_bar()->popup_replace();
  271. } break;
  272. case SEARCH_GOTO_LINE: {
  273. goto_line_dialog->popup_find_line(shader_editor->get_text_edit());
  274. } break;
  275. case BOOKMARK_TOGGLE: {
  276. shader_editor->toggle_bookmark();
  277. } break;
  278. case BOOKMARK_GOTO_NEXT: {
  279. shader_editor->goto_next_bookmark();
  280. } break;
  281. case BOOKMARK_GOTO_PREV: {
  282. shader_editor->goto_prev_bookmark();
  283. } break;
  284. case BOOKMARK_REMOVE_ALL: {
  285. shader_editor->remove_all_bookmarks();
  286. } break;
  287. case HELP_DOCS: {
  288. OS::get_singleton()->shell_open("https://docs.godotengine.org/en/3.3/tutorials/shading/shading_reference/index.html");
  289. } break;
  290. }
  291. if (p_option != SEARCH_FIND && p_option != SEARCH_REPLACE && p_option != SEARCH_GOTO_LINE) {
  292. shader_editor->get_text_edit()->call_deferred("grab_focus");
  293. }
  294. }
  295. void ShaderEditor::_notification(int p_what) {
  296. if (p_what == MainLoop::NOTIFICATION_WM_FOCUS_IN) {
  297. _check_for_external_edit();
  298. }
  299. }
  300. void ShaderEditor::_params_changed() {
  301. shader_editor->_validate_script();
  302. }
  303. void ShaderEditor::_editor_settings_changed() {
  304. shader_editor->get_text_edit()->set_auto_brace_completion(EditorSettings::get_singleton()->get("text_editor/completion/auto_brace_complete"));
  305. shader_editor->get_text_edit()->set_scroll_pass_end_of_file(EditorSettings::get_singleton()->get("text_editor/cursor/scroll_past_end_of_file"));
  306. shader_editor->get_text_edit()->set_indent_size(EditorSettings::get_singleton()->get("text_editor/indent/size"));
  307. shader_editor->get_text_edit()->set_indent_using_spaces(EditorSettings::get_singleton()->get("text_editor/indent/type"));
  308. shader_editor->get_text_edit()->set_auto_indent(EditorSettings::get_singleton()->get("text_editor/indent/auto_indent"));
  309. shader_editor->get_text_edit()->set_draw_tabs(EditorSettings::get_singleton()->get("text_editor/indent/draw_tabs"));
  310. shader_editor->get_text_edit()->set_draw_spaces(EditorSettings::get_singleton()->get("text_editor/indent/draw_spaces"));
  311. shader_editor->get_text_edit()->set_show_line_numbers(EditorSettings::get_singleton()->get("text_editor/appearance/show_line_numbers"));
  312. shader_editor->get_text_edit()->set_syntax_coloring(EditorSettings::get_singleton()->get("text_editor/highlighting/syntax_highlighting"));
  313. shader_editor->get_text_edit()->set_highlight_all_occurrences(EditorSettings::get_singleton()->get("text_editor/highlighting/highlight_all_occurrences"));
  314. shader_editor->get_text_edit()->set_highlight_current_line(EditorSettings::get_singleton()->get("text_editor/highlighting/highlight_current_line"));
  315. shader_editor->get_text_edit()->cursor_set_blink_enabled(EditorSettings::get_singleton()->get("text_editor/cursor/caret_blink"));
  316. shader_editor->get_text_edit()->cursor_set_blink_speed(EditorSettings::get_singleton()->get("text_editor/cursor/caret_blink_speed"));
  317. shader_editor->get_text_edit()->add_constant_override("line_spacing", EditorSettings::get_singleton()->get("text_editor/theme/line_spacing"));
  318. shader_editor->get_text_edit()->cursor_set_block_mode(EditorSettings::get_singleton()->get("text_editor/cursor/block_caret"));
  319. shader_editor->get_text_edit()->set_smooth_scroll_enabled(EditorSettings::get_singleton()->get("text_editor/navigation/smooth_scrolling"));
  320. shader_editor->get_text_edit()->set_v_scroll_speed(EditorSettings::get_singleton()->get("text_editor/navigation/v_scroll_speed"));
  321. shader_editor->get_text_edit()->set_draw_minimap(EditorSettings::get_singleton()->get("text_editor/navigation/show_minimap"));
  322. shader_editor->get_text_edit()->set_minimap_width((int)EditorSettings::get_singleton()->get("text_editor/navigation/minimap_width") * EDSCALE);
  323. shader_editor->get_text_edit()->set_show_line_length_guideline(EditorSettings::get_singleton()->get("text_editor/appearance/show_line_length_guideline"));
  324. shader_editor->get_text_edit()->set_line_length_guideline_column(EditorSettings::get_singleton()->get("text_editor/appearance/line_length_guideline_column"));
  325. shader_editor->get_text_edit()->set_breakpoint_gutter_enabled(false);
  326. }
  327. void ShaderEditor::_bind_methods() {
  328. ClassDB::bind_method("_reload_shader_from_disk", &ShaderEditor::_reload_shader_from_disk);
  329. ClassDB::bind_method("_editor_settings_changed", &ShaderEditor::_editor_settings_changed);
  330. ClassDB::bind_method("_text_edit_gui_input", &ShaderEditor::_text_edit_gui_input);
  331. ClassDB::bind_method("_update_bookmark_list", &ShaderEditor::_update_bookmark_list);
  332. ClassDB::bind_method("_bookmark_item_pressed", &ShaderEditor::_bookmark_item_pressed);
  333. ClassDB::bind_method("_menu_option", &ShaderEditor::_menu_option);
  334. ClassDB::bind_method("_params_changed", &ShaderEditor::_params_changed);
  335. ClassDB::bind_method("apply_shaders", &ShaderEditor::apply_shaders);
  336. ClassDB::bind_method("save_external_data", &ShaderEditor::save_external_data);
  337. }
  338. void ShaderEditor::ensure_select_current() {
  339. /*
  340. if (tab_container->get_child_count() && tab_container->get_current_tab()>=0) {
  341. ShaderTextEditor *ste = Object::cast_to<ShaderTextEditor>(tab_container->get_child(tab_container->get_current_tab()));
  342. if (!ste)
  343. return;
  344. Ref<Shader> shader = ste->get_edited_shader();
  345. get_scene()->get_root_node()->call("_resource_selected",shader);
  346. }*/
  347. }
  348. void ShaderEditor::goto_line_selection(int p_line, int p_begin, int p_end) {
  349. shader_editor->goto_line_selection(p_line, p_begin, p_end);
  350. }
  351. void ShaderEditor::_check_for_external_edit() {
  352. if (shader.is_null() || !shader.is_valid()) {
  353. return;
  354. }
  355. // internal shader.
  356. if (shader->get_path() == "" || shader->get_path().find("local://") != -1 || shader->get_path().find("::") != -1) {
  357. return;
  358. }
  359. bool use_autoreload = bool(EDITOR_DEF("text_editor/files/auto_reload_scripts_on_external_change", false));
  360. if (shader->get_last_modified_time() != FileAccess::get_modified_time(shader->get_path())) {
  361. if (use_autoreload) {
  362. _reload_shader_from_disk();
  363. } else {
  364. disk_changed->call_deferred("popup_centered");
  365. }
  366. }
  367. }
  368. void ShaderEditor::_reload_shader_from_disk() {
  369. Ref<Shader> rel_shader = ResourceLoader::load(shader->get_path(), shader->get_class(), true);
  370. ERR_FAIL_COND(!rel_shader.is_valid());
  371. shader->set_code(rel_shader->get_code());
  372. shader->set_last_modified_time(rel_shader->get_last_modified_time());
  373. shader_editor->reload_text();
  374. }
  375. void ShaderEditor::edit(const Ref<Shader> &p_shader) {
  376. if (p_shader.is_null() || !p_shader->is_text_shader()) {
  377. return;
  378. }
  379. if (shader == p_shader) {
  380. return;
  381. }
  382. shader = p_shader;
  383. shader_editor->set_edited_shader(p_shader);
  384. //vertex_editor->set_edited_shader(shader,ShaderLanguage::SHADER_MATERIAL_VERTEX);
  385. // see if already has it
  386. }
  387. void ShaderEditor::save_external_data(const String &p_str) {
  388. if (shader.is_null()) {
  389. disk_changed->hide();
  390. return;
  391. }
  392. apply_shaders();
  393. if (shader->get_path() != "" && shader->get_path().find("local://") == -1 && shader->get_path().find("::") == -1) {
  394. //external shader, save it
  395. ResourceSaver::save(shader->get_path(), shader);
  396. }
  397. disk_changed->hide();
  398. }
  399. void ShaderEditor::apply_shaders() {
  400. if (shader.is_valid()) {
  401. String shader_code = shader->get_code();
  402. String editor_code = shader_editor->get_text_edit()->get_text();
  403. if (shader_code != editor_code) {
  404. shader->set_code(editor_code);
  405. shader->set_edited(true);
  406. }
  407. }
  408. }
  409. void ShaderEditor::_text_edit_gui_input(const Ref<InputEvent> &ev) {
  410. Ref<InputEventMouseButton> mb = ev;
  411. if (mb.is_valid()) {
  412. if (mb->get_button_index() == BUTTON_RIGHT && mb->is_pressed()) {
  413. int col, row;
  414. TextEdit *tx = shader_editor->get_text_edit();
  415. tx->_get_mouse_pos(mb->get_global_position() - tx->get_global_position(), row, col);
  416. tx->set_right_click_moves_caret(EditorSettings::get_singleton()->get("text_editor/cursor/right_click_moves_caret"));
  417. if (tx->is_right_click_moving_caret()) {
  418. if (tx->is_selection_active()) {
  419. int from_line = tx->get_selection_from_line();
  420. int to_line = tx->get_selection_to_line();
  421. int from_column = tx->get_selection_from_column();
  422. int to_column = tx->get_selection_to_column();
  423. if (row < from_line || row > to_line || (row == from_line && col < from_column) || (row == to_line && col > to_column)) {
  424. // Right click is outside the selected text
  425. tx->deselect();
  426. }
  427. }
  428. if (!tx->is_selection_active()) {
  429. tx->cursor_set_line(row, true, false);
  430. tx->cursor_set_column(col);
  431. }
  432. }
  433. _make_context_menu(tx->is_selection_active(), get_local_mouse_position());
  434. }
  435. }
  436. Ref<InputEventKey> k = ev;
  437. if (k.is_valid() && k->is_pressed() && k->get_scancode() == KEY_MENU) {
  438. TextEdit *tx = shader_editor->get_text_edit();
  439. _make_context_menu(tx->is_selection_active(), (get_global_transform().inverse() * tx->get_global_transform()).xform(tx->_get_cursor_pixel_pos()));
  440. context_menu->grab_focus();
  441. }
  442. }
  443. void ShaderEditor::_update_bookmark_list() {
  444. bookmarks_menu->clear();
  445. bookmarks_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/toggle_bookmark"), BOOKMARK_TOGGLE);
  446. bookmarks_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/remove_all_bookmarks"), BOOKMARK_REMOVE_ALL);
  447. bookmarks_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/goto_next_bookmark"), BOOKMARK_GOTO_NEXT);
  448. bookmarks_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/goto_previous_bookmark"), BOOKMARK_GOTO_PREV);
  449. Array bookmark_list = shader_editor->get_text_edit()->get_bookmarks_array();
  450. if (bookmark_list.size() == 0) {
  451. return;
  452. }
  453. bookmarks_menu->add_separator();
  454. for (int i = 0; i < bookmark_list.size(); i++) {
  455. String line = shader_editor->get_text_edit()->get_line(bookmark_list[i]).strip_edges();
  456. // Limit the size of the line if too big.
  457. if (line.length() > 50) {
  458. line = line.substr(0, 50);
  459. }
  460. bookmarks_menu->add_item(String::num((int)bookmark_list[i] + 1) + " - \"" + line + "\"");
  461. bookmarks_menu->set_item_metadata(bookmarks_menu->get_item_count() - 1, bookmark_list[i]);
  462. }
  463. }
  464. void ShaderEditor::_bookmark_item_pressed(int p_idx) {
  465. if (p_idx < 4) { // Any item before the separator.
  466. _menu_option(bookmarks_menu->get_item_id(p_idx));
  467. } else {
  468. shader_editor->goto_line(bookmarks_menu->get_item_metadata(p_idx));
  469. }
  470. }
  471. void ShaderEditor::_make_context_menu(bool p_selection, Vector2 p_position) {
  472. context_menu->clear();
  473. if (p_selection) {
  474. context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/cut"), EDIT_CUT);
  475. context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/copy"), EDIT_COPY);
  476. }
  477. context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/paste"), EDIT_PASTE);
  478. context_menu->add_separator();
  479. context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/select_all"), EDIT_SELECT_ALL);
  480. context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/undo"), EDIT_UNDO);
  481. context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/redo"), EDIT_REDO);
  482. context_menu->add_separator();
  483. context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/indent_left"), EDIT_INDENT_LEFT);
  484. context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/indent_right"), EDIT_INDENT_RIGHT);
  485. context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/toggle_comment"), EDIT_TOGGLE_COMMENT);
  486. context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/toggle_bookmark"), BOOKMARK_TOGGLE);
  487. context_menu->set_position(get_global_transform().xform(p_position));
  488. context_menu->set_size(Vector2(1, 1));
  489. context_menu->popup();
  490. }
  491. ShaderEditor::ShaderEditor(EditorNode *p_node) {
  492. shader_editor = memnew(ShaderTextEditor);
  493. shader_editor->set_v_size_flags(SIZE_EXPAND_FILL);
  494. shader_editor->add_constant_override("separation", 0);
  495. shader_editor->set_anchors_and_margins_preset(Control::PRESET_WIDE);
  496. shader_editor->connect("script_changed", this, "apply_shaders");
  497. EditorSettings::get_singleton()->connect("settings_changed", this, "_editor_settings_changed");
  498. shader_editor->get_text_edit()->set_callhint_settings(
  499. EditorSettings::get_singleton()->get("text_editor/completion/put_callhint_tooltip_below_current_line"),
  500. EditorSettings::get_singleton()->get("text_editor/completion/callhint_tooltip_offset"));
  501. shader_editor->get_text_edit()->set_select_identifiers_on_hover(true);
  502. shader_editor->get_text_edit()->set_context_menu_enabled(false);
  503. shader_editor->get_text_edit()->connect("gui_input", this, "_text_edit_gui_input");
  504. shader_editor->update_editor_settings();
  505. context_menu = memnew(PopupMenu);
  506. add_child(context_menu);
  507. context_menu->connect("id_pressed", this, "_menu_option");
  508. context_menu->set_hide_on_window_lose_focus(true);
  509. VBoxContainer *main_container = memnew(VBoxContainer);
  510. HBoxContainer *hbc = memnew(HBoxContainer);
  511. edit_menu = memnew(MenuButton);
  512. edit_menu->set_text(TTR("Edit"));
  513. edit_menu->set_switch_on_hover(true);
  514. edit_menu->get_popup()->set_hide_on_window_lose_focus(true);
  515. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/undo"), EDIT_UNDO);
  516. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/redo"), EDIT_REDO);
  517. edit_menu->get_popup()->add_separator();
  518. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/cut"), EDIT_CUT);
  519. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/copy"), EDIT_COPY);
  520. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/paste"), EDIT_PASTE);
  521. edit_menu->get_popup()->add_separator();
  522. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/select_all"), EDIT_SELECT_ALL);
  523. edit_menu->get_popup()->add_separator();
  524. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/move_up"), EDIT_MOVE_LINE_UP);
  525. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/move_down"), EDIT_MOVE_LINE_DOWN);
  526. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/indent_left"), EDIT_INDENT_LEFT);
  527. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/indent_right"), EDIT_INDENT_RIGHT);
  528. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/delete_line"), EDIT_DELETE_LINE);
  529. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/toggle_comment"), EDIT_TOGGLE_COMMENT);
  530. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/clone_down"), EDIT_CLONE_DOWN);
  531. edit_menu->get_popup()->add_separator();
  532. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/complete_symbol"), EDIT_COMPLETE);
  533. edit_menu->get_popup()->connect("id_pressed", this, "_menu_option");
  534. search_menu = memnew(MenuButton);
  535. search_menu->set_text(TTR("Search"));
  536. search_menu->set_switch_on_hover(true);
  537. search_menu->get_popup()->set_hide_on_window_lose_focus(true);
  538. search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/find"), SEARCH_FIND);
  539. search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/find_next"), SEARCH_FIND_NEXT);
  540. search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/find_previous"), SEARCH_FIND_PREV);
  541. search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/replace"), SEARCH_REPLACE);
  542. search_menu->get_popup()->connect("id_pressed", this, "_menu_option");
  543. MenuButton *goto_menu = memnew(MenuButton);
  544. goto_menu->set_text(TTR("Go To"));
  545. goto_menu->set_switch_on_hover(true);
  546. goto_menu->get_popup()->connect("id_pressed", this, "_menu_option");
  547. goto_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/goto_line"), SEARCH_GOTO_LINE);
  548. goto_menu->get_popup()->add_separator();
  549. bookmarks_menu = memnew(PopupMenu);
  550. bookmarks_menu->set_name("Bookmarks");
  551. goto_menu->get_popup()->add_child(bookmarks_menu);
  552. goto_menu->get_popup()->add_submenu_item(TTR("Bookmarks"), "Bookmarks");
  553. _update_bookmark_list();
  554. bookmarks_menu->connect("about_to_show", this, "_update_bookmark_list");
  555. bookmarks_menu->connect("index_pressed", this, "_bookmark_item_pressed");
  556. help_menu = memnew(MenuButton);
  557. help_menu->set_text(TTR("Help"));
  558. help_menu->set_switch_on_hover(true);
  559. help_menu->get_popup()->add_icon_item(p_node->get_gui_base()->get_icon("Instance", "EditorIcons"), TTR("Online Docs"), HELP_DOCS);
  560. help_menu->get_popup()->connect("id_pressed", this, "_menu_option");
  561. add_child(main_container);
  562. main_container->add_child(hbc);
  563. hbc->add_child(search_menu);
  564. hbc->add_child(edit_menu);
  565. hbc->add_child(goto_menu);
  566. hbc->add_child(help_menu);
  567. hbc->add_style_override("panel", p_node->get_gui_base()->get_stylebox("ScriptEditorPanel", "EditorStyles"));
  568. main_container->add_child(shader_editor);
  569. goto_line_dialog = memnew(GotoLineDialog);
  570. add_child(goto_line_dialog);
  571. disk_changed = memnew(ConfirmationDialog);
  572. VBoxContainer *vbc = memnew(VBoxContainer);
  573. disk_changed->add_child(vbc);
  574. Label *dl = memnew(Label);
  575. dl->set_text(TTR("This shader has been modified on on disk.\nWhat action should be taken?"));
  576. vbc->add_child(dl);
  577. disk_changed->connect("confirmed", this, "_reload_shader_from_disk");
  578. disk_changed->get_ok()->set_text(TTR("Reload"));
  579. disk_changed->add_button(TTR("Resave"), !OS::get_singleton()->get_swap_ok_cancel(), "resave");
  580. disk_changed->connect("custom_action", this, "save_external_data");
  581. add_child(disk_changed);
  582. _editor_settings_changed();
  583. }
  584. void ShaderEditorPlugin::edit(Object *p_object) {
  585. Shader *s = Object::cast_to<Shader>(p_object);
  586. shader_editor->edit(s);
  587. }
  588. bool ShaderEditorPlugin::handles(Object *p_object) const {
  589. Shader *shader = Object::cast_to<Shader>(p_object);
  590. return shader != nullptr && shader->is_text_shader();
  591. }
  592. void ShaderEditorPlugin::make_visible(bool p_visible) {
  593. if (p_visible) {
  594. button->show();
  595. editor->make_bottom_panel_item_visible(shader_editor);
  596. } else {
  597. button->hide();
  598. if (shader_editor->is_visible_in_tree()) {
  599. editor->hide_bottom_panel();
  600. }
  601. shader_editor->apply_shaders();
  602. }
  603. }
  604. void ShaderEditorPlugin::selected_notify() {
  605. shader_editor->ensure_select_current();
  606. }
  607. void ShaderEditorPlugin::save_external_data() {
  608. shader_editor->save_external_data();
  609. }
  610. void ShaderEditorPlugin::apply_changes() {
  611. shader_editor->apply_shaders();
  612. }
  613. ShaderEditorPlugin::ShaderEditorPlugin(EditorNode *p_node) {
  614. editor = p_node;
  615. shader_editor = memnew(ShaderEditor(p_node));
  616. shader_editor->set_custom_minimum_size(Size2(0, 300) * EDSCALE);
  617. button = editor->add_bottom_panel_item(TTR("Shader"), shader_editor);
  618. button->hide();
  619. }
  620. ShaderEditorPlugin::~ShaderEditorPlugin() {
  621. }