| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160 |
- #include "UIInventory.h"
- #include "UICraftingProgress.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<Block*, Entity*> 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<UIInventoryElement>()
- {}
- JSONObjectValidationBuilder* UIInventoryElementFactory::addToValidator(
- JSONObjectValidationBuilder* builder) const
- {
- return UIElementFactory<UIInventoryElement>::addToValidator(builder)
- ->withRequiredAttribute("reference",
- Game::INSTANCE->zTypeRegistry()->getValidator<UIReference>())
- ->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<UIInventoryElement>::fromJson(zJson);
- result->setReference(Game::INSTANCE->zTypeRegistry()->fromJson<UIReference>(
- 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<UIInventoryElement>::toJsonObject(zObject);
- result->addValue("reference",
- Game::INSTANCE->zTypeRegistry()->toJson<UIReference>(
- 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";
- }
|