UIElement.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. #pragma once
  2. #include <Array.h>
  3. #include <Either.h>
  4. #include <ReferenceCounter.h>
  5. #include "Game.h"
  6. #include "TypeRegistry.h"
  7. class Block;
  8. class Entity;
  9. class UIElement : public virtual Framework::ReferenceCounter
  10. {
  11. private:
  12. Framework::Text id;
  13. Framework::Text marginLeft;
  14. Framework::Text marginRight;
  15. Framework::Text marginTop;
  16. Framework::Text marginBottom;
  17. Framework::Text width;
  18. Framework::Text height;
  19. Framework::Text alignLeft;
  20. Framework::Text alignRight;
  21. Framework::Text alignTop;
  22. Framework::Text alignBottom;
  23. Framework::Text style;
  24. public:
  25. UIElement();
  26. void setId(const Framework::Text& id);
  27. const Framework::Text& getId() const;
  28. void setMarginLeft(const Framework::Text& marginLeft);
  29. const Framework::Text& getMarginLeft() const;
  30. void setMarginRight(const Framework::Text& marginRight);
  31. const Framework::Text& getMarginRight() const;
  32. void setMarginTop(const Framework::Text& marginTop);
  33. const Framework::Text& getMarginTop() const;
  34. void setMarginBottom(const Framework::Text& marginBottom);
  35. const Framework::Text& getMarginBottom() const;
  36. void setWidth(const Framework::Text& width);
  37. const Framework::Text& getWidth() const;
  38. void setHeight(const Framework::Text& height);
  39. const Framework::Text& getHeight() const;
  40. void setAlignLeft(const Framework::Text& alignLeft);
  41. const Framework::Text& getAlignLeft() const;
  42. void setAlignRight(const Framework::Text& alignRight);
  43. const Framework::Text& getAlignRight() const;
  44. void setAlignTop(const Framework::Text& alignTop);
  45. const Framework::Text& getAlignTop() const;
  46. void setAlignBottom(const Framework::Text& alignBottom);
  47. const Framework::Text& getAlignBottom() const;
  48. void setStyle(const Framework::Text& style);
  49. const Framework::Text& getStyle() const;
  50. virtual Framework::XML::Element* toUIML(
  51. Framework::Either<Block*, Entity*> zTarget, Entity* zActor) const;
  52. };
  53. class UIContainerElement : public UIElement
  54. {
  55. private:
  56. Framework::RCArray<UIElement> children;
  57. public:
  58. UIContainerElement();
  59. void addChild(UIElement* child);
  60. const Framework::RCArray<UIElement>& getChildren() const;
  61. virtual Framework::XML::Element* toUIML(
  62. Framework::Either<Block*, Entity*> zTarget,
  63. Entity* zActor) const override;
  64. };
  65. template<class T> class UIElementFactory : public SubTypeFactory<UIElement, T>
  66. {
  67. public:
  68. UIElementFactory()
  69. : SubTypeFactory<UIElement, T>()
  70. {}
  71. virtual JSONObjectValidationBuilder* addToValidator(
  72. JSONObjectValidationBuilder* builder) const override
  73. {
  74. return builder->withRequiredString("id")
  75. ->finishString()
  76. ->withRequiredNumber("marginLeft")
  77. ->withDefault(0.0)
  78. ->finishNumber()
  79. ->withRequiredString("marginLeft")
  80. ->whichEndsWithMatch("%")
  81. ->finishString()
  82. ->withRequiredNumber("marginRight")
  83. ->withDefault(0.0)
  84. ->finishNumber()
  85. ->withRequiredString("marginRight")
  86. ->whichEndsWithMatch("%")
  87. ->finishString()
  88. ->withRequiredNumber("marginTop")
  89. ->withDefault(0.0)
  90. ->finishNumber()
  91. ->withRequiredString("marginTop")
  92. ->whichEndsWithMatch("%")
  93. ->finishString()
  94. ->withRequiredNumber("marginBottom")
  95. ->withDefault(0.0)
  96. ->finishNumber()
  97. ->withRequiredString("marginBottom")
  98. ->whichEndsWithMatch("%")
  99. ->finishString()
  100. ->withRequiredNumber("width")
  101. ->finishNumber()
  102. ->withRequiredString("width")
  103. ->whichEndsWithMatch("%")
  104. ->finishString()
  105. ->withRequiredString("width")
  106. ->withExactMatch("auto")
  107. ->finishString()
  108. ->withRequiredNumber("height")
  109. ->finishNumber()
  110. ->withRequiredString("height")
  111. ->whichEndsWithMatch("%")
  112. ->finishString()
  113. ->withRequiredString("height")
  114. ->withExactMatch("auto")
  115. ->finishString()
  116. ->withRequiredString("alignLeft")
  117. ->withDefault("")
  118. ->finishString()
  119. ->withRequiredString("alignRight")
  120. ->withDefault("")
  121. ->finishString()
  122. ->withRequiredString("alignTop")
  123. ->withDefault("")
  124. ->finishString()
  125. ->withRequiredString("alignBottom")
  126. ->withDefault("")
  127. ->finishString()
  128. ->withRequiredString("style")
  129. ->withDefault("")
  130. ->finishString();
  131. }
  132. virtual T* fromJson(Framework::JSON::JSONObject* zJson) const override
  133. {
  134. T* result = createElement(zJson);
  135. result->setId(zJson->zValue("id")->asString()->getString());
  136. if (zJson->zValue("marginLeft")->getType()
  137. == Framework::AbstractType::NUMBER)
  138. {
  139. result->setMarginLeft(
  140. (int)zJson->zValue("marginLeft")->asNumber()->getNumber());
  141. }
  142. else
  143. {
  144. result->setMarginLeft(
  145. zJson->zValue("marginLeft")->asString()->getString());
  146. }
  147. if (zJson->zValue("marginRight")->getType()
  148. == Framework::AbstractType::NUMBER)
  149. {
  150. result->setMarginRight(
  151. (int)zJson->zValue("marginRight")->asNumber()->getNumber());
  152. }
  153. else
  154. {
  155. result->setMarginRight(
  156. zJson->zValue("marginRight")->asString()->getString());
  157. }
  158. if (zJson->zValue("marginTop")->getType()
  159. == Framework::AbstractType::NUMBER)
  160. {
  161. result->setMarginTop(
  162. (int)zJson->zValue("marginTop")->asNumber()->getNumber());
  163. }
  164. else
  165. {
  166. result->setMarginTop(
  167. zJson->zValue("marginTop")->asString()->getString());
  168. }
  169. if (zJson->zValue("marginBottom")->getType()
  170. == Framework::AbstractType::NUMBER)
  171. {
  172. result->setMarginBottom(
  173. (int)zJson->zValue("marginBottom")->asNumber()->getNumber());
  174. }
  175. else
  176. {
  177. result->setMarginBottom(
  178. zJson->zValue("marginBottom")->asString()->getString());
  179. }
  180. if (zJson->zValue("width")->getType()
  181. == Framework::AbstractType::NUMBER)
  182. {
  183. result->setWidth(
  184. (int)zJson->zValue("width")->asNumber()->getNumber());
  185. }
  186. else
  187. {
  188. result->setWidth(zJson->zValue("width")->asString()->getString());
  189. }
  190. if (zJson->zValue("height")->getType()
  191. == Framework::AbstractType::NUMBER)
  192. {
  193. result->setHeight(
  194. (int)zJson->zValue("height")->asNumber()->getNumber());
  195. }
  196. else
  197. {
  198. result->setHeight(zJson->zValue("height")->asString()->getString());
  199. }
  200. result->setAlignLeft(
  201. zJson->zValue("alignLeft")->asString()->getString());
  202. result->setAlignRight(
  203. zJson->zValue("alignRight")->asString()->getString());
  204. result->setAlignTop(zJson->zValue("alignTop")->asString()->getString());
  205. result->setAlignBottom(
  206. zJson->zValue("alignBottom")->asString()->getString());
  207. result->setStyle(zJson->zValue("style")->asString()->getString());
  208. return result;
  209. }
  210. virtual Framework::JSON::JSONObject* toJsonObject(T* zObject) const override
  211. {
  212. Framework::JSON::JSONObject* result = new Framework::JSON::JSONObject();
  213. result->addValue(
  214. "id", new Framework::JSON::JSONString(zObject->getId()));
  215. result->addValue("marginLeft",
  216. new Framework::JSON::JSONString(zObject->getMarginLeft()));
  217. result->addValue("marginRight",
  218. new Framework::JSON::JSONString(zObject->getMarginRight()));
  219. result->addValue("marginTop",
  220. new Framework::JSON::JSONString(zObject->getMarginTop()));
  221. result->addValue("marginBottom",
  222. new Framework::JSON::JSONString(zObject->getMarginBottom()));
  223. result->addValue(
  224. "width", new Framework::JSON::JSONString(zObject->getWidth()));
  225. result->addValue(
  226. "height", new Framework::JSON::JSONString(zObject->getHeight()));
  227. result->addValue("alignLeft",
  228. new Framework::JSON::JSONString(zObject->getAlignLeft()));
  229. result->addValue("alignRight",
  230. new Framework::JSON::JSONString(zObject->getAlignRight()));
  231. result->addValue("alignTop",
  232. new Framework::JSON::JSONString(zObject->getAlignTop()));
  233. result->addValue("alignBottom",
  234. new Framework::JSON::JSONString(zObject->getAlignBottom()));
  235. result->addValue(
  236. "style", new Framework::JSON::JSONString(zObject->getStyle()));
  237. return result;
  238. }
  239. protected:
  240. virtual T* createElement(Framework::JSON::JSONObject* zJson) const = 0;
  241. };
  242. template<class T> class UIContainerElementFactory : public UIElementFactory<T>
  243. {
  244. public:
  245. UIContainerElementFactory()
  246. : UIElementFactory<T>()
  247. {}
  248. virtual JSONObjectValidationBuilder* addToValidator(
  249. JSONObjectValidationBuilder* builder) const override
  250. {
  251. return UIElementFactory<T>::addToValidator(builder)
  252. ->withRequiredArray("children")
  253. ->addAcceptedTypeInArray(
  254. Game::INSTANCE->zTypeRegistry()->getValidator<UIElement>())
  255. ->finishArray();
  256. }
  257. virtual T* fromJson(Framework::JSON::JSONObject* zJson) const override
  258. {
  259. T* result = UIElementFactory<T>::fromJson(zJson);
  260. Framework::JSON::JSONArray* childrenJson
  261. = zJson->zValue("children")->asArray();
  262. for (Framework::JSON::JSONValue* childJson : *childrenJson)
  263. {
  264. result->addChild(
  265. Game::INSTANCE->zTypeRegistry()->fromJson<UIElement>(
  266. childJson->asObject()));
  267. }
  268. return result;
  269. }
  270. virtual Framework::JSON::JSONObject* toJsonObject(T* zObject) const override
  271. {
  272. Framework::JSON::JSONObject* result
  273. = UIElementFactory<T>::toJsonObject(zObject);
  274. Framework::JSON::JSONArray* childrenJson
  275. = new Framework::JSON::JSONArray();
  276. for (UIElement* child : zObject->getChildren())
  277. {
  278. childrenJson->addValue(
  279. Game::INSTANCE->zTypeRegistry()->toJson<UIElement>(child));
  280. }
  281. result->addValue("children", childrenJson);
  282. return result;
  283. }
  284. };