1 // Copyright (C) 2008, 2009 Ben Asselstine
3 // This program is free software; you can redistribute it and/or modify
4 // it under the terms of the GNU General Public License as published by
5 // the Free Software Foundation; either version 3 of the License, or
6 // (at your option) any later version.
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU Library General Public License for more details.
13 // You should have received a copy of the GNU General Public License
14 // along with this program; if not, write to the Free Software
15 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 #include "shieldstyle.h"
21 #include "GraphicsCache.h"
22 #include "xmlhelper.h"
24 #include "shieldset.h"
25 #include "gui/image-helpers.h"
27 std::string ShieldStyle::d_tag = "shieldstyle";
29 //#define debug(x) {cerr<<__FILE__<<": "<<__LINE__<<": "<<x<<endl<<flush;}
32 ShieldStyle::ShieldStyle(ShieldStyle::Type type)
40 ShieldStyle::ShieldStyle(XML_Helper* helper)
41 :d_image(0), d_mask(0)
44 helper->getData(type_str, "type");
45 d_type = shieldStyleTypeFromString(type_str);
46 helper->getData(d_image_name, "image");
49 ShieldStyle::~ShieldStyle()
54 std::string ShieldStyle::shieldStyleTypeToString(const ShieldStyle::Type type)
58 case ShieldStyle::SMALL:
59 return "ShieldStyle::SMALL";
61 case ShieldStyle::MEDIUM:
62 return "ShieldStyle::MEDIUM";
64 case ShieldStyle::LARGE:
65 return "ShieldStyle::LARGE";
68 return "ShieldStyle::SMALL";
71 ShieldStyle::Type ShieldStyle::shieldStyleTypeFromString(const std::string str)
73 if (str.size() > 0 && isdigit(str.c_str()[0]))
74 return ShieldStyle::Type(atoi(str.c_str()));
75 if (str == "ShieldStyle::SMALL")
76 return ShieldStyle::SMALL;
77 else if (str == "ShieldStyle::MEDIUM")
78 return ShieldStyle::MEDIUM;
79 else if (str == "ShieldStyle::LARGE")
80 return ShieldStyle::LARGE;
81 return ShieldStyle::SMALL;
84 bool ShieldStyle::save(XML_Helper *helper) const
88 retval &= helper->openTag(d_tag);
89 std::string s = shieldStyleTypeToString(ShieldStyle::Type(d_type));
90 retval &= helper->saveData("type", s);
91 retval &= helper->saveData("image", d_image_name);
92 retval &= helper->closeTag();
95 void ShieldStyle::instantiateImages(std::string filename, Shieldset *s)
97 if (filename.empty() == true)
99 // The shield image consists of two halves. On the left is the shield
100 // image, on the right the mask.
101 debug("loading shield file: " << filename);
102 std::vector<PixMask* > half = disassemble_row(filename, 2);
104 debug("failed to load image file");
110 case ShieldStyle::SMALL:
111 xsize = s->getSmallWidth(); ysize = s->getSmallHeight(); break;
112 case ShieldStyle::MEDIUM:
113 xsize = s->getMediumWidth(); ysize = s->getMediumHeight(); break;
114 case ShieldStyle::LARGE:
115 xsize = s->getLargeWidth(); ysize = s->getLargeHeight(); break;
117 PixMask::scale(half[0], xsize, ysize);
118 PixMask::scale(half[1], xsize, ysize);
123 void ShieldStyle::uninstantiateImages()
125 if (getImage() != NULL)
130 if (getMask() != NULL)