MyGUI  3.2.0
MyGUI_ResourceSkin.cpp
Go to the documentation of this file.
1 
6 /*
7  This file is part of MyGUI.
8 
9  MyGUI is free software: you can redistribute it and/or modify
10  it under the terms of the GNU Lesser General Public License as published by
11  the Free Software Foundation, either version 3 of the License, or
12  (at your option) any later version.
13 
14  MyGUI is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  GNU Lesser General Public License for more details.
18 
19  You should have received a copy of the GNU Lesser General Public License
20  along with MyGUI. If not, see <http://www.gnu.org/licenses/>.
21 */
22 #include "MyGUI_Precompiled.h"
23 #include "MyGUI_ResourceSkin.h"
24 #include "MyGUI_FactoryManager.h"
25 #include "MyGUI_LanguageManager.h"
26 
27 namespace MyGUI
28 {
29 
31  {
32  }
33 
35  {
36  for (MapWidgetStateInfo::iterator item = mStates.begin(); item != mStates.end(); ++ item)
37  {
38  for (VectorStateInfo::iterator info = (*item).second.begin(); info != (*item).second.end(); ++ info)
39  delete (*info);
40  }
41  mStates.clear();
42  }
43 
45  {
46  Base::deserialization(_node, _version);
47 
48  // парсим атрибуты скина
49  std::string name, texture, tmp;
50  IntSize size;
51  _node->findAttribute("name", name);
52  _node->findAttribute("texture", texture);
53  if (_node->findAttribute("size", tmp)) size = IntSize::parse(tmp);
54 
56 
57  // вспомогательный класс для биндинга сабскинов
58  SubWidgetBinding bind;
59 
60  // поддержка замены тегов в скинах
61  if (_version >= Version(1, 1))
62  {
63  texture = localizator.replaceTags(texture);
64  }
65 
66  setInfo(size, texture);
67 
68  // проверяем маску
69  if (_node->findAttribute("mask", tmp))
70  addProperty("MaskPick", tmp);
71 
72  // берем детей и крутимся, цикл с саб скинами
74  while (basis.next())
75  {
76  if (basis->getName() == "Property")
77  {
78  // загружаем свойства
79  std::string key, value;
80  if (!basis->findAttribute("key", key)) continue;
81  if (!basis->findAttribute("value", value)) continue;
82 
83  // поддержка замены тегов в скинах
84  if (_version >= Version(1, 1))
85  {
86  value = localizator.replaceTags(value);
87  }
88 
89  // добавляем свойство
90  addProperty(key, value);
91  }
92  else if (basis->getName() == "Child")
93  {
94  ChildSkinInfo child(
95  basis->findAttribute("type"),
96  WidgetStyle::parse(basis->findAttribute("style")),
97  basis->findAttribute("skin"),
98  IntCoord::parse(basis->findAttribute("offset")),
99  Align::parse(basis->findAttribute("align")),
100  basis->findAttribute("layer"),
101  basis->findAttribute("name")
102  );
103 
104  xml::ElementEnumerator child_params = basis->getElementEnumerator();
105  while (child_params.next("Property"))
106  child.addParam(child_params->findAttribute("key"), child_params->findAttribute("value"));
107 
108  addChild(child);
109  //continue;
110  }
111  else if (basis->getName() == "BasisSkin")
112  {
113  // парсим атрибуты
114  std::string basisSkinType, tmp_str;
115  IntCoord offset;
116  Align align = Align::Default;
117  basis->findAttribute("type", basisSkinType);
118  if (basis->findAttribute("offset", tmp_str)) offset = IntCoord::parse(tmp_str);
119  if (basis->findAttribute("align", tmp_str)) align = Align::parse(tmp_str);
120 
121  bind.create(offset, align, basisSkinType);
122 
123  // берем детей и крутимся, цикл со стейтами
125 
126  // проверяем на новый формат стейтов
127  bool new_format = false;
128  // если версия меньше 1.0 то переименовываем стейты
129  if (_version < Version(1, 0))
130  {
131  while (state.next())
132  {
133  if (state->getName() == "State")
134  {
135  const std::string& name_state = state->findAttribute("name");
136  if ((name_state == "normal_checked") || (state->findAttribute("name") == "normal_check"))
137  {
138  new_format = true;
139  break;
140  }
141  }
142  }
143  // обновляем
144  state = basis->getElementEnumerator();
145  }
146 
147  while (state.next())
148  {
149  if (state->getName() == "State")
150  {
151  // парсим атрибуты стейта
152  std::string basisStateName;
153  state->findAttribute("name", basisStateName);
154 
155  // если версия меньше 1.0 то переименовываем стейты
156  if (_version < Version(1, 0))
157  {
158  // это обсолет новых типов
159  if (basisStateName == "disable_check") basisStateName = "disabled_checked";
160  else if (basisStateName == "normal_check") basisStateName = "normal_checked";
161  else if (basisStateName == "active_check") basisStateName = "highlighted_checked";
162  else if (basisStateName == "pressed_check") basisStateName = "pushed_checked";
163  else if (basisStateName == "disable") basisStateName = "disabled";
164  else if (basisStateName == "active") basisStateName = "highlighted";
165  else if (basisStateName == "select") basisStateName = "pushed";
166  else if (basisStateName == "pressed")
167  {
168  if (new_format) basisStateName = "pushed";
169  else basisStateName = "normal_checked";
170  }
171  }
172 
173  // конвертируем инфу о стейте
174  IStateInfo* data = nullptr;
175  IObject* object = FactoryManager::getInstance().createObject("BasisSkin/State", 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 
266  {
267  return mSize;
268  }
269 
270  const std::string& ResourceSkin::getTextureName() const
271  {
272  return mTexture;
273  }
274 
276  {
277  return mBasis;
278  }
279 
281  {
282  return mStates;
283  }
284 
286  {
287  return mProperties;
288  }
289 
291  {
292  return mChilds;
293  }
294 
295  const std::string& ResourceSkin::getSkinName() const
296  {
297  return mSkinName;
298  }
299 
300 } // namespace MyGUI
std::map< std::string, VectorStateInfo > MapWidgetStateInfo
const MapString & getProperties() const
virtual void deserialization(xml::ElementPtr _node, Version _version)
const std::string & getTextureName() const
static LanguageManager & getInstance()
static WidgetStyle parse(const std::string &_value)
bool findAttribute(const std::string &_name, std::string &_value)
static Align parse(const std::string &_value)
Definition: MyGUI_Align.h:142
const VectorChildSkinInfo & getChild() const
const IntSize & getSize() const
void add(const std::string &_name, IStateInfo *_data, const std::string &_skin)
static TSize< int > parse(const std::string &_value)
Definition: MyGUI_TSize.h:135
virtual void deserialization(xml::ElementPtr _node, Version _version)
const VectorSubWidgetInfo & getBasisInfo() const
std::map< std::string, IStateInfo * > MapStateInfo
std::vector< IStateInfo * > VectorStateInfo
const MapWidgetStateInfo & getStateInfo() const
std::map< std::string, std::string > MapString
Definition: MyGUI_Types.h:53
std::vector< SubWidgetInfo > VectorSubWidgetInfo
Type * castType(bool _throw=true)
Definition: MyGUI_IObject.h:33
ElementEnumerator getElementEnumerator()
const std::string & getSkinName() const
const std::string & getName() const
UString replaceTags(const UString &_line)
void create(const IntCoord &_coord, Align _aligin, const std::string &_type)
std::vector< ChildSkinInfo > VectorChildSkinInfo
static TCoord< int > parse(const std::string &_value)
Definition: MyGUI_TCoord.h:222
IObject * createObject(const std::string &_category, const std::string &_type)