shader_editor_plugin.cpp 31 KB

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