shader_editor_plugin.cpp 30 KB

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