MyGUI 3.2.2

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