UICraftingGrid.cpp 3.6 KB

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