Welcome to mirror list, hosted at ThFree Co, Russian Federation.

skin_loader.cpp « graphics - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: ec5260cb6f888ff4661cc17e709ca85011070506 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#include "skin_loader.hpp"
#include "resource_manager.hpp"
#include "resource_cache.hpp"
#include "resource.hpp"
#include "icon.hpp"

#include "../base/string_utils.hpp"

namespace graphics
{
  SkinLoader::SkinLoader(shared_ptr<ResourceManager> const & resourceManager,
                         vector<shared_ptr<ResourceCache> > & caches)
  : m_id(-1),
    m_texRect(0, 0, 0, 0),
    m_fileName(""),
    m_resourceManager(resourceManager),
    m_caches(caches)
  {
    m_mode.push_back(ERoot);
  }

  void SkinLoader::pushResource()
  {
    m_texRect = m2::RectU(0, 0, 0, 0);
  }

  void SkinLoader::popResource()
  {
    m_texRect = m2::RectU(m_texX,
                          m_texY,
                          m_texX + m_texWidth,
                          m_texY + m_texHeight);
  }

  void SkinLoader::popIcon()
  {
    shared_ptr<Resource> res(
          new Icon(m_texRect, m_caches.size(), Icon::Info(m_resID)));

    pair<int32_t, shared_ptr<Resource> > p(m_id, res);
    m_resourceList.push_back(p);
  }

  void SkinLoader::pushPage()
  {
    m_resourceList.clear();
  }

  void SkinLoader::popPage()
  {
    m_caches.push_back(make_shared_ptr(new ResourceCache(m_resourceManager, m_fileName, m_caches.size())));

    TResourceList::iterator prevIt = m_resourceList.end();

    for (TResourceList::iterator it = m_resourceList.begin();
         it != m_resourceList.end();
         ++it)
    {
      m_caches.back()->m_resources[it->first] = it->second;

      if (it->second->m_cat == Resource::EIcon)
      {
        Icon * icon = static_cast<Icon*>(it->second.get());
        m_caches.back()->m_infos[&icon->m_info] = it->first;
      }

      if (prevIt != m_resourceList.end())
        m_resourceList.erase(prevIt);
      prevIt = it;
    }
  }

  void SkinLoader::popSkin()
  {
  }

#define PUSH_MODE(mode, name) \
  if (element == name) \
    m_mode.push_back(mode);\

#define PUSH_MODE_EX(mode, name, fn)\
  if (element == name)\
  {\
    m_mode.push_back(mode);\
    fn();\
  }

#define POP_MODE(mode, name) \
  if (element == name) \
    m_mode.pop_back();


#define POP_MODE_EX(mode, name, f) \
  if (element == name)\
  {\
      f();\
      m_mode.pop_back();\
  }

  bool SkinLoader::Push(string const & element)
  {
    PUSH_MODE(ESkin, "skin");
    PUSH_MODE_EX(EPage, "page", pushPage);
    PUSH_MODE(EIcon, "symbolStyle");
    PUSH_MODE_EX(EResource, "resourceStyle", pushResource);
    return true;
  }

  void SkinLoader::Pop(string const & element)
  {
    POP_MODE_EX(ESkin, "skin", popSkin);
    POP_MODE_EX(EPage, "page", popPage);
    POP_MODE_EX(EIcon, "symbolStyle", popIcon);
    POP_MODE_EX(EResource, "resourceStyle", popResource);
  }

  int StrToInt(string const & s)
  {
    int i;
    VERIFY ( strings::to_int(s, i), ("Bad int int StrToInt function") );
    return i;
  }

  void SkinLoader::AddAttr(string const & attr, string const & value)
  {
    switch (m_mode.back())
    {
    case ESkin:
      break;
    case EPage:
      if (attr == "file")
        m_fileName = value;
      break;
    case EIcon:
      if (attr == "id")
        m_id = StrToInt(value);
      else if (attr == "name")
        m_resID = value;
      break;
    case EResource:
      if (attr == "x")
        m_texX = StrToInt(value);
      else if (attr == "y")
        m_texY = StrToInt(value);
      else if (attr == "height")
        m_texHeight = StrToInt(value);
      else if (attr == "width")
        m_texWidth = StrToInt(value);
      break;
    default:
      break;
    }
  }
}