Game.cpp 9.2 KB

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