1/*
2 Copyright 2006 Mauricio Piacentini <mauricio@tabuleiro.com>
3 begin : Oct 31 2006
4
5 Kmahjongg is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18*/
19
20#include "TileSprite.h"
21#include <QPixmap>
22#include <QTimer>
23
24TileSprite::TileSprite( KGameCanvasAbstract* canvas, QPixmap & backunselected, QPixmap & backselected, QPixmap & face, TileViewAngle angle, bool selected )
25 : QObject(), KGameCanvasItem(canvas)
26{
27 m_dying = false;
28 setOpacity(255);
29 m_backselected = backselected;
30 m_backunselected = backunselected;
31 m_face = face;
32 m_selected = selected;
33 m_angle = angle;
34 m_woffset = m_backselected.width() - m_face.width();
35 m_hoffset = m_backselected.height() - m_face.height();
36 updateOffset();
37}
38
39TileSprite::~TileSprite()
40{
41}
42
43void TileSprite::setAngle(TileViewAngle angle, QPixmap & backunselected, QPixmap & backselected) {
44 m_angle = angle;
45 m_backselected = backselected;
46 m_backunselected = backunselected;
47 updateOffset();
48 //changed(); We need to call updateSpriteMap to relayer anyway
49}
50
51void TileSprite::updateOffset() {
52 switch( m_angle )
53 {
54 case NW:
55 m_faceoffset = QPoint(m_woffset,0);
56 break;
57 case NE:
58 m_faceoffset = QPoint(0,0);
59 break;
60 case SE:
61 m_faceoffset = QPoint(0,m_hoffset);
62 break;
63 case SW:
64 m_faceoffset = QPoint(m_woffset,m_hoffset);
65 break;
66 }
67}
68
69void TileSprite::paintInternal(QPainter* p, const QRect& /*prect*/,
70 const QRegion& /*preg*/, const QPoint& /*delta*/, double cumulative_opacity) {
71 int op = int(cumulative_opacity*opacity() + 0.5);
72
73 if(op <= 0)
74 return;
75
76 if(op < 255)
77 p->setOpacity( op/255.0 );
78 paint(p);
79 if(op < 255)
80 p->setOpacity(1.0);
81}
82
83void TileSprite::paint(QPainter* p) {
84 if (m_selected) {
85 p->drawPixmap(pos(), m_backselected);
86 p->drawPixmap(pos()+m_faceoffset, m_face);
87 } else {
88 p->drawPixmap(pos(), m_backunselected);
89 p->drawPixmap(pos()+m_faceoffset, m_face);
90 }
91}
92
93void TileSprite::fadeOut() {
94 m_dying = true;
95 setOpacity(opacity()-25);
96 if (opacity() <= 0) {
97 //cancel fade and schedule our destruction!
98 deleteLater();
99 return;
100 }
101 //keep fading
102 QTimer::singleShot(40, this, SLOT(fadeOut()));
103}
104
105void TileSprite::fadeIn() {
106 if (m_dying) return;
107 setOpacity(opacity()+25);
108 if ((opacity() == 255)||(m_dying)) {
109 //cancel fade in
110 return;
111 }
112 //keep fading
113 QTimer::singleShot(40, this, SLOT(fadeIn()));
114}
115
116QRect TileSprite::rect() const {
117 return QRect(pos(), m_backselected.size());
118}
119
120#include "TileSprite.moc"
121