InventoryView.cpp 16 KB

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