CraftingGrid.cpp 11 KB

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