shader_editor_plugin.cpp 25 KB

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