label.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739
  1. /**************************************************************************/
  2. /* label.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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 "label.h"
  31. #include "core/print_string.h"
  32. #include "core/project_settings.h"
  33. #include "core/translation.h"
  34. void Label::set_autowrap(bool p_autowrap) {
  35. if (autowrap == p_autowrap) {
  36. return;
  37. }
  38. autowrap = p_autowrap;
  39. word_cache_dirty = true;
  40. update();
  41. if (clip) {
  42. minimum_size_changed();
  43. }
  44. }
  45. bool Label::has_autowrap() const {
  46. return autowrap;
  47. }
  48. void Label::set_uppercase(bool p_uppercase) {
  49. uppercase = p_uppercase;
  50. word_cache_dirty = true;
  51. update();
  52. }
  53. bool Label::is_uppercase() const {
  54. return uppercase;
  55. }
  56. int Label::get_line_height() const {
  57. return get_font("font")->get_height();
  58. }
  59. void Label::_notification(int p_what) {
  60. if (p_what == NOTIFICATION_TRANSLATION_CHANGED) {
  61. String new_text = tr(text);
  62. if (new_text == xl_text) {
  63. return; //nothing new
  64. }
  65. xl_text = new_text;
  66. regenerate_word_cache();
  67. update();
  68. }
  69. if (p_what == NOTIFICATION_DRAW) {
  70. if (clip) {
  71. VisualServer::get_singleton()->canvas_item_set_clip(get_canvas_item(), true);
  72. }
  73. if (word_cache_dirty) {
  74. regenerate_word_cache();
  75. }
  76. RID ci = get_canvas_item();
  77. Size2 string_size;
  78. Size2 size = get_size();
  79. Ref<StyleBox> style = get_stylebox("normal");
  80. Ref<Font> font = get_font("font");
  81. Color font_color = get_color("font_color");
  82. Color font_color_shadow = get_color("font_color_shadow");
  83. bool use_outline = get_constant("shadow_as_outline");
  84. Point2 shadow_ofs(get_constant("shadow_offset_x"), get_constant("shadow_offset_y"));
  85. int line_spacing = get_constant("line_spacing");
  86. Color font_outline_modulate = get_color("font_outline_modulate");
  87. style->draw(ci, Rect2(Point2(0, 0), get_size()));
  88. VisualServer::get_singleton()->canvas_item_set_distance_field_mode(get_canvas_item(), font.is_valid() && font->is_distance_field_hint());
  89. int font_h = font->get_height() + line_spacing;
  90. int lines_visible = (size.y + line_spacing) / font_h;
  91. real_t space_w = font->get_char_size(' ').width;
  92. int chars_total = 0;
  93. int vbegin = 0, vsep = 0;
  94. if (lines_visible > line_count) {
  95. lines_visible = line_count;
  96. }
  97. if (max_lines_visible >= 0 && lines_visible > max_lines_visible) {
  98. lines_visible = max_lines_visible;
  99. }
  100. float total_h = 0;
  101. total_h += lines_visible * font_h;
  102. total_h += style->get_margin(MARGIN_TOP) + style->get_margin(MARGIN_BOTTOM);
  103. if (lines_visible > 0) {
  104. switch (valign) {
  105. case VALIGN_TOP: {
  106. //nothing
  107. } break;
  108. case VALIGN_CENTER: {
  109. vbegin = (size.y - (total_h - line_spacing)) / 2;
  110. vsep = 0;
  111. } break;
  112. case VALIGN_BOTTOM: {
  113. vbegin = size.y - (total_h - line_spacing);
  114. vsep = 0;
  115. } break;
  116. case VALIGN_FILL: {
  117. vbegin = 0;
  118. if (lines_visible > 1) {
  119. vsep = (size.y - (total_h - line_spacing)) / (lines_visible - 1);
  120. } else {
  121. vsep = 0;
  122. }
  123. } break;
  124. }
  125. }
  126. WordCache *wc = word_cache;
  127. if (!wc) {
  128. return;
  129. }
  130. int line = 0;
  131. int line_to = lines_skipped + (lines_visible > 0 ? lines_visible : 1);
  132. FontDrawer drawer(font, font_outline_modulate);
  133. while (wc) {
  134. /* handle lines not meant to be drawn quickly */
  135. if (line >= line_to) {
  136. break;
  137. }
  138. if (line < lines_skipped) {
  139. while (wc && wc->char_pos >= 0) {
  140. wc = wc->next;
  141. }
  142. if (wc) {
  143. wc = wc->next;
  144. }
  145. line++;
  146. continue;
  147. }
  148. /* handle lines normally */
  149. if (wc->char_pos < 0) {
  150. //empty line
  151. wc = wc->next;
  152. line++;
  153. continue;
  154. }
  155. WordCache *from = wc;
  156. WordCache *to = wc;
  157. int taken = 0;
  158. int spaces = 0;
  159. while (to && to->char_pos >= 0) {
  160. taken += to->pixel_width;
  161. if (to->space_count) {
  162. spaces += to->space_count;
  163. }
  164. to = to->next;
  165. }
  166. bool can_fill = to && (to->char_pos == WordCache::CHAR_WRAPLINE || to->char_pos == WordCache::CHAR_NEWLINE);
  167. float x_ofs = 0;
  168. switch (align) {
  169. case ALIGN_FILL:
  170. case ALIGN_LEFT: {
  171. x_ofs = style->get_offset().x;
  172. } break;
  173. case ALIGN_CENTER: {
  174. x_ofs = int(size.width - (taken + spaces * space_w)) / 2;
  175. } break;
  176. case ALIGN_RIGHT: {
  177. x_ofs = int(size.width - style->get_margin(MARGIN_RIGHT) - (taken + spaces * space_w));
  178. } break;
  179. }
  180. float y_ofs = style->get_offset().y;
  181. y_ofs += (line - lines_skipped) * font_h + font->get_ascent();
  182. y_ofs += vbegin + line * vsep;
  183. while (from != to) {
  184. // draw a word
  185. int pos = from->char_pos;
  186. if (from->char_pos < 0) {
  187. ERR_PRINT("BUG");
  188. return;
  189. }
  190. if (from->space_count) {
  191. /* spacing */
  192. x_ofs += space_w * from->space_count;
  193. if (can_fill && align == ALIGN_FILL && spaces) {
  194. x_ofs += int((size.width - (taken + space_w * spaces)) / spaces);
  195. }
  196. }
  197. if (font_color_shadow.a > 0) {
  198. int chars_total_shadow = chars_total; //save chars drawn
  199. float x_ofs_shadow = x_ofs;
  200. for (int i = 0; i < from->word_len; i++) {
  201. if (visible_chars < 0 || chars_total_shadow < visible_chars) {
  202. CharType c = xl_text[i + pos];
  203. CharType n = xl_text[i + pos + 1];
  204. if (uppercase) {
  205. c = String::char_uppercase(c);
  206. n = String::char_uppercase(n);
  207. }
  208. float move = drawer.draw_char(ci, Point2(x_ofs_shadow, y_ofs) + shadow_ofs, c, n, font_color_shadow);
  209. if (use_outline) {
  210. drawer.draw_char(ci, Point2(x_ofs_shadow, y_ofs) + Vector2(-shadow_ofs.x, shadow_ofs.y), c, n, font_color_shadow);
  211. drawer.draw_char(ci, Point2(x_ofs_shadow, y_ofs) + Vector2(shadow_ofs.x, -shadow_ofs.y), c, n, font_color_shadow);
  212. drawer.draw_char(ci, Point2(x_ofs_shadow, y_ofs) + Vector2(-shadow_ofs.x, -shadow_ofs.y), c, n, font_color_shadow);
  213. }
  214. x_ofs_shadow += move;
  215. chars_total_shadow++;
  216. }
  217. }
  218. }
  219. for (int i = 0; i < from->word_len; i++) {
  220. if (visible_chars < 0 || chars_total < visible_chars) {
  221. CharType c = xl_text[i + pos];
  222. CharType n = xl_text[i + pos + 1];
  223. if (uppercase) {
  224. c = String::char_uppercase(c);
  225. n = String::char_uppercase(n);
  226. }
  227. x_ofs += drawer.draw_char(ci, Point2(x_ofs, y_ofs), c, n, font_color);
  228. chars_total++;
  229. }
  230. }
  231. from = from->next;
  232. }
  233. wc = to ? to->next : nullptr;
  234. line++;
  235. }
  236. }
  237. if (p_what == NOTIFICATION_THEME_CHANGED) {
  238. word_cache_dirty = true;
  239. update();
  240. }
  241. if (p_what == NOTIFICATION_RESIZED) {
  242. word_cache_dirty = true;
  243. }
  244. }
  245. Size2 Label::get_minimum_size() const {
  246. Size2 min_style = get_stylebox("normal")->get_minimum_size();
  247. // don't want to mutable everything
  248. if (word_cache_dirty) {
  249. const_cast<Label *>(this)->regenerate_word_cache();
  250. }
  251. if (autowrap) {
  252. return Size2(1, clip ? 1 : minsize.height) + min_style;
  253. } else {
  254. Size2 ms = minsize;
  255. if (clip) {
  256. ms.width = 1;
  257. }
  258. return ms + min_style;
  259. }
  260. }
  261. int Label::get_longest_line_width() const {
  262. Ref<Font> font = get_font("font");
  263. real_t max_line_width = 0;
  264. real_t line_width = 0;
  265. for (int i = 0; i < xl_text.size(); i++) {
  266. CharType current = xl_text[i];
  267. if (uppercase) {
  268. current = String::char_uppercase(current);
  269. }
  270. if (current < 32) {
  271. if (current == '\n') {
  272. if (line_width > max_line_width) {
  273. max_line_width = line_width;
  274. }
  275. line_width = 0;
  276. }
  277. } else {
  278. real_t char_width = font->get_char_size(current, xl_text[i + 1]).width;
  279. line_width += char_width;
  280. }
  281. }
  282. if (line_width > max_line_width) {
  283. max_line_width = line_width;
  284. }
  285. // ceiling to ensure autowrapping does not cut text
  286. return Math::ceil(max_line_width);
  287. }
  288. int Label::get_line_count() const {
  289. if (!is_inside_tree()) {
  290. return 1;
  291. }
  292. if (word_cache_dirty) {
  293. const_cast<Label *>(this)->regenerate_word_cache();
  294. }
  295. return line_count;
  296. }
  297. int Label::get_visible_line_count() const {
  298. int line_spacing = get_constant("line_spacing");
  299. int font_h = get_font("font")->get_height() + line_spacing;
  300. int lines_visible = (get_size().height - get_stylebox("normal")->get_minimum_size().height + line_spacing) / font_h;
  301. if (lines_visible > line_count) {
  302. lines_visible = line_count;
  303. }
  304. if (max_lines_visible >= 0 && lines_visible > max_lines_visible) {
  305. lines_visible = max_lines_visible;
  306. }
  307. return lines_visible;
  308. }
  309. void Label::regenerate_word_cache() {
  310. while (word_cache) {
  311. WordCache *current = word_cache;
  312. word_cache = current->next;
  313. memdelete(current);
  314. }
  315. int width;
  316. if (autowrap) {
  317. Ref<StyleBox> style = get_stylebox("normal");
  318. width = MAX(get_size().width, get_custom_minimum_size().width) - style->get_minimum_size().width;
  319. } else {
  320. width = get_longest_line_width();
  321. }
  322. Ref<Font> font = get_font("font");
  323. real_t current_word_size = 0;
  324. int word_pos = 0;
  325. real_t line_width = 0;
  326. int space_count = 0;
  327. real_t space_width = font->get_char_size(' ').width;
  328. int line_spacing = get_constant("line_spacing");
  329. line_count = 1;
  330. total_char_cache = 0;
  331. bool was_separatable = false;
  332. WordCache *last = nullptr;
  333. for (int i = 0; i <= xl_text.length(); i++) {
  334. CharType current = i < xl_text.length() ? xl_text[i] : L' '; //always a space at the end, so the algo works
  335. if (uppercase) {
  336. current = String::char_uppercase(current);
  337. }
  338. // ranges taken from https://en.wikipedia.org/wiki/Plane_(Unicode)
  339. // if your language is not well supported, consider helping improve
  340. // the unicode support in Godot.
  341. bool separatable = (current >= 0x2E08 && current <= 0x9FFF) || // CJK scripts and symbols.
  342. (current >= 0xAC00 && current <= 0xD7FF) || // Hangul Syllables and Hangul Jamo Extended-B.
  343. (current >= 0xF900 && current <= 0xFAFF) || // CJK Compatibility Ideographs.
  344. (current >= 0xFE30 && current <= 0xFE4F) || // CJK Compatibility Forms.
  345. (current >= 0xFF65 && current <= 0xFF9F) || // Halfwidth forms of katakana
  346. (current >= 0xFFA0 && current <= 0xFFDC) || // Halfwidth forms of compatibility jamo characters for Hangul
  347. (current >= 0x20000 && current <= 0x2FA1F) || // CJK Unified Ideographs Extension B ~ F and CJK Compatibility Ideographs Supplement.
  348. (current >= 0x30000 && current <= 0x3134F); // CJK Unified Ideographs Extension G.
  349. bool insert_newline = false;
  350. real_t char_width = 0;
  351. bool separation_changed = i > 0 && was_separatable != separatable;
  352. was_separatable = separatable;
  353. if (current < 33) { // Control characters and space.
  354. if (current_word_size > 0) { // These characters always create a word-break.
  355. WordCache *wc = memnew(WordCache);
  356. if (word_cache) {
  357. last->next = wc;
  358. } else {
  359. word_cache = wc;
  360. }
  361. last = wc;
  362. wc->pixel_width = current_word_size;
  363. wc->char_pos = word_pos;
  364. wc->word_len = i - word_pos;
  365. wc->space_count = space_count;
  366. current_word_size = 0;
  367. space_count = 0;
  368. } else if ((i == xl_text.length() || current == '\n') && last != nullptr && space_count != 0) {
  369. // In case there are trailing white spaces we add a placeholder word cache with just the spaces.
  370. WordCache *wc = memnew(WordCache);
  371. if (word_cache) {
  372. last->next = wc;
  373. } else {
  374. word_cache = wc;
  375. }
  376. last = wc;
  377. wc->pixel_width = 0;
  378. wc->char_pos = 0;
  379. wc->word_len = 0;
  380. wc->space_count = space_count;
  381. current_word_size = 0;
  382. space_count = 0;
  383. }
  384. if (current == '\n') {
  385. insert_newline = true;
  386. } else if (current != ' ') {
  387. total_char_cache++;
  388. }
  389. if (i < xl_text.length() && xl_text[i] == ' ') {
  390. if (line_width == 0) {
  391. if (current_word_size == 0) {
  392. word_pos = i;
  393. }
  394. current_word_size += space_width;
  395. line_width += space_width;
  396. } else if (line_width > 0 || last == nullptr || last->char_pos != WordCache::CHAR_WRAPLINE) {
  397. space_count++;
  398. line_width += space_width;
  399. } else {
  400. space_count = 0;
  401. }
  402. }
  403. } else { // Characters with graphical representation.
  404. // Word-break on CJK & non-CJK edge.
  405. if (separation_changed && current_word_size > 0) {
  406. WordCache *wc = memnew(WordCache);
  407. if (word_cache) {
  408. last->next = wc;
  409. } else {
  410. word_cache = wc;
  411. }
  412. last = wc;
  413. wc->pixel_width = current_word_size;
  414. wc->char_pos = word_pos;
  415. wc->word_len = i - word_pos;
  416. wc->space_count = space_count;
  417. current_word_size = 0;
  418. space_count = 0;
  419. }
  420. if (current_word_size == 0) {
  421. word_pos = i;
  422. }
  423. char_width = font->get_char_size(current, xl_text[i + 1]).width;
  424. current_word_size += char_width;
  425. line_width += char_width;
  426. total_char_cache++;
  427. // allow autowrap to cut words when they exceed line width
  428. if (autowrap && (current_word_size > width)) {
  429. separatable = true;
  430. }
  431. }
  432. if ((autowrap && (line_width >= width) && ((last && last->char_pos >= 0) || separatable)) || insert_newline) {
  433. if (separatable) {
  434. if (current_word_size > 0) {
  435. WordCache *wc = memnew(WordCache);
  436. if (word_cache) {
  437. last->next = wc;
  438. } else {
  439. word_cache = wc;
  440. }
  441. last = wc;
  442. wc->pixel_width = current_word_size - char_width;
  443. wc->char_pos = word_pos;
  444. wc->word_len = i - word_pos;
  445. wc->space_count = space_count;
  446. current_word_size = char_width;
  447. word_pos = i;
  448. }
  449. }
  450. WordCache *wc = memnew(WordCache);
  451. if (word_cache) {
  452. last->next = wc;
  453. } else {
  454. word_cache = wc;
  455. }
  456. last = wc;
  457. wc->pixel_width = 0;
  458. wc->char_pos = insert_newline ? WordCache::CHAR_NEWLINE : WordCache::CHAR_WRAPLINE;
  459. line_width = current_word_size;
  460. line_count++;
  461. space_count = 0;
  462. }
  463. }
  464. if (!autowrap) {
  465. minsize.width = width;
  466. }
  467. if (max_lines_visible > 0 && line_count > max_lines_visible) {
  468. minsize.height = (font->get_height() * max_lines_visible) + (line_spacing * (max_lines_visible - 1));
  469. } else {
  470. minsize.height = (font->get_height() * line_count) + (line_spacing * (line_count - 1));
  471. }
  472. if (!autowrap || !clip) {
  473. //helps speed up some labels that may change a lot, as no resizing is requested. Do not change.
  474. minimum_size_changed();
  475. }
  476. word_cache_dirty = false;
  477. }
  478. void Label::set_align(Align p_align) {
  479. ERR_FAIL_INDEX((int)p_align, 4);
  480. align = p_align;
  481. update();
  482. }
  483. Label::Align Label::get_align() const {
  484. return align;
  485. }
  486. void Label::set_valign(VAlign p_align) {
  487. ERR_FAIL_INDEX((int)p_align, 4);
  488. valign = p_align;
  489. update();
  490. }
  491. Label::VAlign Label::get_valign() const {
  492. return valign;
  493. }
  494. void Label::set_text(const String &p_string) {
  495. if (text == p_string) {
  496. return;
  497. }
  498. text = p_string;
  499. xl_text = tr(p_string);
  500. word_cache_dirty = true;
  501. if (percent_visible < 1) {
  502. visible_chars = get_total_character_count() * percent_visible;
  503. }
  504. update();
  505. }
  506. void Label::set_clip_text(bool p_clip) {
  507. clip = p_clip;
  508. update();
  509. minimum_size_changed();
  510. }
  511. bool Label::is_clipping_text() const {
  512. return clip;
  513. }
  514. String Label::get_text() const {
  515. return text;
  516. }
  517. void Label::set_visible_characters(int p_amount) {
  518. visible_chars = p_amount;
  519. if (get_total_character_count() > 0) {
  520. percent_visible = (float)p_amount / (float)total_char_cache;
  521. }
  522. _change_notify("percent_visible");
  523. update();
  524. }
  525. int Label::get_visible_characters() const {
  526. return visible_chars;
  527. }
  528. void Label::set_percent_visible(float p_percent) {
  529. if (p_percent < 0 || p_percent >= 1) {
  530. visible_chars = -1;
  531. percent_visible = 1;
  532. } else {
  533. visible_chars = get_total_character_count() * p_percent;
  534. percent_visible = p_percent;
  535. }
  536. _change_notify("visible_chars");
  537. update();
  538. }
  539. float Label::get_percent_visible() const {
  540. return percent_visible;
  541. }
  542. void Label::set_lines_skipped(int p_lines) {
  543. ERR_FAIL_COND(p_lines < 0);
  544. lines_skipped = p_lines;
  545. update();
  546. }
  547. int Label::get_lines_skipped() const {
  548. return lines_skipped;
  549. }
  550. void Label::set_max_lines_visible(int p_lines) {
  551. max_lines_visible = p_lines;
  552. update();
  553. }
  554. int Label::get_max_lines_visible() const {
  555. return max_lines_visible;
  556. }
  557. int Label::get_total_character_count() const {
  558. if (word_cache_dirty) {
  559. const_cast<Label *>(this)->regenerate_word_cache();
  560. }
  561. return total_char_cache;
  562. }
  563. void Label::_bind_methods() {
  564. ClassDB::bind_method(D_METHOD("set_align", "align"), &Label::set_align);
  565. ClassDB::bind_method(D_METHOD("get_align"), &Label::get_align);
  566. ClassDB::bind_method(D_METHOD("set_valign", "valign"), &Label::set_valign);
  567. ClassDB::bind_method(D_METHOD("get_valign"), &Label::get_valign);
  568. ClassDB::bind_method(D_METHOD("set_text", "text"), &Label::set_text);
  569. ClassDB::bind_method(D_METHOD("get_text"), &Label::get_text);
  570. ClassDB::bind_method(D_METHOD("set_autowrap", "enable"), &Label::set_autowrap);
  571. ClassDB::bind_method(D_METHOD("has_autowrap"), &Label::has_autowrap);
  572. ClassDB::bind_method(D_METHOD("set_clip_text", "enable"), &Label::set_clip_text);
  573. ClassDB::bind_method(D_METHOD("is_clipping_text"), &Label::is_clipping_text);
  574. ClassDB::bind_method(D_METHOD("set_uppercase", "enable"), &Label::set_uppercase);
  575. ClassDB::bind_method(D_METHOD("is_uppercase"), &Label::is_uppercase);
  576. ClassDB::bind_method(D_METHOD("get_line_height"), &Label::get_line_height);
  577. ClassDB::bind_method(D_METHOD("get_line_count"), &Label::get_line_count);
  578. ClassDB::bind_method(D_METHOD("get_visible_line_count"), &Label::get_visible_line_count);
  579. ClassDB::bind_method(D_METHOD("get_total_character_count"), &Label::get_total_character_count);
  580. ClassDB::bind_method(D_METHOD("set_visible_characters", "amount"), &Label::set_visible_characters);
  581. ClassDB::bind_method(D_METHOD("get_visible_characters"), &Label::get_visible_characters);
  582. ClassDB::bind_method(D_METHOD("set_percent_visible", "percent_visible"), &Label::set_percent_visible);
  583. ClassDB::bind_method(D_METHOD("get_percent_visible"), &Label::get_percent_visible);
  584. ClassDB::bind_method(D_METHOD("set_lines_skipped", "lines_skipped"), &Label::set_lines_skipped);
  585. ClassDB::bind_method(D_METHOD("get_lines_skipped"), &Label::get_lines_skipped);
  586. ClassDB::bind_method(D_METHOD("set_max_lines_visible", "lines_visible"), &Label::set_max_lines_visible);
  587. ClassDB::bind_method(D_METHOD("get_max_lines_visible"), &Label::get_max_lines_visible);
  588. BIND_ENUM_CONSTANT(ALIGN_LEFT);
  589. BIND_ENUM_CONSTANT(ALIGN_CENTER);
  590. BIND_ENUM_CONSTANT(ALIGN_RIGHT);
  591. BIND_ENUM_CONSTANT(ALIGN_FILL);
  592. BIND_ENUM_CONSTANT(VALIGN_TOP);
  593. BIND_ENUM_CONSTANT(VALIGN_CENTER);
  594. BIND_ENUM_CONSTANT(VALIGN_BOTTOM);
  595. BIND_ENUM_CONSTANT(VALIGN_FILL);
  596. ADD_PROPERTY(PropertyInfo(Variant::STRING, "text", PROPERTY_HINT_MULTILINE_TEXT, "", PROPERTY_USAGE_DEFAULT_INTL), "set_text", "get_text");
  597. ADD_PROPERTY(PropertyInfo(Variant::INT, "align", PROPERTY_HINT_ENUM, "Left,Center,Right,Fill"), "set_align", "get_align");
  598. ADD_PROPERTY(PropertyInfo(Variant::INT, "valign", PROPERTY_HINT_ENUM, "Top,Center,Bottom,Fill"), "set_valign", "get_valign");
  599. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "autowrap"), "set_autowrap", "has_autowrap");
  600. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "clip_text"), "set_clip_text", "is_clipping_text");
  601. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "uppercase"), "set_uppercase", "is_uppercase");
  602. ADD_PROPERTY(PropertyInfo(Variant::INT, "visible_characters", PROPERTY_HINT_RANGE, "-1,128000,1", PROPERTY_USAGE_EDITOR), "set_visible_characters", "get_visible_characters");
  603. ADD_PROPERTY(PropertyInfo(Variant::REAL, "percent_visible", PROPERTY_HINT_RANGE, "0,1,0.001"), "set_percent_visible", "get_percent_visible");
  604. ADD_PROPERTY(PropertyInfo(Variant::INT, "lines_skipped", PROPERTY_HINT_RANGE, "0,999,1"), "set_lines_skipped", "get_lines_skipped");
  605. ADD_PROPERTY(PropertyInfo(Variant::INT, "max_lines_visible", PROPERTY_HINT_RANGE, "-1,999,1"), "set_max_lines_visible", "get_max_lines_visible");
  606. }
  607. Label::Label(const String &p_text) {
  608. align = ALIGN_LEFT;
  609. valign = VALIGN_TOP;
  610. xl_text = "";
  611. word_cache = nullptr;
  612. word_cache_dirty = true;
  613. autowrap = false;
  614. line_count = 0;
  615. set_v_size_flags(0);
  616. clip = false;
  617. set_mouse_filter(MOUSE_FILTER_IGNORE);
  618. total_char_cache = 0;
  619. visible_chars = -1;
  620. percent_visible = 1;
  621. lines_skipped = 0;
  622. max_lines_visible = -1;
  623. set_text(p_text);
  624. uppercase = false;
  625. set_v_size_flags(SIZE_SHRINK_CENTER);
  626. }
  627. Label::~Label() {
  628. while (word_cache) {
  629. WordCache *current = word_cache;
  630. word_cache = current->next;
  631. memdelete(current);
  632. }
  633. }