InventoryView.cpp 17 KB

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