2
0

shader_editor_plugin.cpp 25 KB

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