code_editor.cpp 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351
  1. /*************************************************************************/
  2. /* code_editor.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2018 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 "code_editor.h"
  31. #include "editor/editor_scale.h"
  32. #include "editor_node.h"
  33. #include "editor_settings.h"
  34. #include "os/keyboard.h"
  35. #include "scene/gui/margin_container.h"
  36. #include "scene/gui/separator.h"
  37. #include "scene/resources/dynamic_font.h"
  38. void GotoLineDialog::popup_find_line(TextEdit *p_edit) {
  39. text_editor = p_edit;
  40. line->set_text(itos(text_editor->cursor_get_line()));
  41. line->select_all();
  42. popup_centered(Size2(180, 80));
  43. line->grab_focus();
  44. }
  45. int GotoLineDialog::get_line() const {
  46. return line->get_text().to_int();
  47. }
  48. void GotoLineDialog::ok_pressed() {
  49. if (get_line() < 1 || get_line() > text_editor->get_line_count())
  50. return;
  51. text_editor->unfold_line(get_line() - 1);
  52. text_editor->cursor_set_line(get_line() - 1);
  53. hide();
  54. }
  55. GotoLineDialog::GotoLineDialog() {
  56. set_title(TTR("Go to Line"));
  57. Label *l = memnew(Label);
  58. l->set_text(TTR("Line Number:"));
  59. l->set_position(Point2(5, 5));
  60. add_child(l);
  61. line = memnew(LineEdit);
  62. line->set_anchor(MARGIN_RIGHT, ANCHOR_END);
  63. line->set_begin(Point2(15, 22));
  64. line->set_end(Point2(-15, 35));
  65. add_child(line);
  66. register_text_enter(line);
  67. text_editor = NULL;
  68. set_hide_on_ok(false);
  69. }
  70. void FindReplaceBar::_notification(int p_what) {
  71. if (p_what == NOTIFICATION_READY) {
  72. find_prev->set_icon(get_icon("MoveUp", "EditorIcons"));
  73. find_next->set_icon(get_icon("MoveDown", "EditorIcons"));
  74. hide_button->set_normal_texture(get_icon("Close", "EditorIcons"));
  75. hide_button->set_hover_texture(get_icon("Close", "EditorIcons"));
  76. hide_button->set_pressed_texture(get_icon("Close", "EditorIcons"));
  77. hide_button->set_custom_minimum_size(hide_button->get_normal_texture()->get_size());
  78. } else if (p_what == NOTIFICATION_VISIBILITY_CHANGED) {
  79. set_process_unhandled_input(is_visible_in_tree());
  80. if (is_visible_in_tree()) {
  81. _update_size();
  82. }
  83. } else if (p_what == EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED) {
  84. find_prev->set_icon(get_icon("MoveUp", "EditorIcons"));
  85. find_next->set_icon(get_icon("MoveDown", "EditorIcons"));
  86. hide_button->set_normal_texture(get_icon("Close", "EditorIcons"));
  87. hide_button->set_hover_texture(get_icon("Close", "EditorIcons"));
  88. hide_button->set_pressed_texture(get_icon("Close", "EditorIcons"));
  89. hide_button->set_custom_minimum_size(hide_button->get_normal_texture()->get_size());
  90. }
  91. }
  92. void FindReplaceBar::_unhandled_input(const Ref<InputEvent> &p_event) {
  93. Ref<InputEventKey> k = p_event;
  94. if (k.is_valid()) {
  95. if (k->is_pressed() && (text_edit->has_focus() || vbc_lineedit->is_a_parent_of(get_focus_owner()))) {
  96. bool accepted = true;
  97. switch (k->get_scancode()) {
  98. case KEY_ESCAPE: {
  99. _hide_bar();
  100. } break;
  101. default: {
  102. accepted = false;
  103. } break;
  104. }
  105. if (accepted) {
  106. accept_event();
  107. }
  108. }
  109. }
  110. }
  111. bool FindReplaceBar::_search(uint32_t p_flags, int p_from_line, int p_from_col) {
  112. int line, col;
  113. String text = get_search_text();
  114. bool found = text_edit->search(text, p_flags, p_from_line, p_from_col, line, col);
  115. if (found) {
  116. if (!preserve_cursor) {
  117. text_edit->unfold_line(line);
  118. text_edit->cursor_set_line(line, false);
  119. text_edit->cursor_set_column(col + text.length(), false);
  120. text_edit->center_viewport_to_cursor();
  121. }
  122. text_edit->set_search_text(text);
  123. text_edit->set_search_flags(p_flags);
  124. text_edit->set_current_search_result(line, col);
  125. result_line = line;
  126. result_col = col;
  127. set_error("");
  128. } else {
  129. result_line = -1;
  130. result_col = -1;
  131. text_edit->set_search_text("");
  132. set_error(text.empty() ? "" : TTR("No Matches"));
  133. }
  134. return found;
  135. }
  136. void FindReplaceBar::_replace() {
  137. if (result_line != -1 && result_col != -1) {
  138. text_edit->begin_complex_operation();
  139. text_edit->unfold_line(result_line);
  140. text_edit->select(result_line, result_col, result_line, result_col + get_search_text().length());
  141. text_edit->insert_text_at_cursor(get_replace_text());
  142. text_edit->end_complex_operation();
  143. }
  144. search_current();
  145. }
  146. void FindReplaceBar::_replace_all() {
  147. text_edit->disconnect("text_changed", this, "_editor_text_changed");
  148. // line as x so it gets priority in comparison, column as y
  149. Point2i orig_cursor(text_edit->cursor_get_line(), text_edit->cursor_get_column());
  150. Point2i prev_match = Point2(-1, -1);
  151. bool selection_enabled = text_edit->is_selection_active();
  152. Point2i selection_begin, selection_end;
  153. if (selection_enabled) {
  154. selection_begin = Point2i(text_edit->get_selection_from_line(), text_edit->get_selection_from_column());
  155. selection_end = Point2i(text_edit->get_selection_to_line(), text_edit->get_selection_to_column());
  156. }
  157. int vsval = text_edit->get_v_scroll();
  158. text_edit->cursor_set_line(0);
  159. text_edit->cursor_set_column(0);
  160. String replace_text = get_replace_text();
  161. int search_text_len = get_search_text().length();
  162. int rc = 0;
  163. replace_all_mode = true;
  164. text_edit->begin_complex_operation();
  165. if (search_current()) {
  166. do {
  167. // replace area
  168. Point2i match_from(result_line, result_col);
  169. Point2i match_to(result_line, result_col + search_text_len);
  170. if (match_from < prev_match) {
  171. break; // done
  172. }
  173. prev_match = Point2i(result_line, result_col + replace_text.length());
  174. text_edit->unfold_line(result_line);
  175. text_edit->select(result_line, result_col, result_line, match_to.y);
  176. if (selection_enabled && is_selection_only()) {
  177. if (match_from < selection_begin || match_to > selection_end) {
  178. continue;
  179. }
  180. // replace but adjust selection bounds
  181. text_edit->insert_text_at_cursor(replace_text);
  182. if (match_to.x == selection_end.x) {
  183. selection_end.y += replace_text.length() - search_text_len;
  184. }
  185. } else {
  186. // just replace
  187. text_edit->insert_text_at_cursor(replace_text);
  188. }
  189. rc++;
  190. } while (search_next());
  191. }
  192. text_edit->end_complex_operation();
  193. replace_all_mode = false;
  194. // restore editor state (selection, cursor, scroll)
  195. text_edit->cursor_set_line(orig_cursor.x);
  196. text_edit->cursor_set_column(orig_cursor.y);
  197. if (selection_enabled && is_selection_only()) {
  198. // reselect
  199. text_edit->select(selection_begin.x, selection_begin.y, selection_end.x, selection_end.y);
  200. } else {
  201. text_edit->deselect();
  202. }
  203. text_edit->set_v_scroll(vsval);
  204. set_error(vformat(TTR("Replaced %d occurrence(s)."), rc));
  205. text_edit->call_deferred("connect", "text_changed", this, "_editor_text_changed");
  206. }
  207. void FindReplaceBar::_get_search_from(int &r_line, int &r_col) {
  208. r_line = text_edit->cursor_get_line();
  209. r_col = text_edit->cursor_get_column();
  210. if (text_edit->is_selection_active() && !replace_all_mode) {
  211. int selection_line = text_edit->get_selection_from_line();
  212. if (text_edit->get_selection_text() == get_search_text() && r_line == selection_line) {
  213. int selection_from_col = text_edit->get_selection_from_column();
  214. if (r_col >= selection_from_col && r_col <= text_edit->get_selection_to_column()) {
  215. r_col = selection_from_col;
  216. }
  217. }
  218. }
  219. if (r_line == result_line && r_col >= result_col && r_col <= result_col + get_search_text().length()) {
  220. r_col = result_col;
  221. }
  222. }
  223. bool FindReplaceBar::search_current() {
  224. uint32_t flags = 0;
  225. if (is_whole_words())
  226. flags |= TextEdit::SEARCH_WHOLE_WORDS;
  227. if (is_case_sensitive())
  228. flags |= TextEdit::SEARCH_MATCH_CASE;
  229. int line, col;
  230. _get_search_from(line, col);
  231. return _search(flags, line, col);
  232. }
  233. bool FindReplaceBar::search_prev() {
  234. uint32_t flags = 0;
  235. String text = get_search_text();
  236. if (is_whole_words())
  237. flags |= TextEdit::SEARCH_WHOLE_WORDS;
  238. if (is_case_sensitive())
  239. flags |= TextEdit::SEARCH_MATCH_CASE;
  240. flags |= TextEdit::SEARCH_BACKWARDS;
  241. int line, col;
  242. _get_search_from(line, col);
  243. col -= text.length();
  244. if (col < 0) {
  245. line -= 1;
  246. if (line < 0)
  247. line = text_edit->get_line_count() - 1;
  248. col = text_edit->get_line(line).length();
  249. }
  250. return _search(flags, line, col);
  251. }
  252. bool FindReplaceBar::search_next() {
  253. uint32_t flags = 0;
  254. String text = get_search_text();
  255. if (is_whole_words())
  256. flags |= TextEdit::SEARCH_WHOLE_WORDS;
  257. if (is_case_sensitive())
  258. flags |= TextEdit::SEARCH_MATCH_CASE;
  259. int line, col;
  260. _get_search_from(line, col);
  261. if (line == result_line && col == result_col) {
  262. col += text.length();
  263. if (col > text_edit->get_line(line).length()) {
  264. line += 1;
  265. if (line >= text_edit->get_line_count())
  266. line = 0;
  267. col = 0;
  268. }
  269. }
  270. return _search(flags, line, col);
  271. }
  272. void FindReplaceBar::_hide_bar() {
  273. if (replace_text->has_focus() || search_text->has_focus())
  274. text_edit->grab_focus();
  275. text_edit->set_search_text("");
  276. result_line = -1;
  277. result_col = -1;
  278. set_error("");
  279. hide();
  280. }
  281. void FindReplaceBar::_show_search() {
  282. show();
  283. search_text->grab_focus();
  284. if (text_edit->is_selection_active() && !selection_only->is_pressed()) {
  285. search_text->set_text(text_edit->get_selection_text());
  286. }
  287. if (!get_search_text().empty()) {
  288. search_text->select_all();
  289. search_text->set_cursor_position(search_text->get_text().length());
  290. search_current();
  291. }
  292. call_deferred("_update_size");
  293. }
  294. void FindReplaceBar::popup_search() {
  295. replace_text->hide();
  296. hbc_button_replace->hide();
  297. hbc_option_replace->hide();
  298. _show_search();
  299. }
  300. void FindReplaceBar::popup_replace() {
  301. if (!replace_text->is_visible_in_tree()) {
  302. replace_text->clear();
  303. replace_text->show();
  304. hbc_button_replace->show();
  305. hbc_option_replace->show();
  306. }
  307. selection_only->set_pressed((text_edit->is_selection_active() && text_edit->get_selection_from_line() < text_edit->get_selection_to_line()));
  308. _show_search();
  309. }
  310. void FindReplaceBar::_search_options_changed(bool p_pressed) {
  311. search_current();
  312. }
  313. void FindReplaceBar::_editor_text_changed() {
  314. if (is_visible_in_tree()) {
  315. preserve_cursor = true;
  316. search_current();
  317. preserve_cursor = false;
  318. }
  319. }
  320. void FindReplaceBar::_search_text_changed(const String &p_text) {
  321. search_current();
  322. }
  323. void FindReplaceBar::_search_text_entered(const String &p_text) {
  324. search_next();
  325. }
  326. void FindReplaceBar::_replace_text_entered(const String &p_text) {
  327. if (selection_only->is_pressed() && text_edit->is_selection_active()) {
  328. _replace_all();
  329. _hide_bar();
  330. }
  331. }
  332. String FindReplaceBar::get_search_text() const {
  333. return search_text->get_text();
  334. }
  335. String FindReplaceBar::get_replace_text() const {
  336. return replace_text->get_text();
  337. }
  338. bool FindReplaceBar::is_case_sensitive() const {
  339. return case_sensitive->is_pressed();
  340. }
  341. bool FindReplaceBar::is_whole_words() const {
  342. return whole_words->is_pressed();
  343. }
  344. bool FindReplaceBar::is_selection_only() const {
  345. return selection_only->is_pressed();
  346. }
  347. void FindReplaceBar::set_error(const String &p_label) {
  348. emit_signal("error", p_label);
  349. }
  350. void FindReplaceBar::set_text_edit(TextEdit *p_text_edit) {
  351. text_edit = p_text_edit;
  352. text_edit->connect("text_changed", this, "_editor_text_changed");
  353. }
  354. void FindReplaceBar::_update_size() {
  355. container->set_size(Size2(hbc->get_size().width, 1));
  356. }
  357. void FindReplaceBar::_bind_methods() {
  358. ClassDB::bind_method("_unhandled_input", &FindReplaceBar::_unhandled_input);
  359. ClassDB::bind_method("_editor_text_changed", &FindReplaceBar::_editor_text_changed);
  360. ClassDB::bind_method("_search_text_changed", &FindReplaceBar::_search_text_changed);
  361. ClassDB::bind_method("_search_text_entered", &FindReplaceBar::_search_text_entered);
  362. ClassDB::bind_method("_replace_text_entered", &FindReplaceBar::_replace_text_entered);
  363. ClassDB::bind_method("_search_current", &FindReplaceBar::search_current);
  364. ClassDB::bind_method("_search_next", &FindReplaceBar::search_next);
  365. ClassDB::bind_method("_search_prev", &FindReplaceBar::search_prev);
  366. ClassDB::bind_method("_replace_pressed", &FindReplaceBar::_replace);
  367. ClassDB::bind_method("_replace_all_pressed", &FindReplaceBar::_replace_all);
  368. ClassDB::bind_method("_search_options_changed", &FindReplaceBar::_search_options_changed);
  369. ClassDB::bind_method("_hide_pressed", &FindReplaceBar::_hide_bar);
  370. ClassDB::bind_method("_update_size", &FindReplaceBar::_update_size);
  371. ADD_SIGNAL(MethodInfo("search"));
  372. ADD_SIGNAL(MethodInfo("error"));
  373. }
  374. FindReplaceBar::FindReplaceBar() {
  375. container = memnew(MarginContainer);
  376. container->add_constant_override("margin_bottom", 5 * EDSCALE);
  377. add_child(container);
  378. container->set_clip_contents(true);
  379. container->set_h_size_flags(SIZE_EXPAND_FILL);
  380. replace_all_mode = false;
  381. preserve_cursor = false;
  382. hbc = memnew(HBoxContainer);
  383. container->add_child(hbc);
  384. hbc->set_anchor_and_margin(MARGIN_RIGHT, 1, 0);
  385. vbc_lineedit = memnew(VBoxContainer);
  386. hbc->add_child(vbc_lineedit);
  387. vbc_lineedit->set_h_size_flags(SIZE_EXPAND_FILL);
  388. VBoxContainer *vbc_button = memnew(VBoxContainer);
  389. hbc->add_child(vbc_button);
  390. VBoxContainer *vbc_option = memnew(VBoxContainer);
  391. hbc->add_child(vbc_option);
  392. HBoxContainer *hbc_button_search = memnew(HBoxContainer);
  393. vbc_button->add_child(hbc_button_search);
  394. hbc_button_replace = memnew(HBoxContainer);
  395. vbc_button->add_child(hbc_button_replace);
  396. HBoxContainer *hbc_option_search = memnew(HBoxContainer);
  397. vbc_option->add_child(hbc_option_search);
  398. hbc_option_replace = memnew(HBoxContainer);
  399. vbc_option->add_child(hbc_option_replace);
  400. // search toolbar
  401. search_text = memnew(LineEdit);
  402. vbc_lineedit->add_child(search_text);
  403. search_text->set_custom_minimum_size(Size2(100 * EDSCALE, 0));
  404. search_text->connect("text_changed", this, "_search_text_changed");
  405. search_text->connect("text_entered", this, "_search_text_entered");
  406. find_prev = memnew(ToolButton);
  407. hbc_button_search->add_child(find_prev);
  408. find_prev->set_focus_mode(FOCUS_NONE);
  409. find_prev->connect("pressed", this, "_search_prev");
  410. find_next = memnew(ToolButton);
  411. hbc_button_search->add_child(find_next);
  412. find_next->set_focus_mode(FOCUS_NONE);
  413. find_next->connect("pressed", this, "_search_next");
  414. case_sensitive = memnew(CheckBox);
  415. hbc_option_search->add_child(case_sensitive);
  416. case_sensitive->set_text(TTR("Match Case"));
  417. case_sensitive->set_focus_mode(FOCUS_NONE);
  418. case_sensitive->connect("toggled", this, "_search_options_changed");
  419. whole_words = memnew(CheckBox);
  420. hbc_option_search->add_child(whole_words);
  421. whole_words->set_text(TTR("Whole Words"));
  422. whole_words->set_focus_mode(FOCUS_NONE);
  423. whole_words->connect("toggled", this, "_search_options_changed");
  424. // replace toolbar
  425. replace_text = memnew(LineEdit);
  426. vbc_lineedit->add_child(replace_text);
  427. replace_text->set_custom_minimum_size(Size2(100 * EDSCALE, 0));
  428. replace_text->connect("text_entered", this, "_replace_text_entered");
  429. replace = memnew(Button);
  430. hbc_button_replace->add_child(replace);
  431. replace->set_text(TTR("Replace"));
  432. replace->connect("pressed", this, "_replace_pressed");
  433. replace_all = memnew(Button);
  434. hbc_button_replace->add_child(replace_all);
  435. replace_all->set_text(TTR("Replace All"));
  436. replace_all->connect("pressed", this, "_replace_all_pressed");
  437. selection_only = memnew(CheckBox);
  438. hbc_option_replace->add_child(selection_only);
  439. selection_only->set_text(TTR("Selection Only"));
  440. selection_only->set_focus_mode(FOCUS_NONE);
  441. selection_only->connect("toggled", this, "_search_options_changed");
  442. hide_button = memnew(TextureButton);
  443. add_child(hide_button);
  444. hide_button->set_focus_mode(FOCUS_NONE);
  445. hide_button->connect("pressed", this, "_hide_pressed");
  446. hide_button->set_v_size_flags(SIZE_SHRINK_CENTER);
  447. }
  448. /*** CODE EDITOR ****/
  449. void CodeTextEditor::_text_editor_gui_input(const Ref<InputEvent> &p_event) {
  450. Ref<InputEventMouseButton> mb = p_event;
  451. if (mb.is_valid()) {
  452. if (mb->is_pressed() && mb->get_command()) {
  453. if (mb->get_button_index() == BUTTON_WHEEL_UP) {
  454. _zoom_in();
  455. } else if (mb->get_button_index() == BUTTON_WHEEL_DOWN) {
  456. _zoom_out();
  457. }
  458. }
  459. }
  460. Ref<InputEventMagnifyGesture> magnify_gesture = p_event;
  461. if (magnify_gesture.is_valid()) {
  462. Ref<DynamicFont> font = text_editor->get_font("font");
  463. if (font.is_valid()) {
  464. if (font->get_size() != (int)font_size) {
  465. font_size = font->get_size();
  466. }
  467. font_size *= powf(magnify_gesture->get_factor(), 0.25);
  468. _add_font_size((int)font_size - font->get_size());
  469. }
  470. return;
  471. }
  472. Ref<InputEventKey> k = p_event;
  473. if (k.is_valid()) {
  474. if (k->is_pressed()) {
  475. if (ED_IS_SHORTCUT("script_editor/zoom_in", p_event)) {
  476. _zoom_in();
  477. }
  478. if (ED_IS_SHORTCUT("script_editor/zoom_out", p_event)) {
  479. _zoom_out();
  480. }
  481. if (ED_IS_SHORTCUT("script_editor/reset_zoom", p_event)) {
  482. _reset_zoom();
  483. }
  484. }
  485. }
  486. }
  487. void CodeTextEditor::_zoom_in() {
  488. font_resize_val += MAX(EDSCALE, 1.0f);
  489. _zoom_changed();
  490. }
  491. void CodeTextEditor::_zoom_out() {
  492. font_resize_val -= MAX(EDSCALE, 1.0f);
  493. _zoom_changed();
  494. }
  495. void CodeTextEditor::_zoom_changed() {
  496. if (font_resize_timer->get_time_left() == 0)
  497. font_resize_timer->start();
  498. }
  499. void CodeTextEditor::_reset_zoom() {
  500. Ref<DynamicFont> font = text_editor->get_font("font"); // reset source font size to default
  501. if (font.is_valid()) {
  502. EditorSettings::get_singleton()->set("interface/editor/code_font_size", 14);
  503. font->set_size(14);
  504. zoom_nb->set_text("100%");
  505. }
  506. }
  507. void CodeTextEditor::_line_col_changed() {
  508. line_nb->set_text(itos(text_editor->cursor_get_line() + 1));
  509. String line = text_editor->get_line(text_editor->cursor_get_line());
  510. int positional_column = 0;
  511. for (int i = 0; i < text_editor->cursor_get_column(); i++) {
  512. if (line[i] == '\t') {
  513. positional_column += text_editor->get_indent_size(); //tab size
  514. } else {
  515. positional_column += 1;
  516. }
  517. }
  518. col_nb->set_text(itos(positional_column + 1));
  519. }
  520. void CodeTextEditor::_text_changed() {
  521. if (text_editor->is_insert_text_operation()) {
  522. code_complete_timer->start();
  523. }
  524. idle->start();
  525. }
  526. void CodeTextEditor::_code_complete_timer_timeout() {
  527. if (!is_visible_in_tree())
  528. return;
  529. if (enable_complete_timer)
  530. text_editor->query_code_comple();
  531. }
  532. void CodeTextEditor::_complete_request() {
  533. List<String> entries;
  534. String ctext = text_editor->get_text_for_completion();
  535. _code_complete_script(ctext, &entries);
  536. bool forced = false;
  537. if (code_complete_func) {
  538. code_complete_func(code_complete_ud, ctext, &entries, forced);
  539. }
  540. if (entries.size() == 0)
  541. return;
  542. Vector<String> strs;
  543. strs.resize(entries.size());
  544. int i = 0;
  545. for (List<String>::Element *E = entries.front(); E; E = E->next()) {
  546. strs.write[i++] = E->get();
  547. }
  548. text_editor->code_complete(strs, forced);
  549. }
  550. void CodeTextEditor::_font_resize_timeout() {
  551. if (_add_font_size(font_resize_val)) {
  552. font_resize_val = 0;
  553. }
  554. }
  555. bool CodeTextEditor::_add_font_size(int p_delta) {
  556. Ref<DynamicFont> font = text_editor->get_font("font");
  557. if (font.is_valid()) {
  558. int new_size = CLAMP(font->get_size() + p_delta, 8 * EDSCALE, 96 * EDSCALE);
  559. zoom_nb->set_text(itos(100 * new_size / (14 * EDSCALE)) + "%");
  560. if (new_size != font->get_size()) {
  561. EditorSettings::get_singleton()->set("interface/editor/code_font_size", new_size / EDSCALE);
  562. font->set_size(new_size);
  563. }
  564. return true;
  565. } else {
  566. return false;
  567. }
  568. }
  569. void CodeTextEditor::update_editor_settings() {
  570. text_editor->set_auto_brace_completion(EditorSettings::get_singleton()->get("text_editor/completion/auto_brace_complete"));
  571. text_editor->set_scroll_pass_end_of_file(EditorSettings::get_singleton()->get("text_editor/cursor/scroll_past_end_of_file"));
  572. text_editor->set_indent_using_spaces(EditorSettings::get_singleton()->get("text_editor/indent/type"));
  573. text_editor->set_indent_size(EditorSettings::get_singleton()->get("text_editor/indent/size"));
  574. text_editor->set_auto_indent(EditorSettings::get_singleton()->get("text_editor/indent/auto_indent"));
  575. text_editor->set_draw_tabs(EditorSettings::get_singleton()->get("text_editor/indent/draw_tabs"));
  576. text_editor->set_show_line_numbers(EditorSettings::get_singleton()->get("text_editor/line_numbers/show_line_numbers"));
  577. text_editor->set_line_numbers_zero_padded(EditorSettings::get_singleton()->get("text_editor/line_numbers/line_numbers_zero_padded"));
  578. text_editor->set_show_line_length_guideline(EditorSettings::get_singleton()->get("text_editor/line_numbers/show_line_length_guideline"));
  579. text_editor->set_line_length_guideline_column(EditorSettings::get_singleton()->get("text_editor/line_numbers/line_length_guideline_column"));
  580. text_editor->set_syntax_coloring(EditorSettings::get_singleton()->get("text_editor/highlighting/syntax_highlighting"));
  581. text_editor->set_highlight_all_occurrences(EditorSettings::get_singleton()->get("text_editor/highlighting/highlight_all_occurrences"));
  582. text_editor->set_highlight_current_line(EditorSettings::get_singleton()->get("text_editor/highlighting/highlight_current_line"));
  583. text_editor->cursor_set_blink_enabled(EditorSettings::get_singleton()->get("text_editor/cursor/caret_blink"));
  584. text_editor->cursor_set_blink_speed(EditorSettings::get_singleton()->get("text_editor/cursor/caret_blink_speed"));
  585. text_editor->set_breakpoint_gutter_enabled(EditorSettings::get_singleton()->get("text_editor/line_numbers/show_breakpoint_gutter"));
  586. text_editor->set_hiding_enabled(EditorSettings::get_singleton()->get("text_editor/line_numbers/code_folding"));
  587. text_editor->set_draw_fold_gutter(EditorSettings::get_singleton()->get("text_editor/line_numbers/code_folding"));
  588. text_editor->set_wrap_enabled(EditorSettings::get_singleton()->get("text_editor/line_numbers/word_wrap"));
  589. text_editor->cursor_set_block_mode(EditorSettings::get_singleton()->get("text_editor/cursor/block_caret"));
  590. text_editor->set_smooth_scroll_enabled(EditorSettings::get_singleton()->get("text_editor/open_scripts/smooth_scrolling"));
  591. text_editor->set_v_scroll_speed(EditorSettings::get_singleton()->get("text_editor/open_scripts/v_scroll_speed"));
  592. }
  593. void CodeTextEditor::trim_trailing_whitespace() {
  594. bool trimed_whitespace = false;
  595. for (int i = 0; i < text_editor->get_line_count(); i++) {
  596. String line = text_editor->get_line(i);
  597. if (line.ends_with(" ") || line.ends_with("\t")) {
  598. if (!trimed_whitespace) {
  599. text_editor->begin_complex_operation();
  600. trimed_whitespace = true;
  601. }
  602. int end = 0;
  603. for (int j = line.length() - 1; j > -1; j--) {
  604. if (line[j] != ' ' && line[j] != '\t') {
  605. end = j + 1;
  606. break;
  607. }
  608. }
  609. text_editor->set_line(i, line.substr(0, end));
  610. }
  611. }
  612. if (trimed_whitespace) {
  613. text_editor->end_complex_operation();
  614. text_editor->update();
  615. }
  616. }
  617. void CodeTextEditor::convert_indent_to_spaces() {
  618. int indent_size = EditorSettings::get_singleton()->get("text_editor/indent/size");
  619. String indent = "";
  620. for (int i = 0; i < indent_size; i++) {
  621. indent += " ";
  622. }
  623. int cursor_line = text_editor->cursor_get_line();
  624. int cursor_column = text_editor->cursor_get_column();
  625. bool changed_indentation = false;
  626. for (int i = 0; i < text_editor->get_line_count(); i++) {
  627. String line = text_editor->get_line(i);
  628. if (line.length() <= 0) {
  629. continue;
  630. }
  631. int j = 0;
  632. while (j < line.length() && (line[j] == ' ' || line[j] == '\t')) {
  633. if (line[j] == '\t') {
  634. if (!changed_indentation) {
  635. text_editor->begin_complex_operation();
  636. changed_indentation = true;
  637. }
  638. if (cursor_line == i && cursor_column > j) {
  639. cursor_column += indent_size - 1;
  640. }
  641. line = line.left(j) + indent + line.right(j + 1);
  642. }
  643. j++;
  644. }
  645. if (changed_indentation) {
  646. text_editor->set_line(i, line);
  647. }
  648. }
  649. if (changed_indentation) {
  650. text_editor->cursor_set_column(cursor_column);
  651. text_editor->end_complex_operation();
  652. text_editor->update();
  653. }
  654. }
  655. void CodeTextEditor::convert_indent_to_tabs() {
  656. int indent_size = EditorSettings::get_singleton()->get("text_editor/indent/size");
  657. indent_size -= 1;
  658. int cursor_line = text_editor->cursor_get_line();
  659. int cursor_column = text_editor->cursor_get_column();
  660. bool changed_indentation = false;
  661. for (int i = 0; i < text_editor->get_line_count(); i++) {
  662. String line = text_editor->get_line(i);
  663. if (line.length() <= 0) {
  664. continue;
  665. }
  666. int j = 0;
  667. int space_count = -1;
  668. while (j < line.length() && (line[j] == ' ' || line[j] == '\t')) {
  669. if (line[j] != '\t') {
  670. space_count++;
  671. if (space_count == indent_size) {
  672. if (!changed_indentation) {
  673. text_editor->begin_complex_operation();
  674. changed_indentation = true;
  675. }
  676. if (cursor_line == i && cursor_column > j) {
  677. cursor_column -= indent_size;
  678. }
  679. line = line.left(j - indent_size) + "\t" + line.right(j + 1);
  680. j = 0;
  681. space_count = -1;
  682. }
  683. } else {
  684. space_count = -1;
  685. }
  686. j++;
  687. }
  688. if (changed_indentation) {
  689. text_editor->set_line(i, line);
  690. }
  691. }
  692. if (changed_indentation) {
  693. text_editor->cursor_set_column(cursor_column);
  694. text_editor->end_complex_operation();
  695. text_editor->update();
  696. }
  697. }
  698. void CodeTextEditor::convert_case(CaseStyle p_case) {
  699. if (!text_editor->is_selection_active()) {
  700. return;
  701. }
  702. text_editor->begin_complex_operation();
  703. int begin = text_editor->get_selection_from_line();
  704. int end = text_editor->get_selection_to_line();
  705. int begin_col = text_editor->get_selection_from_column();
  706. int end_col = text_editor->get_selection_to_column();
  707. for (int i = begin; i <= end; i++) {
  708. int len = text_editor->get_line(i).length();
  709. if (i == end)
  710. len -= len - end_col;
  711. if (i == begin)
  712. len -= begin_col;
  713. String new_line = text_editor->get_line(i).substr(i == begin ? begin_col : 0, len);
  714. switch (p_case) {
  715. case UPPER: {
  716. new_line = new_line.to_upper();
  717. } break;
  718. case LOWER: {
  719. new_line = new_line.to_lower();
  720. } break;
  721. case CAPITALIZE: {
  722. new_line = new_line.capitalize();
  723. } break;
  724. }
  725. if (i == begin) {
  726. new_line = text_editor->get_line(i).left(begin_col) + new_line;
  727. }
  728. if (i == end) {
  729. new_line = new_line + text_editor->get_line(i).right(end_col);
  730. }
  731. text_editor->set_line(i, new_line);
  732. }
  733. text_editor->end_complex_operation();
  734. }
  735. void CodeTextEditor::move_lines_up() {
  736. text_editor->begin_complex_operation();
  737. if (text_editor->is_selection_active()) {
  738. int from_line = text_editor->get_selection_from_line();
  739. int from_col = text_editor->get_selection_from_column();
  740. int to_line = text_editor->get_selection_to_line();
  741. int to_column = text_editor->get_selection_to_column();
  742. for (int i = from_line; i <= to_line; i++) {
  743. int line_id = i;
  744. int next_id = i - 1;
  745. if (line_id == 0 || next_id < 0)
  746. return;
  747. text_editor->unfold_line(line_id);
  748. text_editor->unfold_line(next_id);
  749. text_editor->swap_lines(line_id, next_id);
  750. text_editor->cursor_set_line(next_id);
  751. }
  752. int from_line_up = from_line > 0 ? from_line - 1 : from_line;
  753. int to_line_up = to_line > 0 ? to_line - 1 : to_line;
  754. text_editor->select(from_line_up, from_col, to_line_up, to_column);
  755. } else {
  756. int line_id = text_editor->cursor_get_line();
  757. int next_id = line_id - 1;
  758. if (line_id == 0 || next_id < 0)
  759. return;
  760. text_editor->unfold_line(line_id);
  761. text_editor->unfold_line(next_id);
  762. text_editor->swap_lines(line_id, next_id);
  763. text_editor->cursor_set_line(next_id);
  764. }
  765. text_editor->end_complex_operation();
  766. text_editor->update();
  767. }
  768. void CodeTextEditor::move_lines_down() {
  769. text_editor->begin_complex_operation();
  770. if (text_editor->is_selection_active()) {
  771. int from_line = text_editor->get_selection_from_line();
  772. int from_col = text_editor->get_selection_from_column();
  773. int to_line = text_editor->get_selection_to_line();
  774. int to_column = text_editor->get_selection_to_column();
  775. for (int i = to_line; i >= from_line; i--) {
  776. int line_id = i;
  777. int next_id = i + 1;
  778. if (line_id == text_editor->get_line_count() - 1 || next_id > text_editor->get_line_count())
  779. return;
  780. text_editor->unfold_line(line_id);
  781. text_editor->unfold_line(next_id);
  782. text_editor->swap_lines(line_id, next_id);
  783. text_editor->cursor_set_line(next_id);
  784. }
  785. int from_line_down = from_line < text_editor->get_line_count() ? from_line + 1 : from_line;
  786. int to_line_down = to_line < text_editor->get_line_count() ? to_line + 1 : to_line;
  787. text_editor->select(from_line_down, from_col, to_line_down, to_column);
  788. } else {
  789. int line_id = text_editor->cursor_get_line();
  790. int next_id = line_id + 1;
  791. if (line_id == text_editor->get_line_count() - 1 || next_id > text_editor->get_line_count())
  792. return;
  793. text_editor->unfold_line(line_id);
  794. text_editor->unfold_line(next_id);
  795. text_editor->swap_lines(line_id, next_id);
  796. text_editor->cursor_set_line(next_id);
  797. }
  798. text_editor->end_complex_operation();
  799. text_editor->update();
  800. }
  801. void CodeTextEditor::delete_lines() {
  802. text_editor->begin_complex_operation();
  803. if (text_editor->is_selection_active()) {
  804. int to_line = text_editor->get_selection_to_line();
  805. int from_line = text_editor->get_selection_from_line();
  806. int count = Math::abs(to_line - from_line) + 1;
  807. while (count) {
  808. text_editor->set_line(text_editor->cursor_get_line(), "");
  809. text_editor->backspace_at_cursor();
  810. count--;
  811. if (count)
  812. text_editor->unfold_line(from_line);
  813. }
  814. text_editor->cursor_set_line(from_line - 1);
  815. text_editor->deselect();
  816. } else {
  817. int line = text_editor->cursor_get_line();
  818. text_editor->set_line(text_editor->cursor_get_line(), "");
  819. text_editor->backspace_at_cursor();
  820. text_editor->unfold_line(line);
  821. text_editor->cursor_set_line(line);
  822. }
  823. text_editor->end_complex_operation();
  824. }
  825. void CodeTextEditor::code_lines_down() {
  826. int from_line = text_editor->cursor_get_line();
  827. int to_line = text_editor->cursor_get_line();
  828. int column = text_editor->cursor_get_column();
  829. if (text_editor->is_selection_active()) {
  830. from_line = text_editor->get_selection_from_line();
  831. to_line = text_editor->get_selection_to_line();
  832. column = text_editor->cursor_get_column();
  833. }
  834. int next_line = to_line + 1;
  835. if (to_line >= text_editor->get_line_count() - 1) {
  836. text_editor->set_line(to_line, text_editor->get_line(to_line) + "\n");
  837. }
  838. text_editor->begin_complex_operation();
  839. for (int i = from_line; i <= to_line; i++) {
  840. text_editor->unfold_line(i);
  841. if (i >= text_editor->get_line_count() - 1) {
  842. text_editor->set_line(i, text_editor->get_line(i) + "\n");
  843. }
  844. String line_clone = text_editor->get_line(i);
  845. text_editor->insert_at(line_clone, next_line);
  846. next_line++;
  847. }
  848. text_editor->cursor_set_column(column);
  849. if (text_editor->is_selection_active()) {
  850. text_editor->select(to_line + 1, text_editor->get_selection_from_column(), next_line - 1, text_editor->get_selection_to_column());
  851. }
  852. text_editor->end_complex_operation();
  853. text_editor->update();
  854. }
  855. void CodeTextEditor::goto_line(int p_line) {
  856. text_editor->deselect();
  857. text_editor->unfold_line(p_line);
  858. text_editor->call_deferred("cursor_set_line", p_line);
  859. }
  860. void CodeTextEditor::goto_line_selection(int p_line, int p_begin, int p_end) {
  861. text_editor->unfold_line(p_line);
  862. text_editor->call_deferred("cursor_set_line", p_line);
  863. text_editor->call_deferred("cursor_set_column", p_begin);
  864. text_editor->select(p_line, p_begin, p_line, p_end);
  865. }
  866. Variant CodeTextEditor::get_edit_state() {
  867. Dictionary state;
  868. state["scroll_position"] = text_editor->get_v_scroll();
  869. state["column"] = text_editor->cursor_get_column();
  870. state["row"] = text_editor->cursor_get_line();
  871. return state;
  872. }
  873. void CodeTextEditor::set_edit_state(const Variant &p_state) {
  874. Dictionary state = p_state;
  875. text_editor->cursor_set_column(state["column"]);
  876. text_editor->cursor_set_line(state["row"]);
  877. text_editor->set_v_scroll(state["scroll_position"]);
  878. text_editor->grab_focus();
  879. }
  880. void CodeTextEditor::set_error(const String &p_error) {
  881. error->set_text(p_error);
  882. }
  883. void CodeTextEditor::_update_font() {
  884. text_editor->add_font_override("font", get_font("source", "EditorFonts"));
  885. Ref<Font> status_bar_font = get_font("status_source", "EditorFonts");
  886. int count = status_bar->get_child_count();
  887. for (int i = 0; i < count; i++) {
  888. Control *n = Object::cast_to<Control>(status_bar->get_child(i));
  889. if (n)
  890. n->add_font_override("font", status_bar_font);
  891. }
  892. }
  893. void CodeTextEditor::_on_settings_change() {
  894. _update_font();
  895. // AUTO BRACE COMPLETION
  896. text_editor->set_auto_brace_completion(
  897. EDITOR_DEF("text_editor/completion/auto_brace_complete", true));
  898. code_complete_timer->set_wait_time(
  899. EDITOR_DEF("text_editor/completion/code_complete_delay", .3f));
  900. enable_complete_timer = EDITOR_DEF("text_editor/completion/enable_code_completion_delay", true);
  901. // call hint settings
  902. text_editor->set_callhint_settings(
  903. EDITOR_DEF("text_editor/completion/put_callhint_tooltip_below_current_line", true),
  904. EDITOR_DEF("text_editor/completion/callhint_tooltip_offset", Vector2()));
  905. }
  906. void CodeTextEditor::_text_changed_idle_timeout() {
  907. _validate_script();
  908. emit_signal("validate_script");
  909. }
  910. void CodeTextEditor::_notification(int p_what) {
  911. if (p_what == EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED) {
  912. _load_theme_settings();
  913. emit_signal("load_theme_settings");
  914. }
  915. if (p_what == NOTIFICATION_THEME_CHANGED) {
  916. _update_font();
  917. }
  918. }
  919. void CodeTextEditor::_bind_methods() {
  920. ClassDB::bind_method("_text_editor_gui_input", &CodeTextEditor::_text_editor_gui_input);
  921. ClassDB::bind_method("_line_col_changed", &CodeTextEditor::_line_col_changed);
  922. ClassDB::bind_method("_text_changed", &CodeTextEditor::_text_changed);
  923. ClassDB::bind_method("_on_settings_change", &CodeTextEditor::_on_settings_change);
  924. ClassDB::bind_method("_text_changed_idle_timeout", &CodeTextEditor::_text_changed_idle_timeout);
  925. ClassDB::bind_method("_code_complete_timer_timeout", &CodeTextEditor::_code_complete_timer_timeout);
  926. ClassDB::bind_method("_complete_request", &CodeTextEditor::_complete_request);
  927. ClassDB::bind_method("_font_resize_timeout", &CodeTextEditor::_font_resize_timeout);
  928. ADD_SIGNAL(MethodInfo("validate_script"));
  929. ADD_SIGNAL(MethodInfo("load_theme_settings"));
  930. }
  931. void CodeTextEditor::set_code_complete_func(CodeTextEditorCodeCompleteFunc p_code_complete_func, void *p_ud) {
  932. code_complete_func = p_code_complete_func;
  933. code_complete_ud = p_ud;
  934. }
  935. CodeTextEditor::CodeTextEditor() {
  936. code_complete_func = NULL;
  937. ED_SHORTCUT("script_editor/zoom_in", TTR("Zoom In"), KEY_MASK_CMD | KEY_EQUAL);
  938. ED_SHORTCUT("script_editor/zoom_out", TTR("Zoom Out"), KEY_MASK_CMD | KEY_MINUS);
  939. ED_SHORTCUT("script_editor/reset_zoom", TTR("Reset Zoom"), KEY_MASK_CMD | KEY_0);
  940. find_replace_bar = memnew(FindReplaceBar);
  941. add_child(find_replace_bar);
  942. find_replace_bar->set_h_size_flags(SIZE_EXPAND_FILL);
  943. find_replace_bar->hide();
  944. text_editor = memnew(TextEdit);
  945. add_child(text_editor);
  946. text_editor->set_v_size_flags(SIZE_EXPAND_FILL);
  947. find_replace_bar->set_text_edit(text_editor);
  948. text_editor->set_show_line_numbers(true);
  949. text_editor->set_brace_matching(true);
  950. text_editor->set_auto_indent(true);
  951. status_bar = memnew(HBoxContainer);
  952. add_child(status_bar);
  953. status_bar->set_h_size_flags(SIZE_EXPAND_FILL);
  954. idle = memnew(Timer);
  955. add_child(idle);
  956. idle->set_one_shot(true);
  957. idle->set_wait_time(EDITOR_DEF("text_editor/completion/idle_parse_delay", 2));
  958. code_complete_timer = memnew(Timer);
  959. add_child(code_complete_timer);
  960. code_complete_timer->set_one_shot(true);
  961. enable_complete_timer = EDITOR_DEF("text_editor/completion/enable_code_completion_delay", true);
  962. code_complete_timer->set_wait_time(EDITOR_DEF("text_editor/completion/code_complete_delay", .3f));
  963. error = memnew(Label);
  964. status_bar->add_child(error);
  965. error->set_autowrap(true);
  966. error->set_valign(Label::VALIGN_CENTER);
  967. error->add_color_override("font_color", EditorNode::get_singleton()->get_gui_base()->get_color("error_color", "Editor"));
  968. error->add_font_override("font", EditorNode::get_singleton()->get_gui_base()->get_font("status_source", "EditorFonts"));
  969. error->set_h_size_flags(SIZE_EXPAND_FILL); //required for it to display, given now it's clipping contents, do not touch
  970. find_replace_bar->connect("error", error, "set_text");
  971. status_bar->add_child(memnew(Label)); //to keep the height if the other labels are not visible
  972. warning_label = memnew(Label);
  973. status_bar->add_child(warning_label);
  974. warning_label->set_align(Label::ALIGN_RIGHT);
  975. warning_label->set_valign(Label::VALIGN_CENTER);
  976. warning_label->set_v_size_flags(SIZE_FILL);
  977. warning_label->set_default_cursor_shape(CURSOR_POINTING_HAND);
  978. warning_label->set_mouse_filter(MOUSE_FILTER_STOP);
  979. warning_label->set_text(TTR("Warnings:"));
  980. warning_label->add_font_override("font", EditorNode::get_singleton()->get_gui_base()->get_font("status_source", "EditorFonts"));
  981. warning_count_label = memnew(Label);
  982. status_bar->add_child(warning_count_label);
  983. warning_count_label->set_valign(Label::VALIGN_CENTER);
  984. warning_count_label->set_v_size_flags(SIZE_FILL);
  985. warning_count_label->set_autowrap(true); // workaround to prevent resizing the label on each change, do not touch
  986. warning_count_label->set_clip_text(true); // workaround to prevent resizing the label on each change, do not touch
  987. warning_count_label->set_custom_minimum_size(Size2(40, 1) * EDSCALE);
  988. warning_count_label->set_align(Label::ALIGN_RIGHT);
  989. warning_count_label->set_default_cursor_shape(CURSOR_POINTING_HAND);
  990. warning_count_label->set_mouse_filter(MOUSE_FILTER_STOP);
  991. warning_count_label->add_font_override("font", EditorNode::get_singleton()->get_gui_base()->get_font("status_source", "EditorFonts"));
  992. warning_count_label->set_text("0");
  993. Label *zoom_txt = memnew(Label);
  994. status_bar->add_child(zoom_txt);
  995. zoom_txt->set_align(Label::ALIGN_RIGHT);
  996. zoom_txt->set_valign(Label::VALIGN_CENTER);
  997. zoom_txt->set_v_size_flags(SIZE_FILL);
  998. zoom_txt->set_text(TTR("Zoom:"));
  999. zoom_txt->add_font_override("font", EditorNode::get_singleton()->get_gui_base()->get_font("status_source", "EditorFonts"));
  1000. zoom_nb = memnew(Label);
  1001. status_bar->add_child(zoom_nb);
  1002. zoom_nb->set_valign(Label::VALIGN_CENTER);
  1003. zoom_nb->set_v_size_flags(SIZE_FILL);
  1004. zoom_nb->set_autowrap(true); // workaround to prevent resizing the label on each change, do not touch
  1005. zoom_nb->set_clip_text(true); // workaround to prevent resizing the label on each change, do not touch
  1006. zoom_nb->set_custom_minimum_size(Size2(60, 1) * EDSCALE);
  1007. zoom_nb->set_align(Label::ALIGN_RIGHT);
  1008. zoom_nb->add_font_override("font", EditorNode::get_singleton()->get_gui_base()->get_font("status_source", "EditorFonts"));
  1009. Label *line_txt = memnew(Label);
  1010. status_bar->add_child(line_txt);
  1011. line_txt->set_align(Label::ALIGN_RIGHT);
  1012. line_txt->set_valign(Label::VALIGN_CENTER);
  1013. line_txt->set_v_size_flags(SIZE_FILL);
  1014. line_txt->set_text(TTR("Line:"));
  1015. line_txt->add_font_override("font", EditorNode::get_singleton()->get_gui_base()->get_font("status_source", "EditorFonts"));
  1016. line_nb = memnew(Label);
  1017. status_bar->add_child(line_nb);
  1018. line_nb->set_valign(Label::VALIGN_CENTER);
  1019. line_nb->set_v_size_flags(SIZE_FILL);
  1020. line_nb->set_autowrap(true); // workaround to prevent resizing the label on each change, do not touch
  1021. line_nb->set_clip_text(true); // workaround to prevent resizing the label on each change, do not touch
  1022. line_nb->set_custom_minimum_size(Size2(40, 1) * EDSCALE);
  1023. line_nb->set_align(Label::ALIGN_RIGHT);
  1024. line_nb->add_font_override("font", EditorNode::get_singleton()->get_gui_base()->get_font("status_source", "EditorFonts"));
  1025. Label *col_txt = memnew(Label);
  1026. status_bar->add_child(col_txt);
  1027. col_txt->set_align(Label::ALIGN_RIGHT);
  1028. col_txt->set_valign(Label::VALIGN_CENTER);
  1029. col_txt->set_v_size_flags(SIZE_FILL);
  1030. col_txt->set_text(TTR("Col:"));
  1031. col_txt->add_font_override("font", EditorNode::get_singleton()->get_gui_base()->get_font("status_source", "EditorFonts"));
  1032. col_nb = memnew(Label);
  1033. status_bar->add_child(col_nb);
  1034. col_nb->set_valign(Label::VALIGN_CENTER);
  1035. col_nb->set_v_size_flags(SIZE_FILL);
  1036. col_nb->set_autowrap(true); // workaround to prevent resizing the label on each change, do not touch
  1037. col_nb->set_clip_text(true); // workaround to prevent resizing the label on each change, do not touch
  1038. col_nb->set_custom_minimum_size(Size2(40, 1) * EDSCALE);
  1039. col_nb->set_align(Label::ALIGN_RIGHT);
  1040. col_nb->set("custom_constants/margin_right", 0);
  1041. col_nb->add_font_override("font", EditorNode::get_singleton()->get_gui_base()->get_font("status_source", "EditorFonts"));
  1042. text_editor->connect("gui_input", this, "_text_editor_gui_input");
  1043. text_editor->connect("cursor_changed", this, "_line_col_changed");
  1044. text_editor->connect("text_changed", this, "_text_changed");
  1045. text_editor->connect("request_completion", this, "_complete_request");
  1046. Vector<String> cs;
  1047. cs.push_back(".");
  1048. cs.push_back(",");
  1049. cs.push_back("(");
  1050. cs.push_back("=");
  1051. cs.push_back("$");
  1052. text_editor->set_completion(true, cs);
  1053. idle->connect("timeout", this, "_text_changed_idle_timeout");
  1054. code_complete_timer->connect("timeout", this, "_code_complete_timer_timeout");
  1055. font_resize_val = 0;
  1056. font_size = EditorSettings::get_singleton()->get("interface/editor/code_font_size");
  1057. zoom_nb->set_text(itos(100 * font_size / (14 * EDSCALE)) + "%");
  1058. font_resize_timer = memnew(Timer);
  1059. add_child(font_resize_timer);
  1060. font_resize_timer->set_one_shot(true);
  1061. font_resize_timer->set_wait_time(0.07);
  1062. font_resize_timer->connect("timeout", this, "_font_resize_timeout");
  1063. EditorSettings::get_singleton()->connect("settings_changed", this, "_on_settings_change");
  1064. }