rich_text_label.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. /*************************************************************************/
  2. /* rich_text_label.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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. #ifndef RICH_TEXT_LABEL_H
  31. #define RICH_TEXT_LABEL_H
  32. #include "scene/gui/scroll_bar.h"
  33. class RichTextLabel : public Control {
  34. GDCLASS(RichTextLabel, Control);
  35. public:
  36. enum Align {
  37. ALIGN_LEFT,
  38. ALIGN_CENTER,
  39. ALIGN_RIGHT,
  40. ALIGN_FILL
  41. };
  42. enum ListType {
  43. LIST_NUMBERS,
  44. LIST_LETTERS,
  45. LIST_DOTS
  46. };
  47. enum ItemType {
  48. ITEM_FRAME,
  49. ITEM_TEXT,
  50. ITEM_IMAGE,
  51. ITEM_NEWLINE,
  52. ITEM_FONT,
  53. ITEM_COLOR,
  54. ITEM_UNDERLINE,
  55. ITEM_STRIKETHROUGH,
  56. ITEM_ALIGN,
  57. ITEM_INDENT,
  58. ITEM_LIST,
  59. ITEM_TABLE,
  60. ITEM_META
  61. };
  62. protected:
  63. static void _bind_methods();
  64. private:
  65. struct Item;
  66. struct Line {
  67. Item *from;
  68. Vector<int> offset_caches;
  69. Vector<int> height_caches;
  70. Vector<int> ascent_caches;
  71. Vector<int> descent_caches;
  72. Vector<int> space_caches;
  73. int height_cache;
  74. int height_accum_cache;
  75. int char_count;
  76. int minimum_width;
  77. int maximum_width;
  78. Line() {
  79. from = NULL;
  80. char_count = 0;
  81. }
  82. };
  83. struct Item {
  84. int index;
  85. Item *parent;
  86. ItemType type;
  87. List<Item *> subitems;
  88. List<Item *>::Element *E;
  89. int line;
  90. void _clear_children() {
  91. while (subitems.size()) {
  92. memdelete(subitems.front()->get());
  93. subitems.pop_front();
  94. }
  95. }
  96. Item() {
  97. parent = NULL;
  98. E = NULL;
  99. line = 0;
  100. }
  101. virtual ~Item() { _clear_children(); }
  102. };
  103. struct ItemFrame : public Item {
  104. int parent_line;
  105. bool cell;
  106. Vector<Line> lines;
  107. int first_invalid_line;
  108. ItemFrame *parent_frame;
  109. ItemFrame() {
  110. type = ITEM_FRAME;
  111. parent_frame = NULL;
  112. cell = false;
  113. parent_line = 0;
  114. }
  115. };
  116. struct ItemText : public Item {
  117. String text;
  118. ItemText() { type = ITEM_TEXT; }
  119. };
  120. struct ItemImage : public Item {
  121. Ref<Texture> image;
  122. ItemImage() { type = ITEM_IMAGE; }
  123. };
  124. struct ItemFont : public Item {
  125. Ref<Font> font;
  126. ItemFont() { type = ITEM_FONT; }
  127. };
  128. struct ItemColor : public Item {
  129. Color color;
  130. ItemColor() { type = ITEM_COLOR; }
  131. };
  132. struct ItemUnderline : public Item {
  133. ItemUnderline() { type = ITEM_UNDERLINE; }
  134. };
  135. struct ItemStrikethrough : public Item {
  136. ItemStrikethrough() { type = ITEM_STRIKETHROUGH; }
  137. };
  138. struct ItemMeta : public Item {
  139. Variant meta;
  140. ItemMeta() { type = ITEM_META; }
  141. };
  142. struct ItemAlign : public Item {
  143. Align align;
  144. ItemAlign() { type = ITEM_ALIGN; }
  145. };
  146. struct ItemIndent : public Item {
  147. int level;
  148. ItemIndent() { type = ITEM_INDENT; }
  149. };
  150. struct ItemList : public Item {
  151. ListType list_type;
  152. ItemList() { type = ITEM_LIST; }
  153. };
  154. struct ItemNewline : public Item {
  155. ItemNewline() { type = ITEM_NEWLINE; }
  156. };
  157. struct ItemTable : public Item {
  158. struct Column {
  159. bool expand;
  160. int expand_ratio;
  161. int min_width;
  162. int max_width;
  163. int width;
  164. };
  165. Vector<Column> columns;
  166. int total_width;
  167. ItemTable() { type = ITEM_TABLE; }
  168. };
  169. ItemFrame *main;
  170. Item *current;
  171. ItemFrame *current_frame;
  172. VScrollBar *vscroll;
  173. bool scroll_visible;
  174. bool scroll_follow;
  175. bool scroll_following;
  176. bool scroll_active;
  177. int scroll_w;
  178. bool scroll_updated;
  179. bool updating_scroll;
  180. int current_idx;
  181. int visible_line_count;
  182. int tab_size;
  183. bool underline_meta;
  184. bool override_selected_font_color;
  185. Align default_align;
  186. ItemMeta *meta_hovering;
  187. Variant current_meta;
  188. void _invalidate_current_line(ItemFrame *p_frame);
  189. void _validate_line_caches(ItemFrame *p_frame);
  190. void _add_item(Item *p_item, bool p_enter = false, bool p_ensure_newline = false);
  191. void _remove_item(Item *p_item, const int p_line, const int p_subitem_line);
  192. struct ProcessState {
  193. int line_width;
  194. };
  195. enum ProcessMode {
  196. PROCESS_CACHE,
  197. PROCESS_DRAW,
  198. PROCESS_POINTER
  199. };
  200. struct Selection {
  201. Item *click;
  202. int click_char;
  203. Item *from;
  204. int from_char;
  205. Item *to;
  206. int to_char;
  207. bool active; // anything selected? i.e. from, to, etc. valid?
  208. bool enabled; // allow selections?
  209. };
  210. Selection selection;
  211. int visible_characters;
  212. float percent_visible;
  213. 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 = NULL, int *r_click_char = NULL, bool *r_outside = NULL, int p_char_count = 0);
  214. void _find_click(ItemFrame *p_frame, const Point2i &p_click, Item **r_click_item = NULL, int *r_click_char = NULL, bool *r_outside = NULL);
  215. Ref<Font> _find_font(Item *p_item);
  216. int _find_margin(Item *p_item, const Ref<Font> &p_base_font);
  217. Align _find_align(Item *p_item);
  218. Color _find_color(Item *p_item, const Color &p_default_color);
  219. bool _find_underline(Item *p_item);
  220. bool _find_strikethrough(Item *p_item);
  221. bool _find_meta(Item *p_item, Variant *r_meta);
  222. void _update_scroll();
  223. void _scroll_changed(double);
  224. void _gui_input(Ref<InputEvent> p_event);
  225. Item *_get_next_item(Item *p_item, bool p_free = false);
  226. Item *_get_prev_item(Item *p_item, bool p_free = false);
  227. Rect2 _get_text_rect();
  228. bool use_bbcode;
  229. String bbcode;
  230. void _update_all_lines();
  231. int fixed_width;
  232. protected:
  233. void _notification(int p_what);
  234. public:
  235. String get_text();
  236. void add_text(const String &p_text);
  237. void add_image(const Ref<Texture> &p_image);
  238. void add_newline();
  239. bool remove_line(const int p_line);
  240. void push_font(const Ref<Font> &p_font);
  241. void push_color(const Color &p_color);
  242. void push_underline();
  243. void push_strikethrough();
  244. void push_align(Align p_align);
  245. void push_indent(int p_level);
  246. void push_list(ListType p_list);
  247. void push_meta(const Variant &p_meta);
  248. void push_table(int p_columns);
  249. void set_table_column_expand(int p_column, bool p_expand, int p_ratio = 1);
  250. int get_current_table_column() const;
  251. void push_cell();
  252. void pop();
  253. void clear();
  254. void set_offset(int p_pixel);
  255. void set_meta_underline(bool p_underline);
  256. bool is_meta_underlined() const;
  257. void set_override_selected_font_color(bool p_override_selected_font_color);
  258. bool is_overriding_selected_font_color() const;
  259. void set_scroll_active(bool p_active);
  260. bool is_scroll_active() const;
  261. void set_scroll_follow(bool p_follow);
  262. bool is_scroll_following() const;
  263. void set_tab_size(int p_spaces);
  264. int get_tab_size() const;
  265. bool search(const String &p_string, bool p_from_selection = false, bool p_search_previous = false);
  266. void scroll_to_line(int p_line);
  267. int get_line_count() const;
  268. int get_visible_line_count() const;
  269. int get_content_height();
  270. VScrollBar *get_v_scroll() { return vscroll; }
  271. virtual CursorShape get_cursor_shape(const Point2 &p_pos) const;
  272. void set_selection_enabled(bool p_enabled);
  273. bool is_selection_enabled() const;
  274. void selection_copy();
  275. Error parse_bbcode(const String &p_bbcode);
  276. Error append_bbcode(const String &p_bbcode);
  277. void set_use_bbcode(bool p_enable);
  278. bool is_using_bbcode() const;
  279. void set_bbcode(const String &p_bbcode);
  280. String get_bbcode() const;
  281. void set_text(const String &p_string);
  282. void set_visible_characters(int p_visible);
  283. int get_visible_characters() const;
  284. int get_total_character_count() const;
  285. void set_percent_visible(float p_percent);
  286. float get_percent_visible() const;
  287. void set_fixed_size_to_width(int p_width);
  288. virtual Size2 get_minimum_size() const;
  289. RichTextLabel();
  290. ~RichTextLabel();
  291. };
  292. VARIANT_ENUM_CAST(RichTextLabel::Align);
  293. VARIANT_ENUM_CAST(RichTextLabel::ListType);
  294. VARIANT_ENUM_CAST(RichTextLabel::ItemType);
  295. #endif // RICH_TEXT_LABEL_H