1 // Description:
2 // Different kinds of selectable factories.
3 //
4 // Copyright (C) 2001 Frank Becker
5 //
6 // This program is free software; you can redistribute it and/or modify it under
7 // the terms of the GNU General Public License as published by the Free Software
8 // Foundation; either version 2 of the License, or (at your option) any later
9 // version.
10 //
11 // This program is distributed in the hope that it will be useful, but WITHOUT
12 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13 // FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details
14 //
15 #include "SelectableFactory.hpp"
16
17 using namespace std;
18
19 hash_map<
20 const string,
21 SelectableFactory*,
22 hash<const string>, std::equal_to<const string> > SelectableFactory::_sfMap;
23
24 bool SelectableFactory::_initialized = false;
25
getFactory(const string & name)26 SelectableFactory *SelectableFactory::getFactory( const string &name)
27 {
28 XTRACE();
29 if( ! _initialized)
30 {
31 _initialized = true;
32
33 new ActionItemFactory();
34 new MenuItemFactory();
35 new TextItemFactory();
36 new BoolFactory();
37 new EnumFactory();
38 new FloatFactory();
39 new LeaderBoardFactory();
40 new ResolutionFactory();
41 new BindKeyFactory();
42 }
43 return findHash<const string>( name, _sfMap);
44 }
45
cleanup(void)46 void SelectableFactory::cleanup( void)
47 {
48 hash_map< const string,
49 SelectableFactory*,
50 hash<const string> >::const_iterator ci;
51 for( ci=_sfMap.begin(); ci!=_sfMap.end(); ci++)
52 {
53 SelectableFactory *sf = ci->second;
54 delete sf;
55 }
56 _sfMap.clear();
57
58 _initialized = false;
59 }
60
posToPoint2D(const string & pos,Point2D & point)61 void SelectableFactory::posToPoint2D( const string &pos, Point2D &point)
62 {
63 if( pos == "")
64 {
65 point.x = 100;
66 point.y = 100;
67 return;
68 }
69
70 Tokenizer t(pos);
71 point.x = (float)atof( t.next().c_str());
72 point.y = (float)atof( t.next().c_str());
73 }
74
getAttribute(const TiXmlElement * elem,string attr)75 string SelectableFactory::getAttribute( const TiXmlElement* elem, string attr)
76 {
77 const char *attrVal = elem->Attribute( attr.c_str());
78 if( attrVal)
79 {
80 return string(attrVal);
81 }
82
83 return( string(""));
84 }
85
getBasics(TiXmlElement * elem,Point2D & pos,string & text,string & info,bool & enabled)86 void SelectableFactory::getBasics(
87 TiXmlElement* elem,
88 Point2D &pos,
89 string &text,
90 string &info,
91 bool &enabled)
92 {
93 posToPoint2D( getAttribute( elem, "Position"), pos);
94 text = getAttribute( elem, "Text");
95 info = getAttribute( elem, "Info");
96 enabled = elem->Attribute( "Dis") == 0;
97 }
98
99 //------------------------------------------------------------------------------
100
ActionItemFactory(void)101 ActionItemFactory::ActionItemFactory( void)
102 {
103 _sfMap[ "ActionItem"] = this;
104 }
105
~ActionItemFactory()106 ActionItemFactory::~ActionItemFactory()
107 {
108 }
109
createSelectable(TiXmlNode * node)110 Selectable *ActionItemFactory::createSelectable( TiXmlNode *node)
111 {
112 TiXmlElement* elem = node->ToElement();
113
114 BoundingBox r;
115 string text;
116 string info;
117 bool enabled;
118 getBasics( elem, r.min, text, info, enabled);
119
120 string action = getAttribute( elem, "Action");
121
122 return new ActionSelectable( enabled, r, action, text, info);
123 }
124
125 //------------------------------------------------------------------------------
126
127 //------------------------------------------------------------------------------
128
MenuItemFactory(void)129 MenuItemFactory::MenuItemFactory( void)
130 {
131 _sfMap[ "Menu"] = this;
132 }
133
~MenuItemFactory()134 MenuItemFactory::~MenuItemFactory()
135 {
136 }
137
createSelectable(TiXmlNode * node)138 Selectable *MenuItemFactory::createSelectable( TiXmlNode *node)
139 {
140 TiXmlElement* elem = node->ToElement();
141
142 BoundingBox r;
143 string text;
144 string info;
145 bool enabled;
146 getBasics( elem, r.min, text, info, enabled);
147
148 return new MenuSelectable( node, enabled, r, text, info);
149 }
150
151 //------------------------------------------------------------------------------
152
TextItemFactory(void)153 TextItemFactory::TextItemFactory( void)
154 {
155 _sfMap[ "TextItem"] = this;
156 }
157
~TextItemFactory()158 TextItemFactory::~TextItemFactory()
159 {
160 }
161
createSelectable(TiXmlNode * node)162 Selectable *TextItemFactory::createSelectable( TiXmlNode *node)
163 {
164 TiXmlElement* elem = node->ToElement();
165
166 BoundingBox r;
167 string text;
168 string info;
169 bool enabled;
170 getBasics( elem, r.min, text, info, enabled);
171
172 string size = getAttribute( elem, "Size");
173 float fSize = 0.65f;
174 if( size != "")
175 {
176 fSize = (float)atof( size.c_str());
177 }
178
179 return new TextOnlySelectable( enabled, r, text, info, true, fSize, 1.0,1.0,1.0);
180 }
181
182 //------------------------------------------------------------------------------
183
BoolFactory(void)184 BoolFactory::BoolFactory( void)
185 {
186 _sfMap[ "Bool"] = this;
187 }
188
~BoolFactory()189 BoolFactory::~BoolFactory()
190 {
191 }
192
createSelectable(TiXmlNode * node)193 Selectable *BoolFactory::createSelectable( TiXmlNode *node)
194 {
195 TiXmlElement* elem = node->ToElement();
196
197 BoundingBox r;
198 string text;
199 string info;
200 bool enabled;
201 getBasics( elem, r.min, text, info, enabled);
202
203 string var = getAttribute( elem, "Variable");
204
205 return new BoolSelectable( enabled, r, text, info, var);
206 }
207
208 //------------------------------------------------------------------------------
209
EnumFactory(void)210 EnumFactory::EnumFactory( void)
211 {
212 _sfMap[ "Enum"] = this;
213 }
214
~EnumFactory()215 EnumFactory::~EnumFactory()
216 {
217 }
218
createSelectable(TiXmlNode * node)219 Selectable *EnumFactory::createSelectable( TiXmlNode *node)
220 {
221 TiXmlElement* elem = node->ToElement();
222
223 BoundingBox r;
224 string text;
225 string info;
226 bool enabled;
227 getBasics( elem, r.min, text, info, enabled);
228
229 string var = getAttribute( elem, "Variable");
230 string values = getAttribute( elem, "Values");
231
232 return new EnumSelectable( enabled, r, text, info, var, values);
233 }
234
235 //------------------------------------------------------------------------------
236
FloatFactory(void)237 FloatFactory::FloatFactory( void)
238 {
239 _sfMap[ "Float"] = this;
240 }
241
~FloatFactory()242 FloatFactory::~FloatFactory()
243 {
244 }
245
createSelectable(TiXmlNode * node)246 Selectable *FloatFactory::createSelectable( TiXmlNode *node)
247 {
248 TiXmlElement* elem = node->ToElement();
249
250 BoundingBox r;
251 string text;
252 string info;
253 bool enabled;
254 getBasics( elem, r.min, text, info, enabled);
255
256 string var = getAttribute( elem, "Variable");
257 string range = getAttribute( elem, "Range");
258 string sliderOffset = getAttribute( elem, "SliderOffset");
259
260 return new FloatSelectable( enabled, r, text, info, var, range, sliderOffset);
261 }
262
263 //------------------------------------------------------------------------------
264
LeaderBoardFactory(void)265 LeaderBoardFactory::LeaderBoardFactory( void)
266 {
267 _sfMap[ "LeaderBoard"] = this;
268 }
269
~LeaderBoardFactory()270 LeaderBoardFactory::~LeaderBoardFactory()
271 {
272 }
273
createSelectable(TiXmlNode * node)274 Selectable *LeaderBoardFactory::createSelectable( TiXmlNode *node)
275 {
276 TiXmlElement* elem = node->ToElement();
277
278 BoundingBox r;
279 string text;
280 string info;
281 bool enabled;
282 getBasics( elem, r.min, text, info, enabled);
283
284 return new LeaderBoardSelectable( enabled, r, text, info);
285 }
286
287 //------------------------------------------------------------------------------
288
ResolutionFactory(void)289 ResolutionFactory::ResolutionFactory( void)
290 {
291 _sfMap[ "Resolution"] = this;
292 }
293
~ResolutionFactory()294 ResolutionFactory::~ResolutionFactory()
295 {
296 }
297
createSelectable(TiXmlNode * node)298 Selectable *ResolutionFactory::createSelectable( TiXmlNode *node)
299 {
300 TiXmlElement* elem = node->ToElement();
301
302 BoundingBox r;
303 string text;
304 string info;
305 bool enabled;
306 getBasics( elem, r.min, text, info, enabled);
307
308 return new ResolutionSelectable( enabled, r, text, info);
309 }
310
311 //------------------------------------------------------------------------------
312
BindKeyFactory(void)313 BindKeyFactory::BindKeyFactory( void)
314 {
315 _sfMap[ "BindKey"] = this;
316 }
317
~BindKeyFactory()318 BindKeyFactory::~BindKeyFactory()
319 {
320 }
321
createSelectable(TiXmlNode * node)322 Selectable *BindKeyFactory::createSelectable( TiXmlNode *node)
323 {
324 TiXmlElement* elem = node->ToElement();
325
326 BoundingBox r;
327 string text;
328 string info;
329 bool enabled;
330 getBasics( elem, r.min, text, info, enabled);
331
332 string action = getAttribute( elem, "Action");
333
334 string size = getAttribute( elem, "Size");
335 float fSize = 1.0f;
336 if( size != "")
337 {
338 fSize = (float)atof( size.c_str());
339 }
340
341 return new BindKeySelectable( enabled, r, text, info, action, fSize);
342 }
343
344 //------------------------------------------------------------------------------
345