ItemBar.cpp 6.2 KB

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