|
@@ -0,0 +1,214 @@
|
|
|
|
|
+#pragma once
|
|
|
|
|
+
|
|
|
|
|
+#include <Array.h>
|
|
|
|
|
+#include <Either.h>
|
|
|
|
|
+#include <ReferenceCounter.h>
|
|
|
|
|
+
|
|
|
|
|
+#include "Game.h"
|
|
|
|
|
+#include "TypeRegistry.h"
|
|
|
|
|
+
|
|
|
|
|
+class Block;
|
|
|
|
|
+class Entity;
|
|
|
|
|
+
|
|
|
|
|
+class UIElement : public virtual Framework::ReferenceCounter
|
|
|
|
|
+{
|
|
|
|
|
+private:
|
|
|
|
|
+ Framework::Text id;
|
|
|
|
|
+ int marginLeft;
|
|
|
|
|
+ int marginRight;
|
|
|
|
|
+ int marginTop;
|
|
|
|
|
+ int marginBottom;
|
|
|
|
|
+ int width;
|
|
|
|
|
+ int height;
|
|
|
|
|
+ Framework::Text alignLeft;
|
|
|
|
|
+ Framework::Text alignRight;
|
|
|
|
|
+ Framework::Text alignTop;
|
|
|
|
|
+ Framework::Text alignBottom;
|
|
|
|
|
+
|
|
|
|
|
+public:
|
|
|
|
|
+ UIElement();
|
|
|
|
|
+ void setId(const Framework::Text& id);
|
|
|
|
|
+ const Framework::Text& getId() const;
|
|
|
|
|
+ void setMarginLeft(int marginLeft);
|
|
|
|
|
+ int getMarginLeft() const;
|
|
|
|
|
+ void setMarginRight(int marginRight);
|
|
|
|
|
+ int getMarginRight() const;
|
|
|
|
|
+ void setMarginTop(int marginTop);
|
|
|
|
|
+ int getMarginTop() const;
|
|
|
|
|
+ void setMarginBottom(int marginBottom);
|
|
|
|
|
+ int getMarginBottom() const;
|
|
|
|
|
+ void setWidth(int width);
|
|
|
|
|
+ int getWidth() const;
|
|
|
|
|
+ void setHeight(int height);
|
|
|
|
|
+ int getHeight() const;
|
|
|
|
|
+ void setAlignLeft(const Framework::Text& alignLeft);
|
|
|
|
|
+ const Framework::Text& getAlignLeft() const;
|
|
|
|
|
+ void setAlignRight(const Framework::Text& alignRight);
|
|
|
|
|
+ const Framework::Text& getAlignRight() const;
|
|
|
|
|
+ void setAlignTop(const Framework::Text& alignTop);
|
|
|
|
|
+ const Framework::Text& getAlignTop() const;
|
|
|
|
|
+ void setAlignBottom(const Framework::Text& alignBottom);
|
|
|
|
|
+ const Framework::Text& getAlignBottom() const;
|
|
|
|
|
+ virtual Framework::XML::Element* toUIML(
|
|
|
|
|
+ Framework::Either<Block*, Entity*> zTarget, Entity* zActor) const;
|
|
|
|
|
+};
|
|
|
|
|
+
|
|
|
|
|
+class UIContainerElement : public UIElement
|
|
|
|
|
+{
|
|
|
|
|
+private:
|
|
|
|
|
+ Framework::RCArray<UIElement> children;
|
|
|
|
|
+
|
|
|
|
|
+public:
|
|
|
|
|
+ UIContainerElement();
|
|
|
|
|
+ void addChild(UIElement* child);
|
|
|
|
|
+ const Framework::RCArray<UIElement>& getChildren() const;
|
|
|
|
|
+ virtual Framework::XML::Element* toUIML(
|
|
|
|
|
+ Framework::Either<Block*, Entity*> zTarget,
|
|
|
|
|
+ Entity* zActor) const override;
|
|
|
|
|
+};
|
|
|
|
|
+
|
|
|
|
|
+template<class T> class UIElementFactory : public SubTypeFactory<UIElement, T>
|
|
|
|
|
+{
|
|
|
|
|
+public:
|
|
|
|
|
+ UIElementFactory()
|
|
|
|
|
+ : SubTypeFactory<UIElement, T>()
|
|
|
|
|
+ {}
|
|
|
|
|
+
|
|
|
|
|
+ virtual JSONObjectValidationBuilder* addToValidator(
|
|
|
|
|
+ JSONObjectValidationBuilder* builder) const override
|
|
|
|
|
+ {
|
|
|
|
|
+ return builder->withRequiredString("id")
|
|
|
|
|
+ ->finishString()
|
|
|
|
|
+ ->withRequiredNumber("marginLeft")
|
|
|
|
|
+ ->withDefault(0.0)
|
|
|
|
|
+ ->finishNumber()
|
|
|
|
|
+ ->withRequiredNumber("marginRight")
|
|
|
|
|
+ ->withDefault(0.0)
|
|
|
|
|
+ ->finishNumber()
|
|
|
|
|
+ ->withRequiredNumber("marginTop")
|
|
|
|
|
+ ->withDefault(0.0)
|
|
|
|
|
+ ->finishNumber()
|
|
|
|
|
+ ->withRequiredNumber("marginBottom")
|
|
|
|
|
+ ->withDefault(0.0)
|
|
|
|
|
+ ->finishNumber()
|
|
|
|
|
+ ->withRequiredNumber("width")
|
|
|
|
|
+ ->finishNumber()
|
|
|
|
|
+ ->withRequiredNumber("height")
|
|
|
|
|
+ ->finishNumber()
|
|
|
|
|
+ ->withRequiredString("alignLeft")
|
|
|
|
|
+ ->withDefault("")
|
|
|
|
|
+ ->finishString()
|
|
|
|
|
+ ->withRequiredString("alignRight")
|
|
|
|
|
+ ->withDefault("")
|
|
|
|
|
+ ->finishString()
|
|
|
|
|
+ ->withRequiredString("alignTop")
|
|
|
|
|
+ ->withDefault("")
|
|
|
|
|
+ ->finishString()
|
|
|
|
|
+ ->withRequiredString("alignBottom")
|
|
|
|
|
+ ->withDefault("")
|
|
|
|
|
+ ->finishString();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ virtual T* fromJson(Framework::JSON::JSONObject* zJson) const override
|
|
|
|
|
+ {
|
|
|
|
|
+ T* result = createElement(zJson);
|
|
|
|
|
+ result->setId(zJson->zValue("id")->asString()->getString());
|
|
|
|
|
+ result->setMarginLeft(
|
|
|
|
|
+ (int)zJson->zValue("marginLeft")->asNumber()->getNumber());
|
|
|
|
|
+ result->setMarginRight(
|
|
|
|
|
+ (int)zJson->zValue("marginRight")->asNumber()->getNumber());
|
|
|
|
|
+ result->setMarginTop(
|
|
|
|
|
+ (int)zJson->zValue("marginTop")->asNumber()->getNumber());
|
|
|
|
|
+ result->setMarginBottom(
|
|
|
|
|
+ (int)zJson->zValue("marginBottom")->asNumber()->getNumber());
|
|
|
|
|
+ result->setWidth((int)zJson->zValue("width")->asNumber()->getNumber());
|
|
|
|
|
+ result->setHeight(
|
|
|
|
|
+ (int)zJson->zValue("height")->asNumber()->getNumber());
|
|
|
|
|
+ result->setAlignLeft(
|
|
|
|
|
+ zJson->zValue("alignLeft")->asString()->getString());
|
|
|
|
|
+ result->setAlignRight(
|
|
|
|
|
+ zJson->zValue("alignRight")->asString()->getString());
|
|
|
|
|
+ result->setAlignTop(zJson->zValue("alignTop")->asString()->getString());
|
|
|
|
|
+ result->setAlignBottom(
|
|
|
|
|
+ zJson->zValue("alignBottom")->asString()->getString());
|
|
|
|
|
+ return result;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ virtual Framework::JSON::JSONObject* toJsonObject(T* zObject) const override
|
|
|
|
|
+ {
|
|
|
|
|
+ Framework::JSON::JSONObject* result = new Framework::JSON::JSONObject();
|
|
|
|
|
+ result->addValue(
|
|
|
|
|
+ "id", new Framework::JSON::JSONString(zObject->getId()));
|
|
|
|
|
+ result->addValue("marginLeft",
|
|
|
|
|
+ new Framework::JSON::JSONNumber(zObject->getMarginLeft()));
|
|
|
|
|
+ result->addValue("marginRight",
|
|
|
|
|
+ new Framework::JSON::JSONNumber(zObject->getMarginRight()));
|
|
|
|
|
+ result->addValue("marginTop",
|
|
|
|
|
+ new Framework::JSON::JSONNumber(zObject->getMarginTop()));
|
|
|
|
|
+ result->addValue("marginBottom",
|
|
|
|
|
+ new Framework::JSON::JSONNumber(zObject->getMarginBottom()));
|
|
|
|
|
+ result->addValue(
|
|
|
|
|
+ "width", new Framework::JSON::JSONNumber(zObject->getWidth()));
|
|
|
|
|
+ result->addValue(
|
|
|
|
|
+ "height", new Framework::JSON::JSONNumber(zObject->getHeight()));
|
|
|
|
|
+ result->addValue("alignLeft",
|
|
|
|
|
+ new Framework::JSON::JSONString(zObject->getAlignLeft()));
|
|
|
|
|
+ result->addValue("alignRight",
|
|
|
|
|
+ new Framework::JSON::JSONString(zObject->getAlignRight()));
|
|
|
|
|
+ result->addValue("alignTop",
|
|
|
|
|
+ new Framework::JSON::JSONString(zObject->getAlignTop()));
|
|
|
|
|
+ result->addValue("alignBottom",
|
|
|
|
|
+ new Framework::JSON::JSONString(zObject->getAlignBottom()));
|
|
|
|
|
+ return result;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+protected:
|
|
|
|
|
+ virtual T* createElement(Framework::JSON::JSONObject* zJson) const = 0;
|
|
|
|
|
+};
|
|
|
|
|
+
|
|
|
|
|
+template<class T> class UIContainerElementFactory : public UIElementFactory<T>
|
|
|
|
|
+{
|
|
|
|
|
+public:
|
|
|
|
|
+ UIContainerElementFactory()
|
|
|
|
|
+ : UIElementFactory<T>()
|
|
|
|
|
+ {}
|
|
|
|
|
+
|
|
|
|
|
+ virtual JSONObjectValidationBuilder* addToValidator(
|
|
|
|
|
+ JSONObjectValidationBuilder* builder) const override
|
|
|
|
|
+ {
|
|
|
|
|
+ return UIElementFactory<T>::addToValidator(builder)
|
|
|
|
|
+ ->withRequiredArray("children")
|
|
|
|
|
+ ->addAcceptedTypeInArray(
|
|
|
|
|
+ Game::INSTANCE->zTypeRegistry()->getValidator<UIElement>())
|
|
|
|
|
+ ->finishArray();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ virtual T* fromJson(Framework::JSON::JSONObject* zJson) const override
|
|
|
|
|
+ {
|
|
|
|
|
+ T* result = UIElementFactory<T>::fromJson(zJson);
|
|
|
|
|
+ Framework::JSON::JSONArray* childrenJson
|
|
|
|
|
+ = zJson->zValue("children")->asArray();
|
|
|
|
|
+ for (Framework::JSON::JSONValue* childJson : *childrenJson)
|
|
|
|
|
+ {
|
|
|
|
|
+ result->addChild(
|
|
|
|
|
+ Game::INSTANCE->zTypeRegistry()->fromJson<UIElement>(
|
|
|
|
|
+ childJson->asObject()));
|
|
|
|
|
+ }
|
|
|
|
|
+ return result;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ virtual Framework::JSON::JSONObject* toJsonObject(T* zObject) const override
|
|
|
|
|
+ {
|
|
|
|
|
+ Framework::JSON::JSONObject* result
|
|
|
|
|
+ = UIElementFactory<T>::toJsonObject(zObject);
|
|
|
|
|
+ Framework::JSON::JSONArray* childrenJson
|
|
|
|
|
+ = new Framework::JSON::JSONArray();
|
|
|
|
|
+ for (UIElement* child : zObject->getChildren())
|
|
|
|
|
+ {
|
|
|
|
|
+ childrenJson->addValue(
|
|
|
|
|
+ Game::INSTANCE->zTypeRegistry()->toJson<UIElement>(child));
|
|
|
|
|
+ }
|
|
|
|
|
+ result->addValue("children", childrenJson);
|
|
|
|
|
+ return result;
|
|
|
|
|
+ }
|
|
|
|
|
+};
|