rich_text_label.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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-2014 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_MAIN,
  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_META
  58. };
  59. protected:
  60. static void _bind_methods();
  61. private:
  62. struct Item {
  63. int index;
  64. Item *parent;
  65. ItemType type;
  66. List<Item*> subitems;
  67. List<Item*>::Element *E;
  68. void _clear_children() { while (subitems.size()) { memdelete(subitems.front()->get()); subitems.pop_front(); } }
  69. Item() { parent=NULL; E=NULL; }
  70. virtual ~Item() { _clear_children(); }
  71. };
  72. struct ItemMain : public Item {
  73. ItemMain() { type=ITEM_MAIN; }
  74. };
  75. struct ItemText : public Item {
  76. String text;
  77. ItemText() { type=ITEM_TEXT; }
  78. };
  79. struct ItemImage : public Item {
  80. Ref<Texture> image;
  81. ItemImage() { type=ITEM_IMAGE; }
  82. };
  83. struct ItemFont : public Item {
  84. Ref<Font> font;
  85. ItemFont() { type=ITEM_FONT; }
  86. };
  87. struct ItemColor : public Item {
  88. Color color;
  89. ItemColor() { type=ITEM_COLOR; }
  90. };
  91. struct ItemUnderline : public Item {
  92. ItemUnderline() { type=ITEM_UNDERLINE; }
  93. };
  94. struct ItemMeta : public Item {
  95. Variant meta;
  96. ItemMeta() { type=ITEM_META; }
  97. };
  98. struct ItemAlign : public Item {
  99. Align align;
  100. ItemAlign() { type=ITEM_ALIGN; }
  101. };
  102. struct ItemIndent : public Item {
  103. int level;
  104. ItemIndent() { type=ITEM_INDENT; }
  105. };
  106. struct ItemList : public Item {
  107. ListType list_type;
  108. ItemList() { type=ITEM_LIST; }
  109. };
  110. struct ItemNewline : public Item {
  111. int line;
  112. ItemNewline() { type=ITEM_NEWLINE; }
  113. };
  114. ItemMain *main;
  115. Item *current;
  116. VScrollBar *vscroll;
  117. bool scroll_visible;
  118. bool scroll_follow;
  119. bool scroll_following;
  120. bool scroll_active;
  121. int scroll_w;
  122. bool updating_scroll;
  123. int current_idx;
  124. struct Line {
  125. Item *from;
  126. Vector<int> offset_caches;
  127. Vector<int> height_caches;
  128. int height_cache;
  129. int height_accum_cache;
  130. Line() { from=NULL; }
  131. };
  132. Vector<Line> lines;
  133. int first_invalid_line;
  134. int tab_size;
  135. bool underline_meta;
  136. Align default_align;
  137. void _invalidate_current_line();
  138. void _validate_line_caches();
  139. void _add_item(Item *p_item, bool p_enter=false);
  140. struct ProcessState {
  141. int line_width;
  142. };
  143. enum ProcessMode {
  144. PROCESS_CACHE,
  145. PROCESS_DRAW,
  146. PROCESS_POINTER
  147. };
  148. struct Selection {
  149. Item *click;
  150. int click_char;
  151. Item *from;
  152. int from_char;
  153. Item *to;
  154. int to_char;
  155. bool active;
  156. bool enabled;
  157. };
  158. Selection selection;
  159. void _process_line(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);
  160. void _find_click(const Point2i& p_click,Item **r_click_item=NULL,int *r_click_char=NULL,bool *r_outside=NULL);
  161. Ref<Font> _find_font(Item *p_item);
  162. int _find_margin(Item *p_item,const Ref<Font>& p_base_font);
  163. Align _find_align(Item *p_item);
  164. Color _find_color(Item *p_item,const Color& p_default_color);
  165. bool _find_underline(Item *p_item);
  166. bool _find_meta(Item *p_item,Variant *r_meta);
  167. void _update_scroll();
  168. void _scroll_changed(double);
  169. void _input_event(InputEvent p_event);
  170. Item *_get_next_item(Item* p_item);
  171. protected:
  172. void _notification(int p_what);
  173. public:
  174. void add_text(const String& p_text);
  175. void add_image(const Ref<Texture>& p_image);
  176. void add_newline();
  177. void push_font(const Ref<Font>& p_font);
  178. void push_color(const Color& p_color);
  179. void push_underline();
  180. void push_align(Align p_align);
  181. void push_indent(int p_level);
  182. void push_list(ListType p_list);
  183. void push_meta(const Variant& p_data);
  184. void pop();
  185. void clear();
  186. void set_offset(int p_pixel);
  187. void set_meta_underline(bool p_underline);
  188. bool is_meta_underlined() const;
  189. void set_scroll_active(bool p_active);
  190. bool is_scroll_active() const;
  191. void set_scroll_follow(bool p_follow);
  192. bool is_scroll_following() const;
  193. void set_tab_size(int p_spaces);
  194. int get_tab_size() const;
  195. bool search(const String& p_string,bool p_from_selection=false);
  196. void scroll_to_line(int p_line);
  197. int get_line_count() const;
  198. VScrollBar *get_v_scroll() { return vscroll; }
  199. virtual CursorShape get_cursor_shape(const Point2& p_pos) const;
  200. void set_selection_enabled(bool p_enabled);
  201. bool is_selection_enabled() const;
  202. void selection_copy();
  203. Error parse_bbcode(const String& p_bbcode);
  204. Error append_bbcode(const String& p_bbcode);
  205. RichTextLabel();
  206. ~RichTextLabel();
  207. };
  208. VARIANT_ENUM_CAST( RichTextLabel::Align );
  209. VARIANT_ENUM_CAST( RichTextLabel::ListType );
  210. VARIANT_ENUM_CAST( RichTextLabel::ItemType );
  211. #endif // RICH_TEXT_LABEL_H