CraftingGrid.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  1. #include "CraftingGrid.h"
  2. #include <DateiSystem.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. CraftingGridElement::CraftingGridElement()
  10. : UIMLElement()
  11. {}
  12. //! prüft, ob dieses UIML Element für ein bestimmtes xml Element zuständig ist
  13. bool CraftingGridElement::isApplicableFor(Framework::XML::Element& element)
  14. {
  15. return element.getName().istGleich("craftingGrid");
  16. }
  17. //! erstellt eine neue Zeichnung zu einem gegebenen xml Element
  18. Framework::Zeichnung* CraftingGridElement::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>>(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 CraftingGridView(element.getAttributeValue("id"),
  44. (int)element.getAttributeValue("rowSize"),
  45. (int)element.getAttributeValue("colSize"),
  46. (int)element.getAttributeValue("numOutputSlots"),
  47. target);
  48. }
  49. //! wendet die layout parameter zu einer Zeichnung an
  50. void CraftingGridElement::layout(Framework::XML::Element& element,
  51. Framework::Zeichnung& z,
  52. int pWidth,
  53. int pHeight,
  54. Framework::UIMLContainer& generalLayouter)
  55. {
  56. z.setSize(500, 500); // TODO: calculate size
  57. UIMLElement::layout(element, z, pWidth, pHeight, generalLayouter);
  58. }
  59. CraftingGridView::CraftingGridView(Text id,
  60. int rowSize,
  61. int colSize,
  62. int numOutputSlots,
  63. Either<int, VecN<int, 4>> target)
  64. : ZeichnungHintergrund(),
  65. rowSize(rowSize),
  66. colSize(colSize),
  67. numOutputSlots(numOutputSlots),
  68. target(target),
  69. slots(0),
  70. outputs(0),
  71. id(id),
  72. dragStartId(-1),
  73. dragStopId(-1),
  74. currentTooltipSlot(-1),
  75. requestetTooltipSlot(-1)
  76. {
  77. craft = uiFactory.createKnopf(uiFactory.initParam);
  78. craft->setPosition(rowSize * 60, 0);
  79. craft->setSize(40, 20);
  80. craft->setText("Craft");
  81. craft->setMausEreignis([this](void* p, void* o, MausEreignis me) {
  82. if (me.id == ME_RLinks)
  83. {
  84. char* msg = new char[2 + (this->target.isA() ? 4 : 16)];
  85. msg[0] = 7; // request crafting
  86. msg[1] = (char)this->target.isA();
  87. if (msg[1])
  88. *(int*)(msg + 2) = this->target.getA();
  89. else
  90. {
  91. *(int*)(msg + 2) = this->target.getB()[0];
  92. *(int*)(msg + 6) = this->target.getB()[1];
  93. *(int*)(msg + 10) = this->target.getB()[2];
  94. *(int*)(msg + 14) = this->target.getB()[3];
  95. }
  96. World::INSTANCE->zClient()->sendPlayerAction(
  97. msg, 2 + (this->target.isA() ? 4 : 16));
  98. delete[] msg;
  99. }
  100. return 1;
  101. });
  102. recipies = uiFactory.createKnopf(uiFactory.initParam);
  103. recipies->setPosition(rowSize * 60, colSize * 60 - 49);
  104. recipies->setSize(40, 40);
  105. recipies->addStyle(
  106. Knopf::Style::HBild | Knopf::Style::HAlpha | Knopf::Style::Hintergrund);
  107. recipies->removeStyle(Knopf::Style::Buffered);
  108. recipies->setRahmenBreite(1);
  109. Framework::LTDBDatei file;
  110. file.setDatei(new Framework::Text("data/bilder/gui_icons.ltdb"));
  111. file.leseDaten(0);
  112. recipies->setHintergrundBildZ(file.laden(0, new Text("recipies.png")));
  113. recipies->setMausEreignis([this](void* p, void* o, MausEreignis me) {
  114. if (me.id == ME_RLinks)
  115. {
  116. ((Game*)(Menu*)menuRegister->get("game"))->showItemList();
  117. }
  118. return 1;
  119. });
  120. recipies->setToolTipText(
  121. "Recipies", uiFactory.initParam.bildschirm, uiFactory.initParam.schrift);
  122. setStyle(ZeichnungHintergrund::Style::Sichtbar
  123. | ZeichnungHintergrund::Style::Erlaubt);
  124. char* msg = new char[id.getLength() + 12 + 3];
  125. msg[0] = 0; // request inventory tooltip
  126. msg[1] = (char)id.getLength();
  127. memcpy(msg + 2, id.getText(), id.getLength());
  128. msg[2 + id.getLength()] = (char)12;
  129. memcpy(msg + 3 + id.getLength(), "CraftingGrid", 12);
  130. World::INSTANCE->zClient()->inventoryAPIRequest(
  131. target, msg, id.getLength() + 12 + 3);
  132. delete[] msg;
  133. setNeedToolTipEvent([this](Zeichnung* z, Punkt p) {
  134. int slot = getSlotByLocalPos(p);
  135. if (currentTooltipSlot != slot && currentTooltipSlot != -1)
  136. {
  137. std::cout << "closing tooltip\n";
  138. this->setToolTipZ(0);
  139. currentTooltipSlot = -1;
  140. }
  141. if (requestetTooltipSlot != slot && slot != -1)
  142. {
  143. std::cout << "requesting tooltip for slot " << slot << "\n";
  144. requestetTooltipSlot = slot;
  145. char* msg = new char[this->id.getLength() + 6];
  146. msg[0] = 2; // request inventory tooltip
  147. msg[1] = (char)this->id.getLength();
  148. memcpy(msg + 2, this->id.getText(), this->id.getLength());
  149. *(int*)(msg + 2 + this->id.getLength()) = slot;
  150. World::INSTANCE->zClient()->inventoryAPIRequest(
  151. this->target, msg, this->id.getLength() + 6);
  152. return 1;
  153. }
  154. return 0;
  155. });
  156. }
  157. CraftingGridView::~CraftingGridView()
  158. {
  159. DragController<InventoryDragSource, int>* controller
  160. = ((Game*)(Menu*)menuRegister->get("game"))->zInventoryDragController();
  161. if (controller->getCurrentDragContainer() == this) controller->stopDrag();
  162. if (slots) slots->release();
  163. if (outputs) outputs->release();
  164. char* msg = new char[id.getLength() + 2];
  165. msg[0] = 1;
  166. msg[1] = (char)id.getLength();
  167. memcpy(msg + 2, id.getText(), id.getLength());
  168. World::INSTANCE->zClient()->inventoryAPIRequest(
  169. target, msg, id.getLength() + 2);
  170. delete[] msg;
  171. craft->release();
  172. recipies->release();
  173. }
  174. int CraftingGridView::getSlotByLocalPos(Punkt pos)
  175. {
  176. int x = 0;
  177. int y = 0;
  178. int rowCount = 0;
  179. int slot = 0;
  180. dragStopId = -1;
  181. if (slots)
  182. {
  183. for (SlotInfo info : *slots)
  184. {
  185. if (pos.x >= x && pos.x < x + 50 && pos.y >= y && pos.y < y + 50)
  186. return info.id;
  187. x += 60;
  188. if (++rowCount >= rowSize)
  189. {
  190. y += 60;
  191. x = 0;
  192. rowCount = 0;
  193. }
  194. slot++;
  195. }
  196. }
  197. return -1;
  198. }
  199. void CraftingGridView::api(char* message)
  200. {
  201. switch (message[0])
  202. {
  203. case 0:
  204. // send inventory content
  205. {
  206. Array<SlotInfo>* slots = new Array<SlotInfo>();
  207. int count = *(int*)(++message);
  208. for (int i = 0; i < count; i++)
  209. {
  210. SlotInfo info;
  211. info.id = *(int*)(message += 4);
  212. info.itemCount = *(int*)(message += 4);
  213. if (info.itemCount > 0)
  214. {
  215. info.hp = *(float*)(message += 4);
  216. info.maxHp = *(float*)(message += 4);
  217. info.durability = *(float*)(message += 4);
  218. info.maxDurability = *(float*)(message += 4);
  219. info.zItem = zItemType(*(int*)(message += 4))->zIcon();
  220. char len = *(message += 4);
  221. char* name = new char[len + 1];
  222. memcpy(name, message += 1, len);
  223. name[len] = 0;
  224. info.name = name;
  225. delete[] name;
  226. message += len - 4;
  227. }
  228. slots->add(info);
  229. }
  230. window->zBildschirm()->postAction([this, slots]() {
  231. if (this->slots) this->slots->release();
  232. this->slots = slots;
  233. });
  234. break;
  235. }
  236. case 1: // set count of items
  237. {
  238. if (!slots) return;
  239. int id = *(int*)(message + 1);
  240. int count = *(int*)(message + 5);
  241. for (int i = 0; i < slots->getEintragAnzahl(); i++)
  242. {
  243. if (slots->get(i).id == id)
  244. {
  245. SlotInfo info = slots->get(i);
  246. info.itemCount = count;
  247. if (info.itemCount == 0)
  248. {
  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. break;
  317. }
  318. case 100: // set crafting result
  319. {
  320. Array<SlotInfo>* outputs = new Array<SlotInfo>();
  321. int count = *(int*)(++message);
  322. for (int i = 0; i < count; i++)
  323. {
  324. SlotInfo info;
  325. info.id = i;
  326. info.itemCount = *(int*)(message += 4);
  327. if (info.itemCount > 0)
  328. {
  329. info.hp = *(float*)(message += 4);
  330. info.maxHp = *(float*)(message += 4);
  331. info.durability = *(float*)(message += 4);
  332. info.maxDurability = *(float*)(message += 4);
  333. info.zItem = zItemType(*(int*)(message += 4))->zIcon();
  334. }
  335. outputs->add(info);
  336. }
  337. window->zBildschirm()->postAction([this, outputs]() {
  338. if (this->outputs) this->outputs->release();
  339. this->outputs = outputs;
  340. });
  341. break;
  342. }
  343. }
  344. }
  345. bool CraftingGridView::tick(double tickVal)
  346. {
  347. return ZeichnungHintergrund::tick(tickVal);
  348. }
  349. void CraftingGridView::render(Bild& rObj)
  350. {
  351. ZeichnungHintergrund::render(rObj);
  352. if (!rObj.setDrawOptions(pos.x, pos.y, gr.x, gr.y)) return;
  353. int numRows = 1;
  354. if (slots)
  355. {
  356. int x = 0;
  357. int y = 0;
  358. int rowCount = 0;
  359. int index = 0;
  360. for (SlotInfo info : *slots)
  361. {
  362. info.render(
  363. x, y, rObj, dragStartId == info.id, dragStopId == info.id);
  364. x += 60;
  365. if (++rowCount >= rowSize)
  366. {
  367. y += 60;
  368. x = 0;
  369. rowCount = 0;
  370. if (index < slots->getEintragAnzahl() - 1) numRows++;
  371. }
  372. index++;
  373. }
  374. }
  375. craft->render(rObj);
  376. recipies->setStyle(Knopf::Style::Sichtbar,
  377. !((Game*)(Menu*)menuRegister->get("game"))->isItemListVisible());
  378. recipies->render(rObj);
  379. rObj.fillRegion(rowSize * 60, gr.y / 2 - 5, 25, 10, 0xFF52525E);
  380. rObj.drawDreieck(Punkt(rowSize * 60 + 25, gr.y / 2 - 15),
  381. Punkt(rowSize * 60 + 40, gr.y / 2),
  382. Punkt(rowSize * 60 + 25, gr.y / 2 + 15),
  383. 0xFF52525E);
  384. if (outputs)
  385. {
  386. int x = rowSize * 60 + 50;
  387. int y = 0;
  388. int colCount = 0;
  389. for (SlotInfo info : *outputs)
  390. {
  391. info.render(
  392. x, y, rObj, dragStartId == info.id, dragStopId == info.id);
  393. y += 60;
  394. if (++colCount >= numRows)
  395. {
  396. x += 60;
  397. y = 0;
  398. colCount = 0;
  399. }
  400. }
  401. }
  402. rObj.releaseDrawOptions();
  403. }
  404. void CraftingGridView::doMausEreignis(MausEreignis& me, bool userRet)
  405. {
  406. mausPos.x = me.originalX;
  407. mausPos.y = me.originalY;
  408. if (!slots) return;
  409. if (me.id == ME_Bewegung)
  410. {
  411. if (getSlotByLocalPos(Punkt(me.mx, me.my)) != currentTooltipSlot)
  412. {
  413. if (currentTooltipSlot != -1)
  414. {
  415. std::cout << "closing tooltip\n";
  416. setToolTipZ(0);
  417. }
  418. else
  419. toolTipRequested = 0;
  420. currentTooltipSlot = -1;
  421. }
  422. }
  423. craft->doPublicMausEreignis(me);
  424. recipies->doPublicMausEreignis(me);
  425. DragController<InventoryDragSource, int>* controller
  426. = ((Game*)(Menu*)menuRegister->get("game"))->zInventoryDragController();
  427. int x = 0;
  428. int y = 0;
  429. int rowCount = 0;
  430. int slot = 0;
  431. dragStopId = -1;
  432. for (SlotInfo info : *slots)
  433. {
  434. if (me.mx >= x && me.mx < x + 50 && me.my >= y && me.my < y + 50)
  435. {
  436. if (me.id == ME_RLinks)
  437. {
  438. if (!controller->getCurrentDragContainer()
  439. && info.itemCount > 0)
  440. {
  441. controller->beginDrag(this, info.id, info.zItem, [this]() {
  442. dragStartId = -1;
  443. });
  444. dragStartId = info.id;
  445. }
  446. else if (controller->getCurrentDragContainer())
  447. {
  448. // request to transfer items from source to target slot
  449. Framework::Either<int, Framework::VecN<int, 4>> source
  450. = controller->getCurrentDragContainer()
  451. ->getInventoryTarget();
  452. int len = 2 + (source.isA() ? 4 : 16) + 5
  453. + (target.isA() ? 4 : 16) + 4;
  454. char* msg = new char[len];
  455. int index = 0;
  456. msg[index++] = 6;
  457. msg[index++] = (char)source.isA();
  458. if (source.isA())
  459. {
  460. *(int*)(msg + index) = source.getA();
  461. index += 4;
  462. }
  463. else
  464. {
  465. *(int*)(msg + index) = source.getB()[0];
  466. *(int*)(msg + index + 4) = source.getB()[1];
  467. *(int*)(msg + index + 8) = source.getB()[2];
  468. *(int*)(msg + index + 12) = source.getB()[3];
  469. index += 16;
  470. }
  471. *(int*)(msg + index) = controller->getCurrentDaragElement();
  472. index += 4;
  473. msg[index++] = target.isA();
  474. if (target.isA())
  475. {
  476. *(int*)(msg + index) = target.getA();
  477. index += 4;
  478. }
  479. else
  480. {
  481. *(int*)(msg + index) = target.getB()[0];
  482. *(int*)(msg + index + 4) = target.getB()[1];
  483. *(int*)(msg + index + 8) = target.getB()[2];
  484. *(int*)(msg + index + 12) = target.getB()[3];
  485. index += 16;
  486. }
  487. *(int*)(msg + index) = info.id;
  488. World::INSTANCE->zClient()->sendPlayerAction(msg, len);
  489. delete[] msg;
  490. }
  491. }
  492. else
  493. {
  494. if (controller->getCurrentDragContainer()
  495. && (controller->getCurrentDragContainer() != this
  496. || controller->getCurrentDaragElement() != info.id))
  497. {
  498. dragStopId = info.id;
  499. }
  500. }
  501. break;
  502. }
  503. x += 60;
  504. if (++rowCount >= rowSize)
  505. {
  506. y += 60;
  507. x = 0;
  508. rowCount = 0;
  509. }
  510. slot++;
  511. }
  512. ZeichnungHintergrund::doMausEreignis(me, userRet);
  513. }
  514. Framework::Either<int, Framework::VecN<int, 4>>
  515. CraftingGridView::getInventoryTarget() const
  516. {
  517. return target;
  518. }