Game.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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(
  74. Framework::Knopf::Style::HBild | Framework::Knopf::Style::HAlpha |Framework::Knopf::Style::Hintergrund);
  75. chatButton->setHintergrundBildZ(iconsDat.laden(0, new Text("chat.png")));
  76. chatButton->setMausEreignis(
  77. [this](void* p, void* o, Framework::MausEreignis me) {
  78. if (me.id == ME_RLinks)
  79. {
  80. chat->addStyle(Fenster::Style::Sichtbar);
  81. chatButton->removeStyle(Knopf::Style::Sichtbar);
  82. }
  83. return 1;
  84. });
  85. elements.add(chatButton);
  86. }
  87. Game::~Game()
  88. {
  89. inventoryDragController->release();
  90. filter->release();
  91. itemListContainer->release();
  92. }
  93. void Game::updatePosition(
  94. Vec3<float> position, bool target, Vec3<int> targetPos)
  95. {
  96. Text txt = "Position: (";
  97. txt.setPrecision(2);
  98. txt += position.x;
  99. txt += ", ";
  100. txt += position.y;
  101. txt += ", ";
  102. txt += position.z;
  103. txt += ")";
  104. if (target)
  105. {
  106. txt += "\nTarget: (";
  107. txt += targetPos.x;
  108. txt += ", ";
  109. txt += targetPos.y;
  110. txt += ", ";
  111. txt += targetPos.z;
  112. txt += ")\n";
  113. Block* b = World::INSTANCE->zBlockAt(targetPos);
  114. if (b)
  115. {
  116. txt += "TargetLight: \n";
  117. txt += b->printLightInfo();
  118. }
  119. }
  120. debug->setText(txt);
  121. }
  122. void Game::api(char* data)
  123. {
  124. switch (data[0])
  125. {
  126. case 0: // open dialog
  127. {
  128. bool exists = 0;
  129. short len = *(short*)(data + 1);
  130. char* dialogName = new char[len + 1];
  131. memcpy(dialogName, data + 3, len);
  132. dialogName[len] = 0;
  133. for (UIMLDialog* dialog : dialogs)
  134. {
  135. if (dialog->getName().istGleich(dialogName))
  136. {
  137. exists = 1;
  138. break;
  139. }
  140. }
  141. delete[] dialogName;
  142. if (!exists)
  143. {
  144. int uimlLen = *(int*)(data + 3 + len);
  145. char* uiml = new char[uimlLen + 1];
  146. memcpy(uiml, data + 7 + len, uimlLen);
  147. uiml[uimlLen] = 0;
  148. UIMLDialog* dialog
  149. = new UIMLDialog(uiml, [this](UIMLDialog* dialog) {
  150. window->zBildschirm()->postAction([this, dialog]() {
  151. int index = 0;
  152. for (UIMLDialog* d : dialogs)
  153. {
  154. if (d == dialog)
  155. {
  156. window->zBildschirm()->removeMember(d);
  157. dialogs.remove(index);
  158. World::INSTANCE->zKamera()
  159. ->setControlEnabled(
  160. dialogs.getEintragAnzahl() == 0);
  161. updateRecipieVisibility();
  162. break;
  163. }
  164. index++;
  165. }
  166. });
  167. });
  168. dialogs.add(dialog);
  169. updateRecipieVisibility();
  170. World::INSTANCE->zKamera()->setControlEnabled(0);
  171. window->zBildschirm()->addMember(dialog);
  172. delete[] uiml;
  173. }
  174. break;
  175. }
  176. case 1:
  177. { // element message
  178. for (UIMLDialog* dialog : dialogs)
  179. {
  180. dialog->api(data + 1);
  181. }
  182. short idLen = *(short*)(data + 1);
  183. char* id = new char[idLen + 1];
  184. memcpy(id, data + 3, idLen);
  185. id[idLen] = 0;
  186. NetworkAPIProcessor* processor = dynamic_cast<NetworkAPIProcessor*>(
  187. guiView->zZeichnungById(id));
  188. if (processor) processor->api(data + 3 + idLen);
  189. delete[] id;
  190. break;
  191. }
  192. case 2:
  193. { // set gui
  194. int uimlLen = *(int*)(data + 1);
  195. char* uiml = new char[uimlLen + 1];
  196. memcpy(uiml, data + 5, uimlLen);
  197. uiml[uimlLen] = 0;
  198. guiView->setUIML(uiml);
  199. guiView->layout();
  200. delete[] uiml;
  201. }
  202. }
  203. }
  204. void Game::closeCurrentDialog()
  205. {
  206. if (dialogs.getEintragAnzahl() > 0)
  207. {
  208. UIMLDialog* d = dialogs.get(dialogs.getEintragAnzahl() - 1);
  209. window->zBildschirm()->removeMember(d);
  210. dialogs.remove(dialogs.getEintragAnzahl() - 1);
  211. World::INSTANCE->zKamera()->setControlEnabled(
  212. dialogs.getEintragAnzahl() == 0);
  213. updateRecipieVisibility();
  214. }
  215. }
  216. DragController<InventoryDragSource, int>* Game::zInventoryDragController()
  217. {
  218. return inventoryDragController;
  219. }
  220. void Game::setTargetUIML(Framework::Text uiml)
  221. {
  222. if (uiml.getLength())
  223. {
  224. window->zBildschirm()->lock();
  225. targetUIMLView->setUIML(uiml);
  226. targetUIMLView->layout();
  227. window->zBildschirm()->unlock();
  228. targetUIMLView->setSize(targetUIMLView->calculateContentSize());
  229. targetUIMLView->setPosition(
  230. window->zBildschirm()->zGraphicsApi()->getBackBufferSize()
  231. - targetUIMLView->getSize());
  232. targetUIMLView->addStyle(UIMLView::Style::Sichtbar);
  233. }
  234. else
  235. {
  236. targetUIMLView->removeStyle(UIMLView::Style::Sichtbar);
  237. }
  238. }
  239. void Game::updateRecipieVisibility()
  240. {
  241. if (!recipieVisible)
  242. {
  243. if (dialogs.getEintragAnzahl() > 0)
  244. {
  245. recipieVisible = 1;
  246. window->zBildschirm()->addMember(
  247. dynamic_cast<Zeichnung*>(filter->getThis()));
  248. }
  249. }
  250. else
  251. {
  252. if (dialogs.getEintragAnzahl() == 0)
  253. {
  254. recipieVisible = 0;
  255. window->zBildschirm()->removeMember(filter);
  256. itemListContainer->removeStyle(Fenster::Style::Sichtbar);
  257. }
  258. }
  259. }
  260. void Game::showItemList()
  261. {
  262. itemListContainer->addStyle(Fenster::Style::Sichtbar);
  263. }
  264. bool Game::isItemListVisible()
  265. {
  266. return itemListContainer->hatStyle(Fenster::Style::Sichtbar);
  267. }
  268. const Text* Game::zFilterText()
  269. {
  270. return filter->zText();
  271. }
  272. void Game::makeChatButtonVisible()
  273. {
  274. chatButton->addStyle(Knopf::Style::Sichtbar);
  275. }
  276. Chat* Game::zChat() const
  277. {
  278. return chat;
  279. }