2 * This file implements general functions and classes
3 * from the defaction.hh header
5 * Copyright (C) 2008 Hermann Walth
7 * This file is part of OpenStranded
9 * OpenStranded is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation, either version 3 of the License, or
12 * (at your option) any later version.
14 * OpenStranded is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with OpenStranded. If not, see <http://www.gnu.org/licenses/>.
27 #include "../s2string.hh"
28 #include "../kingdom.hh"
29 #include "../error.hh"
30 #include "defaction.hh"
38 def::ActionList::ActionList ():
39 std::map
<std::string
, def::Action
*> () {}
42 // Destructor, deletes all list's elements
43 def::ActionList::~ActionList ()
45 std::map
<std::string
, def::Action
*>::iterator iter
;
46 for (iter
= this->begin (); iter
!= this->end (); iter
++)
47 delete (iter
->second
);
50 // Wrapper operator[] method for const char* strings
52 def::ActionList::listItem (const char* index
)
54 return (*this) [std::string (index
)];
55 //return (this->find (std::string (index)))->second;
60 * Action classes method definitions
63 def::Action::Action (ID kingdom
)
65 this->kingdomID
= kingdom
;
70 def::IDAction::IDAction (ID kingdom
):
73 def::NameAction::NameAction (ID kingdom
):
76 def::ModelAction::ModelAction (ID kingdom
):
79 def::IconAction::IconAction (ID kingdom
):
82 def::XAction::XAction (ID kingdom
):
85 def::YAction::YAction (ID kingdom
):
88 def::ZAction::ZAction (ID kingdom
):
91 def::ScaleAction::ScaleAction (ID kingdom
):
94 def::RAction::RAction (ID kingdom
):
97 def::GAction::GAction (ID kingdom
):
100 def::BAction::BAction (ID kingdom
):
103 def::ColorAction::ColorAction (ID kingdom
):
106 def::AutofadeAction::AutofadeAction (ID kingdom
):
109 def::AlphaAction::AlphaAction (ID kingdom
):
112 def::ShineAction::ShineAction (ID kingdom
):
118 def::IDAction::operator() (std::string rtext
)
121 std::istringstream (rtext
, std::ios::in
) >> newID
;
125 if (Kingdom::kingdomList
.find (kingdomID
) != Kingdom::kingdomList
.end ())
126 if (!Kingdom::kingdomList
[kingdomID
]->typeExists(newID
))
127 Kingdom::kingdomList
[kingdomID
]->insertType (newID
);
130 std::ostringstream
oss (std::ios::out
);
131 oss
<< "Type #" << newID
<< " has already been declared!";
132 throw StrandedFatal (oss
.str ());
135 throw StrandedFatal ("Kingdom does not exist!");
136 //std::cerr << "Fatal: kingdom does not exist!" << std::endl;
142 def::NameAction::operator() (std::string rtext
)
144 if (currentType
== 0)
146 throw StrandedError ("No type ID specified, assignment of value \"name\" not possible");
150 if (this->lastType
!= currentType
)
151 Kingdom::kingdomList
[kingdomID
]->getType (currentType
) ->setName (rtext
);
153 throw StrandedError ("Double assignment of value \"name\" in object type #" + currentType
);
159 def::ModelAction::operator() (std::string rtext
)
161 if (currentType
== 0)
163 throw StrandedError ("No type ID specified, assignment of value \"model\" not possible");
167 if (this->lastType
!= currentType
)
168 Kingdom::kingdomList
[kingdomID
]->getType (currentType
)->setModel (rtext
);
170 throw StrandedError ("Double assignment of value \"model\" in type #" + currentType
);
176 def::IconAction::operator() (std::string rtext
)
178 if (currentType
== 0)
179 throw StrandedError ("No type ID specified, assignment of value \"icon\" not possible");
181 if (this->lastType
!= currentType
)
182 Kingdom::kingdomList
[kingdomID
]->getType (currentType
) ->setIconPath (rtext
);
184 throw StrandedError ("Double assignment of value \"icon\" in type #" + currentType
);
190 def::XAction::operator() (std::string rtext
)
193 std::istringstream (rtext
) >> newScaleX
;
195 if (currentType
== 0)
197 std::cerr
<< "Error: No type ID specified, assignment of value \"x\" not possible" << std::endl
;
203 std::cerr
<< "Error: Invalid value \"x\"; fallback to 1.0 in type #" << currentType
<< std::endl
;
207 if (this->lastType
!= currentType
)
208 Kingdom::kingdomList
[kingdomID
]->getType (currentType
) ->setScaleX (newScaleX
);
210 std::cerr
<< "Error: Double assignment of value \"x\" in type #" << currentType
<< std::endl
;
216 def::YAction::operator() (std::string rtext
)
219 std::istringstream (rtext
) >> newScaleY
;
221 if (currentType
== 0)
223 std::cerr
<< "Error: No type ID specified, assignment of value \"y\" not possible" << std::endl
;
229 std::cerr
<< "Error: Invalid value \"y\"; fallback to 1.0 in type #" << currentType
<< std::endl
;
233 if (this->lastType
!= currentType
)
234 Kingdom::kingdomList
[kingdomID
]->getType (currentType
) ->setScaleY (newScaleY
);
236 std::cerr
<< "Error: Double assignment of value \"y\" in Object type #" << currentType
<< std::endl
;
242 def::ZAction::operator() (std::string rtext
)
245 std::istringstream (rtext
) >> newScaleZ
;
247 if (currentType
== 0)
249 std::cerr
<< "Error: No type ID specified, assignment of value \"z\" not possible" << std::endl
;
255 std::cerr
<< "Error: Invalid value \"z\"; fallback to 1.0 in type #" << currentType
<< std::endl
;
259 if (this->lastType
!= currentType
)
260 Kingdom::kingdomList
[kingdomID
]->getType (currentType
) ->setScaleZ (newScaleZ
);
262 std::cerr
<< "Error: Double assignment of value \"z\" in type #" << currentType
<< std::endl
;
268 def::ScaleAction::operator() (std::string rtext
)
270 std::string scaleValues
[3];
275 if (currentType
== 0)
277 std::cerr
<< "Error: No type ID specified, assignment of value \"scale\" not possible" << std::endl
;
281 warnFlag
= s2str::explode (rtext
, scaleValues
, ",", 3);
283 // Display error message when number of arguments wasn't either 1 or 3
284 if (warnFlag
!= EXPLODE_OK
&& !inFlag (warnFlag
, EXPLODE_NO_TOKEN
))
286 std::cerr
<< "Warning: Wrong number of arguments for value \"scale\"" << std::endl
;
290 if (this->lastType
!= currentType
)
292 // Catch special case when only one value was given and needs to be applied
294 if (inFlag (warnFlag
, EXPLODE_NO_TOKEN
))
296 if (!s2str::stof (scaleValues
[0], endValue
))
298 std::cerr
<< "Error: Invalid value in \"scale\"; fallback to 1.0 in type #" << currentType
<< std::endl
;
301 Kingdom::kingdomList
[kingdomID
]->getType (currentType
) ->setScaleX (endValue
);
302 Kingdom::kingdomList
[kingdomID
]->getType (currentType
) ->setScaleY (endValue
);
303 Kingdom::kingdomList
[kingdomID
]->getType (currentType
) ->setScaleZ (endValue
);
308 if (!s2str::stof (scaleValues
[0], endValue
))
310 std::cerr
<< "Error: Invalid x value in \"scale\"; fallback to 1.0 in type #" << currentType
<< std::endl
;
313 Kingdom::kingdomList
[kingdomID
]->getType (currentType
) ->setScaleX (endValue
);
317 if (!s2str::stof (scaleValues
[1], endValue
))
319 std::cerr
<< "Error: Invalid y value in \"scale\"; fallback to 1.0 in type #" << currentType
<< std::endl
;
322 Kingdom::kingdomList
[kingdomID
]->getType (currentType
) ->setScaleY (endValue
);
327 if (!s2str::stof (scaleValues
[2], endValue
))
329 std::cerr
<< "Error: Invalid z value in \"scale\"; fallback to 1.0 in type #" << currentType
<< std::endl
;
332 Kingdom::kingdomList
[kingdomID
]->getType (currentType
) ->setScaleZ (endValue
);
336 std::cerr
<< "Error: Double assignment of value \"scale\" in type #" << currentType
<< std::endl
;
343 def::RAction::operator() (std::string rtext
)
348 if (currentType
== 0)
350 std::cerr
<< "Error: No type ID specified, assignment of value \"r\" not possible" << std::endl
;
354 strValid
= s2str::stoi (rtext
, newColorR
);
355 if (!strValid
|| newColorR
< 0 || newColorR
> 255)
357 std::cerr
<< "Error: Invalid argument for \"r\" value in type #" << currentType
<< ", fallback to 0" << std::endl
;
361 if (this->lastType
!= currentType
)
362 Kingdom::kingdomList
[kingdomID
]->getType (currentType
) ->setColorR ( (uint8_t) newColorR
);
364 std::cerr
<< "Error: Double assignment of value \"r\" in type #" << currentType
<< std::endl
;
370 def::GAction::operator() (std::string rtext
)
375 if (currentType
== 0)
377 std::cerr
<< "Error: No type ID specified, assignment of value \"g\" not possible" << std::endl
;
381 strValid
= s2str::stoi (rtext
, newColorG
);
382 if (!strValid
|| newColorG
< 0 || newColorG
> 255)
384 std::cerr
<< "Error: Invalid argument for \"g\" value in type #" << currentType
<< ", fallback to 0" << std::endl
;
388 if (this->lastType
!= currentType
)
389 Kingdom::kingdomList
[kingdomID
]->getType (currentType
) ->setColorG ( (uint8_t) newColorG
);
391 std::cerr
<< "Error: Double assignment of value \"g\" in type #" << currentType
<< std::endl
;
397 def::BAction::operator() (std::string rtext
)
402 if (currentType
== 0)
404 std::cerr
<< "Error: No type ID specified, assignment of value \"b\" not possible" << std::endl
;
408 strValid
= s2str::stoi (rtext
, newColorB
);
409 if (!strValid
|| newColorB
< 0 || newColorB
> 255)
411 std::cerr
<< "Error: Invalid argument for \"b\" value in type #" << currentType
<< ", fallback to 0" << std::endl
;
415 if (this->lastType
!= currentType
)
416 Kingdom::kingdomList
[kingdomID
]->getType (currentType
) ->setColorB ( (uint8_t) newColorB
);
418 std::cerr
<< "Error: Double assignment of value \"b\" in type #" << currentType
<< std::endl
;
424 def::ColorAction::operator() (std::string rtext
)
426 std::string colorValues
[3];
431 if (currentType
== 0)
433 std::cerr
<< "Error: No type ID specified, assignment of value \"color\" not possible" << std::endl
;
437 warnFlag
= s2str::explode (rtext
, colorValues
, ",", 3);
439 // Display error message when number of arguments wasn't either 1 or 3
440 if (warnFlag
!= EXPLODE_OK
&& !inFlag (warnFlag
, EXPLODE_NO_TOKEN
))
442 std::cerr
<< "Warning: Wrong number of arguments for value \"color\"" << std::endl
;
445 if (this->lastType
!= currentType
)
447 // Catch special case when only one value was given and needs to be applied
449 if (inFlag (warnFlag
, EXPLODE_NO_TOKEN
))
451 strValid
= s2str::stoi (colorValues
[0], endValue
);
452 if (!strValid
|| endValue
< 0 || endValue
> 255)
454 std::cerr
<< "Error: Invalid value in \"color\"; fallback to 0.0 in type #" << currentType
<< std::endl
;
457 Kingdom::kingdomList
[kingdomID
]->getType (currentType
) ->setColorR (endValue
);
458 Kingdom::kingdomList
[kingdomID
]->getType (currentType
) ->setColorG (endValue
);
459 Kingdom::kingdomList
[kingdomID
]->getType (currentType
) ->setColorB (endValue
);
464 strValid
= s2str::stoi (colorValues
[0], endValue
);
465 if (!strValid
|| endValue
< 0 || endValue
> 255)
467 std::cerr
<< "Error: Invalid argument for \"r\" value in \"color\" in type #" << currentType
<< ", fallback to 0" << std::endl
;
471 Kingdom::kingdomList
[kingdomID
]->getType (currentType
) ->setColorR ( (uint8_t) endValue
);
475 strValid
= s2str::stoi (colorValues
[1], endValue
);
476 if (!strValid
|| endValue
< 0 || endValue
> 255)
478 std::cerr
<< "Error: Invalid argument for \"g\" value in \"color\" in type #" << currentType
<< ", fallback to 0" << std::endl
;
482 Kingdom::kingdomList
[kingdomID
]->getType (currentType
) ->setColorG ( (uint8_t) endValue
);
486 strValid
= s2str::stoi (colorValues
[2], endValue
);
487 if (!strValid
|| endValue
> 255)
489 std::cerr
<< "Error: Invalid argument for \"b\" value in \"color\" in type #" << currentType
<< ", fallback to 0" << std::endl
;
493 Kingdom::kingdomList
[kingdomID
]->getType (currentType
) ->setColorB ( (uint8_t) endValue
);
498 std::cerr
<< "Error: Double assignment of value \"color\" in type #" << currentType
<< std::endl
;
505 def::AutofadeAction::operator() (std::string rtext
)
509 if (currentType
== 0)
511 std::cerr
<< "Error: No type id specified, assignment of value \"autofade\" not possible" << std::endl
;
515 if (this->lastType
!= currentType
)
517 if (!s2str::stof (rtext
, endValue
))
519 std::cerr
<< "Error: Invalid argument for value \"autofade\" in type #" << currentType
<< ", fallback to 500" << std::endl
;
523 Kingdom::kingdomList
[kingdomID
]->getType (currentType
) ->setFadingDistance (endValue
);
527 std::cerr
<< "Error: Double assignment of value \"autofade\" in type #" << currentType
<< std::endl
;
534 def::AlphaAction::operator() (std::string rtext
)
538 if (currentType
== 0)
540 std::cerr
<< "Error: No type id specified, assignment of value \"alpha\" not possible" << std::endl
;
544 if (this->lastType
!= currentType
)
546 if (!s2str::stof (rtext
, endValue
) || endValue
< 0.0 || endValue
> 1.0)
548 std::cerr
<< "Error: Invalid argument for value \"alpha\" in type #" << currentType
<< ", fallback to 1.0" << std::endl
;
552 Kingdom::kingdomList
[kingdomID
]->getType (currentType
) ->setAlpha (endValue
);
556 std::cerr
<< "Error: Double assignment of value \"alpha\" in type #" << currentType
<< std::endl
;
563 def::ShineAction::operator() (std::string rtext
)
567 if (currentType
== 0)
569 std::cerr
<< "Error: No type id specified, assignment of value \"shine\" not possible" << std::endl
;
573 if (this->lastType
!= currentType
)
575 if (!s2str::stof (rtext
, endValue
) || endValue
< 0.0 || endValue
> 1.0)
577 std::cerr
<< "Error: Invalid argument for value \"shine\" in type #" << currentType
<< ", fallback to 0.0" << std::endl
;
581 Kingdom::kingdomList
[kingdomID
]->getType (currentType
) ->setShine (endValue
);
585 std::cerr
<< "Error: Double assignment of value \"shine\" in type #" << currentType
<< std::endl
;