shader_editor_plugin.cpp 29 KB

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