textPropertiesManager.cxx

00001 // Filename: textPropertiesManager.cxx
00002 // Created by:  drose (07Apr04)
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 #include "textPropertiesManager.h"
00016 #include "indent.h"
00017 
00018 TextPropertiesManager *TextPropertiesManager::_global_ptr = (TextPropertiesManager *)NULL;
00019 
00020 ////////////////////////////////////////////////////////////////////
00021 //     Function: TextPropertiesManager::Constructor
00022 //       Access: Protected
00023 //  Description: The constructor is not intended to be called
00024 //               directly; there is only one TextPropertiesManager and
00025 //               it constructs itself.  This could have been a private
00026 //               constructor, but gcc issues a spurious warning if the
00027 //               constructor is private and the class has no friends.
00028 ////////////////////////////////////////////////////////////////////
00029 TextPropertiesManager::
00030 TextPropertiesManager() {
00031 }
00032 
00033 ////////////////////////////////////////////////////////////////////
00034 //     Function: TextPropertiesManager::Destructor
00035 //       Access: Protected
00036 //  Description: Don't call the destructor.
00037 ////////////////////////////////////////////////////////////////////
00038 TextPropertiesManager::
00039 ~TextPropertiesManager() {
00040 }
00041 
00042 ////////////////////////////////////////////////////////////////////
00043 //     Function: TextPropertiesManager::set_properties
00044 //       Access: Published
00045 //  Description: Defines the TextProperties associated with the
00046 //               indicated name.  When the name is subsequently
00047 //               encountered in text embedded between \1 characters in
00048 //               a TextNode string, the following text will be
00049 //               rendered with these properties.
00050 //
00051 //               If there was already a TextProperties structure
00052 //               associated with this name, it is quietly replaced
00053 //               with the new definition.
00054 ////////////////////////////////////////////////////////////////////
00055 void TextPropertiesManager::
00056 set_properties(const string &name, const TextProperties &properties) {
00057   _properties[name] = properties;
00058 }
00059 
00060 ////////////////////////////////////////////////////////////////////
00061 //     Function: TextPropertiesManager::get_properties
00062 //       Access: Published
00063 //  Description: Returns the TextProperties associated with the
00064 //               indicated name.  If there was not previously a
00065 //               TextProperties associated with this name, a warning
00066 //               is printed and then a default TextProperties
00067 //               structure is associated with the name, and returned.
00068 //
00069 //               Call has_properties() instead to check whether a
00070 //               particular name has been defined.
00071 ////////////////////////////////////////////////////////////////////
00072 TextProperties TextPropertiesManager::
00073 get_properties(const string &name) {
00074   Properties::const_iterator pi;
00075   pi = _properties.find(name);
00076   if (pi != _properties.end()) {
00077     return (*pi).second;
00078   }
00079 
00080   text_cat.warning()
00081     << "Creating default TextProperties for name '" << name << "'\n";
00082 
00083   TextProperties default_properties;
00084   _properties[name] = default_properties;
00085   return default_properties;
00086 }
00087 
00088 ////////////////////////////////////////////////////////////////////
00089 //     Function: TextPropertiesManager::has_properties
00090 //       Access: Published
00091 //  Description: Returns true if a TextProperties structure has been
00092 //               associated with the indicated name, false otherwise.
00093 //               Normally this means set_properties() has been called
00094 //               with this name, but because get_properties() will
00095 //               implicitly create a default TextProperties structure,
00096 //               it may also mean simply that get_properties() has
00097 //               been called with the indicated name.
00098 ////////////////////////////////////////////////////////////////////
00099 bool TextPropertiesManager::
00100 has_properties(const string &name) const {
00101   Properties::const_iterator pi;
00102   pi = _properties.find(name);
00103   return (pi != _properties.end());
00104 }
00105 
00106 ////////////////////////////////////////////////////////////////////
00107 //     Function: TextPropertiesManager::clear_properties
00108 //       Access: Published
00109 //  Description: Removes the named TextProperties structure from the
00110 //               manager.
00111 ////////////////////////////////////////////////////////////////////
00112 void TextPropertiesManager::
00113 clear_properties(const string &name) {
00114   _properties.erase(name);
00115 }
00116 
00117 ////////////////////////////////////////////////////////////////////
00118 //     Function: TextPropertiesManager::set_graphic
00119 //       Access: Published
00120 //  Description: Defines the TextGraphic associated with the
00121 //               indicated name.  When the name is subsequently
00122 //               encountered in text embedded between \5 characters in
00123 //               a TextNode string, the specified graphic will be
00124 //               embedded in the text at that point.
00125 //
00126 //               If there was already a TextGraphic structure
00127 //               associated with this name, it is quietly replaced
00128 //               with the new definition.
00129 ////////////////////////////////////////////////////////////////////
00130 void TextPropertiesManager::
00131 set_graphic(const string &name, const TextGraphic &graphic) {
00132   _graphics[name] = graphic;
00133 }
00134 
00135 ////////////////////////////////////////////////////////////////////
00136 //     Function: TextPropertiesManager::set_graphic
00137 //       Access: Published
00138 //  Description: This flavor of set_graphic implicitly creates a frame
00139 //               for the model using the model's actual computed
00140 //               bounding volume, as derived from
00141 //               NodePath::calc_tight_bounds().  Create a TextGraphic
00142 //               object first if you want to have explicit control of
00143 //               the frame.
00144 ////////////////////////////////////////////////////////////////////
00145 void TextPropertiesManager::
00146 set_graphic(const string &name, const NodePath &model) {
00147   LPoint3f min_point, max_point;
00148   model.calc_tight_bounds(min_point, max_point);
00149 
00150   TextGraphic graphic(model, 
00151                       min_point.dot(LVector3f::right()),
00152                       max_point.dot(LVector3f::right()),
00153                       min_point.dot(LVector3f::up()), 
00154                       max_point.dot(LVector3f::up()));
00155 
00156   _graphics[name] = graphic;
00157 }
00158 
00159 ////////////////////////////////////////////////////////////////////
00160 //     Function: TextPropertiesManager::get_graphic
00161 //       Access: Published
00162 //  Description: Returns the TextGraphic associated with the
00163 //               indicated name.  If there was not previously a
00164 //               TextGraphic associated with this name, a warning
00165 //               is printed and then a default TextGraphic
00166 //               structure is associated with the name, and returned.
00167 //
00168 //               Call has_graphic() instead to check whether a
00169 //               particular name has been defined.
00170 ////////////////////////////////////////////////////////////////////
00171 TextGraphic TextPropertiesManager::
00172 get_graphic(const string &name) {
00173   Graphics::const_iterator pi;
00174   pi = _graphics.find(name);
00175   if (pi != _graphics.end()) {
00176     return (*pi).second;
00177   }
00178 
00179   text_cat.warning()
00180     << "Creating default TextGraphic for name '" << name << "'\n";
00181 
00182   TextGraphic default_graphic;
00183   _graphics[name] = default_graphic;
00184   return default_graphic;
00185 }
00186 
00187 ////////////////////////////////////////////////////////////////////
00188 //     Function: TextPropertiesManager::has_graphic
00189 //       Access: Published
00190 //  Description: Returns true if a TextGraphic structure has been
00191 //               associated with the indicated name, false otherwise.
00192 //               Normally this means set_graphic() has been called
00193 //               with this name, but because get_graphic() will
00194 //               implicitly create a default TextGraphic structure,
00195 //               it may also mean simply that get_graphic() has
00196 //               been called with the indicated name.
00197 ////////////////////////////////////////////////////////////////////
00198 bool TextPropertiesManager::
00199 has_graphic(const string &name) const {
00200   Graphics::const_iterator pi;
00201   pi = _graphics.find(name);
00202   return (pi != _graphics.end());
00203 }
00204 
00205 ////////////////////////////////////////////////////////////////////
00206 //     Function: TextPropertiesManager::clear_graphic
00207 //       Access: Published
00208 //  Description: Removes the named TextGraphic structure from the
00209 //               manager.
00210 ////////////////////////////////////////////////////////////////////
00211 void TextPropertiesManager::
00212 clear_graphic(const string &name) {
00213   _graphics.erase(name);
00214 }
00215 
00216 ////////////////////////////////////////////////////////////////////
00217 //     Function: TextPropertiesManager::write
00218 //       Access: Published
00219 //  Description: 
00220 ////////////////////////////////////////////////////////////////////
00221 void TextPropertiesManager::
00222 write(ostream &out, int indent_level) const {
00223   Properties::const_iterator pi;
00224   for (pi = _properties.begin(); pi != _properties.end(); ++pi) {
00225     indent(out, indent_level)
00226       << "TextProperties " << (*pi).first << ":\n";
00227     (*pi).second.write(out, indent_level + 2);
00228   }
00229 }
00230 
00231 ////////////////////////////////////////////////////////////////////
00232 //     Function: TextPropertiesManager::get_global_ptr
00233 //       Access: Published, Static
00234 //  Description: Returns the pointer to the global TextPropertiesManager
00235 //               object.
00236 ////////////////////////////////////////////////////////////////////
00237 TextPropertiesManager *TextPropertiesManager::
00238 get_global_ptr() {
00239   if (_global_ptr == (TextPropertiesManager *)NULL) {
00240     _global_ptr = new TextPropertiesManager;
00241   }
00242   return _global_ptr;
00243 }
00244 
00245 ////////////////////////////////////////////////////////////////////
00246 //     Function: TextPropertiesManager::get_properties_ptr
00247 //       Access: Public
00248 //  Description: Returns a pointer to the TextProperties with the
00249 //               indicated name, or NULL if there is no properties
00250 //               with that name.
00251 ////////////////////////////////////////////////////////////////////
00252 const TextProperties *TextPropertiesManager::
00253 get_properties_ptr(const string &name) {
00254   Properties::const_iterator pi;
00255   pi = _properties.find(name);
00256   if (pi != _properties.end()) {
00257     return &(*pi).second;
00258   }
00259   return NULL;
00260 }
00261 
00262 ////////////////////////////////////////////////////////////////////
00263 //     Function: TextPropertiesManager::get_graphic_ptr
00264 //       Access: Public
00265 //  Description: Returns a pointer to the TextGraphic with the
00266 //               indicated name, or NULL if there is no graphic
00267 //               with that name.
00268 ////////////////////////////////////////////////////////////////////
00269 const TextGraphic *TextPropertiesManager::
00270 get_graphic_ptr(const string &name) {
00271   Graphics::const_iterator pi;
00272   pi = _graphics.find(name);
00273   if (pi != _graphics.end()) {
00274     return &(*pi).second;
00275   }
00276   return NULL;
00277 }