InventoryView.cpp 16 KB

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