UIInventory.cpp 4.0 KB

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