shader_editor_plugin.cpp 27 KB

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