InventoryView.cpp 17 KB

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