MyGUI 3.4.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
13namespace MyGUI
14{
15
17 {
18 clear();
19 mStates.clear();
20 }
21
23 {
24 Base::deserialization(_node, _version);
25
26 const std::string& stateCategory = SubWidgetManager::getInstance().getStateCategoryName();
27
28 // парсим атрибуты скина
29 std::string name;
30 std::string texture;
31 std::string tmp;
32 IntSize size;
33 _node->findAttribute("name", name);
34 _node->findAttribute("texture", texture);
35 if (_node->findAttribute("size", tmp))
36 size = IntSize::parse(tmp);
37
39
40 // вспомогательный класс для биндинга сабскинов
42
43 // tags replacement support for Skins
44 if (_version >= Version(1, 1))
45 {
46 texture = localizator.replaceTags(texture);
47 }
48
49 setInfo(size, texture);
50
51 // проверяем маску
52 if (_node->findAttribute("mask", tmp))
53 addProperty("MaskPick", tmp);
54
55 // берем детей и крутимся, цикл с саб скинами
57 while (basis.next())
58 {
59 if (basis->getName() == "Property")
60 {
61 // загружаем свойства
62 std::string key;
63 std::string value;
64 if (!basis->findAttribute("key", key))
65 continue;
66 if (!basis->findAttribute("value", value))
67 continue;
68
69 // tags replacement support for Skins
70 if (_version >= Version(1, 1))
71 {
72 value = localizator.replaceTags(value);
73 }
74
75 // добавляем свойство
76 addProperty(key, value);
77 }
78 else if (basis->getName() == "Child")
79 {
80 ChildSkinInfo child(
81 basis->findAttribute("type"),
82 WidgetStyle::parse(basis->findAttribute("style")),
83 basis->findAttribute("skin"),
84 IntCoord::parse(basis->findAttribute("offset")),
85 Align::parse(basis->findAttribute("align")),
86 basis->findAttribute("layer"),
87 basis->findAttribute("name"));
88
89 xml::ElementEnumerator child_params = basis->getElementEnumerator();
90 while (child_params.next("Property"))
91 child.addParam(child_params->findAttribute("key"), child_params->findAttribute("value"));
92
93 addChild(child);
94 //continue;
95 }
96 else if (basis->getName() == "BasisSkin")
97 {
98 // парсим атрибуты
99 std::string basisSkinType;
100 std::string tmp_str;
101 IntCoord offset;
102 Align align = Align::Default;
103 basis->findAttribute("type", basisSkinType);
104 if (basis->findAttribute("offset", tmp_str))
105 offset = IntCoord::parse(tmp_str);
106 if (basis->findAttribute("align", tmp_str))
107 align = Align::parse(tmp_str);
108
109 bind.create(offset, align, basisSkinType);
110
111 // берем детей и крутимся, цикл со стейтами
113
114 // проверяем на новый формат стейтов
115 bool new_format = false;
116 // если версия меньше 1.0 то переименовываем стейты
117 if (_version < Version(1, 0))
118 {
119 while (state.next())
120 {
121 if (state->getName() == "State")
122 {
123 std::string_view name_state = state->findAttribute("name");
124 if ((name_state == "normal_checked") || (state->findAttribute("name") == "normal_check"))
125 {
126 new_format = true;
127 break;
128 }
129 }
130 }
131 // обновляем
132 state = basis->getElementEnumerator();
133 }
134
135 while (state.next())
136 {
137 if (state->getName() == "State")
138 {
139 // парсим атрибуты стейта
140 std::string basisStateName;
141 state->findAttribute("name", basisStateName);
142
143 // если версия меньше 1.0 то переименовываем стейты
144 if (_version < Version(1, 0))
145 {
146 // это обсолет новых типов
147 if (basisStateName == "disable_check")
148 basisStateName = "disabled_checked";
149 else if (basisStateName == "normal_check")
150 basisStateName = "normal_checked";
151 else if (basisStateName == "active_check")
152 basisStateName = "highlighted_checked";
153 else if (basisStateName == "pressed_check")
154 basisStateName = "pushed_checked";
155 else if (basisStateName == "disable")
156 basisStateName = "disabled";
157 else if (basisStateName == "active")
158 basisStateName = "highlighted";
159 else if (basisStateName == "select")
160 basisStateName = "pushed";
161 else if (basisStateName == "pressed")
162 {
163 if (new_format)
164 basisStateName = "pushed";
165 else
166 basisStateName = "normal_checked";
167 }
168 }
169
170 // конвертируем инфу о стейте
171 IStateInfo* data = nullptr;
172 IObject* object = FactoryManager::getInstance().createObject(stateCategory, basisSkinType);
173 if (object != nullptr)
174 {
175 data = object->castType<IStateInfo>();
176 data->deserialization(state.current(), _version);
177 }
178
179 // добавляем инфо о стайте
180 bind.add(basisStateName, data, name);
181 }
182 }
183
184 // теперь всё вместе добавляем в скин
185 addInfo(bind);
186 }
187 }
188 }
189
190 void ResourceSkin::setInfo(const IntSize& _size, std::string_view _texture)
191 {
192 mSize = _size;
193 mTexture = _texture;
194 }
195
196 void ResourceSkin::addInfo(const SubWidgetBinding& _bind)
197 {
198 checkState(_bind.mStates);
199 mBasis.emplace_back(_bind.mType, _bind.mOffset, _bind.mAlign);
200 checkBasis();
201 fillState(_bind.mStates, mBasis.size() - 1);
202 }
203
204 void ResourceSkin::addProperty(std::string_view _key, std::string_view _value)
205 {
206 mapSet(mProperties, _key, _value);
207 }
208
209 void ResourceSkin::addChild(const ChildSkinInfo& _child)
210 {
211 mChilds.push_back(_child);
212 }
213
214 void ResourceSkin::clear()
215 {
216 for (auto& state : mStates)
217 {
218 for (auto& info : state.second)
219 delete info;
220 }
221 }
222
223 void ResourceSkin::checkState(const MapStateInfo& _states)
224 {
225 for (const auto& state : _states)
226 {
227 checkState(state.first);
228 }
229 }
230
231 void ResourceSkin::checkState(std::string_view _name)
232 {
233 // ищем такой же ключ
234 MapWidgetStateInfo::const_iterator iter = mStates.find(_name);
235 if (iter == mStates.end())
236 {
237 // добавляем новый стейт
238 mStates.emplace(_name, VectorStateInfo());
239 }
240 }
241
242 void ResourceSkin::checkBasis()
243 {
244 // и увеличиваем размер смещений по колличеству сабвиджетов
245 for (auto& state : mStates)
246 {
247 state.second.resize(mBasis.size());
248 }
249 }
250
251 void ResourceSkin::fillState(const MapStateInfo& _states, size_t _index)
252 {
253 for (const auto& state : _states)
254 {
255 mStates[state.first][_index] = state.second;
256 }
257 }
258
260 {
261 return mSize;
262 }
263
264 const std::string& ResourceSkin::getTextureName() const
265 {
266 return mTexture;
267 }
268
270 {
271 return mBasis;
272 }
273
275 {
276 return mStates;
277 }
278
280 {
281 return mProperties;
282 }
283
285 {
286 return mChilds;
287 }
288
289 const std::string& ResourceSkin::getSkinName() const
290 {
291 return mSkinName;
292 }
293
294} // namespace MyGUI
static FactoryManager & getInstance()
IObject * createObject(std::string_view _category, std::string_view _type)
Type * castType(bool _throw=true)
virtual void deserialization(xml::ElementPtr, Version)
static LanguageManager & getInstance()
UString replaceTags(const UString &_line)
const MapWidgetStateInfo & getStateInfo() const
const std::string & getSkinName() const
const IntSize & getSize() const
const MapString & getProperties() const
const VectorSubWidgetInfo & getBasisInfo() const
void deserialization(xml::ElementPtr _node, Version _version) override
const std::string & getTextureName() const
const VectorChildSkinInfo & getChild() const
void create(const IntCoord &_coord, Align _aligin, std::string_view _type)
void add(std::string_view _name, IStateInfo *_data, std::string_view _skin)
const std::string & getStateCategoryName() const
static SubWidgetManager & getInstance()
bool findAttribute(std::string_view _name, std::string &_value)
ElementEnumerator getElementEnumerator()
const std::string & getName() const
Element * ElementPtr
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:40
std::vector< SubWidgetInfo > VectorSubWidgetInfo
std::vector< IStateInfo * > VectorStateInfo
types::TCoord< int > IntCoord
Definition MyGUI_Types.h:36
types::TSize< int > IntSize
Definition MyGUI_Types.h:30
std::map< std::string, IStateInfo *, std::less<> > MapStateInfo
std::vector< ChildSkinInfo > VectorChildSkinInfo
static Align parse(std::string_view _value)
void addParam(std::string_view _key, std::string_view _value)
static WidgetStyle parse(std::string_view _value)
static TCoord< int > parse(std::string_view _value)
static TSize< int > parse(std::string_view _value)