Editor.cpp 6.9 KB

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