InventoryView.cpp 15 KB

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