InventoryView.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. #include <XML.h>
  2. #include <Bild.h>
  3. #include "InventoryView.h"
  4. #include "Globals.h"
  5. #include "DragController.h"
  6. #include "Globals.h"
  7. #include "Game.h"
  8. using namespace Framework;
  9. InventoryElement::InventoryElement()
  10. : UIMLElement()
  11. {}
  12. //! prüft, ob dieses UIML Element für ein bestimmtes xml Element zuständig ist
  13. bool InventoryElement::isApplicableFor(Framework::XML::Element& element)
  14. {
  15. return element.getName().istGleich("inventory");
  16. }
  17. //! erstellt eine neue Zeichnung zu einem gegebenen xml Element
  18. Framework::Zeichnung* InventoryElement::parseElement(Framework::XML::Element& element, Framework::UIMLContainer& generalFactory)
  19. {
  20. Text targetValue = element.getAttributeValue("target");
  21. Vec3<int> blockPos(0, 0, 0);
  22. Framework::Either<int, VecN<int, 4>> target((int)targetValue);
  23. if (targetValue.hat(','))
  24. {
  25. Text* first = targetValue.getTeilText(0, targetValue.positionVon(",", 0) + 1);
  26. Text* second = targetValue.getTeilText(targetValue.positionVon(",", 0) + 1, targetValue.positionVon(",", 1));
  27. Text* third = targetValue.getTeilText(targetValue.positionVon(",", 1) + 1, targetValue.positionVon(",", 2));
  28. Text* forth = targetValue.getTeilText(targetValue.positionVon(",", 2) + 1);
  29. target = Framework::Either<int, VecN<int, 4>>(Framework::VecN<int, 4>({ (int)*first, (int)*second, (int)*third, (int)*forth }));
  30. first->release();
  31. second->release();
  32. third->release();
  33. forth->release();
  34. }
  35. return new InventoryView(element.getAttributeValue("id"), target, (int)element.getAttributeValue("rowSize"), element.getAttributeValue("slotNameFilter"));
  36. }
  37. //! wendet die layout parameter zu einer Zeichnung an
  38. void InventoryElement::layout(Framework::XML::Element& element, Framework::Zeichnung& z, int pWidth, int pHeight, Framework::UIMLContainer& generalLayouter)
  39. {
  40. UIMLElement::layout(element, z, pWidth, pHeight, generalLayouter);
  41. }
  42. void SlotInfo::render(int x, int y, Framework::Bild& rObj, bool selected, bool lightBackground)
  43. {
  44. TextRenderer tr;
  45. tr.setSchriftZ(dynamic_cast<Schrift*>(uiFactory.initParam.schrift->getThis()));
  46. tr.setSchriftSize(12);
  47. rObj.fillRegion(x, y, 52, 52, selected ? 0xFFFFFFFF : 0xFF52525E);
  48. rObj.fillRegion(x + 1, y + 1, 50, 50, lightBackground ? 0xFF42424E : 0xFF222222);
  49. if (itemCount > 0)
  50. {
  51. rObj.alphaBild(x + 1, y + 1, 50, 50, *zItem);
  52. if (hp < maxHp)
  53. {
  54. rObj.fillRegion(x + 1, y + 47, 50, 2, 0xFF000000);
  55. rObj.fillRegion(x + 1, y + 47, (int)((hp / maxHp) * 50), 2, 0xFFFFFF00);
  56. }
  57. if (durability < maxDurability)
  58. {
  59. rObj.fillRegion(x + 1, y + 49, 50, 2, 0xFF000000);
  60. rObj.fillRegion(x + 1, y + 49, (int)((durability / maxDurability) * 50), 2, 0xFF00FF00);
  61. }
  62. const char* units[] = { "", "K", "M", "G", "T", "P" };
  63. int i = 0;
  64. for (; i < 6 && itemCount > 1024; i++)
  65. itemCount = itemCount / 1024;
  66. Text count = itemCount;
  67. count += units[i];
  68. tr.renderText(x + 45 - tr.getTextBreite(count), y + 45 - tr.getTextHeight(count), count, rObj, 0xFFFFFFFF);
  69. }
  70. }
  71. InventoryView::InventoryView(Text id, Either<int, VecN<int, 4>> target, int rowSize, Text slotNameFilter)
  72. : ZeichnungHintergrund(),
  73. rowSize(rowSize),
  74. target(target),
  75. slotNameFilter(slotNameFilter),
  76. id(id),
  77. slots(0),
  78. dragStartId(-1),
  79. dragStopId(-1)
  80. {
  81. setStyle(ZeichnungHintergrund::Style::Sichtbar | ZeichnungHintergrund::Style::Erlaubt);
  82. char* msg = new char[id.getLength() + slotNameFilter.getLength() + 3];
  83. msg[0] = 0;
  84. msg[1] = (char)id.getLength();
  85. memcpy(msg + 2, id.getText(), id.getLength());
  86. msg[2 + id.getLength()] = (char)slotNameFilter.getLength();
  87. memcpy(msg + 3 + id.getLength(), slotNameFilter.getText(), slotNameFilter.getLength());
  88. network->zFactoryClient()->inventoryAPIRequest(target, msg, id.getLength() + slotNameFilter.getLength() + 3);
  89. delete[] msg;
  90. }
  91. InventoryView::~InventoryView()
  92. {
  93. DragController<InventoryDragSource, int>* controller = ((Game*)(Menu*)menuRegister->get("game"))->zInventoryDragController();
  94. if (controller->getCurrentDragContainer() == this)
  95. controller->stopDrag();
  96. if (slots)
  97. slots->release();
  98. char* msg = new char[id.getLength() + 2];
  99. msg[0] = 1;
  100. msg[1] = (char)id.getLength();
  101. memcpy(msg + 2, id.getText(), id.getLength());
  102. network->zFactoryClient()->inventoryAPIRequest(target, msg, id.getLength() + 2);
  103. delete[] msg;
  104. }
  105. void InventoryView::api(char* message)
  106. {
  107. switch (message[0])
  108. {
  109. case 0:
  110. // send inventory content
  111. {
  112. Array<SlotInfo>* slots = new Array<SlotInfo>();
  113. int count = *(int*)(++message);
  114. for (int i = 0; i < count; i++)
  115. {
  116. SlotInfo info;
  117. info.id = *(int*)(message += 4);
  118. info.itemCount = *(int*)(message += 4);
  119. if (info.itemCount > 0)
  120. {
  121. info.hp = *(float*)(message += 4);
  122. info.maxHp = *(float*)(message += 4);
  123. info.durability = *(float*)(message += 4);
  124. info.maxDurability = *(float*)(message += 4);
  125. info.zItem = itemIcons->z(*(int*)(message += 4));
  126. }
  127. slots->add(info);
  128. }
  129. postAction([this, slots]()
  130. {
  131. if (this->slots)
  132. this->slots->release();
  133. this->slots = slots;
  134. });
  135. break;
  136. }
  137. case 1: // set count of items
  138. {
  139. if (!slots)
  140. return;
  141. int id = *(int*)(message + 1);
  142. int count = *(int*)(message + 5);
  143. for (int i = 0; i < slots->getEintragAnzahl(); i++)
  144. {
  145. if (slots->get(i).id == id)
  146. {
  147. SlotInfo info = slots->get(i);
  148. info.itemCount = count;
  149. if (info.itemCount == 0)
  150. {
  151. DragController<InventoryDragSource, int>* controller = ((Game*)(Menu*)menuRegister->get("game"))->zInventoryDragController();
  152. if (controller && controller->getCurrentDragContainer() == this && controller->getCurrentDaragElement() == info.id)
  153. {
  154. controller->stopDrag();
  155. }
  156. }
  157. slots->set(info, i);
  158. break;
  159. }
  160. }
  161. break;
  162. }
  163. case 2: // add new stack
  164. {
  165. if (!slots)
  166. return;
  167. int id = *(int*)(message + 1);
  168. for (int i = 0; i < slots->getEintragAnzahl(); i++)
  169. {
  170. if (slots->get(i).id == id)
  171. {
  172. SlotInfo info = slots->get(i);
  173. info.itemCount = *(int*)(message + 5);
  174. info.hp = *(float*)(message + 9);
  175. info.maxHp = *(float*)(message + 13);
  176. info.durability = *(float*)(message + 17);
  177. info.maxDurability = *(float*)(message + 21);
  178. info.zItem = itemIcons->z(*(int*)(message + 25));
  179. slots->set(info, i);
  180. break;
  181. }
  182. }
  183. break;
  184. }
  185. }
  186. }
  187. bool InventoryView::tick(double tickVal)
  188. {
  189. return ZeichnungHintergrund::tick(tickVal);
  190. }
  191. void InventoryView::render(Bild& rObj)
  192. {
  193. ZeichnungHintergrund::render(rObj);
  194. if (!rObj.setDrawOptions(pos.x, pos.y, gr.x, gr.y))
  195. return;
  196. if (slots)
  197. {
  198. int x = 0;
  199. int y = 0;
  200. int rowCount = 0;
  201. for (SlotInfo info : *slots)
  202. {
  203. info.render(x, y, rObj, dragStartId == info.id, dragStopId == info.id);
  204. x += 60;
  205. if (++rowCount >= rowSize)
  206. {
  207. y += 60;
  208. x = 0;
  209. rowCount = 0;
  210. }
  211. }
  212. }
  213. rObj.releaseDrawOptions();
  214. }
  215. void InventoryView::doMausEreignis(MausEreignis& me, bool userRet)
  216. {
  217. if (!slots)
  218. return;
  219. DragController<InventoryDragSource, int>* controller = ((Game*)(Menu*)menuRegister->get("game"))->zInventoryDragController();
  220. int x = 0;
  221. int y = 0;
  222. int rowCount = 0;
  223. int slot = 0;
  224. dragStopId = -1;
  225. for (SlotInfo info : *slots)
  226. {
  227. if (me.mx >= x && me.mx < x + 50 && me.my >= y && me.my < y + 50)
  228. {
  229. if (me.id == ME_RLinks)
  230. {
  231. if (!controller->getCurrentDragContainer() && info.itemCount > 0)
  232. {
  233. controller->beginDrag(this, info.id, info.zItem, [this]()
  234. {
  235. dragStartId = -1;
  236. });
  237. dragStartId = info.id;
  238. }
  239. else if (controller->getCurrentDragContainer())
  240. {
  241. // request to transfer items from source to target slot
  242. Framework::Either<int, Framework::VecN<int, 4>> source = controller->getCurrentDragContainer()->getInventoryTarget();
  243. int len = 2 + (source.isA() ? 4 : 16) + 5 + (target.isA() ? 4 : 16) + 4;
  244. char* msg = new char[len];
  245. int index = 0;
  246. msg[index++] = 6;
  247. msg[index++] = (char)source.isA();
  248. if (source.isA())
  249. {
  250. *(int*)(msg + index) = source.getA();
  251. index += 4;
  252. }
  253. else
  254. {
  255. *(int*)(msg + index) = source.getB()[0];
  256. *(int*)(msg + index + 4) = source.getB()[1];
  257. *(int*)(msg + index + 8) = source.getB()[2];
  258. *(int*)(msg + index + 12) = source.getB()[3];
  259. index += 16;
  260. }
  261. *(int*)(msg + index) = controller->getCurrentDaragElement();
  262. index += 4;
  263. msg[index++] = target.isA();
  264. if (target.isA())
  265. {
  266. *(int*)(msg + index) = target.getA();
  267. index += 4;
  268. }
  269. else
  270. {
  271. *(int*)(msg + index) = target.getB()[0];
  272. *(int*)(msg + index + 4) = target.getB()[1];
  273. *(int*)(msg + index + 8) = target.getB()[2];
  274. *(int*)(msg + index + 12) = target.getB()[3];
  275. index += 16;
  276. }
  277. *(int*)(msg + index) = info.id;
  278. network->zFactoryClient()->sendPlayerAction(msg, len);
  279. delete[] msg;
  280. }
  281. }
  282. else
  283. {
  284. if (controller->getCurrentDragContainer() && (controller->getCurrentDragContainer() != this || controller->getCurrentDaragElement() != info.id))
  285. {
  286. dragStopId = info.id;
  287. }
  288. }
  289. break;
  290. }
  291. x += 60;
  292. if (++rowCount >= rowSize)
  293. {
  294. y += 60;
  295. x = 0;
  296. rowCount = 0;
  297. }
  298. slot++;
  299. }
  300. ZeichnungHintergrund::doMausEreignis(me, userRet);
  301. }
  302. Framework::Either<int, Framework::VecN<int, 4>> InventoryView::getInventoryTarget() const
  303. {
  304. return target;
  305. }