rich_text_label.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  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. #ifndef RICH_TEXT_LABEL_H
  31. #define RICH_TEXT_LABEL_H
  32. #include "rich_text_effect.h"
  33. #include "scene/gui/scroll_bar.h"
  34. class RichTextLabel : public Control {
  35. GDCLASS(RichTextLabel, Control);
  36. public:
  37. enum Align {
  38. ALIGN_LEFT,
  39. ALIGN_CENTER,
  40. ALIGN_RIGHT,
  41. ALIGN_FILL
  42. };
  43. enum InlineAlign {
  44. INLINE_ALIGN_TOP,
  45. INLINE_ALIGN_CENTER,
  46. INLINE_ALIGN_BASELINE,
  47. INLINE_ALIGN_BOTTOM
  48. };
  49. enum ListType {
  50. LIST_NUMBERS,
  51. LIST_LETTERS,
  52. LIST_DOTS
  53. };
  54. enum ItemType {
  55. ITEM_FRAME,
  56. ITEM_TEXT,
  57. ITEM_IMAGE,
  58. ITEM_NEWLINE,
  59. ITEM_FONT,
  60. ITEM_COLOR,
  61. ITEM_UNDERLINE,
  62. ITEM_STRIKETHROUGH,
  63. ITEM_ALIGN,
  64. ITEM_INDENT,
  65. ITEM_LIST,
  66. ITEM_TABLE,
  67. ITEM_FADE,
  68. ITEM_SHAKE,
  69. ITEM_WAVE,
  70. ITEM_TORNADO,
  71. ITEM_RAINBOW,
  72. ITEM_META,
  73. ITEM_CUSTOMFX
  74. };
  75. protected:
  76. static void _bind_methods();
  77. private:
  78. struct Item;
  79. struct Line {
  80. Item *from;
  81. Vector<int> offset_caches;
  82. Vector<int> height_caches;
  83. Vector<int> ascent_caches;
  84. Vector<int> descent_caches;
  85. Vector<int> space_caches;
  86. int height_cache;
  87. int height_accum_cache;
  88. int char_count;
  89. int minimum_width;
  90. int maximum_width;
  91. Line() {
  92. from = nullptr;
  93. char_count = 0;
  94. }
  95. };
  96. struct Item {
  97. int index;
  98. Item *parent;
  99. ItemType type;
  100. List<Item *> subitems;
  101. List<Item *>::Element *E;
  102. int line;
  103. void _clear_children() {
  104. while (subitems.size()) {
  105. memdelete(subitems.front()->get());
  106. subitems.pop_front();
  107. }
  108. }
  109. Item() {
  110. parent = nullptr;
  111. E = nullptr;
  112. line = 0;
  113. }
  114. virtual ~Item() { _clear_children(); }
  115. };
  116. struct ItemFrame : public Item {
  117. int parent_line;
  118. bool cell;
  119. Vector<Line> lines;
  120. int first_invalid_line;
  121. ItemFrame *parent_frame;
  122. ItemFrame() {
  123. type = ITEM_FRAME;
  124. parent_frame = nullptr;
  125. cell = false;
  126. parent_line = 0;
  127. }
  128. };
  129. struct ItemText : public Item {
  130. String text;
  131. ItemText() { type = ITEM_TEXT; }
  132. };
  133. struct ItemImage : public Item {
  134. Ref<Texture> image;
  135. Size2 size;
  136. InlineAlign align;
  137. ItemImage() {
  138. type = ITEM_IMAGE;
  139. align = INLINE_ALIGN_BASELINE;
  140. }
  141. };
  142. struct ItemFont : public Item {
  143. Ref<Font> font;
  144. ItemFont() { type = ITEM_FONT; }
  145. };
  146. struct ItemColor : public Item {
  147. Color color;
  148. ItemColor() { type = ITEM_COLOR; }
  149. };
  150. struct ItemUnderline : public Item {
  151. ItemUnderline() { type = ITEM_UNDERLINE; }
  152. };
  153. struct ItemStrikethrough : public Item {
  154. ItemStrikethrough() { type = ITEM_STRIKETHROUGH; }
  155. };
  156. struct ItemMeta : public Item {
  157. Variant meta;
  158. ItemMeta() { type = ITEM_META; }
  159. };
  160. struct ItemAlign : public Item {
  161. Align align;
  162. ItemAlign() { type = ITEM_ALIGN; }
  163. };
  164. struct ItemIndent : public Item {
  165. int level;
  166. ItemIndent() { type = ITEM_INDENT; }
  167. };
  168. struct ItemList : public Item {
  169. ListType list_type;
  170. ItemList() { type = ITEM_LIST; }
  171. };
  172. struct ItemNewline : public Item {
  173. ItemNewline() { type = ITEM_NEWLINE; }
  174. };
  175. struct ItemTable : public Item {
  176. struct Column {
  177. bool expand;
  178. int expand_ratio;
  179. int min_width;
  180. int max_width;
  181. int width;
  182. };
  183. Vector<Column> columns;
  184. int total_width;
  185. ItemTable() { type = ITEM_TABLE; }
  186. };
  187. struct ItemFade : public Item {
  188. int starting_index;
  189. int length;
  190. ItemFade() { type = ITEM_FADE; }
  191. };
  192. struct ItemFX : public Item {
  193. float elapsed_time;
  194. ItemFX() {
  195. elapsed_time = 0.0f;
  196. }
  197. };
  198. struct ItemShake : public ItemFX {
  199. int strength;
  200. float rate;
  201. uint64_t _current_rng;
  202. uint64_t _previous_rng;
  203. ItemShake() {
  204. strength = 0;
  205. rate = 0.0f;
  206. _current_rng = 0;
  207. type = ITEM_SHAKE;
  208. }
  209. void reroll_random() {
  210. _previous_rng = _current_rng;
  211. _current_rng = Math::rand();
  212. }
  213. uint64_t offset_random(int index) {
  214. return (_current_rng >> (index % 64)) |
  215. (_current_rng << (64 - (index % 64)));
  216. }
  217. uint64_t offset_previous_random(int index) {
  218. return (_previous_rng >> (index % 64)) |
  219. (_previous_rng << (64 - (index % 64)));
  220. }
  221. };
  222. struct ItemWave : public ItemFX {
  223. float frequency;
  224. float amplitude;
  225. ItemWave() {
  226. frequency = 1.0f;
  227. amplitude = 1.0f;
  228. type = ITEM_WAVE;
  229. }
  230. };
  231. struct ItemTornado : public ItemFX {
  232. float radius;
  233. float frequency;
  234. ItemTornado() {
  235. radius = 1.0f;
  236. frequency = 1.0f;
  237. type = ITEM_TORNADO;
  238. }
  239. };
  240. struct ItemRainbow : public ItemFX {
  241. float saturation;
  242. float value;
  243. float frequency;
  244. ItemRainbow() {
  245. saturation = 0.8f;
  246. value = 0.8f;
  247. frequency = 1.0f;
  248. type = ITEM_RAINBOW;
  249. }
  250. };
  251. struct ItemCustomFX : public ItemFX {
  252. Ref<CharFXTransform> char_fx_transform;
  253. Ref<RichTextEffect> custom_effect;
  254. ItemCustomFX() {
  255. type = ITEM_CUSTOMFX;
  256. char_fx_transform.instance();
  257. }
  258. virtual ~ItemCustomFX() {
  259. _clear_children();
  260. char_fx_transform.unref();
  261. custom_effect.unref();
  262. }
  263. };
  264. ItemFrame *main;
  265. Item *current;
  266. ItemFrame *current_frame;
  267. VScrollBar *vscroll;
  268. bool scroll_visible;
  269. bool scroll_follow;
  270. bool scroll_following;
  271. bool scroll_active;
  272. int scroll_w;
  273. bool scroll_updated;
  274. bool updating_scroll;
  275. int current_idx;
  276. int visible_line_count;
  277. int tab_size;
  278. bool underline_meta;
  279. bool override_selected_font_color;
  280. Align default_align;
  281. ItemMeta *meta_hovering;
  282. Variant current_meta;
  283. Vector<Ref<RichTextEffect>> custom_effects;
  284. void _invalidate_current_line(ItemFrame *p_frame);
  285. void _validate_line_caches(ItemFrame *p_frame);
  286. void _add_item(Item *p_item, bool p_enter = false, bool p_ensure_newline = false);
  287. void _remove_item(Item *p_item, const int p_line, const int p_subitem_line);
  288. struct ProcessState {
  289. int line_width;
  290. };
  291. enum ProcessMode {
  292. PROCESS_CACHE,
  293. PROCESS_DRAW,
  294. PROCESS_POINTER
  295. };
  296. struct Selection {
  297. Item *click;
  298. int click_char;
  299. Item *from;
  300. int from_char;
  301. Item *to;
  302. int to_char;
  303. bool active; // anything selected? i.e. from, to, etc. valid?
  304. bool enabled; // allow selections?
  305. bool drag_attempt;
  306. };
  307. Selection selection;
  308. bool deselect_on_focus_loss_enabled;
  309. int visible_characters;
  310. float percent_visible;
  311. bool _is_click_inside_selection() const;
  312. int _process_line(ItemFrame *p_frame, const Vector2 &p_ofs, int &y, int p_width, int p_line, ProcessMode p_mode, const Ref<Font> &p_base_font, const Color &p_base_color, const Color &p_font_color_shadow, bool p_shadow_as_outline, const Point2 &shadow_ofs, const Point2i &p_click_pos = Point2i(), Item **r_click_item = nullptr, int *r_click_char = nullptr, bool *r_outside = nullptr, int p_char_count = 0);
  313. void _find_click(ItemFrame *p_frame, const Point2i &p_click, Item **r_click_item = nullptr, int *r_click_char = nullptr, bool *r_outside = nullptr);
  314. Ref<Font> _find_font(Item *p_item);
  315. int _find_margin(Item *p_item, const Ref<Font> &p_base_font);
  316. Align _find_align(Item *p_item);
  317. Color _find_color(Item *p_item, const Color &p_default_color);
  318. bool _find_underline(Item *p_item);
  319. bool _find_strikethrough(Item *p_item);
  320. bool _find_meta(Item *p_item, Variant *r_meta, ItemMeta **r_item = nullptr);
  321. bool _find_layout_subitem(Item *from, Item *to);
  322. bool _find_by_type(Item *p_item, ItemType p_type);
  323. void _fetch_item_fx_stack(Item *p_item, Vector<ItemFX *> &r_stack);
  324. void _update_scroll();
  325. void _update_fx(ItemFrame *p_frame, float p_delta_time);
  326. void _scroll_changed(double);
  327. void _gui_input(Ref<InputEvent> p_event);
  328. Item *_get_next_item(Item *p_item, bool p_free = false);
  329. Item *_get_prev_item(Item *p_item, bool p_free = false);
  330. Rect2 _get_text_rect();
  331. Ref<RichTextEffect> _get_custom_effect_by_code(String p_bbcode_identifier);
  332. virtual Dictionary parse_expressions_for_values(Vector<String> p_expressions);
  333. bool use_bbcode;
  334. String bbcode;
  335. String cached_text;
  336. int fixed_width;
  337. bool fit_content_height;
  338. protected:
  339. virtual void _validate_property(PropertyInfo &p_property) const;
  340. void _notification(int p_what);
  341. public:
  342. String get_text();
  343. void add_text(const String &p_text);
  344. void add_image(const Ref<Texture> &p_image, const int p_width = 0, const int p_height = 0, RichTextLabel::InlineAlign p_align = INLINE_ALIGN_BASELINE);
  345. void add_newline();
  346. bool remove_line(const int p_line);
  347. void push_font(const Ref<Font> &p_font);
  348. void push_normal();
  349. void push_bold();
  350. void push_bold_italics();
  351. void push_italics();
  352. void push_mono();
  353. void push_color(const Color &p_color);
  354. void push_underline();
  355. void push_strikethrough();
  356. void push_align(Align p_align);
  357. void push_indent(int p_level);
  358. void push_list(ListType p_list);
  359. void push_meta(const Variant &p_meta);
  360. void push_table(int p_columns);
  361. void push_fade(int p_start_index, int p_length);
  362. void push_shake(int p_strength, float p_rate);
  363. void push_wave(float p_frequency, float p_amplitude);
  364. void push_tornado(float p_frequency, float p_radius);
  365. void push_rainbow(float p_saturation, float p_value, float p_frequency);
  366. void push_customfx(Ref<RichTextEffect> p_custom_effect, Dictionary p_environment);
  367. void set_table_column_expand(int p_column, bool p_expand, int p_ratio = 1);
  368. int get_current_table_column() const;
  369. void push_cell();
  370. void pop();
  371. void clear();
  372. void set_offset(int p_pixel);
  373. void set_meta_underline(bool p_underline);
  374. bool is_meta_underlined() const;
  375. void set_override_selected_font_color(bool p_override_selected_font_color);
  376. bool is_overriding_selected_font_color() const;
  377. void set_scroll_active(bool p_active);
  378. bool is_scroll_active() const;
  379. void set_scroll_follow(bool p_follow);
  380. bool is_scroll_following() const;
  381. void set_tab_size(int p_spaces);
  382. int get_tab_size() const;
  383. void set_fit_content_height(bool p_enabled);
  384. bool is_fit_content_height_enabled() const;
  385. bool search(const String &p_string, bool p_from_selection = false, bool p_search_previous = false);
  386. void scroll_to_line(int p_line);
  387. int get_line_count() const;
  388. int get_visible_line_count() const;
  389. int get_content_height() const;
  390. VScrollBar *get_v_scroll() { return vscroll; }
  391. virtual CursorShape get_cursor_shape(const Point2 &p_pos) const;
  392. virtual Variant get_drag_data(const Point2 &p_point);
  393. void set_selection_enabled(bool p_enabled);
  394. bool is_selection_enabled() const;
  395. String get_selected_text();
  396. void selection_copy();
  397. void set_deselect_on_focus_loss_enabled(const bool p_enabled);
  398. bool is_deselect_on_focus_loss_enabled() const;
  399. void deselect();
  400. Error parse_bbcode(const String &p_bbcode);
  401. Error append_bbcode(const String &p_bbcode);
  402. void set_use_bbcode(bool p_enable);
  403. bool is_using_bbcode() const;
  404. void set_bbcode(const String &p_bbcode);
  405. String get_bbcode() const;
  406. void set_text(const String &p_string);
  407. void set_visible_characters(int p_visible);
  408. int get_visible_characters() const;
  409. int get_total_character_count() const;
  410. void set_percent_visible(float p_percent);
  411. float get_percent_visible() const;
  412. void set_effects(const Vector<Variant> &effects);
  413. Vector<Variant> get_effects();
  414. void install_effect(const Variant effect);
  415. void set_fixed_size_to_width(int p_width);
  416. virtual Size2 get_minimum_size() const;
  417. RichTextLabel();
  418. ~RichTextLabel();
  419. };
  420. VARIANT_ENUM_CAST(RichTextLabel::Align);
  421. VARIANT_ENUM_CAST(RichTextLabel::InlineAlign);
  422. VARIANT_ENUM_CAST(RichTextLabel::ListType);
  423. VARIANT_ENUM_CAST(RichTextLabel::ItemType);
  424. #endif // RICH_TEXT_LABEL_H