InventoryView.cpp 17 KB

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