script_text_editor.cpp 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483
  1. /*************************************************************************/
  2. /* script_text_editor.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.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 "script_text_editor.h"
  31. #include "editor/editor_node.h"
  32. #include "editor/editor_settings.h"
  33. #include "editor/script_editor_debugger.h"
  34. #include "os/keyboard.h"
  35. Vector<String> ScriptTextEditor::get_functions() {
  36. String errortxt;
  37. int line = -1, col;
  38. TextEdit *te = code_editor->get_text_edit();
  39. String text = te->get_text();
  40. List<String> fnc;
  41. if (script->get_language()->validate(text, line, col, errortxt, script->get_path(), &fnc)) {
  42. //if valid rewrite functions to latest
  43. functions.clear();
  44. for (List<String>::Element *E = fnc.front(); E; E = E->next()) {
  45. functions.push_back(E->get());
  46. }
  47. }
  48. return functions;
  49. }
  50. void ScriptTextEditor::apply_code() {
  51. if (script.is_null())
  52. return;
  53. //print_line("applying code");
  54. script->set_source_code(code_editor->get_text_edit()->get_text());
  55. script->update_exports();
  56. }
  57. Ref<Script> ScriptTextEditor::get_edited_script() const {
  58. return script;
  59. }
  60. void ScriptTextEditor::_load_theme_settings() {
  61. TextEdit *text_edit = code_editor->get_text_edit();
  62. text_edit->clear_colors();
  63. /* keyword color */
  64. text_edit->add_color_override("background_color", EDITOR_DEF("text_editor/highlighting/background_color", Color(0, 0, 0, 0)));
  65. text_edit->add_color_override("completion_background_color", EDITOR_DEF("text_editor/highlighting/completion_background_color", Color(0, 0, 0, 0)));
  66. text_edit->add_color_override("completion_selected_color", EDITOR_DEF("text_editor/highlighting/completion_selected_color", Color::html("434244")));
  67. text_edit->add_color_override("completion_existing_color", EDITOR_DEF("text_editor/highlighting/completion_existing_color", Color::html("21dfdfdf")));
  68. text_edit->add_color_override("completion_scroll_color", EDITOR_DEF("text_editor/highlighting/completion_scroll_color", Color::html("ffffff")));
  69. text_edit->add_color_override("completion_font_color", EDITOR_DEF("text_editor/highlighting/completion_font_color", Color::html("aaaaaa")));
  70. text_edit->add_color_override("font_color", EDITOR_DEF("text_editor/highlighting/text_color", Color(0, 0, 0)));
  71. text_edit->add_color_override("line_number_color", EDITOR_DEF("text_editor/highlighting/line_number_color", Color(0, 0, 0)));
  72. text_edit->add_color_override("caret_color", EDITOR_DEF("text_editor/highlighting/caret_color", Color(0, 0, 0)));
  73. text_edit->add_color_override("caret_background_color", EDITOR_DEF("text_editor/highlighting/caret_background_color", Color(0, 0, 0)));
  74. text_edit->add_color_override("font_selected_color", EDITOR_DEF("text_editor/highlighting/text_selected_color", Color(1, 1, 1)));
  75. text_edit->add_color_override("selection_color", EDITOR_DEF("text_editor/highlighting/selection_color", Color(0.2, 0.2, 1)));
  76. text_edit->add_color_override("brace_mismatch_color", EDITOR_DEF("text_editor/highlighting/brace_mismatch_color", Color(1, 0.2, 0.2)));
  77. text_edit->add_color_override("current_line_color", EDITOR_DEF("text_editor/highlighting/current_line_color", Color(0.3, 0.5, 0.8, 0.15)));
  78. text_edit->add_color_override("line_length_guideline_color", EDITOR_DEF("text_editor/highlighting/line_length_guideline_color", Color(0, 0, 0)));
  79. text_edit->add_color_override("word_highlighted_color", EDITOR_DEF("text_editor/highlighting/word_highlighted_color", Color(0.8, 0.9, 0.9, 0.15)));
  80. text_edit->add_color_override("number_color", EDITOR_DEF("text_editor/highlighting/number_color", Color(0.9, 0.6, 0.0, 2)));
  81. text_edit->add_color_override("function_color", EDITOR_DEF("text_editor/highlighting/function_color", Color(0.4, 0.6, 0.8)));
  82. text_edit->add_color_override("member_variable_color", EDITOR_DEF("text_editor/highlighting/member_variable_color", Color(0.9, 0.3, 0.3)));
  83. text_edit->add_color_override("mark_color", EDITOR_DEF("text_editor/highlighting/mark_color", Color(1.0, 0.4, 0.4, 0.4)));
  84. text_edit->add_color_override("breakpoint_color", EDITOR_DEF("text_editor/highlighting/breakpoint_color", Color(0.8, 0.8, 0.4, 0.2)));
  85. text_edit->add_color_override("search_result_color", EDITOR_DEF("text_editor/highlighting/search_result_color", Color(0.05, 0.25, 0.05, 1)));
  86. text_edit->add_color_override("search_result_border_color", EDITOR_DEF("text_editor/highlighting/search_result_border_color", Color(0.1, 0.45, 0.1, 1)));
  87. text_edit->add_color_override("symbol_color", EDITOR_DEF("text_editor/highlighting/symbol_color", Color::hex(0x005291ff)));
  88. text_edit->add_constant_override("line_spacing", EDITOR_DEF("text_editor/theme/line_spacing", 4));
  89. Color keyword_color = EDITOR_DEF("text_editor/highlighting/keyword_color", Color(0.5, 0.0, 0.2));
  90. List<String> keywords;
  91. script->get_language()->get_reserved_words(&keywords);
  92. for (List<String>::Element *E = keywords.front(); E; E = E->next()) {
  93. text_edit->add_keyword_color(E->get(), keyword_color);
  94. }
  95. //colorize core types
  96. Color basetype_color = EDITOR_DEF("text_editor/highlighting/base_type_color", Color(0.3, 0.3, 0.0));
  97. text_edit->add_keyword_color("Vector2", basetype_color);
  98. text_edit->add_keyword_color("Vector3", basetype_color);
  99. text_edit->add_keyword_color("Plane", basetype_color);
  100. text_edit->add_keyword_color("Quat", basetype_color);
  101. text_edit->add_keyword_color("AABB", basetype_color);
  102. text_edit->add_keyword_color("Matrix3", basetype_color);
  103. text_edit->add_keyword_color("Transform", basetype_color);
  104. text_edit->add_keyword_color("Color", basetype_color);
  105. text_edit->add_keyword_color("Image", basetype_color);
  106. text_edit->add_keyword_color("InputEvent", basetype_color);
  107. text_edit->add_keyword_color("Rect2", basetype_color);
  108. text_edit->add_keyword_color("NodePath", basetype_color);
  109. //colorize engine types
  110. Color type_color = EDITOR_DEF("text_editor/highlighting/engine_type_color", Color(0.0, 0.2, 0.4));
  111. List<StringName> types;
  112. ClassDB::get_class_list(&types);
  113. for (List<StringName>::Element *E = types.front(); E; E = E->next()) {
  114. String n = E->get();
  115. if (n.begins_with("_"))
  116. n = n.substr(1, n.length());
  117. text_edit->add_keyword_color(n, type_color);
  118. }
  119. //colorize comments
  120. Color comment_color = EDITOR_DEF("text_editor/highlighting/comment_color", Color::hex(0x797e7eff));
  121. List<String> comments;
  122. script->get_language()->get_comment_delimiters(&comments);
  123. for (List<String>::Element *E = comments.front(); E; E = E->next()) {
  124. String comment = E->get();
  125. String beg = comment.get_slice(" ", 0);
  126. String end = comment.get_slice_count(" ") > 1 ? comment.get_slice(" ", 1) : String();
  127. text_edit->add_color_region(beg, end, comment_color, end == "");
  128. }
  129. //colorize strings
  130. Color string_color = EDITOR_DEF("text_editor/highlighting/string_color", Color::hex(0x6b6f00ff));
  131. List<String> strings;
  132. script->get_language()->get_string_delimiters(&strings);
  133. for (List<String>::Element *E = strings.front(); E; E = E->next()) {
  134. String string = E->get();
  135. String beg = string.get_slice(" ", 0);
  136. String end = string.get_slice_count(" ") > 1 ? string.get_slice(" ", 1) : String();
  137. text_edit->add_color_region(beg, end, string_color, end == "");
  138. }
  139. }
  140. void ScriptTextEditor::reload_text() {
  141. ERR_FAIL_COND(script.is_null());
  142. TextEdit *te = code_editor->get_text_edit();
  143. int column = te->cursor_get_column();
  144. int row = te->cursor_get_line();
  145. int h = te->get_h_scroll();
  146. int v = te->get_v_scroll();
  147. te->set_text(script->get_source_code());
  148. te->clear_undo_history();
  149. te->cursor_set_line(row);
  150. te->cursor_set_column(column);
  151. te->set_h_scroll(h);
  152. te->set_v_scroll(v);
  153. te->tag_saved_version();
  154. code_editor->update_line_and_column();
  155. }
  156. void ScriptTextEditor::_notification(int p_what) {
  157. if (p_what == NOTIFICATION_READY) {
  158. //emit_signal("name_changed");
  159. }
  160. }
  161. void ScriptTextEditor::add_callback(const String &p_function, PoolStringArray p_args) {
  162. String code = code_editor->get_text_edit()->get_text();
  163. int pos = script->get_language()->find_function(p_function, code);
  164. if (pos == -1) {
  165. //does not exist
  166. code_editor->get_text_edit()->deselect();
  167. pos = code_editor->get_text_edit()->get_line_count() + 2;
  168. String func = script->get_language()->make_function("", p_function, p_args);
  169. //code=code+func;
  170. code_editor->get_text_edit()->cursor_set_line(pos + 1);
  171. code_editor->get_text_edit()->cursor_set_column(1000000); //none shall be that big
  172. code_editor->get_text_edit()->insert_text_at_cursor("\n\n" + func);
  173. }
  174. code_editor->get_text_edit()->cursor_set_line(pos);
  175. code_editor->get_text_edit()->cursor_set_column(1);
  176. }
  177. void ScriptTextEditor::update_settings() {
  178. code_editor->update_editor_settings();
  179. }
  180. bool ScriptTextEditor::is_unsaved() {
  181. return code_editor->get_text_edit()->get_version() != code_editor->get_text_edit()->get_saved_version();
  182. }
  183. Variant ScriptTextEditor::get_edit_state() {
  184. Dictionary state;
  185. state["scroll_pos"] = code_editor->get_text_edit()->get_v_scroll();
  186. state["column"] = code_editor->get_text_edit()->cursor_get_column();
  187. state["row"] = code_editor->get_text_edit()->cursor_get_line();
  188. return state;
  189. }
  190. void ScriptTextEditor::_convert_case(CaseStyle p_case) {
  191. TextEdit *te = code_editor->get_text_edit();
  192. Ref<Script> scr = get_edited_script();
  193. if (scr.is_null()) {
  194. return;
  195. }
  196. if (te->is_selection_active()) {
  197. te->begin_complex_operation();
  198. int begin = te->get_selection_from_line();
  199. int end = te->get_selection_to_line();
  200. int begin_col = te->get_selection_from_column();
  201. int end_col = te->get_selection_to_column();
  202. for (int i = begin; i <= end; i++) {
  203. String new_line = te->get_line(i);
  204. switch (p_case) {
  205. case UPPER: {
  206. new_line = new_line.to_upper();
  207. } break;
  208. case LOWER: {
  209. new_line = new_line.to_lower();
  210. } break;
  211. case CAPITALIZE: {
  212. new_line = new_line.capitalize();
  213. } break;
  214. }
  215. if (i == begin) {
  216. new_line = te->get_line(i).left(begin_col) + new_line.right(begin_col);
  217. }
  218. if (i == end) {
  219. new_line = new_line.left(end_col) + te->get_line(i).right(end_col);
  220. }
  221. te->set_line(i, new_line);
  222. }
  223. te->end_complex_operation();
  224. }
  225. }
  226. void ScriptTextEditor::trim_trailing_whitespace() {
  227. TextEdit *tx = code_editor->get_text_edit();
  228. bool trimed_whitespace = false;
  229. for (int i = 0; i < tx->get_line_count(); i++) {
  230. String line = tx->get_line(i);
  231. if (line.ends_with(" ") || line.ends_with("\t")) {
  232. if (!trimed_whitespace) {
  233. tx->begin_complex_operation();
  234. trimed_whitespace = true;
  235. }
  236. int end = 0;
  237. for (int j = line.length() - 1; j > -1; j--) {
  238. if (line[j] != ' ' && line[j] != '\t') {
  239. end = j + 1;
  240. break;
  241. }
  242. }
  243. tx->set_line(i, line.substr(0, end));
  244. }
  245. }
  246. if (trimed_whitespace) {
  247. tx->end_complex_operation();
  248. tx->update();
  249. }
  250. }
  251. void ScriptTextEditor::convert_indent_to_spaces() {
  252. TextEdit *tx = code_editor->get_text_edit();
  253. Ref<Script> scr = get_edited_script();
  254. if (scr.is_null()) {
  255. return;
  256. }
  257. int indent_size = EditorSettings::get_singleton()->get("text_editor/indent/size");
  258. String indent = "";
  259. for (int i = 0; i < indent_size; i++) {
  260. indent += " ";
  261. }
  262. int cursor_line = tx->cursor_get_line();
  263. int cursor_column = tx->cursor_get_column();
  264. bool changed_indentation = false;
  265. for (int i = 0; i < tx->get_line_count(); i++) {
  266. String line = tx->get_line(i);
  267. if (line.length() <= 0) {
  268. continue;
  269. }
  270. int j = 0;
  271. while (j < line.length() && (line[j] == ' ' || line[j] == '\t')) {
  272. if (line[j] == '\t') {
  273. if (!changed_indentation) {
  274. tx->begin_complex_operation();
  275. changed_indentation = true;
  276. }
  277. if (cursor_line == i && cursor_column > j) {
  278. cursor_column += indent_size - 1;
  279. }
  280. line = line.left(j) + indent + line.right(j + 1);
  281. }
  282. j++;
  283. }
  284. tx->set_line(i, line);
  285. }
  286. if (changed_indentation) {
  287. tx->cursor_set_column(cursor_column);
  288. tx->end_complex_operation();
  289. tx->update();
  290. }
  291. }
  292. void ScriptTextEditor::convert_indent_to_tabs() {
  293. TextEdit *tx = code_editor->get_text_edit();
  294. Ref<Script> scr = get_edited_script();
  295. if (scr.is_null()) {
  296. return;
  297. }
  298. int indent_size = EditorSettings::get_singleton()->get("text_editor/indent/size");
  299. indent_size -= 1;
  300. int cursor_line = tx->cursor_get_line();
  301. int cursor_column = tx->cursor_get_column();
  302. bool changed_indentation = false;
  303. for (int i = 0; i < tx->get_line_count(); i++) {
  304. String line = tx->get_line(i);
  305. if (line.length() <= 0) {
  306. continue;
  307. }
  308. int j = 0;
  309. int space_count = -1;
  310. while (j < line.length() && (line[j] == ' ' || line[j] == '\t')) {
  311. if (line[j] != '\t') {
  312. space_count++;
  313. if (space_count == indent_size) {
  314. if (!changed_indentation) {
  315. tx->begin_complex_operation();
  316. changed_indentation = true;
  317. }
  318. if (cursor_line == i && cursor_column > j) {
  319. cursor_column -= indent_size;
  320. }
  321. line = line.left(j - indent_size) + "\t" + line.right(j + 1);
  322. j = 0;
  323. space_count = -1;
  324. }
  325. } else {
  326. space_count = -1;
  327. }
  328. j++;
  329. }
  330. tx->set_line(i, line);
  331. }
  332. if (changed_indentation) {
  333. tx->cursor_set_column(cursor_column);
  334. tx->end_complex_operation();
  335. tx->update();
  336. }
  337. }
  338. void ScriptTextEditor::tag_saved_version() {
  339. code_editor->get_text_edit()->tag_saved_version();
  340. }
  341. void ScriptTextEditor::goto_line(int p_line, bool p_with_error) {
  342. code_editor->get_text_edit()->call_deferred("cursor_set_line", p_line);
  343. }
  344. void ScriptTextEditor::ensure_focus() {
  345. code_editor->get_text_edit()->grab_focus();
  346. }
  347. void ScriptTextEditor::set_edit_state(const Variant &p_state) {
  348. Dictionary state = p_state;
  349. code_editor->get_text_edit()->set_v_scroll(state["scroll_pos"]);
  350. code_editor->get_text_edit()->cursor_set_column(state["column"]);
  351. code_editor->get_text_edit()->cursor_set_line(state["row"]);
  352. code_editor->get_text_edit()->grab_focus();
  353. //int scroll_pos;
  354. //int cursor_column;
  355. //int cursor_row;
  356. }
  357. String ScriptTextEditor::get_name() {
  358. String name;
  359. if (script->get_path().find("local://") == -1 && script->get_path().find("::") == -1) {
  360. name = script->get_path().get_file();
  361. if (is_unsaved()) {
  362. name += "(*)";
  363. }
  364. } else if (script->get_name() != "")
  365. name = script->get_name();
  366. else
  367. name = script->get_class() + "(" + itos(script->get_instance_ID()) + ")";
  368. return name;
  369. }
  370. Ref<Texture> ScriptTextEditor::get_icon() {
  371. if (get_parent_control() && get_parent_control()->has_icon(script->get_class(), "EditorIcons")) {
  372. return get_parent_control()->get_icon(script->get_class(), "EditorIcons");
  373. }
  374. return Ref<Texture>();
  375. }
  376. void ScriptTextEditor::set_edited_script(const Ref<Script> &p_script) {
  377. ERR_FAIL_COND(!script.is_null());
  378. script = p_script;
  379. _load_theme_settings();
  380. code_editor->get_text_edit()->set_text(script->get_source_code());
  381. code_editor->get_text_edit()->clear_undo_history();
  382. code_editor->get_text_edit()->tag_saved_version();
  383. emit_signal("name_changed");
  384. code_editor->update_line_and_column();
  385. }
  386. void ScriptTextEditor::_validate_script() {
  387. String errortxt;
  388. int line = -1, col;
  389. TextEdit *te = code_editor->get_text_edit();
  390. String text = te->get_text();
  391. List<String> fnc;
  392. if (!script->get_language()->validate(text, line, col, errortxt, script->get_path(), &fnc)) {
  393. String error_text = "error(" + itos(line) + "," + itos(col) + "): " + errortxt;
  394. code_editor->set_error(error_text);
  395. } else {
  396. code_editor->set_error("");
  397. line = -1;
  398. if (!script->is_tool()) {
  399. script->set_source_code(text);
  400. script->update_exports();
  401. //script->reload(); //will update all the variables in property editors
  402. }
  403. functions.clear();
  404. for (List<String>::Element *E = fnc.front(); E; E = E->next()) {
  405. functions.push_back(E->get());
  406. }
  407. }
  408. line--;
  409. for (int i = 0; i < te->get_line_count(); i++) {
  410. te->set_line_as_marked(i, line == i);
  411. }
  412. emit_signal("name_changed");
  413. }
  414. static Node *_find_node_for_script(Node *p_base, Node *p_current, const Ref<Script> &p_script) {
  415. if (p_current->get_owner() != p_base && p_base != p_current)
  416. return NULL;
  417. Ref<Script> c = p_current->get_script();
  418. if (c == p_script)
  419. return p_current;
  420. for (int i = 0; i < p_current->get_child_count(); i++) {
  421. Node *found = _find_node_for_script(p_base, p_current->get_child(i), p_script);
  422. if (found)
  423. return found;
  424. }
  425. return NULL;
  426. }
  427. static void _find_changed_scripts_for_external_editor(Node *p_base, Node *p_current, Set<Ref<Script> > &r_scripts) {
  428. if (p_current->get_owner() != p_base && p_base != p_current)
  429. return;
  430. Ref<Script> c = p_current->get_script();
  431. if (c.is_valid())
  432. r_scripts.insert(c);
  433. for (int i = 0; i < p_current->get_child_count(); i++) {
  434. _find_changed_scripts_for_external_editor(p_base, p_current->get_child(i), r_scripts);
  435. }
  436. }
  437. void ScriptEditor::_update_modified_scripts_for_external_editor(Ref<Script> p_for_script) {
  438. if (!bool(EditorSettings::get_singleton()->get("text_editor/external/use_external_editor")))
  439. return;
  440. Set<Ref<Script> > scripts;
  441. Node *base = get_tree()->get_edited_scene_root();
  442. if (base) {
  443. _find_changed_scripts_for_external_editor(base, base, scripts);
  444. }
  445. for (Set<Ref<Script> >::Element *E = scripts.front(); E; E = E->next()) {
  446. Ref<Script> script = E->get();
  447. if (p_for_script.is_valid() && p_for_script != script)
  448. continue;
  449. if (script->get_path() == "" || script->get_path().find("local://") != -1 || script->get_path().find("::") != -1) {
  450. continue; //internal script, who cares, though weird
  451. }
  452. uint64_t last_date = script->get_last_modified_time();
  453. uint64_t date = FileAccess::get_modified_time(script->get_path());
  454. if (last_date != date) {
  455. Ref<Script> rel_script = ResourceLoader::load(script->get_path(), script->get_class(), true);
  456. ERR_CONTINUE(!rel_script.is_valid());
  457. script->set_source_code(rel_script->get_source_code());
  458. script->set_last_modified_time(rel_script->get_last_modified_time());
  459. script->update_exports();
  460. }
  461. }
  462. }
  463. void ScriptTextEditor::_code_complete_scripts(void *p_ud, const String &p_code, List<String> *r_options) {
  464. ScriptTextEditor *ste = (ScriptTextEditor *)p_ud;
  465. ste->_code_complete_script(p_code, r_options);
  466. }
  467. void ScriptTextEditor::_code_complete_script(const String &p_code, List<String> *r_options) {
  468. if (color_panel->is_visible_in_tree()) return;
  469. Node *base = get_tree()->get_edited_scene_root();
  470. if (base) {
  471. base = _find_node_for_script(base, base, script);
  472. }
  473. String hint;
  474. Error err = script->get_language()->complete_code(p_code, script->get_path().get_base_dir(), base, r_options, hint);
  475. if (hint != "") {
  476. code_editor->get_text_edit()->set_code_hint(hint);
  477. }
  478. }
  479. void ScriptTextEditor::_breakpoint_toggled(int p_row) {
  480. ScriptEditor::get_singleton()->get_debugger()->set_breakpoint(script->get_path(), p_row + 1, code_editor->get_text_edit()->is_line_set_as_breakpoint(p_row));
  481. }
  482. static void swap_lines(TextEdit *tx, int line1, int line2) {
  483. String tmp = tx->get_line(line1);
  484. String tmp2 = tx->get_line(line2);
  485. tx->set_line(line2, tmp);
  486. tx->set_line(line1, tmp2);
  487. tx->cursor_set_line(line2);
  488. }
  489. void ScriptTextEditor::_lookup_symbol(const String &p_symbol, int p_row, int p_column) {
  490. Node *base = get_tree()->get_edited_scene_root();
  491. if (base) {
  492. base = _find_node_for_script(base, base, script);
  493. }
  494. ScriptLanguage::LookupResult result;
  495. if (script->get_language()->lookup_code(code_editor->get_text_edit()->get_text_for_lookup_completion(), p_symbol, script->get_path().get_base_dir(), base, result) == OK) {
  496. _goto_line(p_row);
  497. switch (result.type) {
  498. case ScriptLanguage::LookupResult::RESULT_SCRIPT_LOCATION: {
  499. if (result.script.is_valid()) {
  500. emit_signal("request_open_script_at_line", result.script, result.location - 1);
  501. } else {
  502. emit_signal("request_save_history");
  503. _goto_line(result.location - 1);
  504. }
  505. } break;
  506. case ScriptLanguage::LookupResult::RESULT_CLASS: {
  507. emit_signal("go_to_help", "class_name:" + result.class_name);
  508. } break;
  509. case ScriptLanguage::LookupResult::RESULT_CLASS_CONSTANT: {
  510. StringName cname = result.class_name;
  511. bool success;
  512. while (true) {
  513. ClassDB::get_integer_constant(cname, result.class_member, &success);
  514. if (success) {
  515. result.class_name = cname;
  516. cname = ClassDB::get_parent_class(cname);
  517. } else {
  518. break;
  519. }
  520. }
  521. emit_signal("go_to_help", "class_constant:" + result.class_name + ":" + result.class_member);
  522. } break;
  523. case ScriptLanguage::LookupResult::RESULT_CLASS_PROPERTY: {
  524. emit_signal("go_to_help", "class_property:" + result.class_name + ":" + result.class_member);
  525. } break;
  526. case ScriptLanguage::LookupResult::RESULT_CLASS_METHOD: {
  527. StringName cname = result.class_name;
  528. while (true) {
  529. if (ClassDB::has_method(cname, result.class_member)) {
  530. result.class_name = cname;
  531. cname = ClassDB::get_parent_class(cname);
  532. } else {
  533. break;
  534. }
  535. }
  536. emit_signal("go_to_help", "class_method:" + result.class_name + ":" + result.class_member);
  537. } break;
  538. }
  539. }
  540. }
  541. void ScriptTextEditor::_edit_option(int p_op) {
  542. switch (p_op) {
  543. case EDIT_UNDO: {
  544. code_editor->get_text_edit()->undo();
  545. code_editor->get_text_edit()->call_deferred("grab_focus");
  546. } break;
  547. case EDIT_REDO: {
  548. code_editor->get_text_edit()->redo();
  549. code_editor->get_text_edit()->call_deferred("grab_focus");
  550. } break;
  551. case EDIT_CUT: {
  552. code_editor->get_text_edit()->cut();
  553. code_editor->get_text_edit()->call_deferred("grab_focus");
  554. } break;
  555. case EDIT_COPY: {
  556. code_editor->get_text_edit()->copy();
  557. code_editor->get_text_edit()->call_deferred("grab_focus");
  558. } break;
  559. case EDIT_PASTE: {
  560. code_editor->get_text_edit()->paste();
  561. code_editor->get_text_edit()->call_deferred("grab_focus");
  562. } break;
  563. case EDIT_SELECT_ALL: {
  564. code_editor->get_text_edit()->select_all();
  565. code_editor->get_text_edit()->call_deferred("grab_focus");
  566. } break;
  567. case EDIT_MOVE_LINE_UP: {
  568. TextEdit *tx = code_editor->get_text_edit();
  569. Ref<Script> scr = script;
  570. if (scr.is_null())
  571. return;
  572. tx->begin_complex_operation();
  573. if (tx->is_selection_active()) {
  574. int from_line = tx->get_selection_from_line();
  575. int from_col = tx->get_selection_from_column();
  576. int to_line = tx->get_selection_to_line();
  577. int to_column = tx->get_selection_to_column();
  578. for (int i = from_line; i <= to_line; i++) {
  579. int line_id = i;
  580. int next_id = i - 1;
  581. if (line_id == 0 || next_id < 0)
  582. return;
  583. swap_lines(tx, line_id, next_id);
  584. }
  585. int from_line_up = from_line > 0 ? from_line - 1 : from_line;
  586. int to_line_up = to_line > 0 ? to_line - 1 : to_line;
  587. tx->select(from_line_up, from_col, to_line_up, to_column);
  588. } else {
  589. int line_id = tx->cursor_get_line();
  590. int next_id = line_id - 1;
  591. if (line_id == 0 || next_id < 0)
  592. return;
  593. swap_lines(tx, line_id, next_id);
  594. }
  595. tx->end_complex_operation();
  596. tx->update();
  597. } break;
  598. case EDIT_MOVE_LINE_DOWN: {
  599. TextEdit *tx = code_editor->get_text_edit();
  600. Ref<Script> scr = get_edited_script();
  601. if (scr.is_null())
  602. return;
  603. tx->begin_complex_operation();
  604. if (tx->is_selection_active()) {
  605. int from_line = tx->get_selection_from_line();
  606. int from_col = tx->get_selection_from_column();
  607. int to_line = tx->get_selection_to_line();
  608. int to_column = tx->get_selection_to_column();
  609. for (int i = to_line; i >= from_line; i--) {
  610. int line_id = i;
  611. int next_id = i + 1;
  612. if (line_id == tx->get_line_count() - 1 || next_id > tx->get_line_count())
  613. return;
  614. swap_lines(tx, line_id, next_id);
  615. }
  616. int from_line_down = from_line < tx->get_line_count() ? from_line + 1 : from_line;
  617. int to_line_down = to_line < tx->get_line_count() ? to_line + 1 : to_line;
  618. tx->select(from_line_down, from_col, to_line_down, to_column);
  619. } else {
  620. int line_id = tx->cursor_get_line();
  621. int next_id = line_id + 1;
  622. if (line_id == tx->get_line_count() - 1 || next_id > tx->get_line_count())
  623. return;
  624. swap_lines(tx, line_id, next_id);
  625. }
  626. tx->end_complex_operation();
  627. tx->update();
  628. } break;
  629. case EDIT_INDENT_LEFT: {
  630. TextEdit *tx = code_editor->get_text_edit();
  631. Ref<Script> scr = get_edited_script();
  632. if (scr.is_null())
  633. return;
  634. tx->begin_complex_operation();
  635. if (tx->is_selection_active()) {
  636. tx->indent_selection_left();
  637. } else {
  638. int begin = tx->cursor_get_line();
  639. String line_text = tx->get_line(begin);
  640. // begins with tab
  641. if (line_text.begins_with("\t")) {
  642. line_text = line_text.substr(1, line_text.length());
  643. tx->set_line(begin, line_text);
  644. }
  645. // begins with 4 spaces
  646. else if (line_text.begins_with(" ")) {
  647. line_text = line_text.substr(4, line_text.length());
  648. tx->set_line(begin, line_text);
  649. }
  650. }
  651. tx->end_complex_operation();
  652. tx->update();
  653. //tx->deselect();
  654. } break;
  655. case EDIT_INDENT_RIGHT: {
  656. TextEdit *tx = code_editor->get_text_edit();
  657. Ref<Script> scr = get_edited_script();
  658. if (scr.is_null())
  659. return;
  660. tx->begin_complex_operation();
  661. if (tx->is_selection_active()) {
  662. tx->indent_selection_right();
  663. } else {
  664. int begin = tx->cursor_get_line();
  665. String line_text = tx->get_line(begin);
  666. line_text = '\t' + line_text;
  667. tx->set_line(begin, line_text);
  668. }
  669. tx->end_complex_operation();
  670. tx->update();
  671. //tx->deselect();
  672. } break;
  673. case EDIT_CLONE_DOWN: {
  674. TextEdit *tx = code_editor->get_text_edit();
  675. Ref<Script> scr = get_edited_script();
  676. if (scr.is_null())
  677. return;
  678. int from_line = tx->cursor_get_line();
  679. int to_line = tx->cursor_get_line();
  680. int column = tx->cursor_get_column();
  681. if (tx->is_selection_active()) {
  682. from_line = tx->get_selection_from_line();
  683. to_line = tx->get_selection_to_line();
  684. column = tx->cursor_get_column();
  685. }
  686. int next_line = to_line + 1;
  687. tx->begin_complex_operation();
  688. for (int i = from_line; i <= to_line; i++) {
  689. if (i >= tx->get_line_count() - 1) {
  690. tx->set_line(i, tx->get_line(i) + "\n");
  691. }
  692. String line_clone = tx->get_line(i);
  693. tx->insert_at(line_clone, next_line);
  694. next_line++;
  695. }
  696. tx->cursor_set_column(column);
  697. if (tx->is_selection_active()) {
  698. tx->select(to_line + 1, tx->get_selection_from_column(), next_line - 1, tx->get_selection_to_column());
  699. }
  700. tx->end_complex_operation();
  701. tx->update();
  702. } break;
  703. case EDIT_TOGGLE_COMMENT: {
  704. TextEdit *tx = code_editor->get_text_edit();
  705. Ref<Script> scr = get_edited_script();
  706. if (scr.is_null())
  707. return;
  708. tx->begin_complex_operation();
  709. if (tx->is_selection_active()) {
  710. int begin = tx->get_selection_from_line();
  711. int end = tx->get_selection_to_line();
  712. // End of selection ends on the first column of the last line, ignore it.
  713. if (tx->get_selection_to_column() == 0)
  714. end -= 1;
  715. for (int i = begin; i <= end; i++) {
  716. String line_text = tx->get_line(i);
  717. if (line_text.begins_with("#"))
  718. line_text = line_text.substr(1, line_text.length());
  719. else
  720. line_text = "#" + line_text;
  721. tx->set_line(i, line_text);
  722. }
  723. } else {
  724. int begin = tx->cursor_get_line();
  725. String line_text = tx->get_line(begin);
  726. if (line_text.begins_with("#"))
  727. line_text = line_text.substr(1, line_text.length());
  728. else
  729. line_text = "#" + line_text;
  730. tx->set_line(begin, line_text);
  731. }
  732. tx->end_complex_operation();
  733. tx->update();
  734. //tx->deselect();
  735. } break;
  736. case EDIT_COMPLETE: {
  737. code_editor->get_text_edit()->query_code_comple();
  738. } break;
  739. case EDIT_AUTO_INDENT: {
  740. TextEdit *te = code_editor->get_text_edit();
  741. String text = te->get_text();
  742. Ref<Script> scr = get_edited_script();
  743. if (scr.is_null())
  744. return;
  745. int begin, end;
  746. if (te->is_selection_active()) {
  747. begin = te->get_selection_from_line();
  748. end = te->get_selection_to_line();
  749. } else {
  750. begin = 0;
  751. end = te->get_line_count() - 1;
  752. }
  753. scr->get_language()->auto_indent_code(text, begin, end);
  754. te->set_text(text);
  755. } break;
  756. case EDIT_TRIM_TRAILING_WHITESAPCE: {
  757. trim_trailing_whitespace();
  758. } break;
  759. case EDIT_CONVERT_INDENT_TO_SPACES: {
  760. convert_indent_to_spaces();
  761. } break;
  762. case EDIT_CONVERT_INDENT_TO_TABS: {
  763. convert_indent_to_tabs();
  764. } break;
  765. case EDIT_PICK_COLOR: {
  766. color_panel->popup();
  767. } break;
  768. case EDIT_TO_UPPERCASE: {
  769. _convert_case(UPPER);
  770. } break;
  771. case EDIT_TO_LOWERCASE: {
  772. _convert_case(LOWER);
  773. } break;
  774. case EDIT_CAPITALIZE: {
  775. _convert_case(CAPITALIZE);
  776. } break;
  777. case SEARCH_FIND: {
  778. code_editor->get_find_replace_bar()->popup_search();
  779. } break;
  780. case SEARCH_FIND_NEXT: {
  781. code_editor->get_find_replace_bar()->search_next();
  782. } break;
  783. case SEARCH_FIND_PREV: {
  784. code_editor->get_find_replace_bar()->search_prev();
  785. } break;
  786. case SEARCH_REPLACE: {
  787. code_editor->get_find_replace_bar()->popup_replace();
  788. } break;
  789. case SEARCH_LOCATE_FUNCTION: {
  790. quick_open->popup(get_functions());
  791. } break;
  792. case SEARCH_GOTO_LINE: {
  793. goto_line_dialog->popup_find_line(code_editor->get_text_edit());
  794. } break;
  795. case DEBUG_TOGGLE_BREAKPOINT: {
  796. int line = code_editor->get_text_edit()->cursor_get_line();
  797. bool dobreak = !code_editor->get_text_edit()->is_line_set_as_breakpoint(line);
  798. code_editor->get_text_edit()->set_line_as_breakpoint(line, dobreak);
  799. ScriptEditor::get_singleton()->get_debugger()->set_breakpoint(get_edited_script()->get_path(), line + 1, dobreak);
  800. } break;
  801. case DEBUG_REMOVE_ALL_BREAKPOINTS: {
  802. List<int> bpoints;
  803. code_editor->get_text_edit()->get_breakpoints(&bpoints);
  804. for (List<int>::Element *E = bpoints.front(); E; E = E->next()) {
  805. int line = E->get();
  806. bool dobreak = !code_editor->get_text_edit()->is_line_set_as_breakpoint(line);
  807. code_editor->get_text_edit()->set_line_as_breakpoint(line, dobreak);
  808. ScriptEditor::get_singleton()->get_debugger()->set_breakpoint(get_edited_script()->get_path(), line + 1, dobreak);
  809. }
  810. }
  811. case DEBUG_GOTO_NEXT_BREAKPOINT: {
  812. List<int> bpoints;
  813. code_editor->get_text_edit()->get_breakpoints(&bpoints);
  814. if (bpoints.size() <= 0) {
  815. return;
  816. }
  817. int line = code_editor->get_text_edit()->cursor_get_line();
  818. // wrap around
  819. if (line >= bpoints[bpoints.size() - 1]) {
  820. code_editor->get_text_edit()->cursor_set_line(bpoints[0]);
  821. } else {
  822. for (List<int>::Element *E = bpoints.front(); E; E = E->next()) {
  823. int bline = E->get();
  824. if (bline > line) {
  825. code_editor->get_text_edit()->cursor_set_line(bline);
  826. return;
  827. }
  828. }
  829. }
  830. } break;
  831. case DEBUG_GOTO_PREV_BREAKPOINT: {
  832. List<int> bpoints;
  833. code_editor->get_text_edit()->get_breakpoints(&bpoints);
  834. if (bpoints.size() <= 0) {
  835. return;
  836. }
  837. int line = code_editor->get_text_edit()->cursor_get_line();
  838. // wrap around
  839. if (line <= bpoints[0]) {
  840. code_editor->get_text_edit()->cursor_set_line(bpoints[bpoints.size() - 1]);
  841. } else {
  842. for (List<int>::Element *E = bpoints.back(); E; E = E->prev()) {
  843. int bline = E->get();
  844. if (bline < line) {
  845. code_editor->get_text_edit()->cursor_set_line(bline);
  846. return;
  847. }
  848. }
  849. }
  850. } break;
  851. case HELP_CONTEXTUAL: {
  852. String text = code_editor->get_text_edit()->get_selection_text();
  853. if (text == "")
  854. text = code_editor->get_text_edit()->get_word_under_cursor();
  855. if (text != "") {
  856. emit_signal("request_help_search", text);
  857. }
  858. } break;
  859. }
  860. }
  861. void ScriptTextEditor::_bind_methods() {
  862. ClassDB::bind_method("_validate_script", &ScriptTextEditor::_validate_script);
  863. ClassDB::bind_method("_load_theme_settings", &ScriptTextEditor::_load_theme_settings);
  864. ClassDB::bind_method("_breakpoint_toggled", &ScriptTextEditor::_breakpoint_toggled);
  865. ClassDB::bind_method("_edit_option", &ScriptTextEditor::_edit_option);
  866. ClassDB::bind_method("_goto_line", &ScriptTextEditor::_goto_line);
  867. ClassDB::bind_method("_lookup_symbol", &ScriptTextEditor::_lookup_symbol);
  868. ClassDB::bind_method("_text_edit_gui_input", &ScriptTextEditor::_text_edit_gui_input);
  869. ClassDB::bind_method("_color_changed", &ScriptTextEditor::_color_changed);
  870. ClassDB::bind_method("get_drag_data_fw", &ScriptTextEditor::get_drag_data_fw);
  871. ClassDB::bind_method("can_drop_data_fw", &ScriptTextEditor::can_drop_data_fw);
  872. ClassDB::bind_method("drop_data_fw", &ScriptTextEditor::drop_data_fw);
  873. }
  874. Control *ScriptTextEditor::get_edit_menu() {
  875. return edit_hb;
  876. }
  877. void ScriptTextEditor::reload(bool p_soft) {
  878. TextEdit *te = code_editor->get_text_edit();
  879. Ref<Script> scr = get_edited_script();
  880. if (scr.is_null())
  881. return;
  882. scr->set_source_code(te->get_text());
  883. bool soft = p_soft || scr->get_instance_base_type() == "EditorPlugin"; //always soft-reload editor plugins
  884. scr->get_language()->reload_tool_script(scr, soft);
  885. }
  886. void ScriptTextEditor::get_breakpoints(List<int> *p_breakpoints) {
  887. code_editor->get_text_edit()->get_breakpoints(p_breakpoints);
  888. }
  889. void ScriptTextEditor::set_tooltip_request_func(String p_method, Object *p_obj) {
  890. code_editor->get_text_edit()->set_tooltip_request_func(p_obj, p_method, this);
  891. }
  892. void ScriptTextEditor::set_debugger_active(bool p_active) {
  893. }
  894. Variant ScriptTextEditor::get_drag_data_fw(const Point2 &p_point, Control *p_from) {
  895. return Variant();
  896. }
  897. bool ScriptTextEditor::can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const {
  898. Dictionary d = p_data;
  899. if (d.has("type") &&
  900. (
  901. String(d["type"]) == "resource" ||
  902. String(d["type"]) == "files" ||
  903. String(d["type"]) == "nodes")) {
  904. return true;
  905. }
  906. return false;
  907. }
  908. #ifdef TOOLS_ENABLED
  909. static Node *_find_script_node(Node *p_edited_scene, Node *p_current_node, const Ref<Script> &script) {
  910. if (p_edited_scene != p_current_node && p_current_node->get_owner() != p_edited_scene)
  911. return NULL;
  912. Ref<Script> scr = p_current_node->get_script();
  913. if (scr.is_valid() && scr == script)
  914. return p_current_node;
  915. for (int i = 0; i < p_current_node->get_child_count(); i++) {
  916. Node *n = _find_script_node(p_edited_scene, p_current_node->get_child(i), script);
  917. if (n)
  918. return n;
  919. }
  920. return NULL;
  921. }
  922. #else
  923. static Node *_find_script_node(Node *p_edited_scene, Node *p_current_node, const Ref<Script> &script) {
  924. return NULL;
  925. }
  926. #endif
  927. void ScriptTextEditor::drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) {
  928. Dictionary d = p_data;
  929. if (d.has("type") && String(d["type"]) == "resource") {
  930. Ref<Resource> res = d["resource"];
  931. if (!res.is_valid()) {
  932. return;
  933. }
  934. if (res->get_path().is_resource_file()) {
  935. EditorNode::get_singleton()->show_warning("Only resources from filesystem can be dropped.");
  936. return;
  937. }
  938. code_editor->get_text_edit()->insert_text_at_cursor(res->get_path());
  939. }
  940. if (d.has("type") && String(d["type"]) == "files") {
  941. Array files = d["files"];
  942. String text_to_drop;
  943. for (int i = 0; i < files.size(); i++) {
  944. if (i > 0)
  945. text_to_drop += ",";
  946. text_to_drop += "\"" + String(files[i]).c_escape() + "\"";
  947. }
  948. code_editor->get_text_edit()->insert_text_at_cursor(text_to_drop);
  949. }
  950. if (d.has("type") && String(d["type"]) == "nodes") {
  951. Node *sn = _find_script_node(get_tree()->get_edited_scene_root(), get_tree()->get_edited_scene_root(), script);
  952. if (!sn) {
  953. EditorNode::get_singleton()->show_warning("Can't drop nodes because script '" + get_name() + "' is not used in this scene.");
  954. return;
  955. }
  956. Array nodes = d["nodes"];
  957. String text_to_drop;
  958. for (int i = 0; i < nodes.size(); i++) {
  959. if (i > 0)
  960. text_to_drop += ",";
  961. NodePath np = nodes[i];
  962. Node *node = get_node(np);
  963. if (!node) {
  964. continue;
  965. }
  966. String path = sn->get_path_to(node);
  967. text_to_drop += "\"" + path.c_escape() + "\"";
  968. }
  969. code_editor->get_text_edit()->insert_text_at_cursor(text_to_drop);
  970. }
  971. }
  972. void ScriptTextEditor::_text_edit_gui_input(const InputEvent &ev) {
  973. if (ev.type == InputEvent::MOUSE_BUTTON) {
  974. InputEventMouseButton mb = ev.mouse_button;
  975. if (mb.button_index == BUTTON_RIGHT && !mb.pressed) {
  976. int col, row;
  977. TextEdit *tx = code_editor->get_text_edit();
  978. tx->_get_mouse_pos(Point2i(mb.global_x, mb.global_y) - tx->get_global_position(), row, col);
  979. Vector2 mpos = Vector2(mb.global_x, mb.global_y) - tx->get_global_position();
  980. bool have_selection = (tx->get_selection_text().length() > 0);
  981. bool have_color = (tx->get_word_at_pos(mpos) == "Color");
  982. if (have_color) {
  983. String line = tx->get_line(row);
  984. color_line = row;
  985. int begin = 0;
  986. int end = 0;
  987. bool valid = false;
  988. for (int i = col; i < line.length(); i++) {
  989. if (line[i] == '(') {
  990. begin = i;
  991. continue;
  992. } else if (line[i] == ')') {
  993. end = i + 1;
  994. valid = true;
  995. break;
  996. }
  997. }
  998. if (valid) {
  999. color_args = line.substr(begin, end - begin);
  1000. String stripped = color_args.replace(" ", "").replace("(", "").replace(")", "");
  1001. Vector<float> color = stripped.split_floats(",");
  1002. if (color.size() > 2) {
  1003. float alpha = color.size() > 3 ? color[3] : 1.0f;
  1004. color_picker->set_pick_color(Color(color[0], color[1], color[2], alpha));
  1005. }
  1006. color_panel->set_position(get_global_transform().xform(get_local_mouse_pos()));
  1007. Size2 ms = Size2(300, color_picker->get_combined_minimum_size().height + 10);
  1008. color_panel->set_size(ms);
  1009. } else {
  1010. have_color = false;
  1011. }
  1012. }
  1013. _make_context_menu(have_selection, have_color);
  1014. }
  1015. }
  1016. }
  1017. void ScriptTextEditor::_color_changed(const Color &p_color) {
  1018. String new_args;
  1019. if (p_color.a == 1.0f) {
  1020. new_args = String("(" + rtos(p_color.r) + ", " + rtos(p_color.g) + ", " + rtos(p_color.b) + ")");
  1021. } else {
  1022. new_args = String("(" + rtos(p_color.r) + ", " + rtos(p_color.g) + ", " + rtos(p_color.b) + ", " + rtos(p_color.a) + ")");
  1023. }
  1024. String line = code_editor->get_text_edit()->get_line(color_line);
  1025. String new_line = line.replace(color_args, new_args);
  1026. color_args = new_args;
  1027. code_editor->get_text_edit()->set_line(color_line, new_line);
  1028. }
  1029. void ScriptTextEditor::_make_context_menu(bool p_selection, bool p_color) {
  1030. context_menu->clear();
  1031. if (p_selection) {
  1032. context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/cut"), EDIT_CUT);
  1033. context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/copy"), EDIT_COPY);
  1034. }
  1035. context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/paste"), EDIT_PASTE);
  1036. context_menu->add_separator();
  1037. context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/select_all"), EDIT_SELECT_ALL);
  1038. context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/undo"), EDIT_UNDO);
  1039. context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/redo"), EDIT_REDO);
  1040. if (p_selection) {
  1041. context_menu->add_separator();
  1042. context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/indent_left"), EDIT_INDENT_LEFT);
  1043. context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/indent_right"), EDIT_INDENT_RIGHT);
  1044. context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/toggle_comment"), EDIT_TOGGLE_COMMENT);
  1045. }
  1046. if (p_color) {
  1047. context_menu->add_separator();
  1048. context_menu->add_item(TTR("Pick Color"), EDIT_PICK_COLOR);
  1049. }
  1050. context_menu->set_position(get_global_transform().xform(get_local_mouse_pos()));
  1051. context_menu->set_size(Vector2(1, 1));
  1052. context_menu->popup();
  1053. }
  1054. ScriptTextEditor::ScriptTextEditor() {
  1055. code_editor = memnew(CodeTextEditor);
  1056. add_child(code_editor);
  1057. code_editor->add_constant_override("separation", 0);
  1058. code_editor->set_area_as_parent_rect();
  1059. code_editor->connect("validate_script", this, "_validate_script");
  1060. code_editor->connect("load_theme_settings", this, "_load_theme_settings");
  1061. code_editor->set_code_complete_func(_code_complete_scripts, this);
  1062. code_editor->get_text_edit()->connect("breakpoint_toggled", this, "_breakpoint_toggled");
  1063. code_editor->get_text_edit()->connect("symbol_lookup", this, "_lookup_symbol");
  1064. update_settings();
  1065. code_editor->get_text_edit()->set_callhint_settings(
  1066. EditorSettings::get_singleton()->get("text_editor/completion/put_callhint_tooltip_below_current_line"),
  1067. EditorSettings::get_singleton()->get("text_editor/completion/callhint_tooltip_offset"));
  1068. code_editor->get_text_edit()->set_select_identifiers_on_hover(true);
  1069. code_editor->get_text_edit()->set_context_menu_enabled(false);
  1070. code_editor->get_text_edit()->connect("gui_input", this, "_text_edit_gui_input");
  1071. context_menu = memnew(PopupMenu);
  1072. add_child(context_menu);
  1073. context_menu->connect("id_pressed", this, "_edit_option");
  1074. color_panel = memnew(PopupPanel);
  1075. add_child(color_panel);
  1076. color_picker = memnew(ColorPicker);
  1077. color_panel->add_child(color_picker);
  1078. color_panel->set_child_rect(color_picker); //NOT
  1079. color_picker->connect("color_changed", this, "_color_changed");
  1080. edit_hb = memnew(HBoxContainer);
  1081. edit_menu = memnew(MenuButton);
  1082. edit_menu->set_text(TTR("Edit"));
  1083. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/undo"), EDIT_UNDO);
  1084. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/redo"), EDIT_REDO);
  1085. edit_menu->get_popup()->add_separator();
  1086. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/cut"), EDIT_CUT);
  1087. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/copy"), EDIT_COPY);
  1088. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/paste"), EDIT_PASTE);
  1089. edit_menu->get_popup()->add_separator();
  1090. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/select_all"), EDIT_SELECT_ALL);
  1091. edit_menu->get_popup()->add_separator();
  1092. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/move_up"), EDIT_MOVE_LINE_UP);
  1093. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/move_down"), EDIT_MOVE_LINE_DOWN);
  1094. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/indent_left"), EDIT_INDENT_LEFT);
  1095. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/indent_right"), EDIT_INDENT_RIGHT);
  1096. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/toggle_comment"), EDIT_TOGGLE_COMMENT);
  1097. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/clone_down"), EDIT_CLONE_DOWN);
  1098. edit_menu->get_popup()->add_separator();
  1099. #ifdef OSX_ENABLED
  1100. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/complete_symbol"), EDIT_COMPLETE);
  1101. #else
  1102. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/complete_symbol"), EDIT_COMPLETE);
  1103. #endif
  1104. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/trim_trailing_whitespace"), EDIT_TRIM_TRAILING_WHITESAPCE);
  1105. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/convert_indent_to_spaces"), EDIT_CONVERT_INDENT_TO_SPACES);
  1106. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/convert_indent_to_tabs"), EDIT_CONVERT_INDENT_TO_TABS);
  1107. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/auto_indent"), EDIT_AUTO_INDENT);
  1108. edit_menu->get_popup()->connect("id_pressed", this, "_edit_option");
  1109. edit_menu->get_popup()->add_separator();
  1110. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/toggle_breakpoint"), DEBUG_TOGGLE_BREAKPOINT);
  1111. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/remove_all_breakpoints"), DEBUG_REMOVE_ALL_BREAKPOINTS);
  1112. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/goto_next_breakpoint"), DEBUG_GOTO_NEXT_BREAKPOINT);
  1113. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/goto_previous_breakpoint"), DEBUG_GOTO_PREV_BREAKPOINT);
  1114. edit_menu->get_popup()->add_separator();
  1115. PopupMenu *convert_case = memnew(PopupMenu);
  1116. convert_case->set_name("convert_case");
  1117. edit_menu->get_popup()->add_child(convert_case);
  1118. edit_menu->get_popup()->add_submenu_item(TTR("Convert Case"), "convert_case");
  1119. convert_case->add_shortcut(ED_SHORTCUT("script_text_editor/convert_to_uppercase", TTR("Uppercase")), EDIT_TO_UPPERCASE);
  1120. convert_case->add_shortcut(ED_SHORTCUT("script_text_editor/convert_to_lowercase", TTR("Lowercase")), EDIT_TO_LOWERCASE);
  1121. convert_case->add_shortcut(ED_SHORTCUT("script_text_editor/capitalize", TTR("Capitalize")), EDIT_CAPITALIZE);
  1122. convert_case->connect("id_pressed", this, "_edit_option");
  1123. search_menu = memnew(MenuButton);
  1124. edit_hb->add_child(search_menu);
  1125. search_menu->set_text(TTR("Search"));
  1126. search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/find"), SEARCH_FIND);
  1127. search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/find_next"), SEARCH_FIND_NEXT);
  1128. search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/find_previous"), SEARCH_FIND_PREV);
  1129. search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/replace"), SEARCH_REPLACE);
  1130. search_menu->get_popup()->add_separator();
  1131. search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/goto_function"), SEARCH_LOCATE_FUNCTION);
  1132. search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/goto_line"), SEARCH_GOTO_LINE);
  1133. search_menu->get_popup()->add_separator();
  1134. search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/contextual_help"), HELP_CONTEXTUAL);
  1135. search_menu->get_popup()->connect("id_pressed", this, "_edit_option");
  1136. edit_hb->add_child(edit_menu);
  1137. quick_open = memnew(ScriptEditorQuickOpen);
  1138. add_child(quick_open);
  1139. quick_open->connect("goto_line", this, "_goto_line");
  1140. goto_line_dialog = memnew(GotoLineDialog);
  1141. add_child(goto_line_dialog);
  1142. code_editor->get_text_edit()->set_drag_forwarding(this);
  1143. }
  1144. static ScriptEditorBase *create_editor(const Ref<Script> &p_script) {
  1145. if (p_script->has_source_code()) {
  1146. return memnew(ScriptTextEditor);
  1147. }
  1148. return NULL;
  1149. }
  1150. void ScriptTextEditor::register_editor() {
  1151. ED_SHORTCUT("script_text_editor/undo", TTR("Undo"), KEY_MASK_CMD | KEY_Z);
  1152. ED_SHORTCUT("script_text_editor/redo", TTR("Redo"), KEY_MASK_CMD | KEY_Y);
  1153. ED_SHORTCUT("script_text_editor/cut", TTR("Cut"), KEY_MASK_CMD | KEY_X);
  1154. ED_SHORTCUT("script_text_editor/copy", TTR("Copy"), KEY_MASK_CMD | KEY_C);
  1155. ED_SHORTCUT("script_text_editor/paste", TTR("Paste"), KEY_MASK_CMD | KEY_V);
  1156. ED_SHORTCUT("script_text_editor/select_all", TTR("Select All"), KEY_MASK_CMD | KEY_A);
  1157. ED_SHORTCUT("script_text_editor/move_up", TTR("Move Up"), KEY_MASK_ALT | KEY_UP);
  1158. ED_SHORTCUT("script_text_editor/move_down", TTR("Move Down"), KEY_MASK_ALT | KEY_DOWN);
  1159. //leave these at zero, same can be accomplished with tab/shift-tab, including selection
  1160. //the next/previous in history shortcut in this case makes a lot more sene.
  1161. ED_SHORTCUT("script_text_editor/indent_left", TTR("Indent Left"), 0);
  1162. ED_SHORTCUT("script_text_editor/indent_right", TTR("Indent Right"), 0);
  1163. ED_SHORTCUT("script_text_editor/toggle_comment", TTR("Toggle Comment"), KEY_MASK_CMD | KEY_K);
  1164. ED_SHORTCUT("script_text_editor/clone_down", TTR("Clone Down"), KEY_MASK_CMD | KEY_B);
  1165. #ifdef OSX_ENABLED
  1166. ED_SHORTCUT("script_text_editor/complete_symbol", TTR("Complete Symbol"), KEY_MASK_CTRL | KEY_SPACE);
  1167. #else
  1168. ED_SHORTCUT("script_text_editor/complete_symbol", TTR("Complete Symbol"), KEY_MASK_CMD | KEY_SPACE);
  1169. #endif
  1170. ED_SHORTCUT("script_text_editor/trim_trailing_whitespace", TTR("Trim Trailing Whitespace"), KEY_MASK_CTRL | KEY_MASK_ALT | KEY_T);
  1171. ED_SHORTCUT("script_text_editor/convert_indent_to_spaces", TTR("Convert Indent To Spaces"), KEY_MASK_CTRL | KEY_MASK_SHIFT | KEY_Y);
  1172. ED_SHORTCUT("script_text_editor/convert_indent_to_tabs", TTR("Convert Indent To Tabs"), KEY_MASK_CTRL | KEY_MASK_SHIFT | KEY_X);
  1173. ED_SHORTCUT("script_text_editor/auto_indent", TTR("Auto Indent"), KEY_MASK_CMD | KEY_I);
  1174. ED_SHORTCUT("script_text_editor/toggle_breakpoint", TTR("Toggle Breakpoint"), KEY_F9);
  1175. ED_SHORTCUT("script_text_editor/remove_all_breakpoints", TTR("Remove All Breakpoints"), KEY_MASK_CTRL | KEY_MASK_SHIFT | KEY_F9);
  1176. ED_SHORTCUT("script_text_editor/goto_next_breakpoint", TTR("Goto Next Breakpoint"), KEY_MASK_CTRL | KEY_PERIOD);
  1177. ED_SHORTCUT("script_text_editor/goto_previous_breakpoint", TTR("Goto Previous Breakpoint"), KEY_MASK_CTRL | KEY_COMMA);
  1178. ED_SHORTCUT("script_text_editor/convert_to_uppercase", TTR("Convert To Uppercase"), KEY_MASK_SHIFT | KEY_F4);
  1179. ED_SHORTCUT("script_text_editor/convert_to_lowercase", TTR("Convert To Lowercase"), KEY_MASK_SHIFT | KEY_F3);
  1180. ED_SHORTCUT("script_text_editor/capitalize", TTR("Capitalize"), KEY_MASK_SHIFT | KEY_F2);
  1181. ED_SHORTCUT("script_text_editor/find", TTR("Find.."), KEY_MASK_CMD | KEY_F);
  1182. ED_SHORTCUT("script_text_editor/find_next", TTR("Find Next"), KEY_F3);
  1183. ED_SHORTCUT("script_text_editor/find_previous", TTR("Find Previous"), KEY_MASK_SHIFT | KEY_F3);
  1184. ED_SHORTCUT("script_text_editor/replace", TTR("Replace.."), KEY_MASK_CMD | KEY_R);
  1185. ED_SHORTCUT("script_text_editor/goto_function", TTR("Goto Function.."), KEY_MASK_SHIFT | KEY_MASK_CMD | KEY_F);
  1186. ED_SHORTCUT("script_text_editor/goto_line", TTR("Goto Line.."), KEY_MASK_CMD | KEY_L);
  1187. ED_SHORTCUT("script_text_editor/contextual_help", TTR("Contextual Help"), KEY_MASK_SHIFT | KEY_F1);
  1188. ScriptEditor::register_create_script_editor_function(create_editor);
  1189. }