UIInventory.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. ->withRequiredString("slotNameFilter")
  93. ->whichIsOptional()
  94. ->finishString();
  95. }
  96. UIInventoryElement* UIInventoryElementFactory::fromJson(
  97. Framework::JSON::JSONObject* zJson) const
  98. {
  99. UIInventoryElement* result
  100. = UIElementFactory<UIInventoryElement>::fromJson(zJson);
  101. result->setReference(Game::INSTANCE->zTypeRegistry()->fromJson<UIReference>(
  102. zJson->zValue("reference")->asObject()));
  103. result->setRowSize((int)zJson->zValue("rowSize")->asNumber()->getNumber());
  104. result->setNumSlots(
  105. (int)zJson->zValue("numSlots")->asNumber()->getNumber());
  106. if (zJson->hasValue("slotNameFilter"))
  107. {
  108. result->setSlotNameFilter(
  109. zJson->zValue("slotNameFilter")->asString()->getString());
  110. }
  111. return result;
  112. }
  113. Framework::JSON::JSONObject* UIInventoryElementFactory::toJsonObject(
  114. UIInventoryElement* zObject) const
  115. {
  116. Framework::JSON::JSONObject* result
  117. = UIElementFactory<UIInventoryElement>::toJsonObject(zObject);
  118. result->addValue("reference",
  119. Game::INSTANCE->zTypeRegistry()->toJson<UIReference>(
  120. zObject->zReference()));
  121. result->addValue(
  122. "rowSize", new Framework::JSON::JSONNumber(zObject->getRowSize()));
  123. result->addValue(
  124. "numSlots", new Framework::JSON::JSONNumber(zObject->getNumSlots()));
  125. if (zObject->hasSlotNameFilter())
  126. {
  127. result->addValue("slotNameFilter",
  128. new Framework::JSON::JSONString(zObject->getSlotNameFilter()));
  129. }
  130. return result;
  131. }
  132. UIInventoryElement* UIInventoryElementFactory::createElement(
  133. Framework::JSON::JSONObject* zJson) const
  134. {
  135. return new UIInventoryElement();
  136. }
  137. const char* UIInventoryElementFactory::getTypeToken() const
  138. {
  139. return "inventory";
  140. }