Panda3D
 All Classes Functions Variables Enumerations
textNode.h
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 &copy);
00056 protected:
00057   TextNode(const TextNode &copy);
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 PN_stdfloat 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(PN_stdfloat r, PN_stdfloat g, PN_stdfloat b, PN_stdfloat a);
00080   INLINE void set_frame_color(const LColor &frame_color);
00081   INLINE LColor get_frame_color() const;
00082 
00083   INLINE void set_card_border(PN_stdfloat size, PN_stdfloat uv_portion);
00084   INLINE void clear_card_border();
00085   INLINE PN_stdfloat get_card_border_size() const;
00086   INLINE PN_stdfloat get_card_border_uv_portion() const;
00087   INLINE bool has_card_border() const;
00088 
00089   INLINE void set_card_color(PN_stdfloat r, PN_stdfloat g, PN_stdfloat b, PN_stdfloat a);
00090   INLINE void set_card_color(const LColor &card_color);
00091   INLINE LColor 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(PN_stdfloat left, PN_stdfloat right,
00099                                   PN_stdfloat bottom, PN_stdfloat top);
00100   INLINE void set_frame_actual(PN_stdfloat left, PN_stdfloat right,
00101                                PN_stdfloat bottom, PN_stdfloat top);
00102   INLINE void clear_frame();
00103   INLINE bool has_frame() const;
00104   INLINE bool is_frame_as_margin() const;
00105   INLINE LVecBase4 get_frame_as_set() const;
00106   INLINE LVecBase4 get_frame_actual() const;
00107 
00108   INLINE void set_frame_line_width(PN_stdfloat line_width);
00109   INLINE PN_stdfloat 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(PN_stdfloat left, PN_stdfloat right,
00114                                  PN_stdfloat bottom, PN_stdfloat top);
00115   INLINE void set_card_actual(PN_stdfloat left, PN_stdfloat right,
00116                               PN_stdfloat bottom, PN_stdfloat 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 LVecBase4 get_card_as_set() const;
00123   INLINE LVecBase4 get_card_actual() const;
00124   INLINE LVecBase4 get_card_transformed() const;
00125 
00126   INLINE void set_transform(const LMatrix4 &transform);
00127   INLINE LMatrix4 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(PN_stdfloat small_caps_scale);
00149   INLINE void clear_small_caps_scale();
00150 
00151   INLINE void set_slant(PN_stdfloat 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(PN_stdfloat indent);
00158   INLINE void clear_indent();
00159 
00160   INLINE void set_wordwrap(PN_stdfloat wordwrap);
00161   INLINE void clear_wordwrap();
00162 
00163   INLINE void set_text_color(PN_stdfloat r, PN_stdfloat g, PN_stdfloat b, PN_stdfloat a);
00164   INLINE void set_text_color(const LColor &text_color);
00165   INLINE void clear_text_color();
00166 
00167   INLINE void set_shadow_color(PN_stdfloat r, PN_stdfloat g, PN_stdfloat b, PN_stdfloat a);
00168   INLINE void set_shadow_color(const LColor &shadow_color);
00169   INLINE void clear_shadow_color();
00170 
00171   INLINE void set_shadow(PN_stdfloat xoffset, PN_stdfloat yoffset);
00172   INLINE void set_shadow(const LVecBase2 &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(PN_stdfloat tab_width);
00182   INLINE void clear_tab_width();
00183 
00184   INLINE void set_glyph_scale(PN_stdfloat glyph_scale);
00185   INLINE void clear_glyph_scale();
00186 
00187   INLINE void set_glyph_shift(PN_stdfloat 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   PN_stdfloat calc_width(wchar_t character) const;
00206   INLINE PN_stdfloat 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   PN_stdfloat 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 PN_stdfloat get_left() const;
00225   INLINE PN_stdfloat get_right() const;
00226   INLINE PN_stdfloat get_bottom() const;
00227   INLINE PN_stdfloat get_top() const;
00228   INLINE PN_stdfloat get_height() const;
00229   INLINE PN_stdfloat get_width() const;
00230 
00231   INLINE LPoint3 get_upper_left_3d() const;
00232   INLINE LPoint3 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(LPoint3 &min_point, LPoint3 &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(GraphicsStateGuardianBase *gsg,
00263                                const RenderState *node_state,
00264                                GeomTransformer &transformer,
00265                                Thread *current_thread);
00266 
00267 private:
00268   INLINE void invalidate_no_measure();
00269   INLINE void invalidate_with_measure();
00270   INLINE void check_rebuild() const;
00271   INLINE void check_measure() const;
00272 
00273   void do_rebuild();
00274   void do_measure();
00275 
00276   PT(PandaNode) make_frame();
00277   PT(PandaNode) make_card();
00278   PT(PandaNode) make_card_with_border();
00279 
00280   static int count_geoms(PandaNode *node);
00281 
00282   PT(PandaNode) _internal_geom;
00283 
00284   PT(Texture) _card_texture;
00285   LColor _frame_color;
00286   LColor _card_color;
00287 
00288   enum Flags {
00289     F_has_frame        =  0x0001,
00290     F_frame_as_margin  =  0x0002,
00291     F_has_card         =  0x0004,
00292     F_card_as_margin   =  0x0008,
00293     F_has_card_texture =  0x0010,
00294     F_frame_corners    =  0x0020,
00295     F_card_transp      =  0x0040,
00296     F_has_card_border  =  0x0080,
00297     F_needs_rebuild    =  0x0100,
00298     F_needs_measure    =  0x0200,
00299     F_has_overflow     =  0x0400,
00300     F_card_decal       =  0x0800,
00301   };
00302 
00303   int _flags;
00304   int _max_rows;
00305   GeomEnums::UsageHint _usage_hint;
00306   int _flatten_flags;
00307   bool _dynamic_merge;
00308   PN_stdfloat _frame_width;
00309   PN_stdfloat _card_border_size;
00310   PN_stdfloat _card_border_uv_portion;
00311 
00312   LVector2 _frame_ul, _frame_lr;
00313   LVector2 _card_ul, _card_lr;
00314 
00315   LMatrix4 _transform;
00316   CoordinateSystem _coordinate_system;
00317 
00318   LPoint3 _ul3d, _lr3d;
00319 
00320   // Returned from TextAssembler:
00321   LVector2 _text_ul, _text_lr;
00322   int _num_rows;
00323   wstring _wordwrapped_wtext;
00324 
00325   static PStatCollector _text_generate_pcollector;
00326 
00327 public:
00328   static TypeHandle get_class_type() {
00329     return _type_handle;
00330   }
00331   static void init_type() {
00332     PandaNode::init_type();
00333     TextProperties::init_type();
00334     register_type(_type_handle, "TextNode",
00335                   PandaNode::get_class_type(),
00336                   TextProperties::get_class_type());
00337   }
00338   virtual TypeHandle get_type() const {
00339     return get_class_type();
00340   }
00341   virtual TypeHandle force_init_type() {init_type(); return get_class_type();}
00342 
00343 private:
00344   static TypeHandle _type_handle;
00345 };
00346 
00347 #include "textNode.I"
00348 
00349 #endif
 All Classes Functions Variables Enumerations