rich_text_label.h 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993
  1. /**************************************************************************/
  2. /* rich_text_label.h */
  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. #pragma once
  31. #include "core/object/worker_thread_pool.h"
  32. #include "core/templates/rid_owner.h"
  33. #include "scene/gui/popup_menu.h"
  34. #include "scene/gui/scroll_bar.h"
  35. #include "scene/resources/image_texture.h"
  36. #include "scene/resources/text_paragraph.h"
  37. class CharFXTransform;
  38. class RichTextEffect;
  39. class RichTextLabel : public Control {
  40. GDCLASS(RichTextLabel, Control);
  41. enum RTLDrawStep {
  42. DRAW_STEP_BACKGROUND,
  43. DRAW_STEP_SHADOW_OUTLINE,
  44. DRAW_STEP_SHADOW,
  45. DRAW_STEP_OUTLINE,
  46. DRAW_STEP_TEXT,
  47. DRAW_STEP_FOREGROUND,
  48. DRAW_STEP_MAX,
  49. };
  50. public:
  51. enum ListType {
  52. LIST_NUMBERS,
  53. LIST_LETTERS,
  54. LIST_ROMAN,
  55. LIST_DOTS
  56. };
  57. enum MetaUnderline {
  58. META_UNDERLINE_NEVER,
  59. META_UNDERLINE_ALWAYS,
  60. META_UNDERLINE_ON_HOVER,
  61. };
  62. enum ItemType {
  63. ITEM_FRAME,
  64. ITEM_TEXT,
  65. ITEM_IMAGE,
  66. ITEM_NEWLINE,
  67. ITEM_FONT,
  68. ITEM_FONT_SIZE,
  69. ITEM_FONT_FEATURES,
  70. ITEM_COLOR,
  71. ITEM_OUTLINE_SIZE,
  72. ITEM_OUTLINE_COLOR,
  73. ITEM_UNDERLINE,
  74. ITEM_STRIKETHROUGH,
  75. ITEM_PARAGRAPH,
  76. ITEM_INDENT,
  77. ITEM_LIST,
  78. ITEM_TABLE,
  79. ITEM_FADE,
  80. ITEM_SHAKE,
  81. ITEM_WAVE,
  82. ITEM_TORNADO,
  83. ITEM_RAINBOW,
  84. ITEM_PULSE,
  85. ITEM_BGCOLOR,
  86. ITEM_FGCOLOR,
  87. ITEM_META,
  88. ITEM_HINT,
  89. ITEM_DROPCAP,
  90. ITEM_CUSTOMFX,
  91. ITEM_CONTEXT,
  92. ITEM_LANGUAGE,
  93. };
  94. enum MenuItems {
  95. MENU_COPY,
  96. MENU_SELECT_ALL,
  97. MENU_MAX
  98. };
  99. enum DefaultFont {
  100. RTL_NORMAL_FONT,
  101. RTL_BOLD_FONT,
  102. RTL_ITALICS_FONT,
  103. RTL_BOLD_ITALICS_FONT,
  104. RTL_MONO_FONT,
  105. RTL_CUSTOM_FONT,
  106. };
  107. enum ImageUpdateMask {
  108. UPDATE_TEXTURE = 1 << 0,
  109. UPDATE_SIZE = 1 << 1,
  110. UPDATE_COLOR = 1 << 2,
  111. UPDATE_ALIGNMENT = 1 << 3,
  112. UPDATE_REGION = 1 << 4,
  113. UPDATE_PAD = 1 << 5,
  114. UPDATE_TOOLTIP = 1 << 6,
  115. UPDATE_WIDTH_IN_PERCENT = 1 << 7,
  116. };
  117. protected:
  118. virtual void _update_theme_item_cache() override;
  119. void _notification(int p_what);
  120. static void _bind_methods();
  121. #ifndef DISABLE_DEPRECATED
  122. void _push_font_bind_compat_79053(const Ref<Font> &p_font, int p_size);
  123. void _set_table_column_expand_bind_compat_79053(int p_column, bool p_expand, int p_ratio);
  124. void _push_meta_bind_compat_99481(const Variant &p_meta, MetaUnderline p_underline_mode);
  125. void _push_meta_bind_compat_89024(const Variant &p_meta);
  126. void _add_image_bind_compat_80410(const Ref<Texture2D> &p_image, const int p_width, const int p_height, const Color &p_color, InlineAlignment p_alignment, const Rect2 &p_region);
  127. void _add_image_bind_compat_76829(const Ref<Texture2D> &p_image, const int p_width, const int p_height, const Color &p_color, InlineAlignment p_alignment, const Rect2 &p_region, const Variant &p_key, bool p_pad, const String &p_tooltip, bool p_size_in_percent);
  128. void _push_table_bind_compat_76829(int p_columns, InlineAlignment p_alignment, int p_align_to_row);
  129. bool _remove_paragraph_bind_compat_91098(int p_paragraph);
  130. void _set_table_column_expand_bind_compat_101482(int p_column, bool p_expand, int p_ratio);
  131. void _push_underline_bind_compat_106300();
  132. void _push_strikethrough_bind_compat_106300();
  133. void _add_image_bind_compat_107347(const Ref<Texture2D> &p_image, int p_width = 0, int p_height = 0, const Color &p_color = Color(1.0, 1.0, 1.0), InlineAlignment p_alignment = INLINE_ALIGNMENT_CENTER, const Rect2 &p_region = Rect2(), const Variant &p_key = Variant(), bool p_pad = false, const String &p_tooltip = String(), bool p_size_in_percent = false, const String &p_alt_text = String());
  134. void _update_image_bind_compat_107347(const Variant &p_key, BitField<ImageUpdateMask> p_mask, const Ref<Texture2D> &p_image, int p_width = 0, int p_height = 0, const Color &p_color = Color(1.0, 1.0, 1.0), InlineAlignment p_alignment = INLINE_ALIGNMENT_CENTER, const Rect2 &p_region = Rect2(), bool p_pad = false, const String &p_tooltip = String(), bool p_size_in_percent = false);
  135. static void _bind_compatibility_methods();
  136. #endif
  137. private:
  138. struct Item;
  139. struct Line {
  140. Item *from = nullptr;
  141. Ref<TextLine> text_prefix;
  142. float prefix_width = 0;
  143. Ref<TextParagraph> text_buf;
  144. RID accessibility_line_element;
  145. RID accessibility_text_element;
  146. Item *dc_item = nullptr;
  147. Color dc_color;
  148. int dc_ol_size = 0;
  149. Color dc_ol_color;
  150. Vector2 offset;
  151. float indent = 0.0;
  152. int char_offset = 0;
  153. int char_count = 0;
  154. Line() {
  155. text_buf.instantiate();
  156. }
  157. ~Line() {
  158. if (accessibility_line_element.is_valid()) {
  159. DisplayServer::get_singleton()->accessibility_free_element(accessibility_line_element);
  160. accessibility_line_element = RID();
  161. accessibility_text_element = RID();
  162. }
  163. }
  164. _FORCE_INLINE_ float get_height(float p_line_separation, float p_paragraph_separation) const {
  165. return offset.y + text_buf->get_size().y + (text_buf->get_line_count() - 1) * p_line_separation + p_paragraph_separation;
  166. }
  167. };
  168. struct Item {
  169. int index = 0;
  170. int char_ofs = 0;
  171. Item *parent = nullptr;
  172. ItemType type = ITEM_FRAME;
  173. List<Item *> subitems;
  174. List<Item *>::Element *E = nullptr;
  175. ObjectID owner;
  176. int line = 0;
  177. RID rid;
  178. RID accessibility_item_element;
  179. void _clear_children() {
  180. RichTextLabel *owner_rtl = ObjectDB::get_instance<RichTextLabel>(owner);
  181. while (subitems.size()) {
  182. Item *subitem = subitems.front()->get();
  183. if (subitem && subitem->rid.is_valid() && owner_rtl) {
  184. owner_rtl->items.free(subitem->rid);
  185. }
  186. memdelete(subitem);
  187. subitems.pop_front();
  188. }
  189. }
  190. virtual ~Item() { _clear_children(); }
  191. };
  192. struct ItemFrame : public Item {
  193. bool cell = false;
  194. LocalVector<Line> lines;
  195. std::atomic<int> first_invalid_line;
  196. std::atomic<int> first_invalid_font_line;
  197. std::atomic<int> first_resized_line;
  198. ItemFrame *parent_frame = nullptr;
  199. Color odd_row_bg = Color(0, 0, 0, 0);
  200. Color even_row_bg = Color(0, 0, 0, 0);
  201. Color border = Color(0, 0, 0, 0);
  202. Size2 min_size_over = Size2(-1, -1);
  203. Size2 max_size_over = Size2(-1, -1);
  204. Rect2 padding;
  205. int indent_level = 0;
  206. ItemFrame() {
  207. type = ITEM_FRAME;
  208. first_invalid_line.store(0);
  209. first_invalid_font_line.store(0);
  210. first_resized_line.store(0);
  211. }
  212. };
  213. struct ItemText : public Item {
  214. String text;
  215. ItemText() { type = ITEM_TEXT; }
  216. };
  217. struct ItemDropcap : public Item {
  218. String text;
  219. Ref<Font> font;
  220. int font_size = 0;
  221. Color color;
  222. int ol_size = 0;
  223. Color ol_color;
  224. Rect2 dropcap_margins;
  225. ItemDropcap() { type = ITEM_DROPCAP; }
  226. ~ItemDropcap() {
  227. if (font.is_valid()) {
  228. RichTextLabel *owner_rtl = ObjectDB::get_instance<RichTextLabel>(owner);
  229. if (owner_rtl) {
  230. font->disconnect_changed(callable_mp(owner_rtl, &RichTextLabel::_invalidate_fonts));
  231. }
  232. }
  233. }
  234. };
  235. struct ItemImage : public Item {
  236. Ref<Texture2D> image;
  237. String alt_text;
  238. InlineAlignment inline_align = INLINE_ALIGNMENT_CENTER;
  239. bool pad = false;
  240. bool width_in_percent = false;
  241. bool height_in_percent = false;
  242. Rect2 region;
  243. Size2 size;
  244. Size2 rq_size;
  245. Color color;
  246. Variant key;
  247. String tooltip;
  248. ItemImage() { type = ITEM_IMAGE; }
  249. ~ItemImage() {
  250. if (image.is_valid()) {
  251. RichTextLabel *owner_rtl = ObjectDB::get_instance<RichTextLabel>(owner);
  252. if (owner_rtl) {
  253. if (owner_rtl->hr_list.has(rid)) {
  254. owner_rtl->hr_list.erase((rid));
  255. }
  256. image->disconnect_changed(callable_mp(owner_rtl, &RichTextLabel::_texture_changed));
  257. }
  258. }
  259. }
  260. };
  261. struct ItemFont : public Item {
  262. DefaultFont def_font = RTL_CUSTOM_FONT;
  263. Ref<Font> font;
  264. bool variation = false;
  265. bool def_size = false;
  266. int font_size = 0;
  267. ItemFont() { type = ITEM_FONT; }
  268. ~ItemFont() {
  269. if (font.is_valid()) {
  270. RichTextLabel *owner_rtl = ObjectDB::get_instance<RichTextLabel>(owner);
  271. if (owner_rtl) {
  272. font->disconnect_changed(callable_mp(owner_rtl, &RichTextLabel::_invalidate_fonts));
  273. }
  274. }
  275. }
  276. };
  277. struct ItemFontSize : public Item {
  278. int font_size = 16;
  279. ItemFontSize() { type = ITEM_FONT_SIZE; }
  280. };
  281. struct ItemColor : public Item {
  282. Color color;
  283. ItemColor() { type = ITEM_COLOR; }
  284. };
  285. struct ItemOutlineSize : public Item {
  286. int outline_size = 0;
  287. ItemOutlineSize() { type = ITEM_OUTLINE_SIZE; }
  288. };
  289. struct ItemOutlineColor : public Item {
  290. Color color;
  291. ItemOutlineColor() { type = ITEM_OUTLINE_COLOR; }
  292. };
  293. struct ItemUnderline : public Item {
  294. Color color;
  295. ItemUnderline() { type = ITEM_UNDERLINE; }
  296. };
  297. struct ItemStrikethrough : public Item {
  298. Color color;
  299. ItemStrikethrough() { type = ITEM_STRIKETHROUGH; }
  300. };
  301. struct ItemMeta : public Item {
  302. Variant meta;
  303. MetaUnderline underline = META_UNDERLINE_ALWAYS;
  304. String tooltip;
  305. ItemMeta() { type = ITEM_META; }
  306. };
  307. struct ItemHint : public Item {
  308. String description;
  309. ItemHint() { type = ITEM_HINT; }
  310. };
  311. struct ItemLanguage : public Item {
  312. String language;
  313. ItemLanguage() { type = ITEM_LANGUAGE; }
  314. };
  315. struct ItemParagraph : public Item {
  316. HorizontalAlignment alignment = HORIZONTAL_ALIGNMENT_LEFT;
  317. String language;
  318. Control::TextDirection direction = Control::TEXT_DIRECTION_AUTO;
  319. TextServer::StructuredTextParser st_parser = TextServer::STRUCTURED_TEXT_DEFAULT;
  320. BitField<TextServer::JustificationFlag> jst_flags = TextServer::JUSTIFICATION_WORD_BOUND | TextServer::JUSTIFICATION_KASHIDA | TextServer::JUSTIFICATION_SKIP_LAST_LINE | TextServer::JUSTIFICATION_DO_NOT_SKIP_SINGLE_LINE;
  321. PackedFloat32Array tab_stops;
  322. ItemParagraph() { type = ITEM_PARAGRAPH; }
  323. };
  324. struct ItemIndent : public Item {
  325. int level = 0;
  326. ItemIndent() { type = ITEM_INDENT; }
  327. };
  328. struct ItemList : public Item {
  329. ListType list_type = LIST_DOTS;
  330. bool capitalize = false;
  331. int level = 0;
  332. String bullet = U"•";
  333. float max_width = 0;
  334. ItemList() { type = ITEM_LIST; }
  335. };
  336. struct ItemNewline : public Item {
  337. ItemNewline() { type = ITEM_NEWLINE; }
  338. };
  339. struct ItemTable : public Item {
  340. struct Column {
  341. String name;
  342. bool expand = false;
  343. bool shrink = true;
  344. int expand_ratio = 0;
  345. int min_width = 0;
  346. int max_width = 0;
  347. int width = 0;
  348. int width_with_padding = 0;
  349. };
  350. LocalVector<Column> columns;
  351. LocalVector<float> rows;
  352. LocalVector<float> rows_no_padding;
  353. LocalVector<float> rows_baseline;
  354. String name;
  355. int align_to_row = -1;
  356. int total_width = 0;
  357. int total_height = 0;
  358. InlineAlignment inline_align = INLINE_ALIGNMENT_TOP;
  359. ItemTable() { type = ITEM_TABLE; }
  360. };
  361. struct ItemFade : public Item {
  362. int starting_index = 0;
  363. int length = 0;
  364. ItemFade() { type = ITEM_FADE; }
  365. };
  366. struct ItemFX : public Item {
  367. double elapsed_time = 0.f;
  368. bool connected = true;
  369. };
  370. struct ItemShake : public ItemFX {
  371. int strength = 0;
  372. float rate = 0.0f;
  373. uint64_t _current_rng = 0;
  374. uint64_t _previous_rng = 0;
  375. Vector2 prev_off;
  376. ItemShake() { type = ITEM_SHAKE; }
  377. void reroll_random() {
  378. _previous_rng = _current_rng;
  379. _current_rng = Math::rand();
  380. }
  381. uint64_t offset_random(int p_index) {
  382. return (_current_rng >> (p_index % 64)) |
  383. (_current_rng << (64 - (p_index % 64)));
  384. }
  385. uint64_t offset_previous_random(int p_index) {
  386. return (_previous_rng >> (p_index % 64)) |
  387. (_previous_rng << (64 - (p_index % 64)));
  388. }
  389. };
  390. struct ItemWave : public ItemFX {
  391. float frequency = 1.0f;
  392. float amplitude = 1.0f;
  393. Vector2 prev_off;
  394. ItemWave() { type = ITEM_WAVE; }
  395. };
  396. struct ItemTornado : public ItemFX {
  397. float radius = 1.0f;
  398. float frequency = 1.0f;
  399. Vector2 prev_off;
  400. ItemTornado() { type = ITEM_TORNADO; }
  401. };
  402. struct ItemRainbow : public ItemFX {
  403. float saturation = 0.9f;
  404. float value = 1.0f;
  405. float frequency = 1.0f;
  406. float speed = 1.0f;
  407. ItemRainbow() { type = ITEM_RAINBOW; }
  408. };
  409. struct ItemPulse : public ItemFX {
  410. Color color = Color(1.0, 1.0, 1.0, 0.25);
  411. float frequency = 1.0f;
  412. float ease = -2.0f;
  413. ItemPulse() { type = ITEM_PULSE; }
  414. };
  415. struct ItemBGColor : public Item {
  416. Color color;
  417. ItemBGColor() { type = ITEM_BGCOLOR; }
  418. };
  419. struct ItemFGColor : public Item {
  420. Color color;
  421. ItemFGColor() { type = ITEM_FGCOLOR; }
  422. };
  423. struct ItemCustomFX : public ItemFX {
  424. Ref<CharFXTransform> char_fx_transform;
  425. Ref<RichTextEffect> custom_effect;
  426. ItemCustomFX();
  427. virtual ~ItemCustomFX();
  428. };
  429. struct ItemContext : public Item {
  430. ItemContext() { type = ITEM_CONTEXT; }
  431. };
  432. ItemFrame *main = nullptr;
  433. Item *current = nullptr;
  434. ItemFrame *current_frame = nullptr;
  435. WorkerThreadPool::TaskID task = WorkerThreadPool::INVALID_TASK_ID;
  436. Mutex data_mutex;
  437. bool threaded = false;
  438. std::atomic<bool> stop_thread;
  439. std::atomic<bool> updating;
  440. std::atomic<bool> validating;
  441. std::atomic<double> loaded;
  442. std::atomic<bool> parsing_bbcode;
  443. uint64_t loading_started = 0;
  444. int progress_delay = 1000;
  445. VScrollBar *vscroll = nullptr;
  446. TextServer::AutowrapMode autowrap_mode = TextServer::AUTOWRAP_WORD_SMART;
  447. BitField<TextServer::LineBreakFlag> autowrap_flags_trim = TextServer::BREAK_TRIM_START_EDGE_SPACES | TextServer::BREAK_TRIM_END_EDGE_SPACES;
  448. bool scroll_visible = false;
  449. bool scroll_follow = false;
  450. bool scroll_follow_visible_characters = false;
  451. int follow_vc_pos = 0;
  452. bool scroll_following = false;
  453. bool scroll_active = true;
  454. int scroll_w = 0;
  455. bool scroll_updated = false;
  456. bool updating_scroll = false;
  457. int current_idx = 1;
  458. int current_char_ofs = 0;
  459. int visible_paragraph_count = 0;
  460. int visible_line_count = 0;
  461. Rect2i visible_rect;
  462. int tab_size = 4;
  463. bool underline_meta = true;
  464. bool underline_hint = true;
  465. bool use_selected_font_color = false;
  466. HorizontalAlignment default_alignment = HORIZONTAL_ALIGNMENT_LEFT;
  467. VerticalAlignment vertical_alignment = VERTICAL_ALIGNMENT_TOP;
  468. BitField<TextServer::JustificationFlag> default_jst_flags = TextServer::JUSTIFICATION_WORD_BOUND | TextServer::JUSTIFICATION_KASHIDA | TextServer::JUSTIFICATION_SKIP_LAST_LINE | TextServer::JUSTIFICATION_DO_NOT_SKIP_SINGLE_LINE;
  469. PackedFloat32Array default_tab_stops;
  470. ItemMeta *meta_hovering = nullptr;
  471. Variant current_meta;
  472. Array custom_effects;
  473. HashMap<RID, Rect2> ac_element_bounds_cache;
  474. void _update_follow_vc();
  475. void _invalidate_accessibility();
  476. void _invalidate_current_line(ItemFrame *p_frame);
  477. void _thread_function(void *p_userdata);
  478. void _thread_end();
  479. void _stop_thread();
  480. bool _validate_line_caches();
  481. void _process_line_caches();
  482. _FORCE_INLINE_ float _update_scroll_exceeds(float p_total_height, float p_ctrl_height, float p_width, int p_idx, float p_old_scroll, float p_text_rect_height);
  483. void _add_item(Item *p_item, bool p_enter = false, bool p_ensure_newline = false);
  484. void _remove_frame(HashSet<Item *> &r_erase_list, ItemFrame *p_frame, int p_line, bool p_erase, int p_char_offset, int p_line_offset);
  485. void _texture_changed(RID p_item);
  486. RID_PtrOwner<Item> items;
  487. List<String> tag_stack;
  488. HashSet<RID> hr_list;
  489. String language;
  490. TextDirection text_direction = TEXT_DIRECTION_AUTO;
  491. TextServer::StructuredTextParser st_parser = TextServer::STRUCTURED_TEXT_DEFAULT;
  492. Array st_args;
  493. struct Selection {
  494. ItemFrame *click_frame = nullptr;
  495. int click_line = 0;
  496. Item *click_item = nullptr;
  497. int click_char = 0;
  498. ItemFrame *from_frame = nullptr;
  499. int from_line = 0;
  500. Item *from_item = nullptr;
  501. int from_char = 0;
  502. ItemFrame *to_frame = nullptr;
  503. int to_line = 0;
  504. Item *to_item = nullptr;
  505. int to_char = 0;
  506. bool double_click = false; // Selecting whole words?
  507. bool active = false; // anything selected? i.e. from, to, etc. valid?
  508. bool enabled = false; // allow selections?
  509. bool drag_attempt = false;
  510. };
  511. Selection selection;
  512. Callable selection_modifier;
  513. bool deselect_on_focus_loss_enabled = true;
  514. bool drag_and_drop_selection_enabled = true;
  515. ItemFrame *keyboard_focus_frame = nullptr;
  516. int keyboard_focus_line = 0;
  517. Item *keyboard_focus_item = nullptr;
  518. bool keyboard_focus_on_text = true;
  519. bool context_menu_enabled = false;
  520. bool shortcut_keys_enabled = true;
  521. // Context menu.
  522. PopupMenu *menu = nullptr;
  523. void _generate_context_menu();
  524. void _update_context_menu();
  525. Key _get_menu_action_accelerator(const String &p_action);
  526. int visible_characters = -1;
  527. float visible_ratio = 1.0;
  528. TextServer::VisibleCharactersBehavior visible_chars_behavior = TextServer::VC_CHARS_BEFORE_SHAPING;
  529. bool _is_click_inside_selection() const;
  530. void _find_click(ItemFrame *p_frame, const Point2i &p_click, ItemFrame **r_click_frame = nullptr, int *r_click_line = nullptr, Item **r_click_item = nullptr, int *r_click_char = nullptr, bool *r_outside = nullptr, bool p_meta = false);
  531. String _get_line_text(ItemFrame *p_frame, int p_line, Selection p_sel) const;
  532. bool _search_line(ItemFrame *p_frame, int p_line, const String &p_string, int p_char_idx, bool p_reverse_search);
  533. bool _search_table(ItemTable *p_table, List<Item *>::Element *p_from, const String &p_string, bool p_reverse_search);
  534. float _shape_line(ItemFrame *p_frame, int p_line, const Ref<Font> &p_base_font, int p_base_font_size, int p_width, float p_h, int *r_char_offset);
  535. float _resize_line(ItemFrame *p_frame, int p_line, const Ref<Font> &p_base_font, int p_base_font_size, int p_width, float p_h);
  536. void _set_table_size(ItemTable *p_table, int p_available_width);
  537. void _update_line_font(ItemFrame *p_frame, int p_line, const Ref<Font> &p_base_font, int p_base_font_size);
  538. int _draw_line(ItemFrame *p_frame, int p_line, const Vector2 &p_ofs, int p_width, float p_vsep, const Color &p_base_color, int p_outline_size, const Color &p_outline_color, const Color &p_font_shadow_color, int p_shadow_outline_size, const Point2 &p_shadow_ofs, int &r_processed_glyphs);
  539. float _find_click_in_line(ItemFrame *p_frame, int p_line, const Vector2 &p_ofs, int p_width, float p_vsep, const Point2i &p_click, ItemFrame **r_click_frame = nullptr, int *r_click_line = nullptr, Item **r_click_item = nullptr, int *r_click_char = nullptr, bool p_table = false, bool p_meta = false);
  540. void _accessibility_update_line(RID p_id, ItemFrame *p_frame, int p_line, const Vector2 &p_ofs, int p_width, float p_vsep);
  541. String _roman(int p_num, bool p_capitalize) const;
  542. String _letters(int p_num, bool p_capitalize) const;
  543. Item *_find_indentable(Item *p_item);
  544. Item *_get_item_at_pos(Item *p_item_from, Item *p_item_to, int p_position);
  545. void _find_frame(Item *p_item, ItemFrame **r_frame, int *r_line);
  546. ItemFontSize *_find_font_size(Item *p_item);
  547. ItemFont *_find_font(Item *p_item);
  548. int _find_outline_size(Item *p_item, int p_default);
  549. ItemList *_find_list_item(Item *p_item);
  550. ItemDropcap *_find_dc_item(Item *p_item);
  551. int _find_list(Item *p_item, Vector<int> &r_index, Vector<int> &r_count, Vector<ItemList *> &r_list);
  552. int _find_margin(Item *p_item, const Ref<Font> &p_base_font, int p_base_font_size);
  553. PackedFloat32Array _find_tab_stops(Item *p_item);
  554. HorizontalAlignment _find_alignment(Item *p_item);
  555. BitField<TextServer::JustificationFlag> _find_jst_flags(Item *p_item);
  556. TextServer::Direction _find_direction(Item *p_item);
  557. TextServer::StructuredTextParser _find_stt(Item *p_item);
  558. String _find_language(Item *p_item);
  559. Color _find_color(Item *p_item, const Color &p_default_color);
  560. Color _find_outline_color(Item *p_item, const Color &p_default_color);
  561. bool _find_underline(Item *p_item, Color *r_color = nullptr);
  562. bool _find_strikethrough(Item *p_item, Color *r_color = nullptr);
  563. bool _find_meta(Item *p_item, Variant *r_meta, ItemMeta **r_item = nullptr);
  564. bool _find_hint(Item *p_item, String *r_description);
  565. Color _find_bgcolor(Item *p_item);
  566. Color _find_fgcolor(Item *p_item);
  567. bool _find_layout_subitem(Item *from, Item *to);
  568. void _fetch_item_fx_stack(Item *p_item, Vector<ItemFX *> &r_stack);
  569. void _normalize_subtags(Vector<String> &subtags);
  570. void _update_fx(ItemFrame *p_frame, double p_delta_time);
  571. void _scroll_changed(double);
  572. int _find_first_line(int p_from, int p_to, int p_vofs) const;
  573. _FORCE_INLINE_ float _calculate_line_vertical_offset(const Line &line) const;
  574. virtual void gui_input(const Ref<InputEvent> &p_event) override;
  575. virtual String get_tooltip(const Point2 &p_pos) const override;
  576. Item *_get_next_item(Item *p_item, bool p_free = false) const;
  577. Item *_get_prev_item(Item *p_item, bool p_free = false) const;
  578. Rect2 _get_text_rect();
  579. Ref<RichTextEffect> _get_custom_effect_by_code(String p_bbcode_identifier);
  580. virtual Dictionary parse_expressions_for_values(Vector<String> p_expressions);
  581. void _invalidate_fonts();
  582. Size2 _get_image_size(const Ref<Texture2D> &p_image, int p_width = 0, int p_height = 0, const Rect2 &p_region = Rect2());
  583. String _get_prefix(Item *p_item, const Vector<int> &p_list_index, const Vector<ItemList *> &p_list_items);
  584. static int _find_unquoted(const String &p_src, char32_t p_chr, int p_from);
  585. static Vector<String> _split_unquoted(const String &p_src, char32_t p_splitter);
  586. static String _get_tag_value(const String &p_tag);
  587. #ifndef DISABLE_DEPRECATED
  588. // Kept for compatibility from 3.x to 4.0.
  589. bool _set(const StringName &p_name, const Variant &p_value);
  590. #endif
  591. bool use_bbcode = false;
  592. String text;
  593. void _apply_translation();
  594. bool internal_stack_editing = false;
  595. bool stack_externally_modified = false;
  596. void _accessibility_action_menu(const Variant &p_data);
  597. void _accessibility_scroll_down(const Variant &p_data);
  598. void _accessibility_scroll_up(const Variant &p_data);
  599. void _accessibility_scroll_set(const Variant &p_data);
  600. void _accessibility_focus_item(const Variant &p_data, uint64_t p_item, bool p_line, bool p_foucs);
  601. void _accessibility_scroll_to_item(const Variant &p_data, uint64_t p_item);
  602. RID accessibility_scroll_element;
  603. bool fit_content = false;
  604. struct ThemeCache {
  605. Ref<StyleBox> normal_style;
  606. Ref<StyleBox> focus_style;
  607. Ref<StyleBox> progress_bg_style;
  608. Ref<StyleBox> progress_fg_style;
  609. Ref<Texture2D> horizontal_rule;
  610. int line_separation;
  611. int paragraph_separation;
  612. Ref<Font> normal_font;
  613. int normal_font_size;
  614. Color default_color;
  615. Color font_selected_color;
  616. Color selection_color;
  617. Color font_outline_color;
  618. Color font_shadow_color;
  619. int shadow_outline_size;
  620. int shadow_offset_x;
  621. int shadow_offset_y;
  622. int outline_size;
  623. Color outline_color;
  624. Ref<Font> bold_font;
  625. int bold_font_size;
  626. Ref<Font> bold_italics_font;
  627. int bold_italics_font_size;
  628. Ref<Font> italics_font;
  629. int italics_font_size;
  630. Ref<Font> mono_font;
  631. int mono_font_size;
  632. int text_highlight_h_padding;
  633. int text_highlight_v_padding;
  634. int underline_alpha;
  635. int strikethrough_alpha;
  636. int table_h_separation;
  637. int table_v_separation;
  638. Color table_odd_row_bg;
  639. Color table_even_row_bg;
  640. Color table_border;
  641. float base_scale = 1.0;
  642. } theme_cache;
  643. public:
  644. virtual RID get_focused_accessibility_element() const override;
  645. PackedStringArray get_accessibility_configuration_warnings() const override;
  646. String get_parsed_text() const;
  647. void add_text(const String &p_text);
  648. void add_hr(int p_width = 90, int p_height = 2, const Color &p_color = Color(1.0, 1.0, 1.0), HorizontalAlignment p_alignment = HORIZONTAL_ALIGNMENT_LEFT, bool p_width_in_percent = true, bool p_height_in_percent = false);
  649. void add_image(const Ref<Texture2D> &p_image, int p_width = 0, int p_height = 0, const Color &p_color = Color(1.0, 1.0, 1.0), InlineAlignment p_alignment = INLINE_ALIGNMENT_CENTER, const Rect2 &p_region = Rect2(), const Variant &p_key = Variant(), bool p_pad = false, const String &p_tooltip = String(), bool p_width_in_percent = false, bool p_height_in_percent = false, const String &p_alt_text = String());
  650. void update_image(const Variant &p_key, BitField<ImageUpdateMask> p_mask, const Ref<Texture2D> &p_image, int p_width = 0, int p_height = 0, const Color &p_color = Color(1.0, 1.0, 1.0), InlineAlignment p_alignment = INLINE_ALIGNMENT_CENTER, const Rect2 &p_region = Rect2(), bool p_pad = false, const String &p_tooltip = String(), bool p_width_in_percent = false, bool p_height_in_percent = false);
  651. void add_newline();
  652. bool remove_paragraph(int p_paragraph, bool p_no_invalidate = false);
  653. bool invalidate_paragraph(int p_paragraph);
  654. void push_dropcap(const String &p_string, const Ref<Font> &p_font, int p_size, const Rect2 &p_dropcap_margins = Rect2(), const Color &p_color = Color(1, 1, 1), int p_ol_size = 0, const Color &p_ol_color = Color(0, 0, 0, 0));
  655. void _push_def_font(DefaultFont p_def_font);
  656. void _push_def_font_var(DefaultFont p_def_font, const Ref<Font> &p_font, int p_size = -1);
  657. void push_font(const Ref<Font> &p_font, int p_size = 0);
  658. void push_font_size(int p_font_size);
  659. void push_outline_size(int p_font_size);
  660. void push_normal();
  661. void push_bold();
  662. void push_bold_italics();
  663. void push_italics();
  664. void push_mono();
  665. void push_color(const Color &p_color);
  666. void push_outline_color(const Color &p_color);
  667. void push_underline(const Color &p_color = Color(0, 0, 0, 0));
  668. void push_strikethrough(const Color &p_color = Color(0, 0, 0, 0));
  669. void push_language(const String &p_language);
  670. void push_paragraph(HorizontalAlignment p_alignment, Control::TextDirection p_direction = Control::TEXT_DIRECTION_INHERITED, const String &p_language = "", TextServer::StructuredTextParser p_st_parser = TextServer::STRUCTURED_TEXT_DEFAULT, BitField<TextServer::JustificationFlag> p_jst_flags = TextServer::JUSTIFICATION_WORD_BOUND | TextServer::JUSTIFICATION_KASHIDA | TextServer::JUSTIFICATION_SKIP_LAST_LINE | TextServer::JUSTIFICATION_DO_NOT_SKIP_SINGLE_LINE, const PackedFloat32Array &p_tab_stops = PackedFloat32Array());
  671. void push_indent(int p_level);
  672. void push_list(int p_level, ListType p_list, bool p_capitalize, const String &p_bullet = String::utf8("•"));
  673. void push_meta(const Variant &p_meta, MetaUnderline p_underline_mode = META_UNDERLINE_ALWAYS, const String &p_tooltip = String());
  674. void push_hint(const String &p_string);
  675. void push_table(int p_columns, InlineAlignment p_alignment = INLINE_ALIGNMENT_TOP, int p_align_to_row = -1, const String &p_name = String());
  676. void push_fade(int p_start_index, int p_length);
  677. void push_shake(int p_strength, float p_rate, bool p_connected);
  678. void push_wave(float p_frequency, float p_amplitude, bool p_connected);
  679. void push_tornado(float p_frequency, float p_radius, bool p_connected);
  680. void push_rainbow(float p_saturation, float p_value, float p_frequency, float p_speed);
  681. void push_pulse(const Color &p_color, float p_frequency, float p_ease);
  682. void push_bgcolor(const Color &p_color);
  683. void push_fgcolor(const Color &p_color);
  684. void push_customfx(Ref<RichTextEffect> p_custom_effect, Dictionary p_environment);
  685. void push_context();
  686. void set_table_column_expand(int p_column, bool p_expand, int p_ratio = 1, bool p_shrink = true);
  687. void set_table_column_name(int p_column, const String &p_name);
  688. void set_cell_row_background_color(const Color &p_odd_row_bg, const Color &p_even_row_bg);
  689. void set_cell_border_color(const Color &p_color);
  690. void set_cell_size_override(const Size2 &p_min_size, const Size2 &p_max_size);
  691. void set_cell_padding(const Rect2 &p_padding);
  692. int get_current_table_column() const;
  693. void push_cell();
  694. void pop();
  695. void pop_context();
  696. void pop_all();
  697. void clear();
  698. void set_offset(int p_pixel);
  699. void set_meta_underline(bool p_underline);
  700. bool is_meta_underlined() const;
  701. void set_hint_underline(bool p_underline);
  702. bool is_hint_underlined() const;
  703. void set_scroll_active(bool p_active);
  704. bool is_scroll_active() const;
  705. void set_scroll_follow(bool p_follow);
  706. bool is_scroll_following() const;
  707. void set_scroll_follow_visible_characters(bool p_follow);
  708. bool is_scroll_following_visible_characters() const;
  709. void set_tab_size(int p_spaces);
  710. int get_tab_size() const;
  711. void set_context_menu_enabled(bool p_enabled);
  712. bool is_context_menu_enabled() const;
  713. void set_shortcut_keys_enabled(bool p_enabled);
  714. bool is_shortcut_keys_enabled() const;
  715. void set_fit_content(bool p_enabled);
  716. bool is_fit_content_enabled() const;
  717. bool search(const String &p_string, bool p_from_selection = false, bool p_search_previous = false);
  718. void scroll_to_paragraph(int p_paragraph);
  719. int get_paragraph_count() const;
  720. int get_visible_paragraph_count() const;
  721. float get_line_offset(int p_line);
  722. float get_paragraph_offset(int p_paragraph);
  723. void scroll_to_line(int p_line);
  724. int get_line_count() const;
  725. Vector2i get_line_range(int p_line);
  726. int get_visible_line_count() const;
  727. int get_content_height() const;
  728. int get_content_width() const;
  729. int get_line_height(int p_line) const;
  730. int get_line_width(int p_line) const;
  731. Rect2i get_visible_content_rect() const;
  732. void scroll_to_selection();
  733. VScrollBar *get_v_scroll_bar() { return vscroll; }
  734. virtual CursorShape get_cursor_shape(const Point2 &p_pos) const override;
  735. virtual Variant get_drag_data(const Point2 &p_point) override;
  736. void set_selection_enabled(bool p_enabled);
  737. bool is_selection_enabled() const;
  738. int get_selection_from() const;
  739. int get_selection_to() const;
  740. float get_selection_line_offset() const;
  741. String get_selected_text() const;
  742. void select_all();
  743. _FORCE_INLINE_ void set_selection_modifier(const Callable &p_modifier) {
  744. selection_modifier = p_modifier;
  745. }
  746. void set_deselect_on_focus_loss_enabled(const bool p_enabled);
  747. bool is_deselect_on_focus_loss_enabled() const;
  748. void set_drag_and_drop_selection_enabled(const bool p_enabled);
  749. bool is_drag_and_drop_selection_enabled() const;
  750. void deselect();
  751. int get_pending_paragraphs() const;
  752. bool is_finished() const;
  753. bool is_updating() const;
  754. void wait_until_finished();
  755. void set_threaded(bool p_threaded);
  756. bool is_threaded() const;
  757. void set_progress_bar_delay(int p_delay_ms);
  758. int get_progress_bar_delay() const;
  759. // Context menu.
  760. PopupMenu *get_menu() const;
  761. bool is_menu_visible() const;
  762. void menu_option(int p_option);
  763. void parse_bbcode(const String &p_bbcode);
  764. void append_text(const String &p_bbcode);
  765. void set_use_bbcode(bool p_enable);
  766. bool is_using_bbcode() const;
  767. void set_text(const String &p_bbcode);
  768. String get_text() const;
  769. void set_horizontal_alignment(HorizontalAlignment p_alignment);
  770. HorizontalAlignment get_horizontal_alignment() const;
  771. void set_vertical_alignment(VerticalAlignment p_alignment);
  772. VerticalAlignment get_vertical_alignment() const;
  773. void set_justification_flags(BitField<TextServer::JustificationFlag> p_flags);
  774. BitField<TextServer::JustificationFlag> get_justification_flags() const;
  775. void set_tab_stops(const PackedFloat32Array &p_tab_stops);
  776. PackedFloat32Array get_tab_stops() const;
  777. void set_text_direction(TextDirection p_text_direction);
  778. TextDirection get_text_direction() const;
  779. void set_language(const String &p_language);
  780. String get_language() const;
  781. void set_autowrap_mode(TextServer::AutowrapMode p_mode);
  782. TextServer::AutowrapMode get_autowrap_mode() const;
  783. void set_autowrap_trim_flags(BitField<TextServer::LineBreakFlag> p_flags);
  784. BitField<TextServer::LineBreakFlag> get_autowrap_trim_flags() const;
  785. void set_structured_text_bidi_override(TextServer::StructuredTextParser p_parser);
  786. TextServer::StructuredTextParser get_structured_text_bidi_override() const;
  787. void set_structured_text_bidi_override_options(Array p_args);
  788. Array get_structured_text_bidi_override_options() const;
  789. void set_visible_characters(int p_visible);
  790. int get_visible_characters() const;
  791. int get_character_line(int p_char);
  792. int get_character_paragraph(int p_char);
  793. int get_total_character_count() const;
  794. int get_total_glyph_count() const;
  795. void set_visible_ratio(float p_ratio);
  796. float get_visible_ratio() const;
  797. TextServer::VisibleCharactersBehavior get_visible_characters_behavior() const;
  798. void set_visible_characters_behavior(TextServer::VisibleCharactersBehavior p_behavior);
  799. void set_effects(Array p_effects);
  800. Array get_effects();
  801. void install_effect(const Variant effect);
  802. void reload_effects();
  803. virtual Size2 get_minimum_size() const override;
  804. RichTextLabel(const String &p_text = String());
  805. ~RichTextLabel();
  806. };
  807. VARIANT_ENUM_CAST(RichTextLabel::ListType);
  808. VARIANT_ENUM_CAST(RichTextLabel::MenuItems);
  809. VARIANT_ENUM_CAST(RichTextLabel::MetaUnderline);
  810. VARIANT_BITFIELD_CAST(RichTextLabel::ImageUpdateMask);