Game.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. #include "Game.h"
  2. #include <AsynchronCall.h>
  3. #include <Bildschirm.h>
  4. #include <DateiSystem.h>
  5. #include "Globals.h"
  6. #include "Initialisierung.h"
  7. #include "ItemBar.h"
  8. #include "StatusBars.h"
  9. Game::Game(Bildschirm* zScreen)
  10. : Menu(zScreen),
  11. recipieVisible(0),
  12. itemListContainer(0)
  13. {
  14. LTDBDatei dat;
  15. dat.setDatei(new Text("data/images/gui_icons.ltdb"));
  16. dat.leseDaten(0);
  17. Bild* search = dat.laden(0, new Text("search.png"));
  18. searchIcon = new BildZ();
  19. searchIcon->setBildZ(search);
  20. searchIcon->setStyle(
  21. BildZ::Style::Sichtbar | BildZ::Style::Alpha | BildZ::Style::Rahmen);
  22. searchIcon->setSize(20, 20);
  23. searchIcon->setRahmenFarbe(0xFF6d6d6d);
  24. elements.add(new ScreenCenter());
  25. inventoryDragController = new DragController<InventoryDragSource, int>();
  26. logout = initKnopf(10, 10, 200, 20, Knopf::Style::Normal, "Verlassen");
  27. logout->setMausEreignis([this, zScreen](void* p, void* o, MausEreignis me) {
  28. if (me.id == ME_RLinks)
  29. {
  30. logout->removeStyle(Knopf::Style::Erlaubt);
  31. zScreen->postAction([this, zScreen]() {
  32. World::INSTANCE->zClient()->leaveGame();
  33. logout->addStyle(Knopf::Style::Erlaubt);
  34. });
  35. }
  36. return 1;
  37. });
  38. elements.add(logout);
  39. debug = initTextFeld(10,
  40. 40,
  41. 500,
  42. 250,
  43. TextFeld::Style::Text | TextFeld::Style::Mehrzeilig,
  44. "");
  45. elements.add(debug);
  46. guiView = new UIMLView("<v/>", uiFactory);
  47. guiView->addKnownElement(new ItemBarElement());
  48. guiView->addKnownElement(new StatusBarsElement());
  49. guiView->setStyle(UIMLView::Style::Sichtbar);
  50. guiView->setSize(window->zBildschirm()->getBackBufferSize());
  51. elements.add(guiView);
  52. targetUIMLView = new UIMLView("<v/>", uiFactory);
  53. targetUIMLView->setStyle(
  54. UIMLView::Style::Hintergrund | UIMLView::Style::HAlpha);
  55. targetUIMLView->setHintergrundFarbe(0xA0000000);
  56. elements.add(targetUIMLView);
  57. filter = initTextFeld(zScreen->getBackBufferSize().x / 2 - 200,
  58. zScreen->getBackBufferSize().y - 200,
  59. 400,
  60. 20,
  61. Framework::TextFeld::Style::TextFeld,
  62. "");
  63. searchIcon->setPosition(
  64. filter->getX() + filter->getBreite() - 20, filter->getY());
  65. chat = new Chat();
  66. elements.add(chat);
  67. LTDBDatei iconsDat;
  68. iconsDat.setDatei(new Text("data/images/gui_icons.ltdb"));
  69. iconsDat.leseDaten(0);
  70. chatButton = uiFactory.createKnopf(uiFactory.initParam);
  71. chatButton->setToolTipText("Chat", zScreen, uiFactory.initParam.schrift);
  72. chatButton->setAlphaFeldFarbe(0x5F337AB7);
  73. chatButton->setSize(40, 40);
  74. chatButton->setPosition(5, zScreen->getBackBufferSize().y - 45);
  75. chatButton->addStyle(Framework::Knopf::Style::HBild
  76. | Framework::Knopf::Style::HAlpha
  77. | Framework::Knopf::Style::Hintergrund);
  78. chatButton->setHintergrundBildZ(iconsDat.laden(0, new Text("chat.png")));
  79. chatButton->setMausEreignis(
  80. [this](void* p, void* o, Framework::MausEreignis me) {
  81. if (me.id == ME_RLinks)
  82. {
  83. chat->addStyle(Fenster::Style::Sichtbar);
  84. chatButton->removeStyle(Knopf::Style::Sichtbar);
  85. }
  86. return 1;
  87. });
  88. elements.add(chatButton);
  89. mapWindow = new MapWindow();
  90. elements.add(mapWindow);
  91. }
  92. Game::~Game()
  93. {
  94. inventoryDragController->release();
  95. filter->release();
  96. searchIcon->release();
  97. if (itemListContainer) itemListContainer->release();
  98. }
  99. void Game::updatePosition(
  100. Vec3<float> position, bool target, Vec3<int> targetPos)
  101. {
  102. Text txt = "Position: (";
  103. txt.setPrecision(2);
  104. txt += position.x;
  105. txt += ", ";
  106. txt += position.y;
  107. txt += ", ";
  108. txt += position.z;
  109. txt += ")";
  110. if (target)
  111. {
  112. txt += "\nTarget: (";
  113. txt += targetPos.x;
  114. txt += ", ";
  115. txt += targetPos.y;
  116. txt += ", ";
  117. txt += targetPos.z;
  118. txt += ")\n";
  119. Block* b = World::INSTANCE->zBlockAt(targetPos);
  120. if (b)
  121. {
  122. txt += "TargetLight: \n";
  123. txt += b->printLightInfo();
  124. }
  125. txt += "Target Chunk: ";
  126. txt += World::INSTANCE->getChunkCenter(targetPos.x, targetPos.y).x;
  127. txt += ", ";
  128. txt += World::INSTANCE->getChunkCenter(targetPos.x, targetPos.y).y;
  129. }
  130. debug->setText(txt);
  131. }
  132. void Game::api(char* data)
  133. {
  134. switch (data[0])
  135. {
  136. case 0: // open dialog
  137. {
  138. bool exists = 0;
  139. short len = *(short*)(data + 1);
  140. char* dialogName = new char[len + 1];
  141. memcpy(dialogName, data + 3, len);
  142. dialogName[len] = 0;
  143. dialogCs.lock();
  144. for (UIMLDialog* dialog : dialogs)
  145. {
  146. if (dialog->getName().istGleich(dialogName))
  147. {
  148. exists = 1;
  149. break;
  150. }
  151. }
  152. dialogCs.unlock();
  153. delete[] dialogName;
  154. if (!exists)
  155. {
  156. int uimlLen = *(int*)(data + 3 + len);
  157. char* uiml = new char[uimlLen + 1];
  158. memcpy(uiml, data + 7 + len, uimlLen);
  159. uiml[uimlLen] = 0;
  160. UIMLDialog* dialog
  161. = new UIMLDialog(uiml, [this](UIMLDialog* dialog) {
  162. window->zBildschirm()->postAction([this, dialog]() {
  163. int index = 0;
  164. dialogCs.lock();
  165. for (UIMLDialog* d : dialogs)
  166. {
  167. if (d == dialog)
  168. {
  169. window->zBildschirm()->removeMember(d);
  170. dialogs.remove(index);
  171. World::INSTANCE->zKamera()
  172. ->setControlEnabled(
  173. dialogs.getEintragAnzahl() == 0);
  174. updateRecipieVisibility();
  175. break;
  176. }
  177. index++;
  178. }
  179. dialogCs.unlock();
  180. });
  181. });
  182. dialogCs.lock();
  183. dialogs.add(dialog);
  184. dialogCs.unlock();
  185. updateRecipieVisibility();
  186. World::INSTANCE->zKamera()->setControlEnabled(0);
  187. window->zBildschirm()->addMember(dialog);
  188. delete[] uiml;
  189. }
  190. break;
  191. }
  192. case 1:
  193. { // element message
  194. dialogCs.lock();
  195. for (UIMLDialog* dialog : dialogs)
  196. {
  197. dialog->api(data + 1);
  198. }
  199. dialogCs.unlock();
  200. short idLen = *(short*)(data + 1);
  201. char* id = new char[idLen + 1];
  202. memcpy(id, data + 3, idLen);
  203. id[idLen] = 0;
  204. NetworkAPIProcessor* processor = dynamic_cast<NetworkAPIProcessor*>(
  205. guiView->zZeichnungById(id));
  206. int processorId = *(int*)(data + 3 + idLen);
  207. if (processor
  208. && (processorId == -1 || processorId == processor->getId()))
  209. {
  210. processor->api(data + 7 + idLen);
  211. }
  212. delete[] id;
  213. break;
  214. }
  215. case 2:
  216. { // set gui
  217. int uimlLen = *(int*)(data + 1);
  218. char* uiml = new char[uimlLen + 1];
  219. memcpy(uiml, data + 5, uimlLen);
  220. uiml[uimlLen] = 0;
  221. guiView->setUIML(uiml);
  222. guiView->layout();
  223. delete[] uiml;
  224. }
  225. case 3:
  226. { // update dialog content
  227. short dialogNameLen = *(short*)(data + 1);
  228. char* dialogName = new char[dialogNameLen + 1];
  229. memcpy(dialogName, data + 3, dialogNameLen);
  230. dialogName[dialogNameLen] = 0;
  231. dialogCs.lock();
  232. for (UIMLDialog* dialog : dialogs)
  233. {
  234. if (dialog->getName().istGleich(dialogName))
  235. {
  236. int uimlLen = *(int*)(data + 3 + dialogNameLen);
  237. char* uiml = new char[uimlLen + 1];
  238. memcpy(uiml, data + 7 + dialogNameLen, uimlLen);
  239. uiml[uimlLen] = 0;
  240. dialog->updateUIML(uiml);
  241. delete[] uiml;
  242. break;
  243. }
  244. }
  245. dialogCs.unlock();
  246. }
  247. }
  248. }
  249. void Game::closeCurrentDialog()
  250. {
  251. dialogCs.lock();
  252. if (dialogs.getEintragAnzahl() > 0)
  253. {
  254. UIMLDialog* d = dialogs.get(dialogs.getEintragAnzahl() - 1);
  255. d->close();
  256. }
  257. dialogCs.unlock();
  258. }
  259. DragController<InventoryDragSource, int>* Game::zInventoryDragController()
  260. {
  261. return inventoryDragController;
  262. }
  263. void Game::setTargetUIML(Framework::Text uiml)
  264. {
  265. if (uiml.getLength())
  266. {
  267. window->zBildschirm()->lock();
  268. targetUIMLView->setUIML(uiml);
  269. targetUIMLView->layout();
  270. window->zBildschirm()->unlock();
  271. targetUIMLView->setSize(targetUIMLView->calculateContentSize());
  272. targetUIMLView->setPosition(
  273. window->zBildschirm()->zGraphicsApi()->getBackBufferSize()
  274. - targetUIMLView->getSize());
  275. targetUIMLView->addStyle(UIMLView::Style::Sichtbar);
  276. }
  277. else
  278. {
  279. targetUIMLView->removeStyle(UIMLView::Style::Sichtbar);
  280. }
  281. }
  282. void Game::updateRecipieVisibility()
  283. {
  284. if (!recipieVisible)
  285. {
  286. if (dialogs.getEintragAnzahl() > 0)
  287. {
  288. recipieVisible = 1;
  289. window->zBildschirm()->addMember(
  290. dynamic_cast<Zeichnung*>(filter->getThis()));
  291. window->zBildschirm()->addMember(
  292. dynamic_cast<Zeichnung*>(searchIcon->getThis()));
  293. }
  294. }
  295. else
  296. {
  297. if (dialogs.getEintragAnzahl() == 0)
  298. {
  299. recipieVisible = 0;
  300. window->zBildschirm()->removeMember(filter);
  301. window->zBildschirm()->removeMember(searchIcon);
  302. if (itemListContainer)
  303. itemListContainer->removeStyle(Fenster::Style::Sichtbar);
  304. }
  305. }
  306. }
  307. void Game::showItemList()
  308. {
  309. if (!itemListContainer)
  310. {
  311. itemListContainer = new ItemListContainer();
  312. window->zBildschirm()->addMember(
  313. dynamic_cast<Zeichnung*>(itemListContainer->getThis()));
  314. }
  315. itemListContainer->addStyle(Fenster::Style::Sichtbar);
  316. }
  317. bool Game::isItemListVisible()
  318. {
  319. return itemListContainer
  320. && itemListContainer->hatStyle(Fenster::Style::Sichtbar);
  321. }
  322. const Text* Game::zFilterText()
  323. {
  324. return filter->zText();
  325. }
  326. void Game::makeChatButtonVisible()
  327. {
  328. chatButton->addStyle(Knopf::Style::Sichtbar);
  329. }
  330. Chat* Game::zChat() const
  331. {
  332. return chat;
  333. }
  334. MapWindow* Game::zMap() const
  335. {
  336. return mapWindow;
  337. }
  338. void Game::hide()
  339. {
  340. Menu::hide();
  341. window->zBildschirm()->postAction([this]() {
  342. if (itemListContainer)
  343. {
  344. window->zBildschirm()->removeMember(itemListContainer);
  345. itemListContainer->release();
  346. itemListContainer = 0;
  347. }
  348. });
  349. }
  350. ScreenCenter::ScreenCenter()
  351. : Zeichnung()
  352. {
  353. setPosition(0, 0);
  354. setSize(uiFactory.initParam.bildschirm->getBackBufferSize());
  355. }
  356. void ScreenCenter::render(Bild& zRObj)
  357. {
  358. zRObj.drawLinieH(gr.x / 2 - 5, gr.y / 2, 10, 0xFFFFFFFF);
  359. zRObj.drawLinieV(gr.x / 2, gr.y / 2 - 5, 10, 0xFFFFFFFF);
  360. }