code_editor.cpp 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950
  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. call_deferred("_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. while (search_next()) {
  166. // replace area
  167. Point2i match_from(result_line, result_col);
  168. Point2i match_to(result_line, result_col + search_text_len);
  169. if (match_from < prev_match)
  170. break; // done
  171. prev_match = Point2i(result_line, result_col + replace_text.length());
  172. text_edit->unfold_line(result_line);
  173. text_edit->select(result_line, result_col, result_line, match_to.y);
  174. if (selection_enabled && is_selection_only()) {
  175. if (match_from < selection_begin || match_to > selection_end)
  176. continue;
  177. // replace but adjust selection bounds
  178. text_edit->insert_text_at_cursor(replace_text);
  179. if (match_to.x == selection_end.x)
  180. selection_end.y += replace_text.length() - search_text_len;
  181. } else {
  182. // just replace
  183. text_edit->insert_text_at_cursor(replace_text);
  184. }
  185. rc++;
  186. }
  187. text_edit->end_complex_operation();
  188. replace_all_mode = false;
  189. // restore editor state (selection, cursor, scroll)
  190. text_edit->cursor_set_line(orig_cursor.x);
  191. text_edit->cursor_set_column(orig_cursor.y);
  192. if (selection_enabled && is_selection_only()) {
  193. // reselect
  194. text_edit->select(selection_begin.x, selection_begin.y, selection_end.x, selection_end.y);
  195. } else {
  196. text_edit->deselect();
  197. }
  198. text_edit->set_v_scroll(vsval);
  199. set_error(vformat(TTR("Replaced %d occurrence(s)."), rc));
  200. text_edit->call_deferred("connect", "text_changed", this, "_editor_text_changed");
  201. }
  202. void FindReplaceBar::_get_search_from(int &r_line, int &r_col) {
  203. r_line = text_edit->cursor_get_line();
  204. r_col = text_edit->cursor_get_column();
  205. if (text_edit->is_selection_active() && !replace_all_mode) {
  206. int selection_line = text_edit->get_selection_from_line();
  207. if (text_edit->get_selection_text() == get_search_text() && r_line == selection_line) {
  208. int selection_from_col = text_edit->get_selection_from_column();
  209. if (r_col >= selection_from_col && r_col <= text_edit->get_selection_to_column()) {
  210. r_col = selection_from_col;
  211. }
  212. }
  213. }
  214. if (r_line == result_line && r_col >= result_col && r_col <= result_col + get_search_text().length()) {
  215. r_col = result_col;
  216. }
  217. }
  218. bool FindReplaceBar::search_current() {
  219. uint32_t flags = 0;
  220. if (is_whole_words())
  221. flags |= TextEdit::SEARCH_WHOLE_WORDS;
  222. if (is_case_sensitive())
  223. flags |= TextEdit::SEARCH_MATCH_CASE;
  224. int line, col;
  225. _get_search_from(line, col);
  226. return _search(flags, line, col);
  227. }
  228. bool FindReplaceBar::search_prev() {
  229. uint32_t flags = 0;
  230. String text = get_search_text();
  231. if (is_whole_words())
  232. flags |= TextEdit::SEARCH_WHOLE_WORDS;
  233. if (is_case_sensitive())
  234. flags |= TextEdit::SEARCH_MATCH_CASE;
  235. flags |= TextEdit::SEARCH_BACKWARDS;
  236. int line, col;
  237. _get_search_from(line, col);
  238. col -= text.length();
  239. if (col < 0) {
  240. line -= 1;
  241. if (line < 0)
  242. line = text_edit->get_line_count() - 1;
  243. col = text_edit->get_line(line).length();
  244. }
  245. return _search(flags, line, col);
  246. }
  247. bool FindReplaceBar::search_next() {
  248. uint32_t flags = 0;
  249. String text = get_search_text();
  250. if (is_whole_words())
  251. flags |= TextEdit::SEARCH_WHOLE_WORDS;
  252. if (is_case_sensitive())
  253. flags |= TextEdit::SEARCH_MATCH_CASE;
  254. int line, col;
  255. _get_search_from(line, col);
  256. if (line == result_line && col == result_col) {
  257. col += text.length();
  258. if (col > text_edit->get_line(line).length()) {
  259. line += 1;
  260. if (line >= text_edit->get_line_count())
  261. line = 0;
  262. col = 0;
  263. }
  264. }
  265. return _search(flags, line, col);
  266. }
  267. void FindReplaceBar::_hide_bar() {
  268. if (replace_text->has_focus() || search_text->has_focus())
  269. text_edit->grab_focus();
  270. text_edit->set_search_text("");
  271. result_line = -1;
  272. result_col = -1;
  273. set_error("");
  274. hide();
  275. }
  276. void FindReplaceBar::_show_search() {
  277. hide(); // to update size correctly
  278. show();
  279. search_text->grab_focus();
  280. if (text_edit->is_selection_active() && !selection_only->is_pressed()) {
  281. search_text->set_text(text_edit->get_selection_text());
  282. }
  283. if (!get_search_text().empty()) {
  284. search_text->select_all();
  285. search_text->set_cursor_position(search_text->get_text().length());
  286. search_current();
  287. }
  288. call_deferred("_update_size");
  289. }
  290. void FindReplaceBar::popup_search() {
  291. replace_text->hide();
  292. hbc_button_replace->hide();
  293. hbc_option_replace->hide();
  294. _show_search();
  295. }
  296. void FindReplaceBar::popup_replace() {
  297. if (!replace_text->is_visible_in_tree()) {
  298. replace_text->clear();
  299. replace_text->show();
  300. hbc_button_replace->show();
  301. hbc_option_replace->show();
  302. }
  303. selection_only->set_pressed((text_edit->is_selection_active() && text_edit->get_selection_from_line() < text_edit->get_selection_to_line()));
  304. _show_search();
  305. }
  306. void FindReplaceBar::_search_options_changed(bool p_pressed) {
  307. search_current();
  308. }
  309. void FindReplaceBar::_editor_text_changed() {
  310. if (is_visible_in_tree()) {
  311. preserve_cursor = true;
  312. search_current();
  313. preserve_cursor = false;
  314. }
  315. }
  316. void FindReplaceBar::_search_text_changed(const String &p_text) {
  317. search_current();
  318. }
  319. void FindReplaceBar::_search_text_entered(const String &p_text) {
  320. search_next();
  321. }
  322. void FindReplaceBar::_replace_text_entered(const String &p_text) {
  323. if (selection_only->is_pressed() && text_edit->is_selection_active()) {
  324. _replace_all();
  325. _hide_bar();
  326. }
  327. }
  328. String FindReplaceBar::get_search_text() const {
  329. return search_text->get_text();
  330. }
  331. String FindReplaceBar::get_replace_text() const {
  332. return replace_text->get_text();
  333. }
  334. bool FindReplaceBar::is_case_sensitive() const {
  335. return case_sensitive->is_pressed();
  336. }
  337. bool FindReplaceBar::is_whole_words() const {
  338. return whole_words->is_pressed();
  339. }
  340. bool FindReplaceBar::is_selection_only() const {
  341. return selection_only->is_pressed();
  342. }
  343. void FindReplaceBar::set_error(const String &p_label) {
  344. emit_signal("error", p_label);
  345. }
  346. void FindReplaceBar::set_text_edit(TextEdit *p_text_edit) {
  347. text_edit = p_text_edit;
  348. text_edit->connect("text_changed", this, "_editor_text_changed");
  349. }
  350. void FindReplaceBar::_update_size() {
  351. container->set_custom_minimum_size(Size2(0, hbc->get_size().height));
  352. }
  353. void FindReplaceBar::_bind_methods() {
  354. ClassDB::bind_method("_unhandled_input", &FindReplaceBar::_unhandled_input);
  355. ClassDB::bind_method("_editor_text_changed", &FindReplaceBar::_editor_text_changed);
  356. ClassDB::bind_method("_search_text_changed", &FindReplaceBar::_search_text_changed);
  357. ClassDB::bind_method("_search_text_entered", &FindReplaceBar::_search_text_entered);
  358. ClassDB::bind_method("_replace_text_entered", &FindReplaceBar::_replace_text_entered);
  359. ClassDB::bind_method("_search_current", &FindReplaceBar::search_current);
  360. ClassDB::bind_method("_search_next", &FindReplaceBar::search_next);
  361. ClassDB::bind_method("_search_prev", &FindReplaceBar::search_prev);
  362. ClassDB::bind_method("_replace_pressed", &FindReplaceBar::_replace);
  363. ClassDB::bind_method("_replace_all_pressed", &FindReplaceBar::_replace_all);
  364. ClassDB::bind_method("_search_options_changed", &FindReplaceBar::_search_options_changed);
  365. ClassDB::bind_method("_hide_pressed", &FindReplaceBar::_hide_bar);
  366. ClassDB::bind_method("_update_size", &FindReplaceBar::_update_size);
  367. ADD_SIGNAL(MethodInfo("search"));
  368. ADD_SIGNAL(MethodInfo("error"));
  369. }
  370. FindReplaceBar::FindReplaceBar() {
  371. container = memnew(Control);
  372. add_child(container);
  373. container->set_clip_contents(true);
  374. container->set_h_size_flags(SIZE_EXPAND_FILL);
  375. replace_all_mode = false;
  376. preserve_cursor = false;
  377. hbc = memnew(HBoxContainer);
  378. container->add_child(hbc);
  379. hbc->set_anchor_and_margin(MARGIN_RIGHT, 1, 0);
  380. vbc_lineedit = memnew(VBoxContainer);
  381. hbc->add_child(vbc_lineedit);
  382. vbc_lineedit->set_h_size_flags(SIZE_EXPAND_FILL);
  383. VBoxContainer *vbc_button = memnew(VBoxContainer);
  384. hbc->add_child(vbc_button);
  385. VBoxContainer *vbc_option = memnew(VBoxContainer);
  386. hbc->add_child(vbc_option);
  387. HBoxContainer *hbc_button_search = memnew(HBoxContainer);
  388. vbc_button->add_child(hbc_button_search);
  389. hbc_button_replace = memnew(HBoxContainer);
  390. vbc_button->add_child(hbc_button_replace);
  391. HBoxContainer *hbc_option_search = memnew(HBoxContainer);
  392. vbc_option->add_child(hbc_option_search);
  393. hbc_option_replace = memnew(HBoxContainer);
  394. vbc_option->add_child(hbc_option_replace);
  395. // search toolbar
  396. search_text = memnew(LineEdit);
  397. vbc_lineedit->add_child(search_text);
  398. search_text->set_custom_minimum_size(Size2(100 * EDSCALE, 0));
  399. search_text->connect("text_changed", this, "_search_text_changed");
  400. search_text->connect("text_entered", this, "_search_text_entered");
  401. find_prev = memnew(ToolButton);
  402. hbc_button_search->add_child(find_prev);
  403. find_prev->set_focus_mode(FOCUS_NONE);
  404. find_prev->connect("pressed", this, "_search_prev");
  405. find_next = memnew(ToolButton);
  406. hbc_button_search->add_child(find_next);
  407. find_next->set_focus_mode(FOCUS_NONE);
  408. find_next->connect("pressed", this, "_search_next");
  409. case_sensitive = memnew(CheckBox);
  410. hbc_option_search->add_child(case_sensitive);
  411. case_sensitive->set_text(TTR("Match Case"));
  412. case_sensitive->set_focus_mode(FOCUS_NONE);
  413. case_sensitive->connect("toggled", this, "_search_options_changed");
  414. whole_words = memnew(CheckBox);
  415. hbc_option_search->add_child(whole_words);
  416. whole_words->set_text(TTR("Whole Words"));
  417. whole_words->set_focus_mode(FOCUS_NONE);
  418. whole_words->connect("toggled", this, "_search_options_changed");
  419. // replace toolbar
  420. replace_text = memnew(LineEdit);
  421. vbc_lineedit->add_child(replace_text);
  422. replace_text->set_custom_minimum_size(Size2(100 * EDSCALE, 0));
  423. replace_text->connect("text_entered", this, "_replace_text_entered");
  424. replace = memnew(Button);
  425. hbc_button_replace->add_child(replace);
  426. replace->set_text(TTR("Replace"));
  427. replace->connect("pressed", this, "_replace_pressed");
  428. replace_all = memnew(Button);
  429. hbc_button_replace->add_child(replace_all);
  430. replace_all->set_text(TTR("Replace All"));
  431. replace_all->connect("pressed", this, "_replace_all_pressed");
  432. selection_only = memnew(CheckBox);
  433. hbc_option_replace->add_child(selection_only);
  434. selection_only->set_text(TTR("Selection Only"));
  435. selection_only->set_focus_mode(FOCUS_NONE);
  436. selection_only->connect("toggled", this, "_search_options_changed");
  437. hide_button = memnew(TextureButton);
  438. add_child(hide_button);
  439. hide_button->set_focus_mode(FOCUS_NONE);
  440. hide_button->connect("pressed", this, "_hide_pressed");
  441. hide_button->set_expand(true);
  442. hide_button->set_stretch_mode(TextureButton::STRETCH_KEEP_CENTERED);
  443. }
  444. /*** CODE EDITOR ****/
  445. void CodeTextEditor::_text_editor_gui_input(const Ref<InputEvent> &p_event) {
  446. Ref<InputEventMouseButton> mb = p_event;
  447. if (mb.is_valid()) {
  448. if (mb->is_pressed() && mb->get_command()) {
  449. if (mb->get_button_index() == BUTTON_WHEEL_UP) {
  450. _zoom_in();
  451. } else if (mb->get_button_index() == BUTTON_WHEEL_DOWN) {
  452. _zoom_out();
  453. }
  454. }
  455. }
  456. Ref<InputEventMagnifyGesture> magnify_gesture = p_event;
  457. if (magnify_gesture.is_valid()) {
  458. Ref<DynamicFont> font = text_editor->get_font("font");
  459. if (font.is_valid()) {
  460. if (font->get_size() != (int)font_size) {
  461. font_size = font->get_size();
  462. }
  463. font_size *= powf(magnify_gesture->get_factor(), 0.25);
  464. _add_font_size((int)font_size - font->get_size());
  465. }
  466. return;
  467. }
  468. Ref<InputEventKey> k = p_event;
  469. if (k.is_valid()) {
  470. if (k->is_pressed()) {
  471. if (ED_IS_SHORTCUT("script_editor/zoom_in", p_event)) {
  472. _zoom_in();
  473. }
  474. if (ED_IS_SHORTCUT("script_editor/zoom_out", p_event)) {
  475. _zoom_out();
  476. }
  477. if (ED_IS_SHORTCUT("script_editor/reset_zoom", p_event)) {
  478. _reset_zoom();
  479. }
  480. }
  481. }
  482. }
  483. void CodeTextEditor::_zoom_in() {
  484. font_resize_val += EDSCALE;
  485. _zoom_changed();
  486. }
  487. void CodeTextEditor::_zoom_out() {
  488. font_resize_val -= EDSCALE;
  489. _zoom_changed();
  490. }
  491. void CodeTextEditor::_zoom_changed() {
  492. if (font_resize_timer->get_time_left() == 0)
  493. font_resize_timer->start();
  494. }
  495. void CodeTextEditor::_reset_zoom() {
  496. Ref<DynamicFont> font = text_editor->get_font("font"); // reset source font size to default
  497. if (font.is_valid()) {
  498. EditorSettings::get_singleton()->set("interface/editor/code_font_size", 14);
  499. font->set_size(14);
  500. }
  501. }
  502. void CodeTextEditor::_line_col_changed() {
  503. line_nb->set_text(itos(text_editor->cursor_get_line() + 1));
  504. col_nb->set_text(itos(text_editor->cursor_get_column() + 1));
  505. }
  506. void CodeTextEditor::_text_changed() {
  507. if (text_editor->is_insert_text_operation()) {
  508. code_complete_timer->start();
  509. }
  510. idle->start();
  511. }
  512. void CodeTextEditor::_code_complete_timer_timeout() {
  513. if (!is_visible_in_tree())
  514. return;
  515. if (enable_complete_timer)
  516. text_editor->query_code_comple();
  517. }
  518. void CodeTextEditor::_complete_request() {
  519. List<String> entries;
  520. String ctext = text_editor->get_text_for_completion();
  521. _code_complete_script(ctext, &entries);
  522. bool forced = false;
  523. if (code_complete_func) {
  524. code_complete_func(code_complete_ud, ctext, &entries, forced);
  525. }
  526. // print_line("COMPLETE: "+p_request);
  527. if (entries.size() == 0)
  528. return;
  529. Vector<String> strs;
  530. strs.resize(entries.size());
  531. int i = 0;
  532. for (List<String>::Element *E = entries.front(); E; E = E->next()) {
  533. strs[i++] = E->get();
  534. }
  535. text_editor->code_complete(strs, forced);
  536. }
  537. void CodeTextEditor::_font_resize_timeout() {
  538. if (_add_font_size(font_resize_val)) {
  539. font_resize_val = 0;
  540. }
  541. }
  542. bool CodeTextEditor::_add_font_size(int p_delta) {
  543. Ref<DynamicFont> font = text_editor->get_font("font");
  544. if (font.is_valid()) {
  545. int new_size = CLAMP(font->get_size() + p_delta, 8 * EDSCALE, 96 * EDSCALE);
  546. if (new_size != font->get_size()) {
  547. EditorSettings::get_singleton()->set("interface/editor/code_font_size", new_size / EDSCALE);
  548. font->set_size(new_size);
  549. }
  550. return true;
  551. } else {
  552. return false;
  553. }
  554. }
  555. void CodeTextEditor::update_editor_settings() {
  556. text_editor->set_auto_brace_completion(EditorSettings::get_singleton()->get("text_editor/completion/auto_brace_complete"));
  557. text_editor->set_scroll_pass_end_of_file(EditorSettings::get_singleton()->get("text_editor/cursor/scroll_past_end_of_file"));
  558. text_editor->set_indent_using_spaces(EditorSettings::get_singleton()->get("text_editor/indent/type"));
  559. text_editor->set_indent_size(EditorSettings::get_singleton()->get("text_editor/indent/size"));
  560. text_editor->set_auto_indent(EditorSettings::get_singleton()->get("text_editor/indent/auto_indent"));
  561. text_editor->set_draw_tabs(EditorSettings::get_singleton()->get("text_editor/indent/draw_tabs"));
  562. text_editor->set_show_line_numbers(EditorSettings::get_singleton()->get("text_editor/line_numbers/show_line_numbers"));
  563. text_editor->set_line_numbers_zero_padded(EditorSettings::get_singleton()->get("text_editor/line_numbers/line_numbers_zero_padded"));
  564. text_editor->set_show_line_length_guideline(EditorSettings::get_singleton()->get("text_editor/line_numbers/show_line_length_guideline"));
  565. text_editor->set_line_length_guideline_column(EditorSettings::get_singleton()->get("text_editor/line_numbers/line_length_guideline_column"));
  566. text_editor->set_syntax_coloring(EditorSettings::get_singleton()->get("text_editor/highlighting/syntax_highlighting"));
  567. text_editor->set_highlight_all_occurrences(EditorSettings::get_singleton()->get("text_editor/highlighting/highlight_all_occurrences"));
  568. text_editor->set_highlight_current_line(EditorSettings::get_singleton()->get("text_editor/highlighting/highlight_current_line"));
  569. text_editor->cursor_set_blink_enabled(EditorSettings::get_singleton()->get("text_editor/cursor/caret_blink"));
  570. text_editor->cursor_set_blink_speed(EditorSettings::get_singleton()->get("text_editor/cursor/caret_blink_speed"));
  571. text_editor->set_draw_breakpoint_gutter(EditorSettings::get_singleton()->get("text_editor/line_numbers/show_breakpoint_gutter"));
  572. text_editor->set_hiding_enabled(EditorSettings::get_singleton()->get("text_editor/line_numbers/code_folding"));
  573. text_editor->set_draw_fold_gutter(EditorSettings::get_singleton()->get("text_editor/line_numbers/code_folding"));
  574. text_editor->cursor_set_block_mode(EditorSettings::get_singleton()->get("text_editor/cursor/block_caret"));
  575. text_editor->set_smooth_scroll_enabled(EditorSettings::get_singleton()->get("text_editor/open_scripts/smooth_scrolling"));
  576. text_editor->set_v_scroll_speed(EditorSettings::get_singleton()->get("text_editor/open_scripts/v_scroll_speed"));
  577. }
  578. void CodeTextEditor::set_error(const String &p_error) {
  579. error->set_text(p_error);
  580. }
  581. void CodeTextEditor::_update_font() {
  582. text_editor->add_font_override("font", get_font("source", "EditorFonts"));
  583. Ref<Font> status_bar_font = get_font("status_source", "EditorFonts");
  584. int count = status_bar->get_child_count();
  585. for (int i = 0; i < count; i++) {
  586. Control *n = Object::cast_to<Control>(status_bar->get_child(i));
  587. if (n)
  588. n->add_font_override("font", status_bar_font);
  589. }
  590. }
  591. void CodeTextEditor::_on_settings_change() {
  592. _update_font();
  593. // AUTO BRACE COMPLETION
  594. text_editor->set_auto_brace_completion(
  595. EDITOR_DEF("text_editor/completion/auto_brace_complete", true));
  596. code_complete_timer->set_wait_time(
  597. EDITOR_DEF("text_editor/completion/code_complete_delay", .3f));
  598. enable_complete_timer = EDITOR_DEF("text_editor/completion/enable_code_completion_delay", true);
  599. // call hint settings
  600. text_editor->set_callhint_settings(
  601. EDITOR_DEF("text_editor/completion/put_callhint_tooltip_below_current_line", true),
  602. EDITOR_DEF("text_editor/completion/callhint_tooltip_offset", Vector2()));
  603. }
  604. void CodeTextEditor::_text_changed_idle_timeout() {
  605. _validate_script();
  606. emit_signal("validate_script");
  607. }
  608. void CodeTextEditor::_notification(int p_what) {
  609. if (p_what == EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED) {
  610. _load_theme_settings();
  611. emit_signal("load_theme_settings");
  612. }
  613. if (p_what == NOTIFICATION_THEME_CHANGED) {
  614. _update_font();
  615. }
  616. }
  617. void CodeTextEditor::_bind_methods() {
  618. ClassDB::bind_method("_text_editor_gui_input", &CodeTextEditor::_text_editor_gui_input);
  619. ClassDB::bind_method("_line_col_changed", &CodeTextEditor::_line_col_changed);
  620. ClassDB::bind_method("_text_changed", &CodeTextEditor::_text_changed);
  621. ClassDB::bind_method("_on_settings_change", &CodeTextEditor::_on_settings_change);
  622. ClassDB::bind_method("_text_changed_idle_timeout", &CodeTextEditor::_text_changed_idle_timeout);
  623. ClassDB::bind_method("_code_complete_timer_timeout", &CodeTextEditor::_code_complete_timer_timeout);
  624. ClassDB::bind_method("_complete_request", &CodeTextEditor::_complete_request);
  625. ClassDB::bind_method("_font_resize_timeout", &CodeTextEditor::_font_resize_timeout);
  626. ADD_SIGNAL(MethodInfo("validate_script"));
  627. ADD_SIGNAL(MethodInfo("load_theme_settings"));
  628. }
  629. void CodeTextEditor::set_code_complete_func(CodeTextEditorCodeCompleteFunc p_code_complete_func, void *p_ud) {
  630. code_complete_func = p_code_complete_func;
  631. code_complete_ud = p_ud;
  632. }
  633. CodeTextEditor::CodeTextEditor() {
  634. code_complete_func = NULL;
  635. ED_SHORTCUT("script_editor/zoom_in", TTR("Zoom In"), KEY_MASK_CMD | KEY_EQUAL);
  636. ED_SHORTCUT("script_editor/zoom_out", TTR("Zoom Out"), KEY_MASK_CMD | KEY_MINUS);
  637. ED_SHORTCUT("script_editor/reset_zoom", TTR("Reset Zoom"), KEY_MASK_CMD | KEY_0);
  638. find_replace_bar = memnew(FindReplaceBar);
  639. add_child(find_replace_bar);
  640. find_replace_bar->set_h_size_flags(SIZE_EXPAND_FILL);
  641. find_replace_bar->hide();
  642. text_editor = memnew(TextEdit);
  643. add_child(text_editor);
  644. text_editor->set_v_size_flags(SIZE_EXPAND_FILL);
  645. find_replace_bar->set_text_edit(text_editor);
  646. text_editor->set_show_line_numbers(true);
  647. text_editor->set_brace_matching(true);
  648. text_editor->set_auto_indent(true);
  649. status_bar = memnew(HBoxContainer);
  650. add_child(status_bar);
  651. status_bar->set_h_size_flags(SIZE_EXPAND_FILL);
  652. idle = memnew(Timer);
  653. add_child(idle);
  654. idle->set_one_shot(true);
  655. idle->set_wait_time(EDITOR_DEF("text_editor/completion/idle_parse_delay", 2));
  656. code_complete_timer = memnew(Timer);
  657. add_child(code_complete_timer);
  658. code_complete_timer->set_one_shot(true);
  659. enable_complete_timer = EDITOR_DEF("text_editor/completion/enable_code_completion_delay", true);
  660. code_complete_timer->set_wait_time(EDITOR_DEF("text_editor/completion/code_complete_delay", .3f));
  661. error = memnew(Label);
  662. status_bar->add_child(error);
  663. error->set_autowrap(true);
  664. error->set_valign(Label::VALIGN_CENTER);
  665. error->add_color_override("font_color", EditorNode::get_singleton()->get_gui_base()->get_color("error_color", "Editor"));
  666. error->add_font_override("font", EditorNode::get_singleton()->get_gui_base()->get_font("status_source", "EditorFonts"));
  667. error->set_h_size_flags(SIZE_EXPAND_FILL); //required for it to display, given now it's clipping contents, do not touch
  668. find_replace_bar->connect("error", error, "set_text");
  669. status_bar->add_child(memnew(Label)); //to keep the height if the other labels are not visible
  670. Label *line_txt = memnew(Label);
  671. status_bar->add_child(line_txt);
  672. line_txt->set_align(Label::ALIGN_RIGHT);
  673. line_txt->set_valign(Label::VALIGN_CENTER);
  674. line_txt->set_v_size_flags(SIZE_FILL);
  675. line_txt->set_text(TTR("Line:"));
  676. line_txt->add_font_override("font", EditorNode::get_singleton()->get_gui_base()->get_font("status_source", "EditorFonts"));
  677. line_nb = memnew(Label);
  678. status_bar->add_child(line_nb);
  679. line_nb->set_valign(Label::VALIGN_CENTER);
  680. line_nb->set_v_size_flags(SIZE_FILL);
  681. line_nb->set_autowrap(true); // workaround to prevent resizing the label on each change, do not touch
  682. line_nb->set_clip_text(true); // workaround to prevent resizing the label on each change, do not touch
  683. line_nb->set_custom_minimum_size(Size2(40, 1) * EDSCALE);
  684. line_nb->set_align(Label::ALIGN_RIGHT);
  685. line_nb->add_font_override("font", EditorNode::get_singleton()->get_gui_base()->get_font("status_source", "EditorFonts"));
  686. Label *col_txt = memnew(Label);
  687. status_bar->add_child(col_txt);
  688. col_txt->set_align(Label::ALIGN_RIGHT);
  689. col_txt->set_valign(Label::VALIGN_CENTER);
  690. col_txt->set_v_size_flags(SIZE_FILL);
  691. col_txt->set_text(TTR("Col:"));
  692. col_txt->add_font_override("font", EditorNode::get_singleton()->get_gui_base()->get_font("status_source", "EditorFonts"));
  693. col_nb = memnew(Label);
  694. status_bar->add_child(col_nb);
  695. col_nb->set_valign(Label::VALIGN_CENTER);
  696. col_nb->set_v_size_flags(SIZE_FILL);
  697. col_nb->set_autowrap(true); // workaround to prevent resizing the label on each change, do not touch
  698. col_nb->set_clip_text(true); // workaround to prevent resizing the label on each change, do not touch
  699. col_nb->set_custom_minimum_size(Size2(40, 1) * EDSCALE);
  700. col_nb->set_align(Label::ALIGN_RIGHT);
  701. col_nb->set("custom_constants/margin_right", 0);
  702. col_nb->add_font_override("font", EditorNode::get_singleton()->get_gui_base()->get_font("status_source", "EditorFonts"));
  703. text_editor->connect("gui_input", this, "_text_editor_gui_input");
  704. text_editor->connect("cursor_changed", this, "_line_col_changed");
  705. text_editor->connect("text_changed", this, "_text_changed");
  706. text_editor->connect("request_completion", this, "_complete_request");
  707. Vector<String> cs;
  708. cs.push_back(".");
  709. cs.push_back(",");
  710. cs.push_back("(");
  711. cs.push_back("=");
  712. cs.push_back("$");
  713. text_editor->set_completion(true, cs);
  714. idle->connect("timeout", this, "_text_changed_idle_timeout");
  715. code_complete_timer->connect("timeout", this, "_code_complete_timer_timeout");
  716. font_resize_val = 0;
  717. font_size = -1;
  718. font_resize_timer = memnew(Timer);
  719. add_child(font_resize_timer);
  720. font_resize_timer->set_one_shot(true);
  721. font_resize_timer->set_wait_time(0.07);
  722. font_resize_timer->connect("timeout", this, "_font_resize_timeout");
  723. EditorSettings::get_singleton()->connect("settings_changed", this, "_on_settings_change");
  724. }