InventoryView.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. #include <XML.h>
  2. #include <Bild.h>
  3. #include "InventoryView.h"
  4. #include "Globals.h"
  5. using namespace Framework;
  6. InventoryElement::InventoryElement()
  7. : UIMLElement()
  8. {}
  9. //! prüft, ob dieses UIML Element für ein bestimmtes xml Element zuständig ist
  10. bool InventoryElement::isApplicableFor(Framework::XML::Element& element)
  11. {
  12. return element.getName().istGleich("inventory");
  13. }
  14. //! erstellt eine neue Zeichnung zu einem gegebenen xml Element
  15. Framework::Zeichnung* InventoryElement::parseElement(Framework::XML::Element& element, Framework::UIMLContainer& generalFactory)
  16. {
  17. Text targetValue = element.getAttributeValue("target");
  18. Vec3<int> blockPos(0, 0, 0);
  19. Framework::Either<int, Vec3<int>> target((int)targetValue);
  20. if (targetValue.hat(','))
  21. {
  22. Text* first = targetValue.getTeilText(0, targetValue.positionVon(",", 0) + 1);
  23. Text* second = targetValue.getTeilText(targetValue.positionVon(",", 0) + 1, targetValue.positionVon(",", 1));
  24. Text* third = targetValue.getTeilText(targetValue.positionVon(",", 1) + 1);
  25. target = Framework::Either<int, Vec3<int>>(Vec3<int>((int)*first, (int)*second, (int)*third));
  26. first->release();
  27. second->release();
  28. third->release();
  29. }
  30. return new InventoryView(element.getAttributeValue("id"), target, (int)element.getAttributeValue("rowSize"), element.getAttributeValue("slotNameFilter"));
  31. }
  32. //! wendet die layout parameter zu einer Zeichnung an
  33. void InventoryElement::layout(Framework::XML::Element& element, Framework::Zeichnung& z, int pWidth, int pHeight, Framework::UIMLContainer& generalLayouter)
  34. {
  35. UIMLElement::layout(element, z, pWidth, pHeight, generalLayouter);
  36. }
  37. void SlotInfo::render(int x, int y, Framework::Bild& rObj, bool selected)
  38. {
  39. TextRenderer tr;
  40. tr.setSchriftZ(dynamic_cast<Schrift*>(uiFactory.initParam.schrift->getThis()));
  41. tr.setSchriftSize(12);
  42. rObj.fillRegion(x, y, 52, 52, selected ? 0xFFFFFFFF : 0xFF52525E);
  43. rObj.fillRegion(x + 1, y + 1, 50, 50, 0xFF222222);
  44. if (itemCount > 0)
  45. {
  46. rObj.alphaBild(x + 1, y + 1, 50, 50, *zItem);
  47. if (damage < maxDamage)
  48. {
  49. rObj.fillRegion(x + 1, y + 47, 50, 2, 0xFF000000);
  50. rObj.fillRegion(x + 1, y + 47, (int)((damage / maxDamage) * 50), 2, 0xFFFF0000);
  51. }
  52. if (durability < maxDurability)
  53. {
  54. rObj.fillRegion(x + 1, y + 49, 50, 2, 0xFF000000);
  55. rObj.fillRegion(x + 1, y + 49, (int)((durability / maxDurability) * 50), 2, 0xFF00FF00);
  56. }
  57. const char* units[] = { "", "K", "M", "G", "T", "P" };
  58. int i = 0;
  59. for (; i < 6 && itemCount > 1024; i++)
  60. itemCount = itemCount / 1024;
  61. Text count = itemCount;
  62. count += units[i];
  63. tr.renderText(x + 45 - tr.getTextBreite(count), y + 45 - tr.getTextHeight(count), count, rObj, 0xFFFFFFFF);
  64. }
  65. }
  66. InventoryView::InventoryView(Text id, Either<int, Vec3<int>> target, int rowSize, Text slotNameFilter)
  67. : ZeichnungHintergrund(),
  68. rowSize(rowSize),
  69. target(target),
  70. slotNameFilter(slotNameFilter),
  71. id(id),
  72. slots(0)
  73. {
  74. if (target.isA())
  75. {
  76. char* msg = new char[id.getLength() + slotNameFilter.getLength() + 3];
  77. msg[0] = 100;
  78. msg[1] = (char)id.getLength();
  79. memcpy(msg + 2, id.getText(), id.getLength());
  80. msg[2 + id.getLength()] = (char)slotNameFilter.getLength();
  81. memcpy(msg + 3 + id.getLength(), slotNameFilter.getText(), slotNameFilter.getLength());
  82. network->zFactoryClient()->entityAPIRequest(target, msg, id.getLength() + slotNameFilter.getLength() + 3);
  83. delete[] msg;
  84. }
  85. setStyle(ZeichnungHintergrund::Style::Sichtbar | ZeichnungHintergrund::Style::Erlaubt);
  86. }
  87. InventoryView::~InventoryView()
  88. {
  89. if (slots)
  90. slots->release();
  91. }
  92. void InventoryView::api(char* message)
  93. {
  94. switch (message[0])
  95. {
  96. case 0:
  97. // send inventory content
  98. {
  99. Array<SlotInfo>* slots = new Array<SlotInfo>();
  100. int count = *(int*)(++message);
  101. for (int i = 0; i < count; i++)
  102. {
  103. SlotInfo info;
  104. info.id = *(int*)(message += 4);
  105. info.itemCount = *(int*)(message += 4);
  106. if (info.itemCount > 0)
  107. {
  108. info.damage = *(float*)(message += 4);
  109. info.maxDamage = *(float*)(message += 4);
  110. info.durability = *(float*)(message += 4);
  111. info.maxDurability = *(float*)(message += 4);
  112. info.zItem = itemIcons->z(*(int*)(message += 4));
  113. }
  114. slots->add(info);
  115. }
  116. postAction([this, slots]()
  117. {
  118. if (this->slots)
  119. this->slots->release();
  120. this->slots = slots;
  121. });
  122. break;
  123. }
  124. }
  125. }
  126. bool InventoryView::tick(double tickVal)
  127. {
  128. return ZeichnungHintergrund::tick(tickVal);
  129. }
  130. void InventoryView::render(Bild& rObj)
  131. {
  132. ZeichnungHintergrund::render(rObj);
  133. if (!rObj.setDrawOptions(pos.x, pos.y, gr.x, gr.y))
  134. return;
  135. if (slots)
  136. {
  137. int x = 0;
  138. int y = 0;
  139. int rowCount = 0;
  140. for (SlotInfo info : *slots)
  141. {
  142. info.render(x, y, rObj, 0);
  143. x += 60;
  144. if (++rowCount >= rowSize)
  145. {
  146. y += 60;
  147. x = 0;
  148. rowCount = 0;
  149. }
  150. }
  151. }
  152. rObj.releaseDrawOptions();
  153. }
  154. void InventoryView::doMausEreignis(MausEreignis& me, bool userRet)
  155. {
  156. ZeichnungHintergrund::doMausEreignis(me, userRet);
  157. }