#include "Editor.h"
#include <Rahmen.h>
#include "../../Global/Variablen.h"

// Inhalt der Editor Klasse aus Editor.h
// Konstruktor
Editor::Editor( Schrift *zSchrift, Fenster *zNachLoginFenster, int x )
	: Zeichnung()
{
	schrift = zSchrift->getThis();
	bildschirmGr��e = BildschirmGr��e();
	pos = Punkt( x, 35 );
	gr = Punkt( 102, 32 );
	rahmen = new LRahmen();
	rahmen->setFarbe( 0xFFFFFFFF );
	rahmen->setSize( 102, 32 );
	alpha = 0;
	animation = 0;
	sichtbar = 0;
	tickVal = 0;
	jetzt = 0;
	prozent1 = 0;
	prozent2 = 0;
	begPos = Punkt( 0, 0 );
	begGr��e = Punkt( 0, 0 );
	gr��e1 = Punkt( 102, 32 );
	pos1 = Punkt( x, 35 );
	gr��e2 = Punkt( 900, 600 );
	pos2 = bildschirmGr��e / 2 - gr��e2 / 2;
	laden = (Animation2D*)ladeAnimation->dublizieren();
	laden->setSichtbar( 0 );
	laden->setPosition( 425, 275 );
	zNachLoginFenster->addMember( this );
	kEditor = new KartenEditor( schrift->getThis() );
	karteAuswahl = new Auswahl( zSchrift, kEditor->getThis() );
	ref = 1;
}

// Destruktor
Editor::~Editor()
{
	rahmen->release();
	laden->release();
	karteAuswahl->release();
	kEditor->release();
	schrift->release();
}

// nicht constant
void Editor::setSichtbar( bool sicht )
{
	begPos = pos;
	begGr��e = gr;
	animation |= ( sicht ? 0x1 : 0x2 );
	rend = 1;
}

void Editor::doMausEreignis( MausEreignis &me )
{
	if( animation || !sichtbar )
		return;
	me.mx -= pos.x;
	me.my -= pos.y;
	switch( jetzt )
	{
	case 1: // Karten Auswahl
		karteAuswahl->doMausEreignis( me );
		break;
	case 2: // Karten Editor
		kEditor->doMausEreignis( me );
		break;
	}
	me.mx += pos.x;
	me.my += pos.y;
}

void Editor::doTastaturEreignis( TastaturEreignis &te )
{
	if( animation || !sichtbar )
		return;
	switch( jetzt )
	{
	case 1: // Karten Auswahl
		karteAuswahl->doTastaturEreignis( te );
		break;
	case 2: // Karten Editor
		kEditor->doTastaturEreignis( te );
		break;
	}
}

bool Editor::tick( double z )
{
	rend |= laden->tick( z );
	rend |= karteAuswahl->tick( z );
	if( jetzt == 1 && !karteAuswahl->istSichtbar() )
	{
		kEditor->setSichtbar( 1 );
		jetzt = 2;
	}
	if( jetzt == 2 && !kEditor->istSichtbar() )
	{
		karteAuswahl->setSichtbar( 1 );
		jetzt = 1;
	}
	rend |= kEditor->tick( z );
	tickVal += z * 150;
	int val = (int)tickVal;
	if( val < 1 )
	{
		bool ret = rend;
		rend = 0;
		return ret;
	}
	tickVal -= val;
	if( ( animation | 0x1 ) == animation ) // Einblenden
	{
		if( prozent1 != 100 )
		{
			prozent1 += val;
			if( prozent1 >= 100 )
			{
				if( !jetzt )
				{
					karteAuswahl->setSichtbar( 1 );
					jetzt = 1;
				}
				prozent1 = 100;
			}
			pos = begPos + (Punkt)( ( ( Vec2< double > )( pos2 - begPos ) / 100.0 ) * prozent1 );
			gr = begGr��e + (Punkt)( ( ( Vec2< double > )( gr��e2 - begGr��e ) / 100.0 ) * prozent1 );
		}
		else if( alpha != 255 )
		{
			alpha += val * 2;
			if( alpha >= 255 || ( animation | 0x2 ) == animation )
			{
				alpha = 255;
				animation &= ~0x1;
				sichtbar = 1;
				prozent1 = 0;
			}
		}
		rend = 1;
	}
	if( ( animation | 0x2 ) == animation ) // ausblenden
	{
		if( alpha != 0 )
		{
			alpha -= val * 2;
			if( alpha < 0 )
				alpha = 0;
		}
		else
		{
			prozent2 += val;
			if( prozent2 > 100 )
				prozent2 = 100;
			pos = begPos + (Punkt)( ( ( Vec2< double > )( pos1 - begPos ) / 100.0 ) * prozent2 );
			gr = begGr��e + (Punkt)( ( ( Vec2< double > )( gr��e1 - begGr��e ) / 100.0 ) * prozent2 );
			if( prozent2 == 100 )
			{
				prozent2 = 0;
				animation &= ~0x2;
				sichtbar = 0;
			}
		}
		rend = 1;
	}
	bool ret = rend;
	rend = 0;
	return ret;
}

void Editor::render( Bild &zRObj )
{
	if( pos == pos1 )
		return;
	if( !zRObj.setDrawOptions( pos.x, pos.y, gr.x, gr.y ) )
		return;
	rahmen->setSize( gr );
	rahmen->render( zRObj );
	int rbr = rahmen->getRBreite();
	zRObj.setAlpha( (unsigned char)alpha );
	karteAuswahl->render( zRObj );
	kEditor->render( zRObj );
	laden->render( zRObj );
	zRObj.releaseAlpha();
	zRObj.releaseDrawOptions();
}

// constant
bool Editor::istAnimiert() const
{
	return animation != 0;
}

bool Editor::istSichtbar() const
{
	return sichtbar || prozent1 != 0;
}

// Reference Counting
Editor *Editor::getThis()
{
	ref++;
	return this;
}

Editor *Editor::release()
{
	ref--;
	if( !ref )
		delete this;
	return 0;
}