All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
IconSetReader.h
1 // Copyright eeGeo Ltd (2012-2014), All Rights Reserved
2 
3 #pragma once
4 
5 #include "document.h"
6 #include "error/en.h"
7 #include "Types.h"
8 
9 #include <string>
10 #include <vector>
11 
12 namespace Eegeo
13 {
14  namespace Icons
15  {
17  {
18  int index;
19  std::string iconKey;
20  int x;
21  int y;
22  int width;
23  int height;
24  int page;
25  };
26 
28  {
29  int width;
30  int height;
31  std::string filename;
32  };
33 
35  {
36  typedef std::vector<IconSetPageDto> TIconSetPageDtos;
37  typedef std::vector<IconDefinitionDto> TIconSetIconDefinitionDtos;
38 
39  TIconSetPageDtos iconSetPages;
40  TIconSetIconDefinitionDtos iconDefinitions;
41  };
42 
44  {
45  public:
46 
47  bool ReadJsonFile(const std::string& json, IconSetReaderResult& result)
48  {
49  rapidjson::Document document;
50  document.Parse<0>(json.c_str());
51  if (document.HasParseError())
52  {
53  std::string errorMsg(GetParseError_En(document.GetParseError()));
54  Eegeo_TTY("Error parsing icon sheet - invalid json. %s", errorMsg.c_str());
55  return false;
56  }
57 
58  Eegeo_ASSERT(document.HasMember("pages"));
59  const rapidjson::Value& pageMappings = document["pages"];
60  result.iconSetPages = ParsePages(pageMappings);
61 
62  Eegeo_ASSERT(document.HasMember("icons"));
63  const rapidjson::Value& iconMappings = document["icons"];
64  result.iconDefinitions = ParseIcons(iconMappings);
65 
66  return true;
67 
68  }
69 
70  private:
71  std::vector<IconSetPageDto> ParsePages(const rapidjson::Value& json)
72  {
73  size_t mappingsCount = json.Size();
74 
75  std::vector<IconSetPageDto> pages;
76  pages.reserve(mappingsCount);
77 
78  for (int i = 0; i < mappingsCount; ++i)
79  {
80  const rapidjson::Value& mappingJson = json[i];
81  Eegeo_ASSERT(mappingJson.HasMember("file"), "Icon page definition has no file defined");
82  Eegeo_ASSERT(mappingJson.HasMember("width"), "Icon page definition has no width defined");
83  Eegeo_ASSERT(mappingJson.HasMember("height"), "Icon page definition has no height defined");
84 
85  IconSetPageDto page;
86  page.filename = mappingJson["file"].GetString();
87  page.width = mappingJson["width"].GetInt();
88  page.height = mappingJson["height"].GetInt();
89 
90  pages.push_back(page);
91  }
92 
93  return pages;
94  }
95 
96  std::vector<IconDefinitionDto> ParseIcons(const rapidjson::Value& json)
97  {
98  Eegeo_ASSERT(json.IsArray());
99 
100  size_t mappingsCount = json.Size();
101 
102  std::vector<IconDefinitionDto> mappingEntries;
103  mappingEntries.reserve(mappingsCount);
104 
105  for (int i = 0; i < mappingsCount; ++i)
106  {
107  const rapidjson::Value& mappingJson = json[i];
108 
109  Eegeo_ASSERT(mappingJson.HasMember("id"), "Icon definition has no id");
110  Eegeo_ASSERT(mappingJson.HasMember("index"), "Icon definition has no index");
111  Eegeo_ASSERT(mappingJson.HasMember("x"), "Icon definition has no x coordinate");
112  Eegeo_ASSERT(mappingJson.HasMember("y"), "Icon definition has no y coordinate");
113  Eegeo_ASSERT(mappingJson.HasMember("w"), "Icon definition has no width");
114  Eegeo_ASSERT(mappingJson.HasMember("h"), "Icon definition has no height");
115  Eegeo_ASSERT(mappingJson.HasMember("page"), "Icon definition has no page");
116 
117  IconDefinitionDto icon;
118  icon.iconKey = mappingJson["id"].GetString();
119  icon.index = mappingJson["index"].GetInt();
120  icon.x = mappingJson["x"].GetInt();
121  icon.y = mappingJson["y"].GetInt();
122  icon.width = mappingJson["w"].GetInt();
123  icon.height = mappingJson["h"].GetInt();
124  icon.page = mappingJson["page"].GetInt();
125 
126 
127  mappingEntries.push_back(icon);
128  }
129 
130  return mappingEntries;
131  }
132 
133  };
134  }
135 }