Game.cpp 11 KB

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