CraftingGrid.cpp 12 KB

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