shader_editor_plugin.cpp 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811
  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-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 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 "scene/resources/shader_graph.h"
  39. #include "servers/visual/shader_types.h"
  40. /*** SHADER SCRIPT EDITOR ****/
  41. Ref<Shader> ShaderTextEditor::get_edited_shader() const {
  42. return shader;
  43. }
  44. void ShaderTextEditor::set_edited_shader(const Ref<Shader> &p_shader) {
  45. shader = p_shader;
  46. _load_theme_settings();
  47. get_text_edit()->set_text(p_shader->get_code());
  48. _line_col_changed();
  49. }
  50. void ShaderTextEditor::_load_theme_settings() {
  51. get_text_edit()->clear_colors();
  52. Color background_color = EDITOR_DEF("text_editor/highlighting/background_color", Color(0, 0, 0, 0));
  53. Color completion_background_color = EDITOR_DEF("text_editor/highlighting/completion_background_color", Color(0, 0, 0, 0));
  54. Color completion_selected_color = EDITOR_DEF("text_editor/highlighting/completion_selected_color", Color::html("434244"));
  55. Color completion_existing_color = EDITOR_DEF("text_editor/highlighting/completion_existing_color", Color::html("21dfdfdf"));
  56. Color completion_scroll_color = EDITOR_DEF("text_editor/highlighting/completion_scroll_color", Color::html("ffffff"));
  57. Color completion_font_color = EDITOR_DEF("text_editor/highlighting/completion_font_color", Color::html("aaaaaa"));
  58. Color text_color = EDITOR_DEF("text_editor/highlighting/text_color", Color(0, 0, 0));
  59. Color line_number_color = EDITOR_DEF("text_editor/highlighting/line_number_color", Color(0, 0, 0));
  60. Color caret_color = EDITOR_DEF("text_editor/highlighting/caret_color", Color(0, 0, 0));
  61. Color caret_background_color = EDITOR_DEF("text_editor/highlighting/caret_background_color", Color(0, 0, 0));
  62. Color text_selected_color = EDITOR_DEF("text_editor/highlighting/text_selected_color", Color(1, 1, 1));
  63. Color selection_color = EDITOR_DEF("text_editor/highlighting/selection_color", Color(0.2, 0.2, 1));
  64. Color brace_mismatch_color = EDITOR_DEF("text_editor/highlighting/brace_mismatch_color", Color(1, 0.2, 0.2));
  65. Color current_line_color = EDITOR_DEF("text_editor/highlighting/current_line_color", Color(0.3, 0.5, 0.8, 0.15));
  66. Color line_length_guideline_color = EDITOR_DEF("text_editor/highlighting/line_length_guideline_color", Color(0, 0, 0));
  67. Color word_highlighted_color = EDITOR_DEF("text_editor/highlighting/word_highlighted_color", Color(0.8, 0.9, 0.9, 0.15));
  68. Color number_color = EDITOR_DEF("text_editor/highlighting/number_color", Color(0.9, 0.6, 0.0, 2));
  69. Color function_color = EDITOR_DEF("text_editor/highlighting/function_color", Color(0.4, 0.6, 0.8));
  70. Color member_variable_color = EDITOR_DEF("text_editor/highlighting/member_variable_color", Color(0.9, 0.3, 0.3));
  71. Color mark_color = EDITOR_DEF("text_editor/highlighting/mark_color", Color(1.0, 0.4, 0.4, 0.4));
  72. Color breakpoint_color = EDITOR_DEF("text_editor/highlighting/breakpoint_color", Color(0.8, 0.8, 0.4, 0.2));
  73. Color code_folding_color = EDITOR_DEF("text_editor/highlighting/code_folding_color", Color(0.8, 0.8, 0.8, 0.8));
  74. Color search_result_color = EDITOR_DEF("text_editor/highlighting/search_result_color", Color(0.05, 0.25, 0.05, 1));
  75. Color search_result_border_color = EDITOR_DEF("text_editor/highlighting/search_result_border_color", Color(0.1, 0.45, 0.1, 1));
  76. Color symbol_color = EDITOR_DEF("text_editor/highlighting/symbol_color", Color::hex(0x005291ff));
  77. Color keyword_color = EDITOR_DEF("text_editor/highlighting/keyword_color", Color(0.5, 0.0, 0.2));
  78. Color basetype_color = EDITOR_DEF("text_editor/highlighting/base_type_color", Color(0.3, 0.3, 0.0));
  79. Color type_color = EDITOR_DEF("text_editor/highlighting/engine_type_color", Color(0.0, 0.2, 0.4));
  80. Color comment_color = EDITOR_DEF("text_editor/highlighting/comment_color", Color::hex(0x797e7eff));
  81. Color string_color = EDITOR_DEF("text_editor/highlighting/string_color", Color::hex(0x6b6f00ff));
  82. // Adapt
  83. if (EditorSettings::get_singleton()->get("text_editor/theme/color_theme") == "Adaptive") {
  84. Ref<Theme> tm = EditorNode::get_singleton()->get_theme_base()->get_theme();
  85. symbol_color = tm->get_color("text_editor/theme/symbol_color", "Editor");
  86. keyword_color = tm->get_color("text_editor/theme/keyword_color", "Editor");
  87. basetype_color = tm->get_color("text_editor/theme/basetype_color", "Editor");
  88. type_color = tm->get_color("text_editor/theme/type_color", "Editor");
  89. comment_color = tm->get_color("text_editor/theme/comment_color", "Editor");
  90. string_color = tm->get_color("text_editor/theme/string_color", "Editor");
  91. background_color = tm->get_color("text_editor/theme/background_color", "Editor");
  92. completion_background_color = tm->get_color("text_editor/theme/completion_background_color", "Editor");
  93. completion_selected_color = tm->get_color("text_editor/theme/completion_selected_color", "Editor");
  94. completion_existing_color = tm->get_color("text_editor/theme/completion_existing_color", "Editor");
  95. completion_scroll_color = tm->get_color("text_editor/theme/completion_scroll_color", "Editor");
  96. completion_font_color = tm->get_color("text_editor/theme/completion_font_color", "Editor");
  97. text_color = tm->get_color("text_editor/theme/text_color", "Editor");
  98. line_number_color = tm->get_color("text_editor/theme/line_number_color", "Editor");
  99. caret_color = tm->get_color("text_editor/theme/caret_color", "Editor");
  100. caret_background_color = tm->get_color("text_editor/theme/caret_background_color", "Editor");
  101. text_selected_color = tm->get_color("text_editor/theme/text_selected_color", "Editor");
  102. selection_color = tm->get_color("text_editor/theme/selection_color", "Editor");
  103. brace_mismatch_color = tm->get_color("text_editor/theme/brace_mismatch_color", "Editor");
  104. current_line_color = tm->get_color("text_editor/theme/current_line_color", "Editor");
  105. line_length_guideline_color = tm->get_color("text_editor/theme/line_length_guideline_color", "Editor");
  106. word_highlighted_color = tm->get_color("text_editor/theme/word_highlighted_color", "Editor");
  107. number_color = tm->get_color("text_editor/theme/number_color", "Editor");
  108. function_color = tm->get_color("text_editor/theme/function_color", "Editor");
  109. member_variable_color = tm->get_color("text_editor/theme/member_variable_color", "Editor");
  110. mark_color = tm->get_color("text_editor/theme/mark_color", "Editor");
  111. breakpoint_color = tm->get_color("text_editor/theme/breakpoint_color", "Editor");
  112. code_folding_color = tm->get_color("text_editor/theme/code_folding_color", "Editor");
  113. search_result_color = tm->get_color("text_editor/theme/search_result_color", "Editor");
  114. search_result_border_color = tm->get_color("text_editor/theme/search_result_border_color", "Editor");
  115. }
  116. get_text_edit()->add_color_override("background_color", background_color);
  117. get_text_edit()->add_color_override("completion_background_color", completion_background_color);
  118. get_text_edit()->add_color_override("completion_selected_color", completion_selected_color);
  119. get_text_edit()->add_color_override("completion_existing_color", completion_existing_color);
  120. get_text_edit()->add_color_override("completion_scroll_color", completion_scroll_color);
  121. get_text_edit()->add_color_override("completion_font_color", completion_font_color);
  122. get_text_edit()->add_color_override("font_color", text_color);
  123. get_text_edit()->add_color_override("line_number_color", line_number_color);
  124. get_text_edit()->add_color_override("caret_color", caret_color);
  125. get_text_edit()->add_color_override("caret_background_color", caret_background_color);
  126. get_text_edit()->add_color_override("font_selected_color", text_selected_color);
  127. get_text_edit()->add_color_override("selection_color", selection_color);
  128. get_text_edit()->add_color_override("brace_mismatch_color", brace_mismatch_color);
  129. get_text_edit()->add_color_override("current_line_color", current_line_color);
  130. get_text_edit()->add_color_override("line_length_guideline_color", line_length_guideline_color);
  131. get_text_edit()->add_color_override("word_highlighted_color", word_highlighted_color);
  132. get_text_edit()->add_color_override("number_color", number_color);
  133. get_text_edit()->add_color_override("function_color", function_color);
  134. get_text_edit()->add_color_override("member_variable_color", member_variable_color);
  135. get_text_edit()->add_color_override("mark_color", mark_color);
  136. get_text_edit()->add_color_override("breakpoint_color", breakpoint_color);
  137. get_text_edit()->add_color_override("code_folding_color", code_folding_color);
  138. get_text_edit()->add_color_override("search_result_color", search_result_color);
  139. get_text_edit()->add_color_override("search_result_border_color", search_result_border_color);
  140. get_text_edit()->add_color_override("symbol_color", symbol_color);
  141. List<String> keywords;
  142. ShaderLanguage::get_keyword_list(&keywords);
  143. if (shader.is_valid()) {
  144. for (const Map<StringName, ShaderLanguage::FunctionInfo>::Element *E = ShaderTypes::get_singleton()->get_functions(VisualServer::ShaderMode(shader->get_mode())).front(); E; E = E->next()) {
  145. for (const Map<StringName, ShaderLanguage::BuiltInInfo>::Element *F = E->get().built_ins.front(); F; F = F->next()) {
  146. keywords.push_back(F->key());
  147. }
  148. }
  149. for (const Set<String>::Element *E = ShaderTypes::get_singleton()->get_modes(VisualServer::ShaderMode(shader->get_mode())).front(); E; E = E->next()) {
  150. keywords.push_back(E->get());
  151. }
  152. }
  153. for (List<String>::Element *E = keywords.front(); E; E = E->next()) {
  154. get_text_edit()->add_keyword_color(E->get(), keyword_color);
  155. }
  156. //colorize core types
  157. //Color basetype_color= EDITOR_DEF("text_editor/base_type_color",Color(0.3,0.3,0.0));
  158. //colorize comments
  159. get_text_edit()->add_color_region("/*", "*/", comment_color, false);
  160. get_text_edit()->add_color_region("//", "", comment_color, false);
  161. /*//colorize strings
  162. Color string_color = EDITOR_DEF("text_editor/string_color",Color::hex(0x6b6f00ff));
  163. List<String> strings;
  164. shader->get_shader_mode()->get_string_delimiters(&strings);
  165. for (List<String>::Element *E=strings.front();E;E=E->next()) {
  166. String string = E->get();
  167. String beg = string.get_slice(" ",0);
  168. String end = string.get_slice_count(" ")>1?string.get_slice(" ",1):String();
  169. get_text_edit()->add_color_region(beg,end,string_color,end=="");
  170. }*/
  171. }
  172. void ShaderTextEditor::_check_shader_mode() {
  173. String type = ShaderLanguage::get_shader_type(get_text_edit()->get_text());
  174. print_line("type is: " + type);
  175. Shader::Mode mode;
  176. if (type == "canvas_item") {
  177. mode = Shader::MODE_CANVAS_ITEM;
  178. } else if (type == "particles") {
  179. mode = Shader::MODE_PARTICLES;
  180. } else {
  181. mode = Shader::MODE_SPATIAL;
  182. }
  183. if (shader->get_mode() != mode) {
  184. shader->set_code(get_text_edit()->get_text());
  185. _load_theme_settings();
  186. }
  187. }
  188. void ShaderTextEditor::_code_complete_script(const String &p_code, List<String> *r_options) {
  189. _check_shader_mode();
  190. ShaderLanguage sl;
  191. String calltip;
  192. 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);
  193. if (err != OK)
  194. ERR_PRINT("Shaderlang complete failed");
  195. if (calltip != "") {
  196. get_text_edit()->set_code_hint(calltip);
  197. }
  198. }
  199. void ShaderTextEditor::_validate_script() {
  200. _check_shader_mode();
  201. String code = get_text_edit()->get_text();
  202. //List<StringName> params;
  203. //shader->get_param_list(&params);
  204. ShaderLanguage sl;
  205. 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());
  206. if (err != OK) {
  207. String error_text = "error(" + itos(sl.get_error_line()) + "): " + sl.get_error_text();
  208. set_error(error_text);
  209. for (int i = 0; i < get_text_edit()->get_line_count(); i++)
  210. get_text_edit()->set_line_as_marked(i, false);
  211. get_text_edit()->set_line_as_marked(sl.get_error_line() - 1, true);
  212. } else {
  213. for (int i = 0; i < get_text_edit()->get_line_count(); i++)
  214. get_text_edit()->set_line_as_marked(i, false);
  215. set_error("");
  216. }
  217. emit_signal("script_changed");
  218. }
  219. void ShaderTextEditor::_bind_methods() {
  220. }
  221. ShaderTextEditor::ShaderTextEditor() {
  222. }
  223. /*** SCRIPT EDITOR ******/
  224. void ShaderEditor::_menu_option(int p_option) {
  225. switch (p_option) {
  226. case EDIT_UNDO: {
  227. shader_editor->get_text_edit()->undo();
  228. } break;
  229. case EDIT_REDO: {
  230. shader_editor->get_text_edit()->redo();
  231. } break;
  232. case EDIT_CUT: {
  233. shader_editor->get_text_edit()->cut();
  234. } break;
  235. case EDIT_COPY: {
  236. shader_editor->get_text_edit()->copy();
  237. } break;
  238. case EDIT_PASTE: {
  239. shader_editor->get_text_edit()->paste();
  240. } break;
  241. case EDIT_SELECT_ALL: {
  242. shader_editor->get_text_edit()->select_all();
  243. } break;
  244. case EDIT_MOVE_LINE_UP: {
  245. TextEdit *tx = shader_editor->get_text_edit();
  246. if (shader.is_null())
  247. return;
  248. tx->begin_complex_operation();
  249. if (tx->is_selection_active()) {
  250. int from_line = tx->get_selection_from_line();
  251. int from_col = tx->get_selection_from_column();
  252. int to_line = tx->get_selection_to_line();
  253. int to_column = tx->get_selection_to_column();
  254. for (int i = from_line; i <= to_line; i++) {
  255. int line_id = i;
  256. int next_id = i - 1;
  257. if (line_id == 0 || next_id < 0)
  258. return;
  259. tx->swap_lines(line_id, next_id);
  260. tx->cursor_set_line(next_id);
  261. }
  262. int from_line_up = from_line > 0 ? from_line - 1 : from_line;
  263. int to_line_up = to_line > 0 ? to_line - 1 : to_line;
  264. tx->select(from_line_up, from_col, to_line_up, to_column);
  265. } else {
  266. int line_id = tx->cursor_get_line();
  267. int next_id = line_id - 1;
  268. if (line_id == 0 || next_id < 0)
  269. return;
  270. tx->swap_lines(line_id, next_id);
  271. tx->cursor_set_line(next_id);
  272. }
  273. tx->end_complex_operation();
  274. tx->update();
  275. } break;
  276. case EDIT_MOVE_LINE_DOWN: {
  277. TextEdit *tx = shader_editor->get_text_edit();
  278. if (shader.is_null())
  279. return;
  280. tx->begin_complex_operation();
  281. if (tx->is_selection_active()) {
  282. int from_line = tx->get_selection_from_line();
  283. int from_col = tx->get_selection_from_column();
  284. int to_line = tx->get_selection_to_line();
  285. int to_column = tx->get_selection_to_column();
  286. for (int i = to_line; i >= from_line; i--) {
  287. int line_id = i;
  288. int next_id = i + 1;
  289. if (line_id == tx->get_line_count() - 1 || next_id > tx->get_line_count())
  290. return;
  291. tx->swap_lines(line_id, next_id);
  292. tx->cursor_set_line(next_id);
  293. }
  294. int from_line_down = from_line < tx->get_line_count() ? from_line + 1 : from_line;
  295. int to_line_down = to_line < tx->get_line_count() ? to_line + 1 : to_line;
  296. tx->select(from_line_down, from_col, to_line_down, to_column);
  297. } else {
  298. int line_id = tx->cursor_get_line();
  299. int next_id = line_id + 1;
  300. if (line_id == tx->get_line_count() - 1 || next_id > tx->get_line_count())
  301. return;
  302. tx->swap_lines(line_id, next_id);
  303. tx->cursor_set_line(next_id);
  304. }
  305. tx->end_complex_operation();
  306. tx->update();
  307. } break;
  308. case EDIT_INDENT_LEFT: {
  309. TextEdit *tx = shader_editor->get_text_edit();
  310. if (shader.is_null())
  311. return;
  312. tx->indent_left();
  313. } break;
  314. case EDIT_INDENT_RIGHT: {
  315. TextEdit *tx = shader_editor->get_text_edit();
  316. if (shader.is_null())
  317. return;
  318. tx->indent_right();
  319. } break;
  320. case EDIT_DELETE_LINE: {
  321. TextEdit *tx = shader_editor->get_text_edit();
  322. if (shader.is_null())
  323. return;
  324. tx->begin_complex_operation();
  325. int line = tx->cursor_get_line();
  326. tx->set_line(tx->cursor_get_line(), "");
  327. tx->backspace_at_cursor();
  328. tx->cursor_set_line(line);
  329. tx->end_complex_operation();
  330. } break;
  331. case EDIT_CLONE_DOWN: {
  332. TextEdit *tx = shader_editor->get_text_edit();
  333. if (shader.is_null())
  334. return;
  335. int from_line = tx->cursor_get_line();
  336. int to_line = tx->cursor_get_line();
  337. int column = tx->cursor_get_column();
  338. if (tx->is_selection_active()) {
  339. from_line = tx->get_selection_from_line();
  340. to_line = tx->get_selection_to_line();
  341. column = tx->cursor_get_column();
  342. }
  343. int next_line = to_line + 1;
  344. tx->begin_complex_operation();
  345. for (int i = from_line; i <= to_line; i++) {
  346. if (i >= tx->get_line_count() - 1) {
  347. tx->set_line(i, tx->get_line(i) + "\n");
  348. }
  349. String line_clone = tx->get_line(i);
  350. tx->insert_at(line_clone, next_line);
  351. next_line++;
  352. }
  353. tx->cursor_set_column(column);
  354. if (tx->is_selection_active()) {
  355. tx->select(to_line + 1, tx->get_selection_from_column(), next_line - 1, tx->get_selection_to_column());
  356. }
  357. tx->end_complex_operation();
  358. tx->update();
  359. } break;
  360. case EDIT_TOGGLE_COMMENT: {
  361. TextEdit *tx = shader_editor->get_text_edit();
  362. if (shader.is_null())
  363. return;
  364. tx->begin_complex_operation();
  365. if (tx->is_selection_active()) {
  366. int begin = tx->get_selection_from_line();
  367. int end = tx->get_selection_to_line();
  368. // End of selection ends on the first column of the last line, ignore it.
  369. if (tx->get_selection_to_column() == 0)
  370. end -= 1;
  371. // Check if all lines in the selected block are commented
  372. bool is_commented = true;
  373. for (int i = begin; i <= end; i++) {
  374. if (!tx->get_line(i).begins_with("//")) {
  375. is_commented = false;
  376. break;
  377. }
  378. }
  379. for (int i = begin; i <= end; i++) {
  380. String line_text = tx->get_line(i);
  381. if (line_text.strip_edges().empty()) {
  382. line_text = "//";
  383. } else {
  384. if (is_commented) {
  385. line_text = line_text.substr(2, line_text.length());
  386. } else {
  387. line_text = "//" + line_text;
  388. }
  389. }
  390. tx->set_line(i, line_text);
  391. }
  392. } else {
  393. int begin = tx->cursor_get_line();
  394. String line_text = tx->get_line(begin);
  395. if (line_text.begins_with("//"))
  396. line_text = line_text.substr(2, line_text.length());
  397. else
  398. line_text = "//" + line_text;
  399. tx->set_line(begin, line_text);
  400. }
  401. tx->end_complex_operation();
  402. tx->update();
  403. //tx->deselect();
  404. } break;
  405. case EDIT_COMPLETE: {
  406. shader_editor->get_text_edit()->query_code_comple();
  407. } break;
  408. case SEARCH_FIND: {
  409. shader_editor->get_find_replace_bar()->popup_search();
  410. } break;
  411. case SEARCH_FIND_NEXT: {
  412. shader_editor->get_find_replace_bar()->search_next();
  413. } break;
  414. case SEARCH_FIND_PREV: {
  415. shader_editor->get_find_replace_bar()->search_prev();
  416. } break;
  417. case SEARCH_REPLACE: {
  418. shader_editor->get_find_replace_bar()->popup_replace();
  419. } break;
  420. case SEARCH_GOTO_LINE: {
  421. goto_line_dialog->popup_find_line(shader_editor->get_text_edit());
  422. } break;
  423. }
  424. if (p_option != SEARCH_FIND && p_option != SEARCH_REPLACE && p_option != SEARCH_GOTO_LINE) {
  425. shader_editor->get_text_edit()->call_deferred("grab_focus");
  426. }
  427. }
  428. void ShaderEditor::_notification(int p_what) {
  429. if (p_what == NOTIFICATION_ENTER_TREE) {
  430. }
  431. if (p_what == NOTIFICATION_DRAW) {
  432. }
  433. }
  434. void ShaderEditor::_params_changed() {
  435. shader_editor->_validate_script();
  436. }
  437. void ShaderEditor::_editor_settings_changed() {
  438. shader_editor->get_text_edit()->set_auto_brace_completion(EditorSettings::get_singleton()->get("text_editor/completion/auto_brace_complete"));
  439. shader_editor->get_text_edit()->set_scroll_pass_end_of_file(EditorSettings::get_singleton()->get("text_editor/cursor/scroll_past_end_of_file"));
  440. shader_editor->get_text_edit()->set_indent_size(EditorSettings::get_singleton()->get("text_editor/indent/size"));
  441. shader_editor->get_text_edit()->set_indent_using_spaces(EditorSettings::get_singleton()->get("text_editor/indent/type"));
  442. shader_editor->get_text_edit()->set_auto_indent(EditorSettings::get_singleton()->get("text_editor/indent/auto_indent"));
  443. shader_editor->get_text_edit()->set_draw_tabs(EditorSettings::get_singleton()->get("text_editor/indent/draw_tabs"));
  444. shader_editor->get_text_edit()->set_show_line_numbers(EditorSettings::get_singleton()->get("text_editor/line_numbers/show_line_numbers"));
  445. shader_editor->get_text_edit()->set_syntax_coloring(EditorSettings::get_singleton()->get("text_editor/highlighting/syntax_highlighting"));
  446. shader_editor->get_text_edit()->set_highlight_all_occurrences(EditorSettings::get_singleton()->get("text_editor/highlighting/highlight_all_occurrences"));
  447. shader_editor->get_text_edit()->set_highlight_current_line(EditorSettings::get_singleton()->get("text_editor/highlighting/highlight_current_line"));
  448. shader_editor->get_text_edit()->cursor_set_blink_enabled(EditorSettings::get_singleton()->get("text_editor/cursor/caret_blink"));
  449. shader_editor->get_text_edit()->cursor_set_blink_speed(EditorSettings::get_singleton()->get("text_editor/cursor/caret_blink_speed"));
  450. shader_editor->get_text_edit()->add_constant_override("line_spacing", EditorSettings::get_singleton()->get("text_editor/theme/line_spacing"));
  451. shader_editor->get_text_edit()->cursor_set_block_mode(EditorSettings::get_singleton()->get("text_editor/cursor/block_caret"));
  452. shader_editor->get_text_edit()->set_smooth_scroll_enabled(EditorSettings::get_singleton()->get("text_editor/open_scripts/smooth_scrolling"));
  453. shader_editor->get_text_edit()->set_v_scroll_speed(EditorSettings::get_singleton()->get("text_editor/open_scripts/v_scroll_speed"));
  454. }
  455. void ShaderEditor::_bind_methods() {
  456. ClassDB::bind_method("_editor_settings_changed", &ShaderEditor::_editor_settings_changed);
  457. ClassDB::bind_method("_text_edit_gui_input", &ShaderEditor::_text_edit_gui_input);
  458. ClassDB::bind_method("_menu_option", &ShaderEditor::_menu_option);
  459. ClassDB::bind_method("_params_changed", &ShaderEditor::_params_changed);
  460. ClassDB::bind_method("apply_shaders", &ShaderEditor::apply_shaders);
  461. //ClassDB::bind_method("_close_current_tab",&ShaderEditor::_close_current_tab);
  462. }
  463. void ShaderEditor::ensure_select_current() {
  464. /*
  465. if (tab_container->get_child_count() && tab_container->get_current_tab()>=0) {
  466. ShaderTextEditor *ste = Object::cast_to<ShaderTextEditor>(tab_container->get_child(tab_container->get_current_tab()));
  467. if (!ste)
  468. return;
  469. Ref<Shader> shader = ste->get_edited_shader();
  470. get_scene()->get_root_node()->call("_resource_selected",shader);
  471. }*/
  472. }
  473. void ShaderEditor::edit(const Ref<Shader> &p_shader) {
  474. if (p_shader.is_null())
  475. return;
  476. shader = p_shader;
  477. shader_editor->set_edited_shader(p_shader);
  478. //vertex_editor->set_edited_shader(shader,ShaderLanguage::SHADER_MATERIAL_VERTEX);
  479. // see if already has it
  480. }
  481. void ShaderEditor::save_external_data() {
  482. if (shader.is_null())
  483. return;
  484. apply_shaders();
  485. if (shader->get_path() != "" && shader->get_path().find("local://") == -1 && shader->get_path().find("::") == -1) {
  486. //external shader, save it
  487. ResourceSaver::save(shader->get_path(), shader);
  488. }
  489. }
  490. void ShaderEditor::apply_shaders() {
  491. if (shader.is_valid()) {
  492. shader->set_code(shader_editor->get_text_edit()->get_text());
  493. shader->set_edited(true);
  494. }
  495. }
  496. void ShaderEditor::_text_edit_gui_input(const Ref<InputEvent> &ev) {
  497. Ref<InputEventMouseButton> mb = ev;
  498. if (mb.is_valid()) {
  499. if (mb->get_button_index() == BUTTON_RIGHT) {
  500. int col, row;
  501. TextEdit *tx = shader_editor->get_text_edit();
  502. tx->_get_mouse_pos(mb->get_global_position() - tx->get_global_position(), row, col);
  503. Vector2 mpos = mb->get_global_position() - tx->get_global_position();
  504. tx->set_right_click_moves_caret(EditorSettings::get_singleton()->get("text_editor/cursor/right_click_moves_caret"));
  505. if (tx->is_right_click_moving_caret()) {
  506. if (tx->is_selection_active()) {
  507. int from_line = tx->get_selection_from_line();
  508. int to_line = tx->get_selection_to_line();
  509. int from_column = tx->get_selection_from_column();
  510. int to_column = tx->get_selection_to_column();
  511. if (row < from_line || row > to_line || (row == from_line && col < from_column) || (row == to_line && col > to_column)) {
  512. // Right click is outside the seleted text
  513. tx->deselect();
  514. }
  515. }
  516. if (!tx->is_selection_active()) {
  517. tx->cursor_set_line(row, true, false);
  518. tx->cursor_set_column(col);
  519. }
  520. }
  521. if (!mb->is_pressed()) {
  522. _make_context_menu(tx->is_selection_active());
  523. }
  524. }
  525. }
  526. }
  527. void ShaderEditor::_make_context_menu(bool p_selection) {
  528. context_menu->clear();
  529. if (p_selection) {
  530. context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/cut"), EDIT_CUT);
  531. context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/copy"), EDIT_COPY);
  532. }
  533. context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/paste"), EDIT_PASTE);
  534. context_menu->add_separator();
  535. context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/select_all"), EDIT_SELECT_ALL);
  536. context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/undo"), EDIT_UNDO);
  537. context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/redo"), EDIT_REDO);
  538. context_menu->add_separator();
  539. context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/indent_left"), EDIT_INDENT_LEFT);
  540. context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/indent_right"), EDIT_INDENT_RIGHT);
  541. context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/toggle_comment"), EDIT_TOGGLE_COMMENT);
  542. context_menu->set_position(get_global_transform().xform(get_local_mouse_position()));
  543. context_menu->set_size(Vector2(1, 1));
  544. context_menu->popup();
  545. }
  546. ShaderEditor::ShaderEditor(EditorNode *p_node) {
  547. shader_editor = memnew(ShaderTextEditor);
  548. shader_editor->set_v_size_flags(SIZE_EXPAND_FILL);
  549. shader_editor->add_constant_override("separation", 0);
  550. shader_editor->set_anchors_and_margins_preset(Control::PRESET_WIDE);
  551. shader_editor->connect("script_changed", this, "apply_shaders");
  552. EditorSettings::get_singleton()->connect("settings_changed", this, "_editor_settings_changed");
  553. shader_editor->get_text_edit()->set_callhint_settings(
  554. EditorSettings::get_singleton()->get("text_editor/completion/put_callhint_tooltip_below_current_line"),
  555. EditorSettings::get_singleton()->get("text_editor/completion/callhint_tooltip_offset"));
  556. shader_editor->get_text_edit()->set_select_identifiers_on_hover(true);
  557. shader_editor->get_text_edit()->set_context_menu_enabled(false);
  558. shader_editor->get_text_edit()->connect("gui_input", this, "_text_edit_gui_input");
  559. shader_editor->update_editor_settings();
  560. context_menu = memnew(PopupMenu);
  561. add_child(context_menu);
  562. context_menu->connect("id_pressed", this, "_menu_option");
  563. VBoxContainer *main_container = memnew(VBoxContainer);
  564. HBoxContainer *hbc = memnew(HBoxContainer);
  565. edit_menu = memnew(MenuButton);
  566. //edit_menu->set_position(Point2(5, -1));
  567. edit_menu->set_text(TTR("Edit"));
  568. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/undo"), EDIT_UNDO);
  569. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/redo"), EDIT_REDO);
  570. edit_menu->get_popup()->add_separator();
  571. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/cut"), EDIT_CUT);
  572. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/copy"), EDIT_COPY);
  573. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/paste"), EDIT_PASTE);
  574. edit_menu->get_popup()->add_separator();
  575. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/select_all"), EDIT_SELECT_ALL);
  576. edit_menu->get_popup()->add_separator();
  577. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/move_up"), EDIT_MOVE_LINE_UP);
  578. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/move_down"), EDIT_MOVE_LINE_DOWN);
  579. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/indent_left"), EDIT_INDENT_LEFT);
  580. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/indent_right"), EDIT_INDENT_RIGHT);
  581. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/delete_line"), EDIT_DELETE_LINE);
  582. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/toggle_comment"), EDIT_TOGGLE_COMMENT);
  583. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/clone_down"), EDIT_CLONE_DOWN);
  584. edit_menu->get_popup()->add_separator();
  585. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/complete_symbol"), EDIT_COMPLETE);
  586. edit_menu->get_popup()->connect("id_pressed", this, "_menu_option");
  587. search_menu = memnew(MenuButton);
  588. //search_menu->set_position(Point2(38, -1));
  589. search_menu->set_text(TTR("Search"));
  590. search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/find"), SEARCH_FIND);
  591. search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/find_next"), SEARCH_FIND_NEXT);
  592. search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/find_previous"), SEARCH_FIND_PREV);
  593. search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/replace"), SEARCH_REPLACE);
  594. search_menu->get_popup()->add_separator();
  595. search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/goto_line"), SEARCH_GOTO_LINE);
  596. search_menu->get_popup()->connect("id_pressed", this, "_menu_option");
  597. add_child(main_container);
  598. main_container->add_child(hbc);
  599. hbc->add_child(search_menu);
  600. hbc->add_child(edit_menu);
  601. hbc->add_style_override("panel", p_node->get_gui_base()->get_stylebox("ScriptEditorPanel", "EditorStyles"));
  602. main_container->add_child(shader_editor);
  603. goto_line_dialog = memnew(GotoLineDialog);
  604. add_child(goto_line_dialog);
  605. _editor_settings_changed();
  606. }
  607. void ShaderEditorPlugin::edit(Object *p_object) {
  608. Shader *s = Object::cast_to<Shader>(p_object);
  609. shader_editor->edit(s);
  610. }
  611. bool ShaderEditorPlugin::handles(Object *p_object) const {
  612. Shader *shader = Object::cast_to<Shader>(p_object);
  613. return shader != NULL;
  614. }
  615. void ShaderEditorPlugin::make_visible(bool p_visible) {
  616. if (p_visible) {
  617. button->show();
  618. editor->make_bottom_panel_item_visible(shader_editor);
  619. } else {
  620. button->hide();
  621. if (shader_editor->is_visible_in_tree())
  622. editor->hide_bottom_panel();
  623. shader_editor->apply_shaders();
  624. }
  625. }
  626. void ShaderEditorPlugin::selected_notify() {
  627. shader_editor->ensure_select_current();
  628. }
  629. void ShaderEditorPlugin::save_external_data() {
  630. shader_editor->save_external_data();
  631. }
  632. void ShaderEditorPlugin::apply_changes() {
  633. shader_editor->apply_shaders();
  634. }
  635. ShaderEditorPlugin::ShaderEditorPlugin(EditorNode *p_node) {
  636. editor = p_node;
  637. shader_editor = memnew(ShaderEditor(p_node));
  638. shader_editor->set_custom_minimum_size(Size2(0, 300));
  639. button = editor->add_bottom_panel_item(TTR("Shader"), shader_editor);
  640. button->hide();
  641. }
  642. ShaderEditorPlugin::~ShaderEditorPlugin() {
  643. }