InventoryView.cpp 16 KB

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