CraftingGrid.cpp 19 KB

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