ItemBar.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. #include <XML.h>
  2. #include <Bild.h>
  3. #include "ItemBar.h"
  4. #include "Globals.h"
  5. using namespace Framework;
  6. ItemBarElement::ItemBarElement()
  7. : UIMLElement()
  8. {}
  9. //! prüft, ob dieses UIML Element für ein bestimmtes xml Element zuständig ist
  10. bool ItemBarElement::isApplicableFor(Framework::XML::Element& element)
  11. {
  12. return element.getName().istGleich("itemBar");
  13. }
  14. //! erstellt eine neue Zeichnung zu einem gegebenen xml Element
  15. Framework::Zeichnung* ItemBarElement::parseElement(Framework::XML::Element& element, Framework::UIMLContainer& generalFactory)
  16. {
  17. return new ItemBarView(element.getAttributeValue("id"), (int)element.getAttributeValue("rowSize"), (int)element.getAttributeValue("target"), element.getAttributeValue("slotNameFilter"));
  18. }
  19. //! wendet die layout parameter zu einer Zeichnung an
  20. void ItemBarElement::layout(Framework::XML::Element& element, Framework::Zeichnung& z, int pWidth, int pHeight, Framework::UIMLContainer& generalLayouter)
  21. {
  22. UIMLElement::layout(element, z, pWidth, pHeight, generalLayouter);
  23. }
  24. ItemBarView::ItemBarView(Framework::Text id, int rowSize, int targetEntity, Framework::Text slotNameFilter)
  25. : ZeichnungHintergrund(),
  26. id(id),
  27. rowSize(rowSize),
  28. targetEntity(targetEntity),
  29. slotNameFilter(slotNameFilter),
  30. slots(0),
  31. leftHandPos(0)
  32. {
  33. char* msg = new char[id.getLength() + slotNameFilter.getLength() + 3];
  34. msg[0] = 0;
  35. msg[1] = (char)id.getLength();
  36. memcpy(msg + 2, id.getText(), id.getLength());
  37. msg[2 + id.getLength()] = (char)slotNameFilter.getLength();
  38. memcpy(msg + 3 + id.getLength(), slotNameFilter.getText(), slotNameFilter.getLength());
  39. World::INSTANCE->zClient()->inventoryAPIRequest(targetEntity, msg, id.getLength() + slotNameFilter.getLength() + 3);
  40. delete[] msg;
  41. }
  42. ItemBarView::~ItemBarView()
  43. {
  44. if (slots)
  45. slots->release();
  46. char* msg = new char[id.getLength() + 2];
  47. msg[0] = 1;
  48. msg[1] = (char)id.getLength();
  49. memcpy(msg + 2, id.getText(), id.getLength());
  50. msg[2 + id.getLength()] = (char)slotNameFilter.getLength();
  51. World::INSTANCE->zClient()->inventoryAPIRequest(targetEntity, msg, id.getLength() + 2);
  52. delete[] msg;
  53. }
  54. void ItemBarView::api(char* message)
  55. {
  56. switch (message[0])
  57. {
  58. case 0:
  59. // send inventory content
  60. {
  61. Array<SlotInfo>* slots = new Array<SlotInfo>();
  62. int count = *(int*)(++message);
  63. for (int i = 0; i < count; i++)
  64. {
  65. SlotInfo info;
  66. info.id = *(int*)(message += 4);
  67. info.itemCount = *(int*)(message += 4);
  68. if (info.itemCount > 0)
  69. {
  70. info.hp = *(float*)(message += 4);
  71. info.maxHp = *(float*)(message += 4);
  72. info.durability = *(float*)(message += 4);
  73. info.maxDurability = *(float*)(message += 4);
  74. info.zItem = itemIcons->z(*(int*)(message += 4));
  75. }
  76. slots->add(info);
  77. }
  78. postAction([this, slots]()
  79. {
  80. if (this->slots)
  81. this->slots->release();
  82. this->slots = slots;
  83. });
  84. break;
  85. }
  86. case 1: // set count of items
  87. {
  88. int id = *(int*)(message + 1);
  89. int count = *(int*)(message + 5);
  90. for (int i = 0; i < slots->getEintragAnzahl(); i++)
  91. {
  92. if (slots->get(i).id == id)
  93. {
  94. SlotInfo info = slots->get(i);
  95. info.itemCount = count;
  96. slots->set(info, i);
  97. break;
  98. }
  99. }
  100. break;
  101. }
  102. case 2: // add new stack
  103. {
  104. int id = *(int*)(message + 1);
  105. for (int i = 0; i < slots->getEintragAnzahl(); i++)
  106. {
  107. if (slots->get(i).id == id)
  108. {
  109. SlotInfo info = slots->get(i);
  110. info.itemCount = *(int*)(message + 5);
  111. info.hp = *(float*)(message + 9);
  112. info.maxHp = *(float*)(message + 13);
  113. info.durability = *(float*)(message + 17);
  114. info.maxDurability = *(float*)(message + 21);
  115. info.zItem = itemIcons->z(*(int*)(message + 25));
  116. slots->set(info, i);
  117. break;
  118. }
  119. }
  120. break;
  121. }
  122. case 3: // set selected slot
  123. {
  124. leftHandPos = *(int*)(message + 1);
  125. break;
  126. }
  127. }
  128. }
  129. void ItemBarView::render(Bild& rObj)
  130. {
  131. ZeichnungHintergrund::render(rObj);
  132. if (!rObj.setDrawOptions(pos.x, pos.y, gr.x, gr.y))
  133. return;
  134. if (slots)
  135. {
  136. int x = 0;
  137. int y = 0;
  138. int rowCount = 0;
  139. int index = 0;
  140. for (SlotInfo info : *slots)
  141. {
  142. info.render(x, y, rObj, index == leftHandPos, index == (leftHandPos + 1) % slots->getEintragAnzahl());
  143. x += 60;
  144. if (++rowCount >= rowSize)
  145. {
  146. y += 60;
  147. x = 0;
  148. rowCount = 0;
  149. }
  150. index++;
  151. }
  152. }
  153. rObj.releaseDrawOptions();
  154. }