#pragma once #include #include #include #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 zTarget, Entity* zActor) const; }; class UIContainerElement : public UIElement { private: Framework::RCArray children; public: UIContainerElement(); void addChild(UIElement* child); const Framework::RCArray& getChildren() const; virtual Framework::XML::Element* toUIML( Framework::Either zTarget, Entity* zActor) const override; }; template class UIElementFactory : public SubTypeFactory { public: UIElementFactory() : SubTypeFactory() {} 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 UIContainerElementFactory : public UIElementFactory { public: UIContainerElementFactory() : UIElementFactory() {} virtual JSONObjectValidationBuilder* addToValidator( JSONObjectValidationBuilder* builder) const override { return UIElementFactory::addToValidator(builder) ->withRequiredArray("children") ->addAcceptedTypeInArray( Game::INSTANCE->zTypeRegistry()->getValidator()) ->finishArray(); } virtual T* fromJson(Framework::JSON::JSONObject* zJson) const override { T* result = UIElementFactory::fromJson(zJson); Framework::JSON::JSONArray* childrenJson = zJson->zValue("children")->asArray(); for (Framework::JSON::JSONValue* childJson : *childrenJson) { result->addChild( Game::INSTANCE->zTypeRegistry()->fromJson( childJson->asObject())); } return result; } virtual Framework::JSON::JSONObject* toJsonObject(T* zObject) const override { Framework::JSON::JSONObject* result = UIElementFactory::toJsonObject(zObject); Framework::JSON::JSONArray* childrenJson = new Framework::JSON::JSONArray(); for (UIElement* child : zObject->getChildren()) { childrenJson->addValue( Game::INSTANCE->zTypeRegistry()->toJson(child)); } result->addValue("children", childrenJson); return result; } };