code_editor.cpp 42 KB

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