SVN_SILENT made messages (.desktop file)
[kdegames.git] / kmahjongg / TileSprite.cpp
blob8d9624997196e44677d654e9bc870e96c5e9b2d3
1 /*
2 Copyright 2006 Mauricio Piacentini <mauricio@tabuleiro.com>
3 begin : Oct 31 2006
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.
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.
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.
20 #include "TileSprite.h"
21 #include <QPixmap>
22 #include <QTimer>
24 TileSprite::TileSprite( KGameCanvasAbstract* canvas, QPixmap & backunselected, QPixmap & backselected, QPixmap & face, TileViewAngle angle, bool selected )
25 : QObject(), KGameCanvasItem(canvas)
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();
39 TileSprite::~TileSprite()
43 void 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
51 void TileSprite::updateOffset() {
52 switch( m_angle )
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;
69 void 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);
73 if(op <= 0)
74 return;
76 if(op < 255)
77 p->setOpacity( op/255.0 );
78 paint(p);
79 if(op < 255)
80 p->setOpacity(1.0);
83 void 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);
93 void 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;
101 //keep fading
102 QTimer::singleShot(40, this, SLOT(fadeOut()));
105 void TileSprite::fadeIn() {
106 if (m_dying) return;
107 setOpacity(opacity()+25);
108 if ((opacity() == 255)||(m_dying)) {
109 //cancel fade in
110 return;
112 //keep fading
113 QTimer::singleShot(40, this, SLOT(fadeIn()));
116 QRect TileSprite::rect() const {
117 return QRect(pos(), m_backselected.size());
120 #include "TileSprite.moc"