| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300 |
- #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;
- Framework::Text marginLeft;
- Framework::Text marginRight;
- Framework::Text marginTop;
- Framework::Text marginBottom;
- Framework::Text width;
- Framework::Text height;
- Framework::Text alignLeft;
- Framework::Text alignRight;
- Framework::Text alignTop;
- Framework::Text alignBottom;
- Framework::Text style;
- public:
- UIElement();
- void setId(const Framework::Text& id);
- const Framework::Text& getId() const;
- void setMarginLeft(const Framework::Text& marginLeft);
- const Framework::Text& getMarginLeft() const;
- void setMarginRight(const Framework::Text& marginRight);
- const Framework::Text& getMarginRight() const;
- void setMarginTop(const Framework::Text& marginTop);
- const Framework::Text& getMarginTop() const;
- void setMarginBottom(const Framework::Text& marginBottom);
- const Framework::Text& getMarginBottom() const;
- void setWidth(const Framework::Text& width);
- const Framework::Text& getWidth() const;
- void setHeight(const Framework::Text& height);
- const Framework::Text& 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;
- void setStyle(const Framework::Text& style);
- const Framework::Text& getStyle() 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()
- ->withRequiredString("marginLeft")
- ->whichEndsWithMatch("%")
- ->finishString()
- ->withRequiredNumber("marginRight")
- ->withDefault(0.0)
- ->finishNumber()
- ->withRequiredString("marginRight")
- ->whichEndsWithMatch("%")
- ->finishString()
- ->withRequiredNumber("marginTop")
- ->withDefault(0.0)
- ->finishNumber()
- ->withRequiredString("marginTop")
- ->whichEndsWithMatch("%")
- ->finishString()
- ->withRequiredNumber("marginBottom")
- ->withDefault(0.0)
- ->finishNumber()
- ->withRequiredString("marginBottom")
- ->whichEndsWithMatch("%")
- ->finishString()
- ->withRequiredNumber("width")
- ->finishNumber()
- ->withRequiredString("width")
- ->whichEndsWithMatch("%")
- ->finishString()
- ->withRequiredString("width")
- ->withExactMatch("auto")
- ->finishString()
- ->withRequiredNumber("height")
- ->finishNumber()
- ->withRequiredString("height")
- ->whichEndsWithMatch("%")
- ->finishString()
- ->withRequiredString("height")
- ->withExactMatch("auto")
- ->finishString()
- ->withRequiredString("alignLeft")
- ->withDefault("")
- ->finishString()
- ->withRequiredString("alignRight")
- ->withDefault("")
- ->finishString()
- ->withRequiredString("alignTop")
- ->withDefault("")
- ->finishString()
- ->withRequiredString("alignBottom")
- ->withDefault("")
- ->finishString()
- ->withRequiredString("style")
- ->withDefault("")
- ->finishString();
- }
- virtual T* fromJson(Framework::JSON::JSONObject* zJson) const override
- {
- T* result = createElement(zJson);
- result->setId(zJson->zValue("id")->asString()->getString());
- if (zJson->zValue("marginLeft")->getType()
- == Framework::AbstractType::NUMBER)
- {
- result->setMarginLeft(
- (int)zJson->zValue("marginLeft")->asNumber()->getNumber());
- }
- else
- {
- result->setMarginLeft(
- zJson->zValue("marginLeft")->asString()->getString());
- }
- if (zJson->zValue("marginRight")->getType()
- == Framework::AbstractType::NUMBER)
- {
- result->setMarginRight(
- (int)zJson->zValue("marginRight")->asNumber()->getNumber());
- }
- else
- {
- result->setMarginRight(
- zJson->zValue("marginRight")->asString()->getString());
- }
- if (zJson->zValue("marginTop")->getType()
- == Framework::AbstractType::NUMBER)
- {
- result->setMarginTop(
- (int)zJson->zValue("marginTop")->asNumber()->getNumber());
- }
- else
- {
- result->setMarginTop(
- zJson->zValue("marginTop")->asString()->getString());
- }
- if (zJson->zValue("marginBottom")->getType()
- == Framework::AbstractType::NUMBER)
- {
- result->setMarginBottom(
- (int)zJson->zValue("marginBottom")->asNumber()->getNumber());
- }
- else
- {
- result->setMarginBottom(
- zJson->zValue("marginBottom")->asString()->getString());
- }
- if (zJson->zValue("width")->getType()
- == Framework::AbstractType::NUMBER)
- {
- result->setWidth(
- (int)zJson->zValue("width")->asNumber()->getNumber());
- }
- else
- {
- result->setWidth(zJson->zValue("width")->asString()->getString());
- }
- if (zJson->zValue("height")->getType()
- == Framework::AbstractType::NUMBER)
- {
- result->setHeight(
- (int)zJson->zValue("height")->asNumber()->getNumber());
- }
- else
- {
- result->setHeight(zJson->zValue("height")->asString()->getString());
- }
- 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());
- result->setStyle(zJson->zValue("style")->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::JSONString(zObject->getMarginLeft()));
- result->addValue("marginRight",
- new Framework::JSON::JSONString(zObject->getMarginRight()));
- result->addValue("marginTop",
- new Framework::JSON::JSONString(zObject->getMarginTop()));
- result->addValue("marginBottom",
- new Framework::JSON::JSONString(zObject->getMarginBottom()));
- result->addValue(
- "width", new Framework::JSON::JSONString(zObject->getWidth()));
- result->addValue(
- "height", new Framework::JSON::JSONString(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()));
- result->addValue(
- "style", new Framework::JSON::JSONString(zObject->getStyle()));
- 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;
- }
- };
|