Editor.cpp 10 KB

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