//****************************************************************************
//Copyright (C) 2005-2006 Beijing BlueDJ Technology Co.,Ltd. All rights reserved.

//This program is free software; you can redistribute it and/or
//modify it under the terms of the GNU General Public License
//as published by the Free Software Foundation; either version 2
//of the License, or (at your option) any later version.

//This program is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//GNU General Public License for more details.

//You should have received a copy of the GNU General Public License
//along with this program (in the file LICENSE.GPL); if not, write to the Free Software
//Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

//Please visit http://www.bluedj.com for more infomation about us.
//Contact us at ggwizard@gmail.com or darkdong@gmail.com.
//****************************************************************************/

#include "DJMatrixCanvasSprite.h"

DJMatrixCanvasSprite::DJMatrixCanvasSprite( Q3Canvas *canvas, const QString& filePath )
	:m_canvas(canvas),m_filePath(filePath)
{
	djDebug() << "DJMatrixCanvasSprite constructor";
	
	m_x	= 0;
	m_y	= 0;
	m_alignment	= 0;
	m_pixmaps 	= 0;
    m_sprite	= 0;
    
    initWithFile( filePath );
}
DJMatrixCanvasSprite::~DJMatrixCanvasSprite()
{
	djDebug() << "DJMatrixCanvasSprite destructor";
	//Note : delete sprite first
	delete m_sprite;
	m_sprite	= 0;
	delete m_pixmaps;
	m_pixmaps	= 0;
}
void DJMatrixCanvasSprite::setAlignment( Qt::Alignment align )
{
	if ( m_alignment != align ) {
		m_alignment	= align;
		//adjust the current position
		matrimove( m_x, m_y );
	}
}
void DJMatrixCanvasSprite::setInnerMatrix( const QMatrix& matrix )
{
	m_innerMatrix	= matrix;
	//initWithImageReader();
	initWithFile( m_filePath );
}
void DJMatrixCanvasSprite::setExternalMatrix( const QMatrix& matrix )
{
	m_externalMatrix	= matrix;
	//initWithImageReader();
	initWithFile( m_filePath );
}
int DJMatrixCanvasSprite::width() const
{
	if ( !m_sprite )
		return 0;
	return m_sprite->width();
}
int DJMatrixCanvasSprite::height() const
{
	if ( !m_sprite )
		return 0;
	return m_sprite->height();
}
int DJMatrixCanvasSprite::realWidth() const
{
	if ( !m_sprite )
		return 0;
		/*
	qreal scale	= m_innerMatrix.m11() * m_externalMatrix.m11();
	if( 0 == scale )
		scale	= 1;
	return m_sprite->width()/scale;
	*/
	qreal scale	= m_innerMatrix.m11();
	if( 0 == scale )
		scale	= 1;
	return m_sprite->width()/scale;
}
int DJMatrixCanvasSprite::realHeight() const
{
	if ( !m_sprite )
		return 0;
	/*
	qreal scale	= m_innerMatrix.m22() * m_externalMatrix.m22();
	if( 0 == scale )
		scale	= 1;
	return static_cast<int>(m_sprite->height()/scale);
	*/
	qreal scale	= m_innerMatrix.m22();
	if( 0 == scale )
		scale	= 1;
	return static_cast<int>(m_sprite->height()/scale);
}

double DJMatrixCanvasSprite::x() const
{
	if ( !m_sprite )
		return 0;
	else
		return m_sprite->x();
}
double DJMatrixCanvasSprite::y() const
{
	if ( !m_sprite )
		return 0;
	else
		return m_sprite->y();
}
void DJMatrixCanvasSprite::matrimove( double x, double y )
{
	//save the original coordinate
	m_x	= x;
	m_y	= y;
	
	//transformed coordinate
	double tx	= x;
	double ty	= y;
	
	if ( !m_externalMatrix.isIdentity() ) {
		m_externalMatrix.map( x, y, &tx, &ty );
	}
		
	move( tx, ty );
}
void DJMatrixCanvasSprite::move( double x, double y )
{
	if ( !m_sprite )
		return;
	if( m_alignment & Qt::AlignRight )
		x -= m_sprite->width();
	else if( m_alignment & Qt::AlignHCenter )
		x -= m_sprite->width() >> 1;
	if( m_alignment & Qt::AlignBottom )
		y -= m_sprite->height();
	else if( m_alignment & Qt::AlignVCenter )
		y -= m_sprite->height() >> 1;
	m_sprite->move( x, y );
}
void DJMatrixCanvasSprite::setZ( double z )
{
	if ( !m_sprite )
		return;
	m_sprite->setZ( z );
}
void DJMatrixCanvasSprite::show()
{
	if ( !m_sprite )
		return;
	m_sprite->show();
}
void DJMatrixCanvasSprite::hide()
{
	if ( !m_sprite )
		return;
	m_sprite->hide();
}
void DJMatrixCanvasSprite::initWithFile( const QString& filePath )
{
	if ( !QFile::exists( filePath ) ) {
		return;
	}
		
	QMatrix	matrix	= m_innerMatrix * m_externalMatrix;
	
	Q3CanvasPixmapArray	*pixmapArray 	= new Q3CanvasPixmapArray;
	int i	= 0;
	QImageReader	imageReader( filePath );
	QImage	image	= imageReader.read();
	if ( !matrix.isIdentity() )
		image	= image.transformed ( matrix, Qt::SmoothTransformation );
    while( !image.isNull() ) {
    	Q3CanvasPixmap	*pix	= new Q3CanvasPixmap( image );
    	pixmapArray->setImage( i++, pix );
    	imageReader.jumpToNextImage();
    	image	= imageReader.read();
    	if ( !matrix.isIdentity() )
			image	= image.transformed ( matrix, Qt::SmoothTransformation );
    }
    
    if ( m_sprite ) {
    	m_sprite->setSequence( pixmapArray );
    }else {
    	m_sprite	=  new Q3CanvasSprite( pixmapArray, m_canvas );
    	m_sprite->setFrameAnimation();
    }
    
    delete m_pixmaps;
    m_pixmaps	= pixmapArray;
    
    //adjust the current position
	matrimove( m_x, m_y );
}
