Panda3D
|
00001 // Filename: textNode.h 00002 // Created by: drose (13Mar02) 00003 // 00004 //////////////////////////////////////////////////////////////////// 00005 // 00006 // PANDA 3D SOFTWARE 00007 // Copyright (c) Carnegie Mellon University. All rights reserved. 00008 // 00009 // All use of this software is subject to the terms of the revised BSD 00010 // license. You should have received a copy of this license along 00011 // with this source code in a file named "LICENSE." 00012 // 00013 //////////////////////////////////////////////////////////////////// 00014 00015 #ifndef TEXTNODE_H 00016 #define TEXTNODE_H 00017 00018 #include "pandabase.h" 00019 00020 #include "config_text.h" 00021 #include "textEncoder.h" 00022 #include "textProperties.h" 00023 #include "textFont.h" 00024 #include "textAssembler.h" 00025 #include "pandaNode.h" 00026 #include "luse.h" 00027 #include "geom.h" 00028 00029 //////////////////////////////////////////////////////////////////// 00030 // Class : TextNode 00031 // Description : The primary interface to this module. This class 00032 // does basic text assembly; given a string of text and 00033 // a TextFont object, it creates a piece of geometry 00034 // that may be placed in the 3-d or 2-d world to 00035 // represent the indicated text. 00036 // 00037 // The TextNode may be used in one of two ways. 00038 // Naively, it may simply be parented directly into the 00039 // scene graph and rendered as if it were a GeomNode; in 00040 // this mode, the actual polygon geometry that renders 00041 // the text is not directly visible or accessible, but 00042 // remains hidden within the TextNode. 00043 // 00044 // The second way TextNode may be used is as a text 00045 // generator. To use it in this way, do not parent the 00046 // TextNode to the scene graph; instead, set the 00047 // properties of the text and call generate() to return 00048 // an ordinary node, containing ordinary geometry, which 00049 // you may use however you like. Each time you call 00050 // generate() a new node is returned. 00051 //////////////////////////////////////////////////////////////////// 00052 class EXPCL_PANDA_TEXT TextNode : public PandaNode, public TextEncoder, public TextProperties { 00053 PUBLISHED: 00054 TextNode(const string &name); 00055 TextNode(const string &name, const TextProperties ©); 00056 protected: 00057 TextNode(const TextNode ©); 00058 virtual PandaNode *make_copy() const; 00059 00060 PUBLISHED: 00061 ~TextNode(); 00062 00063 enum FlattenFlags { 00064 FF_none = 0x0000, 00065 FF_light = 0x0001, 00066 FF_medium = 0x0002, 00067 FF_strong = 0x0004, 00068 FF_dynamic_merge = 0x0008, 00069 }; 00070 00071 INLINE float get_line_height() const; 00072 00073 INLINE void set_max_rows(int max_rows); 00074 INLINE void clear_max_rows(); 00075 INLINE bool has_max_rows() const; 00076 INLINE int get_max_rows() const; 00077 INLINE bool has_overflow() const; 00078 00079 INLINE void set_frame_color(float r, float g, float b, float a); 00080 INLINE void set_frame_color(const Colorf &frame_color); 00081 INLINE Colorf get_frame_color() const; 00082 00083 INLINE void set_card_border(float size, float uv_portion); 00084 INLINE void clear_card_border(); 00085 INLINE float get_card_border_size() const; 00086 INLINE float get_card_border_uv_portion() const; 00087 INLINE bool has_card_border() const; 00088 00089 INLINE void set_card_color(float r, float g, float b, float a); 00090 INLINE void set_card_color(const Colorf &card_color); 00091 INLINE Colorf get_card_color() const; 00092 00093 INLINE void set_card_texture(Texture *card_texture); 00094 INLINE void clear_card_texture(); 00095 INLINE bool has_card_texture() const; 00096 INLINE Texture *get_card_texture() const; 00097 00098 INLINE void set_frame_as_margin(float left, float right, 00099 float bottom, float top); 00100 INLINE void set_frame_actual(float left, float right, 00101 float bottom, float top); 00102 INLINE void clear_frame(); 00103 INLINE bool has_frame() const; 00104 INLINE bool is_frame_as_margin() const; 00105 INLINE LVecBase4f get_frame_as_set() const; 00106 INLINE LVecBase4f get_frame_actual() const; 00107 00108 INLINE void set_frame_line_width(float line_width); 00109 INLINE float get_frame_line_width() const; 00110 INLINE void set_frame_corners(bool corners); 00111 INLINE bool get_frame_corners() const; 00112 00113 INLINE void set_card_as_margin(float left, float right, 00114 float bottom, float top); 00115 INLINE void set_card_actual(float left, float right, 00116 float bottom, float top); 00117 INLINE void set_card_decal(bool card_decal); 00118 INLINE void clear_card(); 00119 INLINE bool has_card() const; 00120 INLINE bool get_card_decal() const; 00121 INLINE bool is_card_as_margin() const; 00122 INLINE LVecBase4f get_card_as_set() const; 00123 INLINE LVecBase4f get_card_actual() const; 00124 INLINE LVecBase4f get_card_transformed() const; 00125 00126 INLINE void set_transform(const LMatrix4f &transform); 00127 INLINE LMatrix4f get_transform() const; 00128 00129 INLINE void set_coordinate_system(CoordinateSystem cs); 00130 INLINE CoordinateSystem get_coordinate_system() const; 00131 00132 INLINE void set_usage_hint(Geom::UsageHint usage_hint); 00133 INLINE Geom::UsageHint get_usage_hint() const; 00134 00135 INLINE void set_flatten_flags(int flatten_flags); 00136 INLINE int get_flatten_flags() const; 00137 00138 // These methods are inherited from TextProperties, but we override 00139 // here so we can flag the TextNode as dirty when they have been 00140 // changed. 00141 00142 INLINE void set_font(TextFont *font); 00143 INLINE void clear_font(); 00144 00145 INLINE void set_small_caps(bool small_caps); 00146 INLINE void clear_small_caps(); 00147 00148 INLINE void set_small_caps_scale(float small_caps_scale); 00149 INLINE void clear_small_caps_scale(); 00150 00151 INLINE void set_slant(float slant); 00152 INLINE void clear_slant(); 00153 00154 INLINE void set_align(Alignment align_type); 00155 INLINE void clear_align(); 00156 00157 INLINE void set_indent(float indent); 00158 INLINE void clear_indent(); 00159 00160 INLINE void set_wordwrap(float wordwrap); 00161 INLINE void clear_wordwrap(); 00162 00163 INLINE void set_text_color(float r, float g, float b, float a); 00164 INLINE void set_text_color(const Colorf &text_color); 00165 INLINE void clear_text_color(); 00166 00167 INLINE void set_shadow_color(float r, float g, float b, float a); 00168 INLINE void set_shadow_color(const Colorf &shadow_color); 00169 INLINE void clear_shadow_color(); 00170 00171 INLINE void set_shadow(float xoffset, float yoffset); 00172 INLINE void set_shadow(const LVecBase2f &shadow_offset); 00173 INLINE void clear_shadow(); 00174 00175 INLINE void set_bin(const string &bin); 00176 INLINE void clear_bin(); 00177 00178 INLINE int set_draw_order(int draw_order); 00179 INLINE void clear_draw_order(); 00180 00181 INLINE void set_tab_width(float tab_width); 00182 INLINE void clear_tab_width(); 00183 00184 INLINE void set_glyph_scale(float glyph_scale); 00185 INLINE void clear_glyph_scale(); 00186 00187 INLINE void set_glyph_shift(float glyph_shift); 00188 INLINE void clear_glyph_shift(); 00189 00190 // These methods are inherited from TextEncoder, but we override 00191 // here so we can flag the TextNode as dirty when they have been 00192 // changed. 00193 INLINE void set_text(const string &text); 00194 INLINE void set_text(const string &text, Encoding encoding); 00195 INLINE void clear_text(); 00196 INLINE void append_text(const string &text); 00197 INLINE void append_unicode_char(wchar_t character); 00198 00199 // After the text has been set, you can query this to determine how 00200 // it will be wordwrapped. 00201 INLINE string get_wordwrapped_text() const; 00202 00203 // These methods calculate the width of a single character or a line 00204 // of text in the current font. 00205 float calc_width(wchar_t character) const; 00206 INLINE float calc_width(const string &line) const; 00207 00208 bool has_exact_character(wchar_t character) const; 00209 bool has_character(wchar_t character) const; 00210 bool is_whitespace(wchar_t character) const; 00211 00212 // Direct support for wide-character strings. 00213 INLINE void set_wtext(const wstring &wtext); 00214 INLINE void append_wtext(const wstring &text); 00215 00216 INLINE wstring get_wordwrapped_wtext() const; 00217 float calc_width(const wstring &line) const; 00218 00219 virtual void output(ostream &out) const; 00220 virtual void write(ostream &out, int indent_level = 0) const; 00221 00222 // The following functions return information about the text that 00223 // was last built (and is currently visible). 00224 INLINE float get_left() const; 00225 INLINE float get_right() const; 00226 INLINE float get_bottom() const; 00227 INLINE float get_top() const; 00228 INLINE float get_height() const; 00229 INLINE float get_width() const; 00230 00231 INLINE LPoint3f get_upper_left_3d() const; 00232 INLINE LPoint3f get_lower_right_3d() const; 00233 00234 INLINE int get_num_rows() const; 00235 00236 PT(PandaNode) generate(); 00237 INLINE void update(); 00238 INLINE void force_update(); 00239 00240 PandaNode *get_internal_geom() const; 00241 00242 public: 00243 // From parent class PandaNode 00244 virtual int get_unsafe_to_apply_attribs() const; 00245 virtual void apply_attribs_to_vertices(const AccumulatedAttribs &attribs, 00246 int attrib_types, 00247 GeomTransformer &transformer); 00248 virtual CPT(TransformState) 00249 calc_tight_bounds(LPoint3f &min_point, LPoint3f &max_point, 00250 bool &found_any, 00251 const TransformState *transform, 00252 Thread *current_thread) const; 00253 00254 virtual bool cull_callback(CullTraverser *trav, CullTraverserData &data); 00255 virtual bool is_renderable() const; 00256 00257 virtual void compute_internal_bounds(CPT(BoundingVolume) &internal_bounds, 00258 int &internal_vertices, 00259 int pipeline_stage, 00260 Thread *current_thread) const; 00261 00262 virtual void r_prepare_scene(const RenderState *state, 00263 PreparedGraphicsObjects *prepared_objects, 00264 Thread *current_thread); 00265 00266 private: 00267 INLINE void invalidate_no_measure(); 00268 INLINE void invalidate_with_measure(); 00269 INLINE void check_rebuild() const; 00270 INLINE void check_measure() const; 00271 00272 void do_rebuild(); 00273 void do_measure(); 00274 00275 PT(PandaNode) make_frame(); 00276 PT(PandaNode) make_card(); 00277 PT(PandaNode) make_card_with_border(); 00278 00279 static int count_geoms(PandaNode *node); 00280 00281 PT(PandaNode) _internal_geom; 00282 00283 PT(Texture) _card_texture; 00284 Colorf _frame_color; 00285 Colorf _card_color; 00286 00287 enum Flags { 00288 F_has_frame = 0x0001, 00289 F_frame_as_margin = 0x0002, 00290 F_has_card = 0x0004, 00291 F_card_as_margin = 0x0008, 00292 F_has_card_texture = 0x0010, 00293 F_frame_corners = 0x0020, 00294 F_card_transp = 0x0040, 00295 F_has_card_border = 0x0080, 00296 F_needs_rebuild = 0x0100, 00297 F_needs_measure = 0x0200, 00298 F_has_overflow = 0x0400, 00299 F_card_decal = 0x0800, 00300 }; 00301 00302 int _flags; 00303 int _max_rows; 00304 GeomEnums::UsageHint _usage_hint; 00305 int _flatten_flags; 00306 bool _dynamic_merge; 00307 float _frame_width; 00308 float _card_border_size; 00309 float _card_border_uv_portion; 00310 00311 LVector2f _frame_ul, _frame_lr; 00312 LVector2f _card_ul, _card_lr; 00313 00314 LMatrix4f _transform; 00315 CoordinateSystem _coordinate_system; 00316 00317 LPoint3f _ul3d, _lr3d; 00318 00319 // Returned from TextAssembler: 00320 LVector2f _text_ul, _text_lr; 00321 int _num_rows; 00322 wstring _wordwrapped_wtext; 00323 00324 public: 00325 static TypeHandle get_class_type() { 00326 return _type_handle; 00327 } 00328 static void init_type() { 00329 PandaNode::init_type(); 00330 TextEncoder::init_type(); 00331 TextProperties::init_type(); 00332 register_type(_type_handle, "TextNode", 00333 PandaNode::get_class_type(), 00334 TextEncoder::get_class_type(), 00335 TextProperties::get_class_type()); 00336 } 00337 virtual TypeHandle get_type() const { 00338 return get_class_type(); 00339 } 00340 virtual TypeHandle force_init_type() {init_type(); return get_class_type();} 00341 00342 private: 00343 static TypeHandle _type_handle; 00344 }; 00345 00346 #include "textNode.I" 00347 00348 #endif