MyGUI: MyGUI_ResourceSkin.cpp Source File

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"
8#include "MyGUI_ResourceSkin.h"
9#include "MyGUI_FactoryManager.h"
10#include "MyGUI_LanguageManager.h"
11#include "MyGUI_SubWidgetManager.h"
12
13namespace MyGUI
14{
15
16 ResourceSkin::~ResourceSkin()
17 {
18 clear();
19 mStates.clear();
20 }
21
22 void ResourceSkin::deserialization(xml::ElementPtr _node, Version _version)
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
38 LanguageManager& localizator = LanguageManager::getInstance();
39
40 // вспомогательный класс для биндинга сабскинов
41 SubWidgetBinding bind;
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 // берем детей и крутимся, цикл с саб скинами
56 xml::ElementEnumerator basis = _node->getElementEnumerator();
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 // берем детей и крутимся, цикл со стейтами
112 xml::ElementEnumerator state = basis->getElementEnumerator();
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
259 const IntSize& ResourceSkin::getSize() const
260 {
261 return mSize;
262 }
263
264 const std::string& ResourceSkin::getTextureName() const
265 {
266 return mTexture;
267 }
268
269 const VectorSubWidgetInfo& ResourceSkin::getBasisInfo() const
270 {
271 return mBasis;
272 }
273
274 const MapWidgetStateInfo& ResourceSkin::getStateInfo() const
275 {
276 return mStates;
277 }
278
279 const MapString& ResourceSkin::getProperties() const
280 {
281 return mProperties;
282 }
283
284 const VectorChildSkinInfo& ResourceSkin::getChild() const
285 {
286 return mChilds;
287 }
288
289 const std::string& ResourceSkin::getSkinName() const
290 {
291 return mSkinName;
292 }
293
294} // namespace MyGUI
MyGUI_FactoryManager.h
MyGUI_LanguageManager.h
MyGUI_Precompiled.h
MyGUI_ResourceSkin.h
MyGUI_SubWidgetManager.h
MyGUI::FactoryManager::getInstance
static FactoryManager & getInstance()
MyGUI::FactoryManager::createObject
IObject * createObject(std::string_view _category, std::string_view _type)
Definition MyGUI_FactoryManager.cpp:80
MyGUI::IObject
Definition MyGUI_IObject.h:17
MyGUI::IObject::castType
Type * castType(bool _throw=true)
Definition MyGUI_IObject.h:18
MyGUI::ISerializable::deserialization
virtual void deserialization(xml::ElementPtr, Version)
Definition MyGUI_ISerializable.h:26
MyGUI::IStateInfo
Definition MyGUI_IStateInfo.h:17
MyGUI::LanguageManager
Definition MyGUI_LanguageManager.h:21
MyGUI::LanguageManager::getInstance
static LanguageManager & getInstance()
Definition MyGUI_LanguageManager.cpp:18
MyGUI::LanguageManager::replaceTags
UString replaceTags(const UString &_line)
Definition MyGUI_LanguageManager.cpp:199
MyGUI::ResourceSkin::getStateInfo
const MapWidgetStateInfo & getStateInfo() const
Definition MyGUI_ResourceSkin.cpp:274
MyGUI::ResourceSkin::getSkinName
const std::string & getSkinName() const
Definition MyGUI_ResourceSkin.cpp:289
MyGUI::ResourceSkin::getSize
const IntSize & getSize() const
Definition MyGUI_ResourceSkin.cpp:259
MyGUI::ResourceSkin::getProperties
const MapString & getProperties() const
Definition MyGUI_ResourceSkin.cpp:279
MyGUI::ResourceSkin::~ResourceSkin
~ResourceSkin() override
Definition MyGUI_ResourceSkin.cpp:16
MyGUI::ResourceSkin::getBasisInfo
const VectorSubWidgetInfo & getBasisInfo() const
Definition MyGUI_ResourceSkin.cpp:269
MyGUI::ResourceSkin::deserialization
void deserialization(xml::ElementPtr _node, Version _version) override
Definition MyGUI_ResourceSkin.cpp:22
MyGUI::ResourceSkin::getTextureName
const std::string & getTextureName() const
Definition MyGUI_ResourceSkin.cpp:264
MyGUI::ResourceSkin::getChild
const VectorChildSkinInfo & getChild() const
Definition MyGUI_ResourceSkin.cpp:284
MyGUI::SubWidgetBinding
Definition MyGUI_SubWidgetBinding.h:18
MyGUI::SubWidgetBinding::create
void create(const IntCoord &_coord, Align _aligin, std::string_view _type)
Definition MyGUI_SubWidgetBinding.cpp:23
MyGUI::SubWidgetBinding::add
void add(std::string_view _name, IStateInfo *_data, std::string_view _skin)
Definition MyGUI_SubWidgetBinding.cpp:38
MyGUI::SubWidgetManager::getStateCategoryName
const std::string & getStateCategoryName() const
Definition MyGUI_SubWidgetManager.cpp:91
MyGUI::SubWidgetManager::getInstance
static SubWidgetManager & getInstance()
MyGUI::Version
Definition MyGUI_Version.h:18
MyGUI::xml::ElementEnumerator
Definition MyGUI_XmlDocument.h:121
MyGUI::xml::ElementEnumerator::next
bool next()
Definition MyGUI_XmlDocument.cpp:107
MyGUI::xml::ElementEnumerator::current
ElementPtr current()
Definition MyGUI_XmlDocument.cpp:136
MyGUI::xml::Element::findAttribute
bool findAttribute(std::string_view _name, std::string &_value)
Definition MyGUI_XmlDocument.cpp:242
MyGUI::xml::Element::getElementEnumerator
ElementEnumerator getElementEnumerator()
Definition MyGUI_XmlDocument.cpp:348
MyGUI::xml::Element::getName
const std::string & getName() const
Definition MyGUI_XmlDocument.cpp:328
MyGUI::xml::ElementPtr
Element * ElementPtr
Definition MyGUI_XmlDocument.h:112
MyGUI
Definition MyGUI_ActionController.h:15
MyGUI::MapWidgetStateInfo
std::map< std::string, VectorStateInfo, std::less<> > MapWidgetStateInfo
Definition MyGUI_IStateInfo.h:23
MyGUI::mapSet
void mapSet(Map &map, std::string_view key, const Value &value)
Definition MyGUI_Macros.h:36
MyGUI::MapString
std::map< std::string, std::string, std::less<> > MapString
Definition MyGUI_Types.h:40
MyGUI::VectorSubWidgetInfo
std::vector< SubWidgetInfo > VectorSubWidgetInfo
Definition MyGUI_SubWidgetInfo.h:33
MyGUI::VectorStateInfo
std::vector< IStateInfo * > VectorStateInfo
Definition MyGUI_IStateInfo.h:21
MyGUI::IntCoord
types::TCoord< int > IntCoord
Definition MyGUI_Types.h:36
MyGUI::IntSize
types::TSize< int > IntSize
Definition MyGUI_Types.h:30
MyGUI::MapStateInfo
std::map< std::string, IStateInfo *, std::less<> > MapStateInfo
Definition MyGUI_IStateInfo.h:22
MyGUI::VectorChildSkinInfo
std::vector< ChildSkinInfo > VectorChildSkinInfo
Definition MyGUI_ChildSkinInfo.h:40
MyGUI::Align
Definition MyGUI_Align.h:20
MyGUI::Align::parse
static Align parse(std::string_view _value)
Definition MyGUI_Align.h:127
MyGUI::Align::Default
@ Default
Definition MyGUI_Align.h:36
MyGUI::ChildSkinInfo
Definition MyGUI_ChildSkinInfo.h:18
MyGUI::ChildSkinInfo::addParam
void addParam(std::string_view _key, std::string_view _value)
Definition MyGUI_ChildSkinInfo.cpp:40
MyGUI::WidgetStyle::parse
static WidgetStyle parse(std::string_view _value)
Definition MyGUI_WidgetStyle.h:38
MyGUI::types::TCoord< int >::parse
static TCoord< int > parse(std::string_view _value)
Definition MyGUI_TCoord.h:183
MyGUI::types::TSize< int >::parse
static TSize< int > parse(std::string_view _value)
Definition MyGUI_TSize.h:102