Game.cpp 10 KB

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