UIInventory.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. #include "UIInventory.h"
  2. #include "UIReference.h"
  3. UIInventoryElement::UIInventoryElement()
  4. : UIElement(),
  5. reference(nullptr),
  6. rowSize(0),
  7. numSlots(0)
  8. {}
  9. UIInventoryElement::~UIInventoryElement()
  10. {
  11. if (reference)
  12. {
  13. reference->release();
  14. }
  15. }
  16. void UIInventoryElement::setReference(UIReference* reference)
  17. {
  18. if (this->reference)
  19. {
  20. this->reference->release();
  21. }
  22. this->reference = reference;
  23. }
  24. UIReference* UIInventoryElement::zReference() const
  25. {
  26. return reference;
  27. }
  28. void UIInventoryElement::setRowSize(int rowSize)
  29. {
  30. this->rowSize = rowSize;
  31. }
  32. int UIInventoryElement::getRowSize() const
  33. {
  34. return rowSize;
  35. }
  36. void UIInventoryElement::setNumSlots(int numSlots)
  37. {
  38. this->numSlots = numSlots;
  39. }
  40. int UIInventoryElement::getNumSlots() const
  41. {
  42. return numSlots;
  43. }
  44. void UIInventoryElement::setSlotNameFilter(
  45. const Framework::Text& slotNameFilter)
  46. {
  47. this->slotNameFilter = slotNameFilter;
  48. }
  49. const Framework::Text& UIInventoryElement::getSlotNameFilter() const
  50. {
  51. return slotNameFilter;
  52. }
  53. Framework::XML::Element* UIInventoryElement::toUIML(
  54. Framework::Either<Block*, Entity*> zTarget, Entity* zActor) const
  55. {
  56. Framework::XML::Element* result = UIElement::toUIML(zTarget, zActor);
  57. result->setName("inventory");
  58. if (reference)
  59. {
  60. result->setAttribute(
  61. "target", reference->getReferenceId(zTarget, zActor));
  62. }
  63. result->setAttribute("rowSize", rowSize);
  64. result->setAttribute("numSlots", numSlots);
  65. if (slotNameFilter.getLength())
  66. {
  67. result->setAttribute("slotNameFilter", slotNameFilter);
  68. }
  69. return result;
  70. }
  71. UIInventoryElementFactory::UIInventoryElementFactory()
  72. : UIElementFactory<UIInventoryElement>()
  73. {}
  74. JSONObjectValidationBuilder* UIInventoryElementFactory::addToValidator(
  75. JSONObjectValidationBuilder* builder) const
  76. {
  77. return UIElementFactory<UIInventoryElement>::addToValidator(builder)
  78. ->withRequiredAttribute("reference",
  79. Game::INSTANCE->zTypeRegistry()->getValidator<UIReference>())
  80. ->withRequiredNumber("rowSize")
  81. ->whichIsGreaterThen(0)
  82. ->finishNumber()
  83. ->withRequiredNumber("numSlots")
  84. ->whichIsGreaterThen(0)
  85. ->finishNumber()
  86. ->withRequiredString("slotNameFilter")
  87. ->whichIsOptional()
  88. ->finishString();
  89. }
  90. UIInventoryElement* UIInventoryElementFactory::fromJson(
  91. Framework::JSON::JSONObject* zJson) const
  92. {
  93. UIInventoryElement* result
  94. = UIElementFactory<UIInventoryElement>::fromJson(zJson);
  95. result->setReference(Game::INSTANCE->zTypeRegistry()->fromJson<UIReference>(
  96. zJson->zValue("reference")->asObject()));
  97. result->setRowSize((int)zJson->zValue("rowSize")->asNumber()->getNumber());
  98. result->setNumSlots(
  99. (int)zJson->zValue("numSlots")->asNumber()->getNumber());
  100. if (zJson->hasValue("slotNameFilter"))
  101. {
  102. result->setSlotNameFilter(
  103. zJson->zValue("slotNameFilter")->asString()->getString());
  104. }
  105. return result;
  106. }
  107. Framework::JSON::JSONObject* UIInventoryElementFactory::toJsonObject(
  108. UIInventoryElement* zObject) const
  109. {
  110. Framework::JSON::JSONObject* result
  111. = UIElementFactory<UIInventoryElement>::toJsonObject(zObject);
  112. result->addValue("reference",
  113. Game::INSTANCE->zTypeRegistry()->toJson<UIReference>(
  114. zObject->zReference()));
  115. result->addValue(
  116. "rowSize", new Framework::JSON::JSONNumber(zObject->getRowSize()));
  117. result->addValue(
  118. "numSlots", new Framework::JSON::JSONNumber(zObject->getNumSlots()));
  119. if (zObject->getSlotNameFilter().getLength() > 0)
  120. {
  121. result->addValue("slotNameFilter",
  122. new Framework::JSON::JSONString(zObject->getSlotNameFilter()));
  123. }
  124. return result;
  125. }
  126. UIInventoryElement* UIInventoryElementFactory::createElement(
  127. Framework::JSON::JSONObject* zJson) const
  128. {
  129. return new UIInventoryElement();
  130. }
  131. const char* UIInventoryElementFactory::getTypeToken() const
  132. {
  133. return "inventory";
  134. }