Game.cpp 9.3 KB

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