Panda3D
textPropertiesManager.cxx
Go to the documentation of this file.
1 /**
2  * PANDA 3D SOFTWARE
3  * Copyright (c) Carnegie Mellon University. All rights reserved.
4  *
5  * All use of this software is subject to the terms of the revised BSD
6  * license. You should have received a copy of this license along
7  * with this source code in a file named "LICENSE."
8  *
9  * @file textPropertiesManager.cxx
10  * @author drose
11  * @date 2004-04-07
12  */
13 
14 #include "textPropertiesManager.h"
15 #include "indent.h"
16 
17 using std::string;
18 
19 TextPropertiesManager *TextPropertiesManager::_global_ptr = nullptr;
20 
21 /**
22  * The constructor is not intended to be called directly; there is only one
23  * TextPropertiesManager and it constructs itself. This could have been a
24  * private constructor, but gcc issues a spurious warning if the constructor
25  * is private and the class has no friends.
26  */
27 TextPropertiesManager::
28 TextPropertiesManager() {
29 }
30 
31 /**
32  * Don't call the destructor.
33  */
34 TextPropertiesManager::
35 ~TextPropertiesManager() {
36 }
37 
38 /**
39  * Defines the TextProperties associated with the indicated name. When the
40  * name is subsequently encountered in text embedded between \1 characters in
41  * a TextNode string, the following text will be rendered with these
42  * properties.
43  *
44  * If there was already a TextProperties structure associated with this name,
45  * it is quietly replaced with the new definition.
46  */
48 set_properties(const string &name, const TextProperties &properties) {
49  _properties[name] = properties;
50 }
51 
52 /**
53  * Returns the TextProperties associated with the indicated name. If there
54  * was not previously a TextProperties associated with this name, a warning is
55  * printed and then a default TextProperties structure is associated with the
56  * name, and returned.
57  *
58  * Call has_properties() instead to check whether a particular name has been
59  * defined.
60  */
62 get_properties(const string &name) {
63  Properties::const_iterator pi;
64  pi = _properties.find(name);
65  if (pi != _properties.end()) {
66  return (*pi).second;
67  }
68 
69  text_cat.warning()
70  << "Creating default TextProperties for name '" << name << "'\n";
71 
72  TextProperties default_properties;
73  _properties[name] = default_properties;
74  return default_properties;
75 }
76 
77 /**
78  * Returns true if a TextProperties structure has been associated with the
79  * indicated name, false otherwise. Normally this means set_properties() has
80  * been called with this name, but because get_properties() will implicitly
81  * create a default TextProperties structure, it may also mean simply that
82  * get_properties() has been called with the indicated name.
83  */
85 has_properties(const string &name) const {
86  Properties::const_iterator pi;
87  pi = _properties.find(name);
88  return (pi != _properties.end());
89 }
90 
91 /**
92  * Removes the named TextProperties structure from the manager.
93  */
95 clear_properties(const string &name) {
96  _properties.erase(name);
97 }
98 
99 /**
100  * Defines the TextGraphic associated with the indicated name. When the name
101  * is subsequently encountered in text embedded between \5 characters in a
102  * TextNode string, the specified graphic will be embedded in the text at that
103  * point.
104  *
105  * If there was already a TextGraphic structure associated with this name, it
106  * is quietly replaced with the new definition.
107  */
109 set_graphic(const string &name, const TextGraphic &graphic) {
110  _graphics[name] = graphic;
111 }
112 
113 /**
114  * This flavor of set_graphic implicitly creates a frame for the model using
115  * the model's actual computed bounding volume, as derived from
116  * NodePath::calc_tight_bounds(). Create a TextGraphic object first if you
117  * want to have explicit control of the frame.
118  */
120 set_graphic(const string &name, const NodePath &model) {
121  LPoint3 min_point, max_point;
122  model.calc_tight_bounds(min_point, max_point);
123 
124  TextGraphic graphic(model,
125  min_point.dot(LVector3::right()),
126  max_point.dot(LVector3::right()),
127  min_point.dot(LVector3::up()),
128  max_point.dot(LVector3::up()));
129 
130  _graphics[name] = graphic;
131 }
132 
133 /**
134  * Returns the TextGraphic associated with the indicated name. If there was
135  * not previously a TextGraphic associated with this name, a warning is
136  * printed and then a default TextGraphic structure is associated with the
137  * name, and returned.
138  *
139  * Call has_graphic() instead to check whether a particular name has been
140  * defined.
141  */
143 get_graphic(const string &name) {
144  Graphics::const_iterator pi;
145  pi = _graphics.find(name);
146  if (pi != _graphics.end()) {
147  return (*pi).second;
148  }
149 
150  text_cat.warning()
151  << "Creating default TextGraphic for name '" << name << "'\n";
152 
153  TextGraphic default_graphic;
154  _graphics[name] = default_graphic;
155  return default_graphic;
156 }
157 
158 /**
159  * Returns true if a TextGraphic structure has been associated with the
160  * indicated name, false otherwise. Normally this means set_graphic() has
161  * been called with this name, but because get_graphic() will implicitly
162  * create a default TextGraphic structure, it may also mean simply that
163  * get_graphic() has been called with the indicated name.
164  */
166 has_graphic(const string &name) const {
167  Graphics::const_iterator pi;
168  pi = _graphics.find(name);
169  return (pi != _graphics.end());
170 }
171 
172 /**
173  * Removes the named TextGraphic structure from the manager.
174  */
176 clear_graphic(const string &name) {
177  _graphics.erase(name);
178 }
179 
180 /**
181  *
182  */
183 void TextPropertiesManager::
184 write(std::ostream &out, int indent_level) const {
185  Properties::const_iterator pi;
186  for (pi = _properties.begin(); pi != _properties.end(); ++pi) {
187  indent(out, indent_level)
188  << "TextProperties " << (*pi).first << ":\n";
189  (*pi).second.write(out, indent_level + 2);
190  }
191 }
192 
193 /**
194  * Returns the pointer to the global TextPropertiesManager object.
195  */
198  if (_global_ptr == nullptr) {
199  _global_ptr = new TextPropertiesManager;
200  }
201  return _global_ptr;
202 }
203 
204 /**
205  * Returns a pointer to the TextProperties with the indicated name, or NULL if
206  * there is no properties with that name.
207  */
209 get_properties_ptr(const string &name) {
210  Properties::const_iterator pi;
211  pi = _properties.find(name);
212  if (pi != _properties.end()) {
213  return &(*pi).second;
214  }
215  return nullptr;
216 }
217 
218 /**
219  * Returns a pointer to the TextGraphic with the indicated name, or NULL if
220  * there is no graphic with that name.
221  */
223 get_graphic_ptr(const string &name) {
224  Graphics::const_iterator pi;
225  pi = _graphics.find(name);
226  if (pi != _graphics.end()) {
227  return &(*pi).second;
228  }
229  return nullptr;
230 }
void set_graphic(const std::string &name, const TextGraphic &graphic)
Defines the TextGraphic associated with the indicated name.
TextGraphic get_graphic(const std::string &name)
Returns the TextGraphic associated with the indicated name.
void clear_properties(const std::string &name)
Removes the named TextProperties structure from the manager.
void set_properties(const std::string &name, const TextProperties &properties)
Defines the TextProperties associated with the indicated name.
bool calc_tight_bounds(LPoint3 &min_point, LPoint3 &max_point, const NodePath &other=NodePath(), Thread *current_thread=Thread::get_current_thread()) const
Calculates the minimum and maximum vertices of all Geoms at this NodePath's bottom node and below.
Definition: nodePath.cxx:5419
static TextPropertiesManager * get_global_ptr()
Returns the pointer to the global TextPropertiesManager object.
void clear_graphic(const std::string &name)
Removes the named TextGraphic structure from the manager.
bool has_properties(const std::string &name) const
Returns true if a TextProperties structure has been associated with the indicated name,...
std::ostream & indent(std::ostream &out, int indent_level)
A handy function for doing text formatting.
Definition: indent.cxx:20
bool has_graphic(const std::string &name) const
Returns true if a TextGraphic structure has been associated with the indicated name,...
const TextProperties * get_properties_ptr(const std::string &name)
Returns a pointer to the TextProperties with the indicated name, or NULL if there is no properties wi...
This defines the set of visual properties that may be assigned to the individual characters of the te...
This defines all of the TextProperties structures that might be referenced by name from an embedded t...
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
TextProperties get_properties(const std::string &name)
Returns the TextProperties associated with the indicated name.
const TextGraphic * get_graphic_ptr(const std::string &name)
Returns a pointer to the TextGraphic with the indicated name, or NULL if there is no graphic with tha...
NodePath is the fundamental system for disambiguating instances, and also provides a higher-level int...
Definition: nodePath.h:161
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
This defines a special model that has been constructed for the purposes of embedding an arbitrary gra...
Definition: textGraphic.h:37