#include "UIInventory.h" #include "UIReference.h" UIInventoryElement::UIInventoryElement() : UIElement(), reference(nullptr), rowSize(0), numSlots(0), hasFilter(0) {} UIInventoryElement::~UIInventoryElement() { if (reference) { reference->release(); } } void UIInventoryElement::setReference(UIReference* reference) { if (this->reference) { this->reference->release(); } this->reference = reference; } UIReference* UIInventoryElement::zReference() const { return reference; } void UIInventoryElement::setRowSize(int rowSize) { this->rowSize = rowSize; } int UIInventoryElement::getRowSize() const { return rowSize; } void UIInventoryElement::setNumSlots(int numSlots) { this->numSlots = numSlots; } int UIInventoryElement::getNumSlots() const { return numSlots; } void UIInventoryElement::setSlotNameFilter( const Framework::Text& slotNameFilter) { this->slotNameFilter = slotNameFilter; hasFilter = 1; } const Framework::Text& UIInventoryElement::getSlotNameFilter() const { return slotNameFilter; } bool UIInventoryElement::hasSlotNameFilter() const { return hasFilter; } Framework::XML::Element* UIInventoryElement::toUIML( Framework::Either zTarget, Entity* zActor) const { Framework::XML::Element* result = UIElement::toUIML(zTarget, zActor); result->setName("inventory"); if (reference) { result->setAttribute( "target", reference->getReferenceId(zTarget, zActor)); } result->setAttribute("rowSize", rowSize); result->setAttribute("numSlots", numSlots); if (hasFilter) { result->setAttribute("slotNameFilter", slotNameFilter); } return result; } UIInventoryElementFactory::UIInventoryElementFactory() : UIElementFactory() {} JSONObjectValidationBuilder* UIInventoryElementFactory::addToValidator( JSONObjectValidationBuilder* builder) const { return UIElementFactory::addToValidator(builder) ->withRequiredAttribute("reference", Game::INSTANCE->zTypeRegistry()->getValidator()) ->withRequiredNumber("rowSize") ->whichIsGreaterThen(0) ->finishNumber() ->withRequiredNumber("numSlots") ->whichIsGreaterThen(0) ->finishNumber() ->withRequiredString("slotNameFilter") ->whichIsOptional() ->finishString(); } UIInventoryElement* UIInventoryElementFactory::fromJson( Framework::JSON::JSONObject* zJson) const { UIInventoryElement* result = UIElementFactory::fromJson(zJson); result->setReference(Game::INSTANCE->zTypeRegistry()->fromJson( zJson->zValue("reference")->asObject())); result->setRowSize((int)zJson->zValue("rowSize")->asNumber()->getNumber()); result->setNumSlots( (int)zJson->zValue("numSlots")->asNumber()->getNumber()); if (zJson->hasValue("slotNameFilter")) { result->setSlotNameFilter( zJson->zValue("slotNameFilter")->asString()->getString()); } return result; } Framework::JSON::JSONObject* UIInventoryElementFactory::toJsonObject( UIInventoryElement* zObject) const { Framework::JSON::JSONObject* result = UIElementFactory::toJsonObject(zObject); result->addValue("reference", Game::INSTANCE->zTypeRegistry()->toJson( zObject->zReference())); result->addValue( "rowSize", new Framework::JSON::JSONNumber(zObject->getRowSize())); result->addValue( "numSlots", new Framework::JSON::JSONNumber(zObject->getNumSlots())); if (zObject->hasSlotNameFilter()) { result->addValue("slotNameFilter", new Framework::JSON::JSONString(zObject->getSlotNameFilter())); } return result; } UIInventoryElement* UIInventoryElementFactory::createElement( Framework::JSON::JSONObject* zJson) const { return new UIInventoryElement(); } const char* UIInventoryElementFactory::getTypeToken() const { return "inventory"; }