Editor.cpp 8.6 KB

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