File: TileSprite.cpp

package info (click to toggle)
kmahjongg 4:4.14.0-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 1,968 kB
  • ctags: 427
  • sloc: cpp: 3,945; makefile: 9; sh: 2
file content (120 lines) | stat: -rw-r--r-- 3,246 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*
    Copyright 2006 Mauricio Piacentini  <mauricio@tabuleiro.com>
    begin                : Oct 31 2006

    Kmahjongg 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; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

#include "TileSprite.h"
#include <QPixmap>
#include <QTimer>

TileSprite::TileSprite( KGameCanvasAbstract* canvas, QPixmap & backunselected, QPixmap & backselected, QPixmap & face, TileViewAngle angle, bool selected )
    : QObject(), KGameCanvasItem(canvas)
{
    m_dying = false;
    setOpacity(255);
    m_backselected = backselected;
    m_backunselected = backunselected;
    m_face = face;
    m_selected = selected;
    m_angle = angle;
    m_woffset = m_backselected.width() - m_face.width();
    m_hoffset = m_backselected.height() - m_face.height();
    updateOffset();
}

TileSprite::~TileSprite()
{
}

void TileSprite::setAngle(TileViewAngle angle, QPixmap & backunselected, QPixmap & backselected) {
    m_angle = angle;
    m_backselected = backselected;
    m_backunselected = backunselected;
    updateOffset();
    //changed(); We need to call updateSpriteMap to relayer anyway
}

void TileSprite::updateOffset() {
    switch( m_angle )
        {
            case NW:
		m_faceoffset = QPoint(m_woffset,0);
		break;
	    case NE:
		m_faceoffset = QPoint(0,0);
		break;
	    case SE:
		m_faceoffset = QPoint(0,m_hoffset);
		break;
	    case SW:
		m_faceoffset = QPoint(m_woffset,m_hoffset);
		break;
	}
}

void TileSprite::paintInternal(QPainter* p, const QRect& /*prect*/,
                  const QRegion& /*preg*/, const QPoint& /*delta*/, double cumulative_opacity) {
  int op = int(cumulative_opacity*opacity() + 0.5);

  if(op <= 0)
    return;

  if(op < 255)
    p->setOpacity( op/255.0 );
    paint(p);
  if(op < 255)
    p->setOpacity(1.0);
}

void TileSprite::paint(QPainter* p) {
  if (m_selected) {
	  p->drawPixmap(pos(), m_backselected);
	  p->drawPixmap(pos()+m_faceoffset, m_face);
  } else {
	  p->drawPixmap(pos(), m_backunselected);
	  p->drawPixmap(pos()+m_faceoffset, m_face);
  }
}

void TileSprite::fadeOut() {
    m_dying = true;
    setOpacity(opacity()-25);
    if (opacity() <= 0) {
	//cancel fade and schedule our destruction!
	deleteLater();
	return;
    }
    //keep fading
    QTimer::singleShot(40, this, SLOT(fadeOut()));
}

void TileSprite::fadeIn() {
    if (m_dying) return;
    setOpacity(opacity()+25);
    if ((opacity() == 255)||(m_dying)) {
	//cancel fade in
	return;
    }
    //keep fading
    QTimer::singleShot(40, this, SLOT(fadeIn()));
}

QRect TileSprite::rect() const {
    return QRect(pos(), m_backselected.size());
}

#include "TileSprite.moc"