Game.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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. for (UIMLDialog* dialog : dialogs)
  144. {
  145. if (dialog->getName().istGleich(dialogName))
  146. {
  147. exists = 1;
  148. break;
  149. }
  150. }
  151. delete[] dialogName;
  152. if (!exists)
  153. {
  154. int uimlLen = *(int*)(data + 3 + len);
  155. char* uiml = new char[uimlLen + 1];
  156. memcpy(uiml, data + 7 + len, uimlLen);
  157. uiml[uimlLen] = 0;
  158. UIMLDialog* dialog
  159. = new UIMLDialog(uiml, [this](UIMLDialog* dialog) {
  160. window->zBildschirm()->postAction([this, dialog]() {
  161. int index = 0;
  162. for (UIMLDialog* d : dialogs)
  163. {
  164. if (d == dialog)
  165. {
  166. window->zBildschirm()->removeMember(d);
  167. dialogs.remove(index);
  168. World::INSTANCE->zKamera()
  169. ->setControlEnabled(
  170. dialogs.getEintragAnzahl() == 0);
  171. updateRecipieVisibility();
  172. break;
  173. }
  174. index++;
  175. }
  176. });
  177. });
  178. dialogs.add(dialog);
  179. updateRecipieVisibility();
  180. World::INSTANCE->zKamera()->setControlEnabled(0);
  181. window->zBildschirm()->addMember(dialog);
  182. delete[] uiml;
  183. }
  184. break;
  185. }
  186. case 1:
  187. { // element message
  188. for (UIMLDialog* dialog : dialogs)
  189. {
  190. dialog->api(data + 1);
  191. }
  192. short idLen = *(short*)(data + 1);
  193. char* id = new char[idLen + 1];
  194. memcpy(id, data + 3, idLen);
  195. id[idLen] = 0;
  196. NetworkAPIProcessor* processor = dynamic_cast<NetworkAPIProcessor*>(
  197. guiView->zZeichnungById(id));
  198. if (processor) processor->api(data + 3 + idLen);
  199. delete[] id;
  200. break;
  201. }
  202. case 2:
  203. { // set gui
  204. int uimlLen = *(int*)(data + 1);
  205. char* uiml = new char[uimlLen + 1];
  206. memcpy(uiml, data + 5, uimlLen);
  207. uiml[uimlLen] = 0;
  208. guiView->setUIML(uiml);
  209. guiView->layout();
  210. delete[] uiml;
  211. }
  212. case 3:
  213. { // update dialog content
  214. short dialogNameLen = *(short*)(data + 1);
  215. char* dialogName = new char[dialogNameLen + 1];
  216. memcpy(dialogName, data + 3, dialogNameLen);
  217. dialogName[dialogNameLen] = 0;
  218. for (UIMLDialog* dialog : dialogs)
  219. {
  220. if (dialog->getName().istGleich(dialogName))
  221. {
  222. int uimlLen = *(int*)(data + 3 + dialogNameLen);
  223. char* uiml = new char[uimlLen + 1];
  224. memcpy(uiml, data + 7 + dialogNameLen, uimlLen);
  225. uiml[uimlLen] = 0;
  226. dialog->updateUIML(uiml);
  227. delete[] uiml;
  228. break;
  229. }
  230. }
  231. }
  232. }
  233. }
  234. void Game::closeCurrentDialog()
  235. {
  236. if (dialogs.getEintragAnzahl() > 0)
  237. {
  238. UIMLDialog* d = dialogs.get(dialogs.getEintragAnzahl() - 1);
  239. d->close();
  240. }
  241. }
  242. DragController<InventoryDragSource, int>* Game::zInventoryDragController()
  243. {
  244. return inventoryDragController;
  245. }
  246. void Game::setTargetUIML(Framework::Text uiml)
  247. {
  248. if (uiml.getLength())
  249. {
  250. window->zBildschirm()->lock();
  251. targetUIMLView->setUIML(uiml);
  252. targetUIMLView->layout();
  253. window->zBildschirm()->unlock();
  254. targetUIMLView->setSize(targetUIMLView->calculateContentSize());
  255. targetUIMLView->setPosition(
  256. window->zBildschirm()->zGraphicsApi()->getBackBufferSize()
  257. - targetUIMLView->getSize());
  258. targetUIMLView->addStyle(UIMLView::Style::Sichtbar);
  259. }
  260. else
  261. {
  262. targetUIMLView->removeStyle(UIMLView::Style::Sichtbar);
  263. }
  264. }
  265. void Game::updateRecipieVisibility()
  266. {
  267. if (!recipieVisible)
  268. {
  269. if (dialogs.getEintragAnzahl() > 0)
  270. {
  271. recipieVisible = 1;
  272. window->zBildschirm()->addMember(
  273. dynamic_cast<Zeichnung*>(filter->getThis()));
  274. window->zBildschirm()->addMember(
  275. dynamic_cast<Zeichnung*>(searchIcon->getThis()));
  276. }
  277. }
  278. else
  279. {
  280. if (dialogs.getEintragAnzahl() == 0)
  281. {
  282. recipieVisible = 0;
  283. window->zBildschirm()->removeMember(filter);
  284. window->zBildschirm()->removeMember(searchIcon);
  285. if (itemListContainer)
  286. itemListContainer->removeStyle(Fenster::Style::Sichtbar);
  287. }
  288. }
  289. }
  290. void Game::showItemList()
  291. {
  292. if (!itemListContainer)
  293. {
  294. itemListContainer = new ItemListContainer();
  295. window->zBildschirm()->addMember(
  296. dynamic_cast<Zeichnung*>(itemListContainer->getThis()));
  297. }
  298. itemListContainer->addStyle(Fenster::Style::Sichtbar);
  299. }
  300. bool Game::isItemListVisible()
  301. {
  302. return itemListContainer
  303. && itemListContainer->hatStyle(Fenster::Style::Sichtbar);
  304. }
  305. const Text* Game::zFilterText()
  306. {
  307. return filter->zText();
  308. }
  309. void Game::makeChatButtonVisible()
  310. {
  311. chatButton->addStyle(Knopf::Style::Sichtbar);
  312. }
  313. Chat* Game::zChat() const
  314. {
  315. return chat;
  316. }
  317. MapWindow* Game::zMap() const
  318. {
  319. return mapWindow;
  320. }
  321. void Game::hide()
  322. {
  323. Menu::hide();
  324. window->zBildschirm()->postAction([this]() {
  325. if (itemListContainer)
  326. {
  327. window->zBildschirm()->removeMember(itemListContainer);
  328. itemListContainer->release();
  329. itemListContainer = 0;
  330. }
  331. });
  332. }
  333. ScreenCenter::ScreenCenter()
  334. : Zeichnung()
  335. {
  336. setPosition(0, 0);
  337. setSize(uiFactory.initParam.bildschirm->getBackBufferSize());
  338. }
  339. void ScreenCenter::render(Bild& zRObj)
  340. {
  341. zRObj.drawLinieH(gr.x / 2 - 5, gr.y / 2, 10, 0xFFFFFFFF);
  342. zRObj.drawLinieV(gr.x / 2, gr.y / 2 - 5, 10, 0xFFFFFFFF);
  343. }