script_text_editor.cpp 48 KB

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