Editor.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. #include "Editor.h"
  2. #include <AsynchronCall.h>
  3. #include "Interface\Dialogs\Frage.h"
  4. #include "Interface\Dialogs\Nachricht.h"
  5. #include <iostream>
  6. #include <Bild.h>
  7. #include <Globals.h>
  8. using namespace Editor;
  9. // Inhalt der Editor Klasse aus Editor.h
  10. // Konstruktor
  11. AsteroidsEditor::AsteroidsEditor()
  12. : ReferenceCounter()
  13. {
  14. uiFactory.initParam.schrift = 0;
  15. kamera = new Kamera2D();
  16. minimap = new Kamera2D();
  17. minimap->setSize( 200, 200 );
  18. minimap->setStyle( ZeichnungHintergrund::Style::Sichtbar | ZeichnungHintergrund::Style::Rahmen );
  19. minimap->setRahmenFarbe( 0xFFFFFFFF );
  20. minimap->setName( "minimap" );
  21. klient = 0;
  22. laden = 0;
  23. i = 0;
  24. alpha = 0;
  25. status = START;
  26. dialogs = new Array< Dialog* >();
  27. sts = 0;
  28. rend = 1;
  29. daten = 0;
  30. moveObj = 0;
  31. rotObj = 0;
  32. }
  33. // Destruktor
  34. AsteroidsEditor::~AsteroidsEditor()
  35. {
  36. if( sts )
  37. sts->release();
  38. if( klient )
  39. klient->release();
  40. if( laden )
  41. laden->release();
  42. if( i )
  43. i->release();
  44. if( daten )
  45. {
  46. daten->stopWork();
  47. daten->release();
  48. }
  49. dialogs->release();
  50. kamera->release();
  51. minimap->release();
  52. }
  53. void AsteroidsEditor::ladeKarte()
  54. {
  55. if( sts )
  56. sts = (SpielerTeamStruktur*)sts->release();
  57. sts = new SpielerTeamStruktur();
  58. klient->getSpielerTeamStruktur( sts );
  59. if( daten )
  60. {
  61. daten->stopWork();
  62. daten->release();
  63. }
  64. daten = new KarteDaten( dynamic_cast<EditorKlient*>(klient->getThis()), sts );
  65. i->setDaten( dynamic_cast<KarteDaten*>(daten->getThis()) );
  66. minimap->setWelt( daten->getWelt(), 0 );
  67. minimap->lookAtWorldPos( daten->zWelt()->getWorldInfo().size / 2 );
  68. minimap->lookAtWorldArea( (float)daten->zWelt()->getWorldInfo().size.x, (float)daten->zWelt()->getWorldInfo().size.y );
  69. kamera->setWelt( daten->getWelt(), 1 );
  70. }
  71. // nicht constant
  72. void AsteroidsEditor::addDialog( Dialog* d )
  73. {
  74. c.lock();
  75. dialogs->add( d );
  76. c.unlock();
  77. }
  78. // nicht constant
  79. void AsteroidsEditor::setUIFactory( UIInit& uiFactory )
  80. {
  81. this->uiFactory = uiFactory;
  82. if( !i && windowSize != Punkt() )
  83. i = new Interface( uiFactory, windowSize );
  84. }
  85. void AsteroidsEditor::setKlient( KSGClient::EditorServerClient* ekv )
  86. {
  87. if( klient )
  88. klient->release();
  89. klient = new EditorKlient( ekv );
  90. }
  91. void AsteroidsEditor::setLadeAnimation( Animation2D* la )
  92. {
  93. if( laden )
  94. laden->release();
  95. laden = la;
  96. }
  97. void AsteroidsEditor::setSichtbar()
  98. {
  99. status = START;
  100. Punkt mS = windowSize;
  101. if( windowSize != Punkt() )
  102. {
  103. EditorKlient* k = dynamic_cast<EditorKlient*>(klient->getThis());
  104. TextRenderer* zS = new TextRenderer( dynamic_cast<Schrift*>(uiFactory.initParam.schrift->getThis()) );
  105. new AsynchronCall( [this, k, zS, mS]( void ) -> void {
  106. int ret = k->init();
  107. if( ret == 2 )
  108. {
  109. std::function< void() > wiederherstellen = [this, k] {
  110. status = START;
  111. ladeKarte();
  112. this->status = INITIALIZED;
  113. };
  114. std::function< void() > verwerfen = [this, k, zS, mS] {
  115. status = START;
  116. if( !k->sitzungVerwerfen() )
  117. {
  118. Text t = "Fehler beim verwerfen der Sitzung: ";
  119. t += k->getLastError();
  120. this->addDialog( new Nachricht( dynamic_cast<TextRenderer*>(zS->getThis()), uiFactory, t, mS, 0 ) );
  121. }
  122. ladeKarte();
  123. this->status = INITIALIZED;
  124. };
  125. this->addDialog( new Frage( dynamic_cast<TextRenderer*>(zS->getThis()), uiFactory, "Es wurde eine alte ungespeicherte Sitzung gefunden. möchtest du sie Wiederherstellen?", "Ja", "Nein", wiederherstellen, verwerfen, verwerfen, mS ) );
  126. this->status = INITIALIZED;
  127. }
  128. else if( ret == 1 )
  129. {
  130. ladeKarte();
  131. this->status = INITIALIZED;
  132. }
  133. else
  134. {
  135. Status* st = &status;
  136. this->status = INITIALIZED;
  137. this->addDialog( new Nachricht( dynamic_cast<TextRenderer*>(zS->getThis()), uiFactory, Text( "Fehler beim Initialisieren: " ) += k->getLastError(), mS, [st]() { *st = EXIT; } ) );
  138. }
  139. zS->release();
  140. k->release();
  141. } );
  142. }
  143. rend = 1;
  144. }
  145. void AsteroidsEditor::doPublicMausEreignis( MausEreignis& me )
  146. {
  147. c.lock();
  148. for( auto i : *dialogs )
  149. i->doPublicMausEreignis( me );
  150. bool dialogActive = dialogs->hat( 0 );
  151. int anz = dialogs->getEintragAnzahl();
  152. c.unlock();
  153. if( anz == 0 )
  154. {
  155. i->doPublicMausEreignis( me );
  156. if( i->hatVerlassen() && status == INITIALIZED && !dialogActive && me.id == ME_RLinks )
  157. {
  158. status = WARTEND;
  159. EditorKlient* k = dynamic_cast<EditorKlient*>(klient->getThis());
  160. TextRenderer* zS = new TextRenderer( dynamic_cast<Schrift*>(uiFactory.initParam.schrift->getThis()) );
  161. Punkt mS = windowSize;
  162. new AsynchronCall( [this, k, zS, mS]( void ) -> void {
  163. if( !k->sitzungBeenden() )
  164. {
  165. this->addDialog( new Nachricht( dynamic_cast<TextRenderer*>(zS->getThis()), uiFactory, Text( "Fehler beim Speichern: " ) += k->getLastError(), mS, 0 ) );
  166. status = INITIALIZED;
  167. }
  168. else
  169. status = EXIT;
  170. zS->release();
  171. k->release();
  172. } );
  173. }
  174. }
  175. if( !me.verarbeitet )
  176. {
  177. if( me.id == ME_PLinks )
  178. {
  179. Vertex pos = kamera->getWorldCoordinates( Punkt( me.mx, me.my ) );
  180. moveObj = (EditorObject*)daten->zWelt()->zObjectAt( (int)pos.x, (int)pos.y, 1 );
  181. if( moveObj )
  182. me.verarbeitet = 1;
  183. }
  184. if( me.id == ME_PMitte )
  185. {
  186. Vertex pos = kamera->getWorldCoordinates( Punkt( me.mx, me.my ) );
  187. rotObj = (EditorObject*)daten->zWelt()->zObjectAt( (int)pos.x, (int)pos.y, 1 );
  188. if( rotObj )
  189. me.verarbeitet = 1;
  190. }
  191. if( me.id == ME_Bewegung )
  192. {
  193. if( moveObj )
  194. {
  195. moveObj->setPosition( moveObj->getPosition() + Punkt( me.mx, me.my ) - maus );
  196. rend = 1;
  197. me.verarbeitet = 1;
  198. }
  199. if( rotObj )
  200. {
  201. rotObj->addDrehung( (me.mx - maus.x) / 100.f );
  202. rend = 1;
  203. me.verarbeitet = 1;
  204. }
  205. }
  206. if( me.id == ME_RLinks )
  207. {
  208. if( moveObj )
  209. {
  210. moveObj->update();
  211. me.verarbeitet = 1;
  212. moveObj = 0;
  213. }
  214. }
  215. if( me.id == ME_RMitte )
  216. {
  217. if( rotObj )
  218. {
  219. rotObj->update();
  220. me.verarbeitet = 1;
  221. rotObj = 0;
  222. }
  223. }
  224. }
  225. if( !me.verarbeitet )
  226. {
  227. if( getMausStand( M_Links ) )
  228. {
  229. if( me.mx >= minimap->getX() && me.mx < minimap->getX() + minimap->getBreite() &&
  230. me.my >= minimap->getY() && me.my <= minimap->getY() + minimap->getHeight() )
  231. {
  232. kamera->lookAtWorldPos( minimap->getWorldCoordinates( Punkt( me.mx - minimap->getX(), me.my - minimap->getY() ) ) );
  233. }
  234. else
  235. {
  236. if( me.id == ME_Bewegung )
  237. kamera->lookAtWorldPos( kamera->getWorldPosition() + maus - Punkt( me.mx, me.my ) );
  238. }
  239. }
  240. if( me.id == ME_UScroll )
  241. kamera->setZoom( kamera->getZoom() / 0.9f );
  242. if( me.id == ME_DScroll )
  243. kamera->setZoom( kamera->getZoom() / 1.1f );
  244. }
  245. maus.x = me.mx;
  246. maus.y = me.my;
  247. }
  248. void AsteroidsEditor::doTastaturEreignis( TastaturEreignis& te )
  249. {
  250. c.lock();
  251. for( auto i : *dialogs )
  252. i->doTastaturEreignis( te );
  253. int anz = dialogs->getEintragAnzahl();
  254. c.unlock();
  255. if( anz == 0 )
  256. i->doTastaturEreignis( te );
  257. }
  258. bool AsteroidsEditor::tick( double z )
  259. {
  260. if( (status == WARTEND || status == START || (daten && daten->hasAktions())) )
  261. {
  262. if( alpha != 100 )
  263. {
  264. if( laden && !laden->istSichtbar() )
  265. laden->setSichtbar( 1 );
  266. if( alpha < 100 )
  267. {
  268. alpha += (unsigned char)(100 * z);
  269. if( alpha > 100 )
  270. alpha = 100;
  271. }
  272. else
  273. {
  274. alpha -= (unsigned char)(100 * z);
  275. if( alpha < 100 )
  276. alpha = 100;
  277. }
  278. rend = 1;
  279. }
  280. }
  281. else if( alpha != 255 )
  282. {
  283. if( laden && laden->istSichtbar() )
  284. laden->setSichtbar( 0 );
  285. if( alpha + 100 * z > 255 )
  286. alpha = 255;
  287. else
  288. alpha += (unsigned char)(100 * z);
  289. rend = 1;
  290. }
  291. if( laden )
  292. rend |= laden->tick( z );
  293. rend |= i->tick( z );
  294. rend |= minimap->tick( z );
  295. rend |= kamera->tick( z );
  296. c.lock();
  297. for( auto i : *dialogs )
  298. rend |= i->tick( z );
  299. c.unlock();
  300. bool tmp = rend;
  301. rend = 0;
  302. return tmp;
  303. }
  304. void AsteroidsEditor::render( Bild& zRObj )
  305. {
  306. if( windowSize == Punkt() )
  307. {
  308. if( status != EXIT )
  309. windowSize = zRObj.getSize();
  310. setSichtbar();
  311. if( !i && uiFactory.initParam.schrift )
  312. i = new Interface( uiFactory, windowSize );
  313. }
  314. zRObj.setAlpha( alpha );
  315. kamera->setSize( zRObj.getSize() );
  316. kamera->render( zRObj );
  317. minimap->setPosition( 10, zRObj.getHeight() - 210 );
  318. minimap->render( zRObj );
  319. i->render( zRObj );
  320. c.lock();
  321. for( int i = dialogs->getEintragAnzahl() - 1; i >= 0; i-- )
  322. {
  323. dialogs->get( i )->render( zRObj );
  324. if( dialogs->get( i )->hatVerlassen() )
  325. {
  326. delete dialogs->get( i );
  327. dialogs->remove( i );
  328. }
  329. }
  330. c.unlock();
  331. zRObj.releaseAlpha();
  332. if( laden )
  333. laden->render( zRObj );
  334. }
  335. // constant
  336. bool AsteroidsEditor::hatVerlassen( bool jetzt ) const
  337. {
  338. return status == EXIT;
  339. }