rich_text_label.h 8.7 KB

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