CraftingGrid.cpp 16 KB

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