MyGUI 3.5.3
MyGUI_ResourceSkin.cpp
Go to the documentation of this file.
1/*
2 * This source file is part of MyGUI. For the latest info, see http://mygui.info/
3 * Distributed under the MIT License
4 * (See accompanying file COPYING.MIT or copy at http://opensource.org/licenses/MIT)
5 */
6
7#include "MyGUI_Precompiled.h"
12
13#include <pugixml.hpp>
14
15namespace MyGUI
16{
17
18 ResourceSkin::~ResourceSkin()
19 {
20 clear();
21 mStates.clear();
22 }
23
24 void ResourceSkin::deserialization(xml::ElementPtr _node, Version _version)
25 {
26 Base::deserialization(_node, _version);
27
28 const std::string& stateCategory = SubWidgetManager::getInstance().getStateCategoryName();
29
30 std::string_view name{_node.node().attribute("name").value()};
31 std::string texture{_node.node().attribute("texture").value()};
32 IntSize size;
33 std::string_view tmp{_node.node().attribute("size").value()};
34 if (!tmp.empty())
35 size = IntSize::parse(tmp);
36
37 LanguageManager& localizator = LanguageManager::getInstance();
38
39 // helper class for binding subskins
40 SubWidgetBinding bind;
41
42 // tags replacement support for Skins
43 if (_version >= Version(1, 1))
44 {
45 texture = localizator.replaceTags(texture);
46 }
47
48 setInfo(size, texture);
49
50 tmp = _node.node().attribute("mask").value();
51 if (!tmp.empty())
52 addProperty("MaskPick", tmp);
53
54 for (auto basis : _node.node())
55 {
56 if (basis.name() == std::string_view("Property"))
57 {
58 std::string_view key{basis.attribute("key").value()};
59 if (key.empty())
60 continue;
61 std::string value{basis.attribute("value").value()};
62 if (value.empty())
63 continue;
64
65 // tags replacement support for Skins
66 if (_version >= Version(1, 1))
67 {
68 value = localizator.replaceTags(value);
69 }
70
71 addProperty(key, value);
72 }
73 else if (basis.name() == std::string_view("Child"))
74 {
75 ChildSkinInfo child(
76 basis.attribute("type").value(),
77 WidgetStyle::parse(basis.attribute("style").value()),
78 basis.attribute("skin").value(),
79 IntCoord::parse(basis.attribute("offset").value()),
80 Align::parse(basis.attribute("align").value()),
81 basis.attribute("layer").value(),
82 basis.attribute("name").value());
83
84 for (auto child_params : basis.children("Property"))
85 child.addParam(child_params.attribute("key").value(), child_params.attribute("value").value());
86
87 addChild(child);
88 }
89 else if (basis.name() == std::string_view("BasisSkin"))
90 {
91 std::string_view basisSkinType{basis.attribute("type").value()};
92 IntCoord offset;
93 Align align = Align::Default;
94 tmp = basis.attribute("offset").value();
95 if (!tmp.empty())
96 offset = IntCoord::parse(tmp);
97 tmp = basis.attribute("align").value();
98 if (!tmp.empty())
99 align = Align::parse(tmp);
100
101 bind.create(offset, align, basisSkinType);
102
103 // check for new state format
104 bool new_format = false;
105 // if version < 1.0 rename states
106 if (_version < Version(1, 0))
107 {
108 for (auto state : basis)
109 {
110 if (state.name() == std::string_view("State"))
111 {
112 std::string_view name_state = state.attribute("name").value();
113 if (name_state == "normal_checked" || name_state == "normal_check")
114 {
115 new_format = true;
116 break;
117 }
118 }
119 }
120 }
121
122 for (auto state : basis)
123 {
124 if (state.name() == std::string_view("State"))
125 {
126 std::string_view basisStateName{state.attribute("name").value()};
127
128 // if version < 1.0 rename states
129 if (_version < Version(1, 0))
130 {
131 // obsolete new types
132 if (basisStateName == "disable_check")
133 basisStateName = "disabled_checked";
134 else if (basisStateName == "normal_check")
135 basisStateName = "normal_checked";
136 else if (basisStateName == "active_check")
137 basisStateName = "highlighted_checked";
138 else if (basisStateName == "pressed_check")
139 basisStateName = "pushed_checked";
140 else if (basisStateName == "disable")
141 basisStateName = "disabled";
142 else if (basisStateName == "active")
143 basisStateName = "highlighted";
144 else if (basisStateName == "select")
145 basisStateName = "pushed";
146 else if (basisStateName == "pressed")
147 {
148 if (new_format)
149 basisStateName = "pushed";
150 else
151 basisStateName = "normal_checked";
152 }
153 }
154
155 IStateInfo* data = nullptr;
156 IObject* object = FactoryManager::getInstance().createObject(stateCategory, basisSkinType);
157 if (object != nullptr)
158 {
159 data = object->castType<IStateInfo>();
160 data->deserialization(state, _version);
161 }
162
163 bind.add(basisStateName, data, name);
164 }
165 }
166
167 addInfo(bind);
168 }
169 }
170 }
171
172 void ResourceSkin::setInfo(const IntSize& _size, std::string_view _texture)
173 {
174 mSize = _size;
175 mTexture = _texture;
176 }
177
178 void ResourceSkin::addInfo(const SubWidgetBinding& _bind)
179 {
180 checkState(_bind.mStates);
181 mBasis.emplace_back(_bind.mType, _bind.mOffset, _bind.mAlign);
182 checkBasis();
183 fillState(_bind.mStates, mBasis.size() - 1);
184 }
185
186 void ResourceSkin::addProperty(std::string_view _key, std::string_view _value)
187 {
188 mapSet(mProperties, _key, _value);
189 }
190
191 void ResourceSkin::addChild(const ChildSkinInfo& _child)
192 {
193 mChilds.push_back(_child);
194 }
195
196 void ResourceSkin::clear()
197 {
198 for (auto& state : mStates)
199 {
200 for (auto& info : state.second)
201 delete info;
202 }
203 }
204
205 void ResourceSkin::checkState(const MapStateInfo& _states)
206 {
207 for (const auto& state : _states)
208 {
209 checkState(state.first);
210 }
211 }
212
213 void ResourceSkin::checkState(std::string_view _name)
214 {
215 MapWidgetStateInfo::const_iterator iter = mStates.find(_name);
216 if (iter == mStates.end())
217 {
218 mStates.emplace(_name, VectorStateInfo());
219 }
220 }
221
222 void ResourceSkin::checkBasis()
223 {
224 // increase offset array size by number of subwidgets
225 for (auto& state : mStates)
226 {
227 state.second.resize(mBasis.size());
228 }
229 }
230
231 void ResourceSkin::fillState(const MapStateInfo& _states, size_t _index)
232 {
233 for (const auto& state : _states)
234 {
235 mStates[state.first][_index] = state.second;
236 }
237 }
238
239 const IntSize& ResourceSkin::getSize() const
240 {
241 return mSize;
242 }
243
244 const std::string& ResourceSkin::getTextureName() const
245 {
246 return mTexture;
247 }
248
249 const VectorSubWidgetInfo& ResourceSkin::getBasisInfo() const
250 {
251 return mBasis;
252 }
253
254 const MapWidgetStateInfo& ResourceSkin::getStateInfo() const
255 {
256 return mStates;
257 }
258
259 const MapString& ResourceSkin::getProperties() const
260 {
261 return mProperties;
262 }
263
264 const VectorChildSkinInfo& ResourceSkin::getChild() const
265 {
266 return mChilds;
267 }
268
269 const std::string& ResourceSkin::getSkinName() const
270 {
271 return mSkinName;
272 }
273
274} // namespace MyGUI
std::map< std::string, VectorStateInfo, std::less<> > MapWidgetStateInfo
void mapSet(Map &map, std::string_view key, const Value &value)
std::map< std::string, std::string, std::less<> > MapString
Definition MyGUI_Types.h:39
std::vector< SubWidgetInfo > VectorSubWidgetInfo
std::vector< IStateInfo * > VectorStateInfo
types::TCoord< int > IntCoord
Definition MyGUI_Types.h:35
types::TSize< int > IntSize
Definition MyGUI_Types.h:29
std::map< std::string, IStateInfo *, std::less<> > MapStateInfo
std::vector< ChildSkinInfo > VectorChildSkinInfo
static TCoord< int > parse(std::string_view _value)
static TSize< int > parse(std::string_view _value)