//****************************************************************************
//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 "DJCanvasItem.h"
#include "DJGameUser.h"
#include "protocol.h"

const int DJCanvasItem::RTTI = 4000;

DJCanvasItem::DJCanvasItem( Q3Canvas *canvas, const QPixmap& pix, Type type, quint32 id )
    : Q3CanvasRectangle( canvas ), m_pixmap(pix), m_type(type), m_id(id)
{
    m_image	= m_pixmap.toImage();
    setSize( m_pixmap.width(), m_pixmap.height() );
}
DJCanvasItem::~DJCanvasItem()
{
}

void DJCanvasItem::drawShape( QPainter &p )
{	
	p.save();
	p.setClipping( false );
	p.drawPixmap( static_cast<int>(x()), static_cast<int>(y()), m_pixmap );
	p.restore();
}
int DJCanvasItem::rtti() const
{
	return RTTI;
}
bool DJCanvasItem::hit( const QPoint &p ) const
{
    int ix = p.x()-int(x());
    int iy = p.y()-int(y());
    if ( !m_image.valid( ix , iy ) )
		return FALSE;
    QRgb pixel = m_image.pixel( ix, iy );
    	return qAlpha( pixel ) != 0;
}
void DJCanvasItem::setPixmap( const QPixmap& pix )
{
	m_pixmap	= pix;
	m_image	= m_pixmap.toImage();
    setSize( m_pixmap.width(), m_pixmap.height() );
}
//---------------------------------------------------------------------------------
DJCanvasTable::DJCanvasTable( const QPixmap& pix, quint32 id, const QFont& font, Q3Canvas *canvas )
    : DJCanvasItem( canvas, pix, DJCanvasItem::Table, id ), m_font(font)
{
}
DJCanvasTable::~DJCanvasTable()
{
}
void DJCanvasTable::drawShape( QPainter &p )
{	
	DJCanvasItem::drawShape( p );
	
	p.save();
	p.setClipping( false );
	p.setFont( m_font );
	p.setPen( pen() );
	p.drawText(rect(),Qt::AlignCenter, QString::number( id() ) );
	p.restore();
}
//---------------------------------------------------------------------------------
DJCanvasAvatar::DJCanvasAvatar( const QPixmap& avatar, DJGameUser* user, const QPixmap& ready, Q3Canvas *canvas )
	: DJCanvasItem( canvas, avatar, DJCanvasItem::Avatar, user->userId() ),m_user(user),m_ready(ready)
{
}
DJCanvasAvatar::~DJCanvasAvatar()
{
}
void DJCanvasAvatar::drawShape( QPainter &p )
{	
	DJCanvasItem::drawShape( p );
	
	p.save();
	p.setClipping( false );
	if ( m_user->isReady() ) {
		p.drawPixmap( static_cast<int>(x()), static_cast<int>(y()) + size().height() - m_ready.height(), m_ready );
	}
	if ( m_user->isSelf() ) {
		QPen pen( Qt::green );
		pen.setWidth( 3 );
		p.setPen( pen );
		p.drawRect( rect() );
	}
	p.restore();
}
//---------------------------------------------------------------------------------
DJCanvasText::DJCanvasText( const QString& t, Q3Canvas *canvas)
    : Q3CanvasText( t, canvas )
{
}
DJCanvasText::~DJCanvasText()
{
}
void DJCanvasText::draw( QPainter &p )
{	
	p.save();
	p.setFont( font() );
	p.setPen( color() );
	p.setClipping( false );
	p.drawText( boundingRect(), textFlags(), text() );
	p.restore();
}
