InventoryView.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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 (damage < maxDamage)
  53. {
  54. rObj.fillRegion(x + 1, y + 47, 50, 2, 0xFF000000);
  55. rObj.fillRegion(x + 1, y + 47, (int)((damage / maxDamage) * 50), 2, 0xFFFF0000);
  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.damage = *(float*)(message += 4);
  122. info.maxDamage = *(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. int id = *(int*)(message + 1);
  140. int count = *(int*)(message + 5);
  141. for (int i = 0; i < slots->getEintragAnzahl(); i++)
  142. {
  143. if (slots->get(i).id == id)
  144. {
  145. SlotInfo info = slots->get(i);
  146. info.itemCount = count;
  147. if (info.itemCount == 0)
  148. {
  149. DragController<InventoryDragSource, int>* controller = ((Game*)(Menu*)menuRegister->get("game"))->zInventoryDragController();
  150. if (controller && controller->getCurrentDragContainer() == this && controller->getCurrentDaragElement() == info.id)
  151. {
  152. controller->stopDrag();
  153. }
  154. }
  155. slots->set(info, i);
  156. break;
  157. }
  158. }
  159. break;
  160. }
  161. case 2: // add new stack
  162. {
  163. int id = *(int*)(message + 1);
  164. for (int i = 0; i < slots->getEintragAnzahl(); i++)
  165. {
  166. if (slots->get(i).id == id)
  167. {
  168. SlotInfo info = slots->get(i);
  169. info.itemCount = *(int*)(message + 5);
  170. info.damage = *(float*)(message + 9);
  171. info.maxDamage = *(float*)(message + 13);
  172. info.durability = *(float*)(message + 17);
  173. info.maxDurability = *(float*)(message + 21);
  174. info.zItem = itemIcons->z(*(int*)(message + 25));
  175. slots->set(info, i);
  176. break;
  177. }
  178. }
  179. break;
  180. }
  181. }
  182. }
  183. bool InventoryView::tick(double tickVal)
  184. {
  185. return ZeichnungHintergrund::tick(tickVal);
  186. }
  187. void InventoryView::render(Bild& rObj)
  188. {
  189. ZeichnungHintergrund::render(rObj);
  190. if (!rObj.setDrawOptions(pos.x, pos.y, gr.x, gr.y))
  191. return;
  192. if (slots)
  193. {
  194. int x = 0;
  195. int y = 0;
  196. int rowCount = 0;
  197. for (SlotInfo info : *slots)
  198. {
  199. info.render(x, y, rObj, dragStartId == info.id, dragStopId == info.id);
  200. x += 60;
  201. if (++rowCount >= rowSize)
  202. {
  203. y += 60;
  204. x = 0;
  205. rowCount = 0;
  206. }
  207. }
  208. }
  209. rObj.releaseDrawOptions();
  210. }
  211. void InventoryView::doMausEreignis(MausEreignis& me, bool userRet)
  212. {
  213. DragController<InventoryDragSource, int>* controller = ((Game*)(Menu*)menuRegister->get("game"))->zInventoryDragController();
  214. int x = 0;
  215. int y = 0;
  216. int rowCount = 0;
  217. int slot = 0;
  218. dragStopId = -1;
  219. for (SlotInfo info : *slots)
  220. {
  221. if (me.mx >= x && me.mx < x + 50 && me.my >= y && me.my < y + 50)
  222. {
  223. if (me.id == ME_RLinks)
  224. {
  225. if (!controller->getCurrentDragContainer() && info.itemCount > 0)
  226. {
  227. controller->beginDrag(this, info.id, info.zItem, [this]()
  228. {
  229. dragStartId = -1;
  230. });
  231. dragStartId = info.id;
  232. }
  233. else if (controller->getCurrentDragContainer())
  234. {
  235. // request to transfer items from source to target slot
  236. Framework::Either<int, Framework::VecN<int, 4>> source = controller->getCurrentDragContainer()->getInventoryTarget();
  237. int len = 2 + (source.isA() ? 4 : 16) + 5 + (target.isA() ? 4 : 16) + 4;
  238. char* msg = new char[len];
  239. int index = 0;
  240. msg[index++] = 6;
  241. msg[index++] = (char)source.isA();
  242. if (source.isA())
  243. {
  244. *(int*)(msg + index) = source.getA();
  245. index += 4;
  246. }
  247. else
  248. {
  249. *(int*)(msg + index) = source.getB()[0];
  250. *(int*)(msg + index + 4) = source.getB()[1];
  251. *(int*)(msg + index + 8) = source.getB()[2];
  252. *(int*)(msg + index + 12) = source.getB()[3];
  253. index += 16;
  254. }
  255. *(int*)(msg + index) = controller->getCurrentDaragElement();
  256. index += 4;
  257. msg[index++] = target.isA();
  258. if (target.isA())
  259. {
  260. *(int*)(msg + index) = target.getA();
  261. index += 4;
  262. }
  263. else
  264. {
  265. *(int*)(msg + index) = target.getB()[0];
  266. *(int*)(msg + index + 4) = target.getB()[1];
  267. *(int*)(msg + index + 8) = target.getB()[2];
  268. *(int*)(msg + index + 12) = target.getB()[3];
  269. index += 16;
  270. }
  271. *(int*)(msg + index) = info.id;
  272. network->zFactoryClient()->sendPlayerAction(msg, len);
  273. delete[] msg;
  274. }
  275. }
  276. else
  277. {
  278. if (controller->getCurrentDragContainer() && (controller->getCurrentDragContainer() != this || controller->getCurrentDaragElement() != info.id))
  279. {
  280. dragStopId = info.id;
  281. }
  282. }
  283. break;
  284. }
  285. x += 60;
  286. if (++rowCount >= rowSize)
  287. {
  288. y += 60;
  289. x = 0;
  290. rowCount = 0;
  291. }
  292. slot++;
  293. }
  294. ZeichnungHintergrund::doMausEreignis(me, userRet);
  295. }
  296. Framework::Either<int, Framework::VecN<int, 4>> InventoryView::getInventoryTarget() const
  297. {
  298. return target;
  299. }