CraftingGrid.cpp 17 KB

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