InventoryView.cpp 15 KB

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