shader_editor_plugin.cpp 32 KB

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