Panda3D
textNode.h
1 // Filename: textNode.h
2 // Created by: drose (13Mar02)
3 //
4 ////////////////////////////////////////////////////////////////////
5 //
6 // PANDA 3D SOFTWARE
7 // Copyright (c) Carnegie Mellon University. All rights reserved.
8 //
9 // All use of this software is subject to the terms of the revised BSD
10 // license. You should have received a copy of this license along
11 // with this source code in a file named "LICENSE."
12 //
13 ////////////////////////////////////////////////////////////////////
14 
15 #ifndef TEXTNODE_H
16 #define TEXTNODE_H
17 
18 #include "pandabase.h"
19 
20 #include "config_text.h"
21 #include "textEncoder.h"
22 #include "textProperties.h"
23 #include "textFont.h"
24 #include "textAssembler.h"
25 #include "pandaNode.h"
26 #include "luse.h"
27 #include "geom.h"
28 
29 ////////////////////////////////////////////////////////////////////
30 // Class : TextNode
31 // Description : The primary interface to this module. This class
32 // does basic text assembly; given a string of text and
33 // a TextFont object, it creates a piece of geometry
34 // that may be placed in the 3-d or 2-d world to
35 // represent the indicated text.
36 //
37 // The TextNode may be used in one of two ways.
38 // Naively, it may simply be parented directly into the
39 // scene graph and rendered as if it were a GeomNode; in
40 // this mode, the actual polygon geometry that renders
41 // the text is not directly visible or accessible, but
42 // remains hidden within the TextNode.
43 //
44 // The second way TextNode may be used is as a text
45 // generator. To use it in this way, do not parent the
46 // TextNode to the scene graph; instead, set the
47 // properties of the text and call generate() to return
48 // an ordinary node, containing ordinary geometry, which
49 // you may use however you like. Each time you call
50 // generate() a new node is returned.
51 ////////////////////////////////////////////////////////////////////
52 class EXPCL_PANDA_TEXT TextNode : public PandaNode, public TextEncoder, public TextProperties {
53 PUBLISHED:
54  TextNode(const string &name);
55  TextNode(const string &name, const TextProperties &copy);
56 protected:
57  TextNode(const TextNode &copy);
58  virtual PandaNode *make_copy() const;
59 
60 PUBLISHED:
61  ~TextNode();
62 
63  enum FlattenFlags {
64  FF_none = 0x0000,
65  FF_light = 0x0001,
66  FF_medium = 0x0002,
67  FF_strong = 0x0004,
68  FF_dynamic_merge = 0x0008,
69  };
70 
71  INLINE PN_stdfloat get_line_height() const;
72 
73  INLINE void set_max_rows(int max_rows);
74  INLINE void clear_max_rows();
75  INLINE bool has_max_rows() const;
76  INLINE int get_max_rows() const;
77  INLINE bool has_overflow() const;
78 
79  INLINE void set_frame_color(PN_stdfloat r, PN_stdfloat g, PN_stdfloat b, PN_stdfloat a);
80  INLINE void set_frame_color(const LColor &frame_color);
81  INLINE LColor get_frame_color() const;
82 
83  INLINE void set_card_border(PN_stdfloat size, PN_stdfloat uv_portion);
84  INLINE void clear_card_border();
85  INLINE PN_stdfloat get_card_border_size() const;
86  INLINE PN_stdfloat get_card_border_uv_portion() const;
87  INLINE bool has_card_border() const;
88 
89  INLINE void set_card_color(PN_stdfloat r, PN_stdfloat g, PN_stdfloat b, PN_stdfloat a);
90  INLINE void set_card_color(const LColor &card_color);
91  INLINE LColor get_card_color() const;
92 
93  INLINE void set_card_texture(Texture *card_texture);
94  INLINE void clear_card_texture();
95  INLINE bool has_card_texture() const;
96  INLINE Texture *get_card_texture() const;
97 
98  INLINE void set_frame_as_margin(PN_stdfloat left, PN_stdfloat right,
99  PN_stdfloat bottom, PN_stdfloat top);
100  INLINE void set_frame_actual(PN_stdfloat left, PN_stdfloat right,
101  PN_stdfloat bottom, PN_stdfloat top);
102  INLINE void clear_frame();
103  INLINE bool has_frame() const;
104  INLINE bool is_frame_as_margin() const;
105  INLINE LVecBase4 get_frame_as_set() const;
106  INLINE LVecBase4 get_frame_actual() const;
107 
108  INLINE void set_frame_line_width(PN_stdfloat line_width);
109  INLINE PN_stdfloat get_frame_line_width() const;
110  INLINE void set_frame_corners(bool corners);
111  INLINE bool get_frame_corners() const;
112 
113  INLINE void set_card_as_margin(PN_stdfloat left, PN_stdfloat right,
114  PN_stdfloat bottom, PN_stdfloat top);
115  INLINE void set_card_actual(PN_stdfloat left, PN_stdfloat right,
116  PN_stdfloat bottom, PN_stdfloat top);
117  INLINE void set_card_decal(bool card_decal);
118  INLINE void clear_card();
119  INLINE bool has_card() const;
120  INLINE bool get_card_decal() const;
121  INLINE bool is_card_as_margin() const;
122  INLINE LVecBase4 get_card_as_set() const;
123  INLINE LVecBase4 get_card_actual() const;
124  INLINE LVecBase4 get_card_transformed() const;
125 
126  INLINE void set_transform(const LMatrix4 &transform);
127  INLINE LMatrix4 get_transform() const;
128 
129  INLINE void set_coordinate_system(CoordinateSystem cs);
130  INLINE CoordinateSystem get_coordinate_system() const;
131 
132  INLINE void set_usage_hint(Geom::UsageHint usage_hint);
133  INLINE Geom::UsageHint get_usage_hint() const;
134 
135  INLINE void set_flatten_flags(int flatten_flags);
136  INLINE int get_flatten_flags() const;
137 
138  // These methods are inherited from TextProperties, but we override
139  // here so we can flag the TextNode as dirty when they have been
140  // changed.
141 
142  INLINE void set_font(TextFont *font);
143  INLINE void clear_font();
144 
145  INLINE void set_small_caps(bool small_caps);
146  INLINE void clear_small_caps();
147 
148  INLINE void set_small_caps_scale(PN_stdfloat small_caps_scale);
149  INLINE void clear_small_caps_scale();
150 
151  INLINE void set_slant(PN_stdfloat slant);
152  INLINE void clear_slant();
153 
154  INLINE void set_align(Alignment align_type);
155  INLINE void clear_align();
156 
157  INLINE void set_indent(PN_stdfloat indent);
158  INLINE void clear_indent();
159 
160  INLINE void set_wordwrap(PN_stdfloat wordwrap);
161  INLINE void clear_wordwrap();
162 
163  INLINE void set_text_color(PN_stdfloat r, PN_stdfloat g, PN_stdfloat b, PN_stdfloat a);
164  INLINE void set_text_color(const LColor &text_color);
165  INLINE void clear_text_color();
166 
167  INLINE void set_shadow_color(PN_stdfloat r, PN_stdfloat g, PN_stdfloat b, PN_stdfloat a);
168  INLINE void set_shadow_color(const LColor &shadow_color);
169  INLINE void clear_shadow_color();
170 
171  INLINE void set_shadow(PN_stdfloat xoffset, PN_stdfloat yoffset);
172  INLINE void set_shadow(const LVecBase2 &shadow_offset);
173  INLINE void clear_shadow();
174 
175  INLINE void set_bin(const string &bin);
176  INLINE void clear_bin();
177 
178  INLINE int set_draw_order(int draw_order);
179  INLINE void clear_draw_order();
180 
181  INLINE void set_tab_width(PN_stdfloat tab_width);
182  INLINE void clear_tab_width();
183 
184  INLINE void set_glyph_scale(PN_stdfloat glyph_scale);
185  INLINE void clear_glyph_scale();
186 
187  INLINE void set_glyph_shift(PN_stdfloat glyph_shift);
188  INLINE void clear_glyph_shift();
189 
190  // These methods are inherited from TextEncoder, but we override
191  // here so we can flag the TextNode as dirty when they have been
192  // changed.
193  INLINE void set_text(const string &text);
194  INLINE void set_text(const string &text, Encoding encoding);
195  INLINE void clear_text();
196  INLINE void append_text(const string &text);
197  INLINE void append_unicode_char(wchar_t character);
198 
199  // After the text has been set, you can query this to determine how
200  // it will be wordwrapped.
201  INLINE string get_wordwrapped_text() const;
202 
203  // These methods calculate the width of a single character or a line
204  // of text in the current font.
205  PN_stdfloat calc_width(wchar_t character) const;
206  INLINE PN_stdfloat calc_width(const string &line) const;
207 
208  bool has_exact_character(wchar_t character) const;
209  bool has_character(wchar_t character) const;
210  bool is_whitespace(wchar_t character) const;
211 
212  // Direct support for wide-character strings.
213  INLINE void set_wtext(const wstring &wtext);
214  INLINE void append_wtext(const wstring &text);
215 
216  INLINE wstring get_wordwrapped_wtext() const;
217  PN_stdfloat calc_width(const wstring &line) const;
218 
219  virtual void output(ostream &out) const;
220  virtual void write(ostream &out, int indent_level = 0) const;
221 
222  // The following functions return information about the text that
223  // was last built (and is currently visible).
224  INLINE PN_stdfloat get_left() const;
225  INLINE PN_stdfloat get_right() const;
226  INLINE PN_stdfloat get_bottom() const;
227  INLINE PN_stdfloat get_top() const;
228  INLINE PN_stdfloat get_height() const;
229  INLINE PN_stdfloat get_width() const;
230 
231  INLINE LPoint3 get_upper_left_3d() const;
232  INLINE LPoint3 get_lower_right_3d() const;
233 
234  INLINE int get_num_rows() const;
235 
236  PT(PandaNode) generate();
237  INLINE void update();
238  INLINE void force_update();
239 
240  PandaNode *get_internal_geom() const;
241 
242 public:
243  // From parent class PandaNode
244  virtual int get_unsafe_to_apply_attribs() const;
245  virtual void apply_attribs_to_vertices(const AccumulatedAttribs &attribs,
246  int attrib_types,
247  GeomTransformer &transformer);
248  virtual CPT(TransformState)
249  calc_tight_bounds(LPoint3 &min_point, LPoint3 &max_point,
250  bool &found_any,
251  const TransformState *transform,
252  Thread *current_thread) const;
253 
254  virtual bool cull_callback(CullTraverser *trav, CullTraverserData &data);
255  virtual bool is_renderable() const;
256 
257  virtual void compute_internal_bounds(CPT(BoundingVolume) &internal_bounds,
258  int &internal_vertices,
259  int pipeline_stage,
260  Thread *current_thread) const;
261 
262  virtual void r_prepare_scene(GraphicsStateGuardianBase *gsg,
263  const RenderState *node_state,
264  GeomTransformer &transformer,
265  Thread *current_thread);
266 
267 private:
268  INLINE void invalidate_no_measure();
269  INLINE void invalidate_with_measure();
270  INLINE void check_rebuild() const;
271  INLINE void check_measure() const;
272 
273  void do_rebuild();
274  void do_measure();
275 
276  PT(PandaNode) make_frame();
277  PT(PandaNode) make_card();
278  PT(PandaNode) make_card_with_border();
279 
280  static int count_geoms(PandaNode *node);
281 
282  PT(PandaNode) _internal_geom;
283 
284  PT(Texture) _card_texture;
285  LColor _frame_color;
286  LColor _card_color;
287 
288  enum Flags {
289  F_has_frame = 0x0001,
290  F_frame_as_margin = 0x0002,
291  F_has_card = 0x0004,
292  F_card_as_margin = 0x0008,
293  F_has_card_texture = 0x0010,
294  F_frame_corners = 0x0020,
295  F_card_transp = 0x0040,
296  F_has_card_border = 0x0080,
297  F_needs_rebuild = 0x0100,
298  F_needs_measure = 0x0200,
299  F_has_overflow = 0x0400,
300  F_card_decal = 0x0800,
301  };
302 
303  int _flags;
304  int _max_rows;
305  GeomEnums::UsageHint _usage_hint;
306  int _flatten_flags;
307  bool _dynamic_merge;
308  PN_stdfloat _frame_width;
309  PN_stdfloat _card_border_size;
310  PN_stdfloat _card_border_uv_portion;
311 
312  LVector2 _frame_ul, _frame_lr;
313  LVector2 _card_ul, _card_lr;
314 
315  LMatrix4 _transform;
316  CoordinateSystem _coordinate_system;
317 
318  LPoint3 _ul3d, _lr3d;
319 
320  // Returned from TextAssembler:
321  LVector2 _text_ul, _text_lr;
322  int _num_rows;
323  wstring _wordwrapped_wtext;
324 
325  static PStatCollector _text_generate_pcollector;
326 
327 public:
328  static TypeHandle get_class_type() {
329  return _type_handle;
330  }
331  static void init_type() {
332  PandaNode::init_type();
333  TextProperties::init_type();
334  register_type(_type_handle, "TextNode",
335  PandaNode::get_class_type(),
336  TextProperties::get_class_type());
337  }
338  virtual TypeHandle get_type() const {
339  return get_class_type();
340  }
341  virtual TypeHandle force_init_type() {init_type(); return get_class_type();}
342 
343 private:
344  static TypeHandle _type_handle;
345 };
346 
347 #include "textNode.I"
348 
349 #endif
void append_wtext(const wstring &text)
Appends the indicates string to the end of the stored wide-character text.
Definition: textEncoder.I:545
A basic node of the scene graph or data graph.
Definition: pandaNode.h:72
void clear_font()
Restores the default font to the text.
void set_shadow(PN_stdfloat xoffset, PN_stdfloat yoffset)
Specifies that the text should be drawn with a shadow, by creating a second copy of the text and offs...
void append_text(const string &text)
Appends the indicates string to the end of the stored text.
Definition: textEncoder.I:193
void set_font(TextFont *font)
Sets the font that will be used when making text.
virtual void apply_attribs_to_vertices(const AccumulatedAttribs &attribs, int attrib_types, GeomTransformer &transformer)
Applies whatever attributes are specified in the AccumulatedAttribs object (and by the attrib_types b...
Definition: pandaNode.cxx:352
void set_align(Alignment align_type)
Specifies the alignment of the text within its margins.
This class can be used to convert text between multiple representations, e.g.
Definition: textEncoder.h:37
void clear_align()
Restores the default alignment of the text.
void set_glyph_scale(PN_stdfloat glyph_scale)
Specifies the factor by which to scale each letter of the text as it is placed, in addition to any sc...
void set_bin(const string &bin)
Names the CullBin that the text geometry should be assigned to.
Represents a texture object, which is typically a single 2-d image but may also represent a 1-d or 3-...
Definition: texture.h:75
This collects together the pieces of data that are accumulated for each node while walking the scene ...
void clear_text_color()
Removes the text color specification; the text will be colored whatever it was in the source font fil...
This is a three-component point in space (as opposed to a three-component vector, which represents a ...
Definition: lpoint3.h:99
void clear_text()
Removes the text from the TextEncoder.
Definition: textEncoder.I:140
void set_small_caps(bool small_caps)
Sets the small_caps flag.
virtual void r_prepare_scene(GraphicsStateGuardianBase *gsg, const RenderState *node_state, GeomTransformer &transformer, Thread *current_thread)
The recursive implementation of prepare_scene().
Definition: pandaNode.cxx:2925
An encapsulation of a font; i.e.
Definition: textFont.h:36
This is an abstract class for any volume in any sense which can be said to define the locality of ref...
A lightweight class that represents a single element that may be timed and/or counted via stats...
void set_wordwrap(PN_stdfloat wordwrap)
Sets the text up to automatically wordwrap when it exceeds the indicated width.
This class is used by the SceneGraphReducer to maintain and accumulate the set of attributes we have ...
void append_unicode_char(int character)
Appends a single character to the end of the stored text.
Definition: textEncoder.I:206
This is a 4-by-4 transform matrix.
Definition: lmatrix.h:451
void set_small_caps_scale(PN_stdfloat small_caps_scale)
Sets the scale factor applied to lowercase letters from their uppercase equivalents, when the small_caps flag is in effect.
void clear_shadow()
Specifies that a shadow will not be drawn behind the text.
void clear_bin()
Removes the effect of a previous call to set_bin().
void set_slant(PN_stdfloat slant)
Specifies the factor by which the text slants to the right.
This is the base class for all two-component vectors and points.
Definition: lvecBase2.h:105
virtual int get_unsafe_to_apply_attribs() const
Returns the union of all attributes from SceneGraphReducer::AttribTypes that may not safely be applie...
Definition: pandaNode.cxx:335
void clear_wordwrap()
Removes the wordwrap setting from the text.
virtual PandaNode * make_copy() const
Returns a newly-allocated PandaNode that is a shallow copy of this one.
Definition: pandaNode.cxx:604
int set_draw_order(int draw_order)
Sets the drawing order of text created by the TextNode.
void set_indent(PN_stdfloat indent)
Specifies the amount of extra space that is inserted before the first character of each line...
This represents a unique collection of RenderAttrib objects that correspond to a particular renderabl...
Definition: renderState.h:53
This is the base class for all three-component vectors and points.
Definition: lvecBase4.h:111
This is a base class for the GraphicsStateGuardian class, which is itself a base class for the variou...
This is a two-component vector offset.
Definition: lvector2.h:91
A thread; that is, a lightweight process.
Definition: thread.h:51
The primary interface to this module.
Definition: textNode.h:52
This defines the set of visual properties that may be assigned to the individual characters of the te...
void clear_indent()
Removes the indent setting from the text.
void clear_shadow_color()
Removes the shadow color specification.
void set_text(const string &text)
Changes the text that is stored in the encoder.
Definition: textEncoder.I:112
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:85
void set_glyph_shift(PN_stdfloat glyph_shift)
Specifies a vertical amount to shift each letter of the text as it is placed.
void set_wtext(const wstring &wtext)
Changes the text that is stored in the encoder.
Definition: textEncoder.I:516
void set_tab_width(PN_stdfloat tab_width)
Sets the width of each tab stop, in screen units.
virtual bool is_renderable() const
Returns true if there is some value to visiting this particular node during the cull traversal for an...
Definition: pandaNode.cxx:578
void set_transform(const TransformState *transform, Thread *current_thread=Thread::get_current_thread())
Sets the transform that will be applied to this node and below.
Definition: pandaNode.cxx:1267
This object performs a depth-first traversal of the scene graph, with optional view-frustum culling...
Definition: cullTraverser.h:48
An object specifically designed to transform the vertices of a Geom without disturbing indexing or af...