Editor.cpp 7.5 KB

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