ItemBar.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. #include "ItemBar.h"
  2. #include <Bild.h>
  3. #include <XML.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(
  16. Framework::XML::Element& element, Framework::UIMLContainer& generalFactory)
  17. {
  18. return new ItemBarView(element.getAttributeValue("id"),
  19. (int)element.getAttributeValue("rowSize"),
  20. (int)element.getAttributeValue("target"),
  21. element.getAttributeValue("slotNameFilter"));
  22. }
  23. bool ItemBarElement::updateElement(Framework::XML::Element& element,
  24. Framework::Zeichnung& z,
  25. Framework::UIMLContainer& generalFactory)
  26. {
  27. return false;
  28. }
  29. //! wendet die layout parameter zu einer Zeichnung an
  30. void ItemBarElement::layout(Framework::XML::Element& element,
  31. Framework::Zeichnung& z,
  32. int pWidth,
  33. int pHeight,
  34. Framework::UIMLContainer& generalLayouter)
  35. {
  36. z.setHeight(52);
  37. z.setWidth(592);
  38. UIMLElement::layout(element, z, pWidth, pHeight, generalLayouter);
  39. }
  40. ItemBarView::ItemBarView(Framework::Text id,
  41. int rowSize,
  42. int targetEntity,
  43. Framework::Text slotNameFilter)
  44. : ZeichnungHintergrund(),
  45. id(id),
  46. rowSize(rowSize),
  47. targetEntity(targetEntity),
  48. slotNameFilter(slotNameFilter),
  49. slots(0),
  50. leftHandPos(0)
  51. {
  52. char* msg = new char[id.getLength() + slotNameFilter.getLength() + 3];
  53. msg[0] = 0;
  54. msg[1] = (char)id.getLength();
  55. memcpy(msg + 2, id.getText(), id.getLength());
  56. msg[2 + id.getLength()] = (char)slotNameFilter.getLength();
  57. memcpy(msg + 3 + id.getLength(),
  58. slotNameFilter.getText(),
  59. slotNameFilter.getLength());
  60. World::INSTANCE->zClient()->inventoryAPIRequest(
  61. targetEntity, msg, id.getLength() + slotNameFilter.getLength() + 3);
  62. World::INSTANCE->zClient()->sendPlayerAction("\x8", 1);
  63. delete[] msg;
  64. }
  65. ItemBarView::~ItemBarView()
  66. {
  67. if (slots) slots->release();
  68. char* msg = new char[id.getLength() + 2];
  69. msg[0] = 1;
  70. msg[1] = (char)id.getLength();
  71. memcpy(msg + 2, id.getText(), id.getLength());
  72. World::INSTANCE->zClient()->inventoryAPIRequest(
  73. targetEntity, msg, id.getLength() + 2);
  74. delete[] msg;
  75. }
  76. void ItemBarView::api(char* message)
  77. {
  78. switch (message[0])
  79. {
  80. case 0:
  81. // send inventory content
  82. {
  83. Array<SlotInfo>* slots = new Array<SlotInfo>();
  84. int count = *(int*)(++message);
  85. for (int i = 0; i < count; i++)
  86. {
  87. SlotInfo info;
  88. info.id = *(int*)(message += 4);
  89. info.itemCount = *(int*)(message += 4);
  90. if (info.itemCount > 0)
  91. {
  92. info.hp = *(float*)(message += 4);
  93. info.maxHp = *(float*)(message += 4);
  94. info.durability = *(float*)(message += 4);
  95. info.maxDurability = *(float*)(message += 4);
  96. info.zItem = zItemType(*(int*)(message += 4))->zIcon();
  97. char len = *(message += 4);
  98. char* name = new char[len + 1];
  99. memcpy(name, message += 1, len);
  100. name[len] = 0;
  101. info.name = name;
  102. delete[] name;
  103. message += len - 4;
  104. }
  105. slots->add(info);
  106. }
  107. window->zBildschirm()->postAction([this, slots]() {
  108. if (this->slots) this->slots->release();
  109. this->slots = slots;
  110. });
  111. break;
  112. }
  113. case 1: // set count of items
  114. {
  115. int id = *(int*)(message + 1);
  116. int count = *(int*)(message + 5);
  117. for (int i = 0; i < slots->getEintragAnzahl(); i++)
  118. {
  119. if (slots->get(i).id == id)
  120. {
  121. SlotInfo info = slots->get(i);
  122. info.itemCount = count;
  123. slots->set(info, i);
  124. break;
  125. }
  126. }
  127. break;
  128. }
  129. case 2: // add new stack
  130. {
  131. int id = *(int*)(message + 1);
  132. for (int i = 0; i < slots->getEintragAnzahl(); i++)
  133. {
  134. if (slots->get(i).id == id)
  135. {
  136. SlotInfo info = slots->get(i);
  137. info.itemCount = *(int*)(message + 5);
  138. info.hp = *(float*)(message + 9);
  139. info.maxHp = *(float*)(message + 13);
  140. info.durability = *(float*)(message + 17);
  141. info.maxDurability = *(float*)(message + 21);
  142. info.zItem = zItemType(*(int*)(message + 25))->zIcon();
  143. char len = *(message + 29);
  144. char* name = new char[len + 1];
  145. memcpy(name, message + 30, len);
  146. name[len] = 0;
  147. info.name = name;
  148. delete[] name;
  149. slots->set(info, i);
  150. break;
  151. }
  152. }
  153. break;
  154. }
  155. case 3: // set selected slot
  156. {
  157. leftHandPos = *(int*)(message + 1);
  158. break;
  159. }
  160. }
  161. }
  162. void ItemBarView::render(Bild& rObj)
  163. {
  164. ZeichnungHintergrund::render(rObj);
  165. if (!rObj.setDrawOptions(pos.x, pos.y, gr.x, gr.y)) return;
  166. if (slots)
  167. {
  168. int x = 0;
  169. int y = 0;
  170. int rowCount = 0;
  171. int index = 0;
  172. for (SlotInfo info : *slots)
  173. {
  174. info.render(x,
  175. y,
  176. rObj,
  177. index == leftHandPos, 0);
  178. x += 60;
  179. if (++rowCount >= rowSize)
  180. {
  181. y += 60;
  182. x = 0;
  183. rowCount = 0;
  184. }
  185. index++;
  186. }
  187. }
  188. rObj.releaseDrawOptions();
  189. }