CraftingGrid.cpp 19 KB

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