rich_text_label.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. /*************************************************************************/
  2. /* rich_text_label.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2016 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #ifndef RICH_TEXT_LABEL_H
  30. #define RICH_TEXT_LABEL_H
  31. #include "scene/gui/scroll_bar.h"
  32. class RichTextLabel : public Control {
  33. OBJ_TYPE( RichTextLabel, Control );
  34. public:
  35. enum Align {
  36. ALIGN_LEFT,
  37. ALIGN_CENTER,
  38. ALIGN_RIGHT,
  39. ALIGN_FILL
  40. };
  41. enum ListType {
  42. LIST_NUMBERS,
  43. LIST_LETTERS,
  44. LIST_DOTS
  45. };
  46. enum ItemType {
  47. ITEM_FRAME,
  48. ITEM_TEXT,
  49. ITEM_IMAGE,
  50. ITEM_NEWLINE,
  51. ITEM_FONT,
  52. ITEM_COLOR,
  53. ITEM_UNDERLINE,
  54. ITEM_ALIGN,
  55. ITEM_INDENT,
  56. ITEM_LIST,
  57. ITEM_TABLE,
  58. ITEM_META
  59. };
  60. protected:
  61. static void _bind_methods();
  62. private:
  63. struct Item;
  64. struct Line {
  65. Item *from;
  66. Vector<int> offset_caches;
  67. Vector<int> height_caches;
  68. Vector<int> space_caches;
  69. int height_cache;
  70. int height_accum_cache;
  71. int char_count;
  72. int minimum_width;
  73. Line() { from=NULL; char_count=0; }
  74. };
  75. struct Item {
  76. int index;
  77. Item *parent;
  78. ItemType type;
  79. List<Item*> subitems;
  80. List<Item*>::Element *E;
  81. int line;
  82. void _clear_children() { while (subitems.size()) { memdelete(subitems.front()->get()); subitems.pop_front(); } }
  83. Item() { parent=NULL; E=NULL; line=0;}
  84. virtual ~Item() { _clear_children(); }
  85. };
  86. struct ItemFrame : public Item{
  87. int parent_line;
  88. bool cell;
  89. Vector<Line> lines;
  90. int first_invalid_line;
  91. ItemFrame *parent_frame;
  92. ItemFrame() { type=ITEM_FRAME; parent_frame=NULL; cell=false; parent_line=0; }
  93. };
  94. struct ItemText : public Item {
  95. String text;
  96. ItemText() { type=ITEM_TEXT; }
  97. };
  98. struct ItemImage : public Item {
  99. Ref<Texture> image;
  100. ItemImage() { type=ITEM_IMAGE; }
  101. };
  102. struct ItemFont : public Item {
  103. Ref<Font> font;
  104. ItemFont() { type=ITEM_FONT; }
  105. };
  106. struct ItemColor : public Item {
  107. Color color;
  108. ItemColor() { type=ITEM_COLOR; }
  109. };
  110. struct ItemUnderline : public Item {
  111. ItemUnderline() { type=ITEM_UNDERLINE; }
  112. };
  113. struct ItemMeta : public Item {
  114. Variant meta;
  115. ItemMeta() { type=ITEM_META; }
  116. };
  117. struct ItemAlign : public Item {
  118. Align align;
  119. ItemAlign() { type=ITEM_ALIGN; }
  120. };
  121. struct ItemIndent : public Item {
  122. int level;
  123. ItemIndent() { type=ITEM_INDENT; }
  124. };
  125. struct ItemList : public Item {
  126. ListType list_type;
  127. ItemList() { type=ITEM_LIST; }
  128. };
  129. struct ItemNewline : public Item {
  130. int line;
  131. ItemNewline() { type=ITEM_NEWLINE; }
  132. };
  133. struct ItemTable : public Item{
  134. struct Column {
  135. bool expand;
  136. int expand_ratio;
  137. int min_width;
  138. int width;
  139. };
  140. Vector<Column> columns;
  141. int total_width;
  142. ItemTable() { type=ITEM_TABLE; }
  143. };
  144. ItemFrame *main;
  145. Item *current;
  146. ItemFrame *current_frame;
  147. VScrollBar *vscroll;
  148. bool scroll_visible;
  149. bool scroll_follow;
  150. bool scroll_following;
  151. bool scroll_active;
  152. int scroll_w;
  153. bool updating_scroll;
  154. int current_idx;
  155. int tab_size;
  156. bool underline_meta;
  157. Align default_align;
  158. void _invalidate_current_line(ItemFrame *p_frame);
  159. void _validate_line_caches(ItemFrame *p_frame);
  160. void _add_item(Item *p_item, bool p_enter=false,bool p_ensure_newline=false);
  161. struct ProcessState {
  162. int line_width;
  163. };
  164. enum ProcessMode {
  165. PROCESS_CACHE,
  166. PROCESS_DRAW,
  167. PROCESS_POINTER
  168. };
  169. struct Selection {
  170. Item *click;
  171. int click_char;
  172. Item *from;
  173. int from_char;
  174. Item *to;
  175. int to_char;
  176. bool active;
  177. bool enabled;
  178. };
  179. Selection selection;
  180. int visible_characters;
  181. void _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);
  182. void _find_click(ItemFrame *p_frame, const Point2i& p_click,Item **r_click_item=NULL,int *r_click_char=NULL,bool *r_outside=NULL);
  183. Ref<Font> _find_font(Item *p_item);
  184. int _find_margin(Item *p_item,const Ref<Font>& p_base_font);
  185. Align _find_align(Item *p_item);
  186. Color _find_color(Item *p_item,const Color& p_default_color);
  187. bool _find_underline(Item *p_item);
  188. bool _find_meta(Item *p_item,Variant *r_meta);
  189. void _update_scroll();
  190. void _scroll_changed(double);
  191. void _input_event(InputEvent p_event);
  192. Item *_get_next_item(Item* p_item, bool p_free=false);
  193. bool use_bbcode;
  194. String bbcode;
  195. void _update_all_lines();
  196. protected:
  197. void _notification(int p_what);
  198. public:
  199. void add_text(const String& p_text);
  200. void add_image(const Ref<Texture>& p_image);
  201. void add_newline();
  202. void push_font(const Ref<Font>& p_font);
  203. void push_color(const Color& p_color);
  204. void push_underline();
  205. void push_align(Align p_align);
  206. void push_indent(int p_level);
  207. void push_list(ListType p_list);
  208. void push_meta(const Variant& p_data);
  209. void push_table(int p_columns);
  210. void set_table_column_expand(int p_column, bool p_expand, int p_ratio=1);
  211. int get_current_table_column() const;
  212. void push_cell();
  213. void pop();
  214. void clear();
  215. void set_offset(int p_pixel);
  216. void set_meta_underline(bool p_underline);
  217. bool is_meta_underlined() const;
  218. void set_scroll_active(bool p_active);
  219. bool is_scroll_active() const;
  220. void set_scroll_follow(bool p_follow);
  221. bool is_scroll_following() const;
  222. void set_tab_size(int p_spaces);
  223. int get_tab_size() const;
  224. bool search(const String& p_string,bool p_from_selection=false);
  225. void scroll_to_line(int p_line);
  226. int get_line_count() const;
  227. VScrollBar *get_v_scroll() { return vscroll; }
  228. virtual CursorShape get_cursor_shape(const Point2& p_pos) const;
  229. void set_selection_enabled(bool p_enabled);
  230. bool is_selection_enabled() const;
  231. void selection_copy();
  232. Error parse_bbcode(const String& p_bbcode);
  233. Error append_bbcode(const String& p_bbcode);
  234. void set_use_bbcode(bool p_enable);
  235. bool is_using_bbcode() const;
  236. void set_bbcode(const String& p_bbcode);
  237. String get_bbcode() const;
  238. void set_visible_characters(int p_visible);
  239. int get_visible_characters() const;
  240. int get_total_character_count() const;
  241. RichTextLabel();
  242. ~RichTextLabel();
  243. };
  244. VARIANT_ENUM_CAST( RichTextLabel::Align );
  245. VARIANT_ENUM_CAST( RichTextLabel::ListType );
  246. VARIANT_ENUM_CAST( RichTextLabel::ItemType );
  247. #endif // RICH_TEXT_LABEL_H