InventoryView.cpp 11 KB

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