//****************************************************************************
//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<QtGui>
#include <Qt3Support>

#include "protocol.h"
#include "DJGamePanel.h"
#include "DJGameUser.h"
#include "DJGameRoom.h"
#include "DJGameImage.h"
#include "Utility.h"

DJGameImageItem::DJGameImageItem( QImage img, Q3Canvas *canvas,QMatrix &m,unsigned short tid,unsigned short sid,unsigned short type,bool bMovable)
    : Q3CanvasRectangle( canvas ),image(img)
{
	c_x = 0;
	c_y = 0;
	setSize( image.width(), image.height() );
    c_tid = tid;
	c_sid = sid;
	c_type = type;
	c_vAlignment = Qt::AlignTop;
	c_hAlignment = Qt::AlignLeft;
	c_matrix = m;
	c_staticMatrix.setMatrix(1.0,0,0,1.0,0,0);
	setMovable(bMovable);

    c_bMark = false;
    m_pen.setColor( Qt::green );
    m_pen.setWidth( 2 );
}
void DJGameImageItem::changeImage( QImage &img)
{
	image = img;
	setMatrix(c_matrix);
}
QPixmap DJGameImageItem::getPixmap() const
{
	return pixmap;
}
void DJGameImageItem::setMatrix(QMatrix &m)
{
	c_matrix.setMatrix(m.m11()*c_staticMatrix.m11(),
						m.m12(),m.m21(),m.m22()*c_staticMatrix.m22(),
						m.dx(),m.dy());
	QImage img = image.smoothScale(static_cast<int>(image.width()*m.m11()),static_cast<int>(image.height()*m.m22()));
	setSize( img.width(), img.height() );

	pixmap	= QPixmap::fromImage(img,Qt::OrderedAlphaDither);
	
	move(c_x,c_y);
}

void DJGameImageItem::setStaticMatrix(QMatrix &m)
{
	c_staticMatrix = m;
}

double DJGameImageItem::x()
{
	return c_x;
}

double DJGameImageItem::y()
{
	return c_y;
}
int DJGameImageItem::width()
{
	return image.width();
}

int DJGameImageItem::height()
{
	return image.height();
}

void DJGameImageItem::setHAlignment(quint16 align)
{
	c_hAlignment = align;
}

void DJGameImageItem::setVAlignment(quint16 align)
{
	c_vAlignment = align;
}

void DJGameImageItem::move(double x, double y)
{
	double tx,ty;
	c_matrix.map(x,y,&tx,&ty);
	if(c_vAlignment == Qt::AlignBottom)
	{
		ty -= (Q3CanvasRectangle::height());
	}else if(c_vAlignment == Qt::AlignVCenter)
		ty -= (Q3CanvasRectangle::height()>>1);
	
	if(c_hAlignment == Qt::AlignRight)
		tx -= (Q3CanvasRectangle::width());
	else if(c_hAlignment == Qt::AlignHCenter)
		tx -= (Q3CanvasRectangle::width()>>1);
	
	Q3CanvasRectangle::move(tx,ty);
	c_x = x;
	c_y = y;
}

void DJGameImageItem::drawShape( QPainter &p )
{
    p.drawPixmap( int(Q3CanvasRectangle::x()), int(Q3CanvasRectangle::y()), pixmap);
	
	if ( c_bMark  ) {
		p.setPen( m_pen );
		p.drawRect( rect() );
	}
}
bool DJGameImageItem::hit( const QPoint &p ) const
{
    int ix = p.x()-int(Q3CanvasRectangle::x());
    int iy = p.y()-int(Q3CanvasRectangle::y());
    if ( !image.valid( ix , iy ) )
		return FALSE;
    QRgb pixel = image.pixel( ix, iy );
    return qAlpha( pixel ) != 0;
}

void DJGameImageItem::setMark( bool flag )
{
    c_bMark = flag;
}
bool DJGameImageItem::isMarked()
{
	return c_bMark;
}
QPen DJGameImageItem::pen() const
{
	return m_pen;
}
void DJGameImageItem::setPen( const QPen& pen )
{
	m_pen	= pen;
}
bool DJGameImageItem::movable()
{
	return c_bMovable;
}
void DJGameImageItem::setMovable(bool b)
{
	c_bMovable = b;
}

unsigned short DJGameImageItem::type()
{
	return c_type;
}
unsigned short DJGameImageItem::tid()
{
	return c_tid;
}
unsigned short DJGameImageItem::sid()
{
	return c_sid;
}

//////////////////////////////////////////////////////////////////////////////////////
DJGameLineItem::DJGameLineItem(Q3Canvas * canvas,QMatrix &m,unsigned short tid,unsigned short sid,unsigned short type,bool bMovable):Q3CanvasLine(canvas)
{
	c_x = 0;
	c_y = 0;
	c_matrix = m;
	setMovable(bMovable);
	c_tid = tid;
	c_sid = sid;
	c_type = type;
}
DJGameLineItem::~DJGameLineItem()
{
}

bool DJGameLineItem::movable()
{
	return c_bMovable;
}

void DJGameLineItem::setMovable(bool b)
{
	c_bMovable = b;
}

unsigned short DJGameLineItem::type()
{
	return c_type;
}
unsigned short DJGameLineItem::tid()
{
	return c_tid;
}
unsigned short DJGameLineItem::sid()
{
	return c_sid;
}
void DJGameLineItem::setPoints( int xa, int ya, int xb, int yb )
{
	c_xa = xa;
	c_xb = xb;
	c_ya = ya;
	c_yb = yb;
	
	double tx,ty;
	c_matrix.map(xa,ya,&tx,&ty);
	xa = (int)tx;
	ya = (int)ty;
	c_matrix.map(xb,yb,&tx,&ty);
	xb = (int)tx;
	yb = (int)ty;
	
	Q3CanvasLine::setPoints(xa,ya,xb,yb);
}

void DJGameLineItem::setMatrix(QMatrix & m)
{
	c_matrix = m;
	setPoints(c_xa,c_ya,c_xb,c_yb);
}
//////////////////////////////////////////////////////////////////////////////////////
DJGameTextItem::DJGameTextItem(const QString &t,Q3Canvas * canvas,QMatrix &m,unsigned short tid,unsigned short sid,unsigned short type,bool bMovable):Q3CanvasText(t,canvas)
{
	c_matrix = m;
	setMovable(bMovable);
	c_tid = tid;
	c_sid = sid;
	c_type = type;
	c_vAlignment = Qt::AlignTop;
	c_hAlignment = Qt::AlignLeft;
	c_staticMatrix.setMatrix(1.0,0,0,1.0,0,0);
	QFont c_font = Q3CanvasText::font();
	setText(t);
}
void DJGameTextItem::setFont( const QFont & f )
{
	c_font = f;
	QFont f1 = f;
	int fs = static_cast<int>(f1.pointSize()*c_matrix.m22());
	if(fs <4)
		fs = 4;
	f1.setPointSize(fs);
	Q3CanvasText::setFont(f1);
}

QFont DJGameTextItem::font()
{
	return c_font;
}

void DJGameTextItem::setMatrix(QMatrix &m)
{
	c_matrix = m * c_staticMatrix;
	QFont f1 = c_font;
	setFont(f1);
	move(c_x,c_y);
}

void DJGameTextItem::setStaticMatrix(QMatrix &m)
{
	c_staticMatrix = m;
}
double DJGameTextItem::x()
{
	return c_x;
}

double DJGameTextItem::y()
{
	return c_y;
}
void DJGameTextItem::setHAlignment(quint16 align)
{
	c_hAlignment = align;
}

void DJGameTextItem::setVAlignment(quint16 align)
{
	c_vAlignment = align;
}

void DJGameTextItem::move(double x, double y)
{
	double tx,ty;
	c_matrix.map(x,y,&tx,&ty);
	
	QRect rt = Q3CanvasText::boundingRect();
	if(c_vAlignment == Qt::AlignBottom)
	{
		ty -= rt.height();
	}else if(c_vAlignment == Qt::AlignVCenter)
		ty -= (rt.height()>>1);
	
	if(c_hAlignment == Qt::AlignRight)
		tx -= rt.width();
	else if(c_hAlignment == Qt::AlignHCenter)
		tx -= (rt.width()>>1);
	
	Q3CanvasText::move(tx,ty);
	
	c_x = x;
	c_y = y;
}

DJGameTextItem::~DJGameTextItem()
{
}

bool DJGameTextItem::movable()
{
	return c_bMovable;
}

void DJGameTextItem::setMovable(bool b)
{
	c_bMovable = b;
}

unsigned short DJGameTextItem::type()
{
	return c_type;
}
unsigned short DJGameTextItem::tid()
{
	return c_tid;
}
unsigned short DJGameTextItem::sid()
{
	return c_sid;
}

//////////////////////////////////////////////////////////////////////////////

DJGameDesktopSpriteItem::DJGameDesktopSpriteItem( QMatrix &m,Q3CanvasPixmapArray * a, Q3Canvas * cv ) : 
			Q3CanvasSprite(a,cv)
{
	c_x = 0;
	c_y = 0;
	c_staticMatrix.setMatrix(1.0,0,0,1.0,0,0);
	c_matrix = m;
	c_vAlignment = Qt::AlignTop;
	c_hAlignment = Qt::AlignLeft;
}

void DJGameDesktopSpriteItem::setMatrix(QMatrix &m)
{
	c_matrix = m*c_staticMatrix;
	move(c_x,c_y);
}

void DJGameDesktopSpriteItem::setStaticMatrix(QMatrix &m)
{
	c_staticMatrix = m;
}
void DJGameDesktopSpriteItem::move(double x, double y)
{
	double tx,ty;
	c_matrix.map(x,y,&tx,&ty);
	
	if(c_vAlignment == Qt::AlignBottom)
	{
		ty -= height();
	}else if(c_vAlignment == Qt::AlignVCenter)
		ty -= (height()>>1);
	
	if(c_hAlignment == Qt::AlignRight)
		tx -= width();
	else if(c_hAlignment == Qt::AlignHCenter)
		tx -= (width()>>1);
	
	Q3CanvasSprite::move(tx,ty);
	c_x = x;
	c_y = y;
}
double DJGameDesktopSpriteItem::x()
{
	return c_x;
}

double DJGameDesktopSpriteItem::y()
{
	return c_y;
}

void DJGameDesktopSpriteItem::setHAlignment(quint16 align)
{
	c_hAlignment = align;
}

void DJGameDesktopSpriteItem::setVAlignment(quint16 align)
{
	c_vAlignment = align;
}


DJGameDesktopSpriteItem::~DJGameDesktopSpriteItem()
{
	
}
