rich_text_label.h
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 
32 
33 #include "scene/gui/scroll_bar.h"
34 
35 class RichTextLabel : public Control {
36 
37  OBJ_TYPE( RichTextLabel, Control );
38 public:
39 
40  enum Align {
41 
42  ALIGN_LEFT,
43  ALIGN_CENTER,
44  ALIGN_RIGHT,
45  ALIGN_FILL
46  };
47 
48  enum ListType {
49 
50  LIST_NUMBERS,
51  LIST_LETTERS,
52  LIST_DOTS
53  };
54 
55  enum ItemType {
56 
57  ITEM_FRAME,
58  ITEM_TEXT,
59  ITEM_IMAGE,
60  ITEM_NEWLINE,
61  ITEM_FONT,
62  ITEM_COLOR,
63  ITEM_UNDERLINE,
64  ITEM_ALIGN,
65  ITEM_INDENT,
66  ITEM_LIST,
67  ITEM_TABLE,
68  ITEM_META
69  };
70 
71 protected:
72 
73  static void _bind_methods();
74 private:
75 
76  struct Item;
77 
78  struct Line {
79 
80  Item *from;
81  Vector<int> offset_caches;
82  Vector<int> height_caches;
83  Vector<int> space_caches;
84  int height_cache;
85  int height_accum_cache;
86  int char_count;
87  int minimum_width;
88 
89  Line() { from=NULL; char_count=0; }
90  };
91 
92 
93 
94  struct Item {
95 
96  int index;
97  Item *parent;
98  ItemType type;
99  List<Item*> subitems;
101  int line;
102 
103  void _clear_children() { while (subitems.size()) { memdelete(subitems.front()->get()); subitems.pop_front(); } }
104 
105  Item() { parent=NULL; E=NULL; line=0;}
106  virtual ~Item() { _clear_children(); }
107  };
108 
109  struct ItemFrame : public Item{
110 
111  int parent_line;
112  bool cell;
113  Vector<Line> lines;
114  int first_invalid_line;
115  ItemFrame *parent_frame;
116 
117  ItemFrame() { type=ITEM_FRAME; parent_frame=NULL; cell=false; parent_line=0; }
118  };
119 
120 
121  struct ItemText : public Item {
122 
123  String text;
124  ItemText() { type=ITEM_TEXT; }
125  };
126 
127  struct ItemImage : public Item {
128 
129  Ref<Texture> image;
130  ItemImage() { type=ITEM_IMAGE; }
131  };
132 
133  struct ItemFont : public Item {
134 
135  Ref<Font> font;
136  ItemFont() { type=ITEM_FONT; }
137  };
138 
139  struct ItemColor : public Item {
140 
141  Color color;
142  ItemColor() { type=ITEM_COLOR; }
143  };
144 
145  struct ItemUnderline : public Item {
146 
147  ItemUnderline() { type=ITEM_UNDERLINE; }
148  };
149 
150  struct ItemMeta : public Item {
151 
152  Variant meta;
153  ItemMeta() { type=ITEM_META; }
154  };
155 
156  struct ItemAlign : public Item {
157 
158  Align align;
159  ItemAlign() { type=ITEM_ALIGN; }
160  };
161 
162  struct ItemIndent : public Item {
163 
164  int level;
165  ItemIndent() { type=ITEM_INDENT; }
166  };
167 
168  struct ItemList : public Item {
169 
170  ListType list_type;
171  ItemList() { type=ITEM_LIST; }
172  };
173 
174  struct ItemNewline : public Item {
175 
176  int line;
177  ItemNewline() { type=ITEM_NEWLINE; }
178  };
179 
180 
181  struct ItemTable : public Item{
182 
183  struct Column {
184  bool expand;
185  int expand_ratio;
186  int min_width;
187  int width;
188  };
189 
190  Vector<Column> columns;
191  int total_width;
192  ItemTable() { type=ITEM_TABLE; }
193  };
194 
195  ItemFrame *main;
196  Item *current;
197  ItemFrame *current_frame;
198 
199  VScrollBar *vscroll;
200 
201 
202  bool scroll_visible;
203  bool scroll_follow;
204  bool scroll_following;
205  bool scroll_active;
206  int scroll_w;
207  bool updating_scroll;
208  int current_idx;
209 
210 
211  int tab_size;
212  bool underline_meta;
213 
214  Align default_align;
215 
216  void _invalidate_current_line(ItemFrame *p_frame);
217  void _validate_line_caches(ItemFrame *p_frame);
218 
219  void _add_item(Item *p_item, bool p_enter=false,bool p_ensure_newline=false);
220 
221 
222 
223 
224  struct ProcessState {
225 
226  int line_width;
227  };
228 
229  enum ProcessMode {
230 
231  PROCESS_CACHE,
232  PROCESS_DRAW,
233  PROCESS_POINTER
234  };
235 
236  struct Selection {
237 
238  Item *click;
239  int click_char;
240 
241  Item *from;
242  int from_char;
243  Item *to;
244  int to_char;
245 
246  bool active;
247  bool enabled;
248  };
249 
250  Selection selection;
251 
252 
253  int visible_characters;
254 
255 
256  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);
257  void _find_click(ItemFrame *p_frame, const Point2i& p_click,Item **r_click_item=NULL,int *r_click_char=NULL,bool *r_outside=NULL);
258 
259 
260  Ref<Font> _find_font(Item *p_item);
261  int _find_margin(Item *p_item,const Ref<Font>& p_base_font);
262  Align _find_align(Item *p_item);
263  Color _find_color(Item *p_item,const Color& p_default_color);
264  bool _find_underline(Item *p_item);
265  bool _find_meta(Item *p_item,Variant *r_meta);
266 
267  void _update_scroll();
268  void _scroll_changed(double);
269 
270  void _input_event(InputEvent p_event);
271  Item *_get_next_item(Item* p_item, bool p_free=false);
272 
273  bool use_bbcode;
274  String bbcode;
275 
276  void _update_all_lines();
277 
278 protected:
279  void _notification(int p_what);
280 
281 public:
282 
283  void add_text(const String& p_text);
284  void add_image(const Ref<Texture>& p_image);
285  void add_newline();
286  void push_font(const Ref<Font>& p_font);
287  void push_color(const Color& p_color);
288  void push_underline();
289  void push_align(Align p_align);
290  void push_indent(int p_level);
291  void push_list(ListType p_list);
292  void push_meta(const Variant& p_data);
293  void push_table(int p_columns);
294  void set_table_column_expand(int p_column, bool p_expand, int p_ratio=1);
295  int get_current_table_column() const;
296  void push_cell();
297  void pop();
298 
299  void clear();
300 
301  void set_offset(int p_pixel);
302 
303  void set_meta_underline(bool p_underline);
304  bool is_meta_underlined() const;
305 
306  void set_scroll_active(bool p_active);
307  bool is_scroll_active() const;
308 
309  void set_scroll_follow(bool p_follow);
310  bool is_scroll_following() const;
311 
312  void set_tab_size(int p_spaces);
313  int get_tab_size() const;
314 
315 
316 
317  bool search(const String& p_string,bool p_from_selection=false);
318 
319  void scroll_to_line(int p_line);
320  int get_line_count() const;
321 
322  VScrollBar *get_v_scroll() { return vscroll; }
323 
324  virtual CursorShape get_cursor_shape(const Point2& p_pos) const;
325 
326  void set_selection_enabled(bool p_enabled);
327  bool is_selection_enabled() const;
328  void selection_copy();
329 
330 
331  Error parse_bbcode(const String& p_bbcode);
332  Error append_bbcode(const String& p_bbcode);
333 
334  void set_use_bbcode(bool p_enable);
335  bool is_using_bbcode() const;
336 
337  void set_bbcode(const String& p_bbcode);
338  String get_bbcode() const;
339 
340  void set_visible_characters(int p_visible);
341  int get_visible_characters() const;
342  int get_total_character_count() const;
343 
344  RichTextLabel();
345  ~RichTextLabel();
346 };
347 
348 VARIANT_ENUM_CAST( RichTextLabel::Align );
349 VARIANT_ENUM_CAST( RichTextLabel::ListType );
350 VARIANT_ENUM_CAST( RichTextLabel::ItemType );
351 
352 #endif // RICH_TEXT_LABEL_H
Definition: variant.h:74
Definition: math_2d.h:369
_FORCE_INLINE_ const Element * front() const
Definition: list.h:189
Definition: color.h:37
Definition: scroll_bar.h:118
Definition: input_event.h:263
Definition: rich_text_label.h:35
Definition: math_2d.h:65
Definition: rich_text_label.h:183
Definition: control.h:47
Definition: ustring.h:64