CraftingGrid.cpp 19 KB

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