CraftingGrid.cpp 18 KB

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