Editor.cpp 9.6 KB

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