UIInventory.cpp 4.1 KB

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