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