Panda3D
textPropertiesManager.cxx
1 // Filename: textPropertiesManager.cxx
2 // Created by: drose (07Apr04)
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 #include "textPropertiesManager.h"
16 #include "indent.h"
17 
18 TextPropertiesManager *TextPropertiesManager::_global_ptr = (TextPropertiesManager *)NULL;
19 
20 ////////////////////////////////////////////////////////////////////
21 // Function: TextPropertiesManager::Constructor
22 // Access: Protected
23 // Description: The constructor is not intended to be called
24 // directly; there is only one TextPropertiesManager and
25 // it constructs itself. This could have been a private
26 // constructor, but gcc issues a spurious warning if the
27 // constructor is private and the class has no friends.
28 ////////////////////////////////////////////////////////////////////
29 TextPropertiesManager::
30 TextPropertiesManager() {
31 }
32 
33 ////////////////////////////////////////////////////////////////////
34 // Function: TextPropertiesManager::Destructor
35 // Access: Protected
36 // Description: Don't call the destructor.
37 ////////////////////////////////////////////////////////////////////
38 TextPropertiesManager::
39 ~TextPropertiesManager() {
40 }
41 
42 ////////////////////////////////////////////////////////////////////
43 // Function: TextPropertiesManager::set_properties
44 // Access: Published
45 // Description: Defines the TextProperties associated with the
46 // indicated name. When the name is subsequently
47 // encountered in text embedded between \1 characters in
48 // a TextNode string, the following text will be
49 // rendered with these properties.
50 //
51 // If there was already a TextProperties structure
52 // associated with this name, it is quietly replaced
53 // with the new definition.
54 ////////////////////////////////////////////////////////////////////
56 set_properties(const string &name, const TextProperties &properties) {
57  _properties[name] = properties;
58 }
59 
60 ////////////////////////////////////////////////////////////////////
61 // Function: TextPropertiesManager::get_properties
62 // Access: Published
63 // Description: Returns the TextProperties associated with the
64 // indicated name. If there was not previously a
65 // TextProperties associated with this name, a warning
66 // is printed and then a default TextProperties
67 // structure is associated with the name, and returned.
68 //
69 // Call has_properties() instead to check whether a
70 // particular name has been defined.
71 ////////////////////////////////////////////////////////////////////
73 get_properties(const string &name) {
74  Properties::const_iterator pi;
75  pi = _properties.find(name);
76  if (pi != _properties.end()) {
77  return (*pi).second;
78  }
79 
80  text_cat.warning()
81  << "Creating default TextProperties for name '" << name << "'\n";
82 
83  TextProperties default_properties;
84  _properties[name] = default_properties;
85  return default_properties;
86 }
87 
88 ////////////////////////////////////////////////////////////////////
89 // Function: TextPropertiesManager::has_properties
90 // Access: Published
91 // Description: Returns true if a TextProperties structure has been
92 // associated with the indicated name, false otherwise.
93 // Normally this means set_properties() has been called
94 // with this name, but because get_properties() will
95 // implicitly create a default TextProperties structure,
96 // it may also mean simply that get_properties() has
97 // been called with the indicated name.
98 ////////////////////////////////////////////////////////////////////
100 has_properties(const string &name) const {
101  Properties::const_iterator pi;
102  pi = _properties.find(name);
103  return (pi != _properties.end());
104 }
105 
106 ////////////////////////////////////////////////////////////////////
107 // Function: TextPropertiesManager::clear_properties
108 // Access: Published
109 // Description: Removes the named TextProperties structure from the
110 // manager.
111 ////////////////////////////////////////////////////////////////////
113 clear_properties(const string &name) {
114  _properties.erase(name);
115 }
116 
117 ////////////////////////////////////////////////////////////////////
118 // Function: TextPropertiesManager::set_graphic
119 // Access: Published
120 // Description: Defines the TextGraphic associated with the
121 // indicated name. When the name is subsequently
122 // encountered in text embedded between \5 characters in
123 // a TextNode string, the specified graphic will be
124 // embedded in the text at that point.
125 //
126 // If there was already a TextGraphic structure
127 // associated with this name, it is quietly replaced
128 // with the new definition.
129 ////////////////////////////////////////////////////////////////////
131 set_graphic(const string &name, const TextGraphic &graphic) {
132  _graphics[name] = graphic;
133 }
134 
135 ////////////////////////////////////////////////////////////////////
136 // Function: TextPropertiesManager::set_graphic
137 // Access: Published
138 // Description: This flavor of set_graphic implicitly creates a frame
139 // for the model using the model's actual computed
140 // bounding volume, as derived from
141 // NodePath::calc_tight_bounds(). Create a TextGraphic
142 // object first if you want to have explicit control of
143 // the frame.
144 ////////////////////////////////////////////////////////////////////
146 set_graphic(const string &name, const NodePath &model) {
147  LPoint3 min_point, max_point;
148  model.calc_tight_bounds(min_point, max_point);
149 
150  TextGraphic graphic(model,
151  min_point.dot(LVector3::right()),
152  max_point.dot(LVector3::right()),
153  min_point.dot(LVector3::up()),
154  max_point.dot(LVector3::up()));
155 
156  _graphics[name] = graphic;
157 }
158 
159 ////////////////////////////////////////////////////////////////////
160 // Function: TextPropertiesManager::get_graphic
161 // Access: Published
162 // Description: Returns the TextGraphic associated with the
163 // indicated name. If there was not previously a
164 // TextGraphic associated with this name, a warning
165 // is printed and then a default TextGraphic
166 // structure is associated with the name, and returned.
167 //
168 // Call has_graphic() instead to check whether a
169 // particular name has been defined.
170 ////////////////////////////////////////////////////////////////////
172 get_graphic(const string &name) {
173  Graphics::const_iterator pi;
174  pi = _graphics.find(name);
175  if (pi != _graphics.end()) {
176  return (*pi).second;
177  }
178 
179  text_cat.warning()
180  << "Creating default TextGraphic for name '" << name << "'\n";
181 
182  TextGraphic default_graphic;
183  _graphics[name] = default_graphic;
184  return default_graphic;
185 }
186 
187 ////////////////////////////////////////////////////////////////////
188 // Function: TextPropertiesManager::has_graphic
189 // Access: Published
190 // Description: Returns true if a TextGraphic structure has been
191 // associated with the indicated name, false otherwise.
192 // Normally this means set_graphic() has been called
193 // with this name, but because get_graphic() will
194 // implicitly create a default TextGraphic structure,
195 // it may also mean simply that get_graphic() has
196 // been called with the indicated name.
197 ////////////////////////////////////////////////////////////////////
199 has_graphic(const string &name) const {
200  Graphics::const_iterator pi;
201  pi = _graphics.find(name);
202  return (pi != _graphics.end());
203 }
204 
205 ////////////////////////////////////////////////////////////////////
206 // Function: TextPropertiesManager::clear_graphic
207 // Access: Published
208 // Description: Removes the named TextGraphic structure from the
209 // manager.
210 ////////////////////////////////////////////////////////////////////
212 clear_graphic(const string &name) {
213  _graphics.erase(name);
214 }
215 
216 ////////////////////////////////////////////////////////////////////
217 // Function: TextPropertiesManager::write
218 // Access: Published
219 // Description:
220 ////////////////////////////////////////////////////////////////////
221 void TextPropertiesManager::
222 write(ostream &out, int indent_level) const {
223  Properties::const_iterator pi;
224  for (pi = _properties.begin(); pi != _properties.end(); ++pi) {
225  indent(out, indent_level)
226  << "TextProperties " << (*pi).first << ":\n";
227  (*pi).second.write(out, indent_level + 2);
228  }
229 }
230 
231 ////////////////////////////////////////////////////////////////////
232 // Function: TextPropertiesManager::get_global_ptr
233 // Access: Published, Static
234 // Description: Returns the pointer to the global TextPropertiesManager
235 // object.
236 ////////////////////////////////////////////////////////////////////
239  if (_global_ptr == (TextPropertiesManager *)NULL) {
240  _global_ptr = new TextPropertiesManager;
241  }
242  return _global_ptr;
243 }
244 
245 ////////////////////////////////////////////////////////////////////
246 // Function: TextPropertiesManager::get_properties_ptr
247 // Access: Public
248 // Description: Returns a pointer to the TextProperties with the
249 // indicated name, or NULL if there is no properties
250 // with that name.
251 ////////////////////////////////////////////////////////////////////
253 get_properties_ptr(const string &name) {
254  Properties::const_iterator pi;
255  pi = _properties.find(name);
256  if (pi != _properties.end()) {
257  return &(*pi).second;
258  }
259  return NULL;
260 }
261 
262 ////////////////////////////////////////////////////////////////////
263 // Function: TextPropertiesManager::get_graphic_ptr
264 // Access: Public
265 // Description: Returns a pointer to the TextGraphic with the
266 // indicated name, or NULL if there is no graphic
267 // with that name.
268 ////////////////////////////////////////////////////////////////////
270 get_graphic_ptr(const string &name) {
271  Graphics::const_iterator pi;
272  pi = _graphics.find(name);
273  if (pi != _graphics.end()) {
274  return &(*pi).second;
275  }
276  return NULL;
277 }
const TextGraphic * get_graphic_ptr(const string &name)
Returns a pointer to the TextGraphic with the indicated name, or NULL if there is no graphic with tha...
bool calc_tight_bounds(LPoint3 &min_point, LPoint3 &max_point, Thread *current_thread=Thread::get_current_thread()) const
Calculates the minimum and maximum vertices of all Geoms at this NodePath&#39;s bottom node and below...
Definition: nodePath.cxx:6198
TextProperties get_properties(const string &name)
Returns the TextProperties associated with the indicated name.
void clear_properties(const string &name)
Removes the named TextProperties structure from the manager.
This is a three-component point in space (as opposed to a three-component vector, which represents a ...
Definition: lpoint3.h:99
static TextPropertiesManager * get_global_ptr()
Returns the pointer to the global TextPropertiesManager object.
void clear_graphic(const string &name)
Removes the named TextGraphic structure from the manager.
bool has_graphic(const string &name) const
Returns true if a TextGraphic structure has been associated with the indicated name, false otherwise.
const TextProperties * get_properties_ptr(const string &name)
Returns a pointer to the TextProperties with the indicated name, or NULL if there is no properties wi...
static LVector3f right(CoordinateSystem cs=CS_default)
Returns the right vector for the given coordinate system.
Definition: lvector3.h:568
void set_properties(const string &name, const TextProperties &properties)
Defines the TextProperties associated with the indicated name.
This defines the set of visual properties that may be assigned to the individual characters of the te...
void set_graphic(const string &name, const TextGraphic &graphic)
Defines the TextGraphic associated with the indicated name.
bool has_properties(const string &name) const
Returns true if a TextProperties structure has been associated with the indicated name...
This defines all of the TextProperties structures that might be referenced by name from an embedded t...
TextGraphic get_graphic(const string &name)
Returns the TextGraphic associated with the indicated name.
NodePath is the fundamental system for disambiguating instances, and also provides a higher-level int...
Definition: nodePath.h:165
static LVector3f up(CoordinateSystem cs=CS_default)
Returns the up vector for the given coordinate system.
Definition: lvector3.h:541
This defines a special model that has been constructed for the purposes of embedding an arbitrary gra...
Definition: textGraphic.h:43