CraftingGrid.cpp 19 KB

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