gameremovedtiles.cpp 6.01 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/* Copyright (C) 2019 Christian Krippendorf <Coding@Christian-Krippendorf.de>
 *
 * 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. */

// own
#include "gameremovedtiles.h"
19
#include "kmahjonggtileset.h"
20
#include "gamedata.h"
21
22
23
24
25
26

// Qt
#include <QGraphicsSceneMouseEvent>
#include <QPainter>
#include <QPixmap>

27
#include <KLocalizedString>
28

29
30
31
32
33

GameRemovedTiles::GameRemovedTiles(QGraphicsObject * object)
    : QGraphicsObject(object)
    , m_width(100)
    , m_height(100)
34
35
36
37
38
39
40
41
42
43
44
45
    , m_borderWidthFrac(0.05)
    , m_tileScale(0.9)
    , m_titleHeightFrac(0.1)
    , m_borderWidthPixel(0)
    , m_titleHeightPixel(0)
    , m_tileSpaceRow(0)
    , m_tileSpaceCol(0)
    , m_tileFaceWidth(0)
    , m_tileFaceHeight(0)
    , m_faceScale(1.0)
    , m_tileFaceWidthScaled(0)
    , m_tileFaceHeightScaled(0)
46
47
48
49
50
    , m_maxTilesRow(0)
    , m_maxTilesCol(0)
    , m_itemFaces(new QList<USHORT>())
    , m_tiles(nullptr)
    , m_gameData(nullptr)
51
52
53
54
55
{
}

GameRemovedTiles::~GameRemovedTiles()
{
56
    delete m_itemFaces;
57
58
59
60
61
62
63
64
}

void GameRemovedTiles::setSize(qreal width, qreal height)
{
    m_width = width;
    m_height = height;
}

65
66
67
68
69
void GameRemovedTiles::setTileset(KMahjonggTileset * tiles)
{
    m_tiles = tiles;
}

70
71
72
73
74
void GameRemovedTiles::prepareForGeometryChange()
{
    prepareGeometryChange();
}

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
void GameRemovedTiles::setGameData(GameData * gameData)
{
    m_gameData = gameData;
}

void GameRemovedTiles::updateTileCalculations()
{
    int maxTilesRow = 0;
    int maxTilesCol = 0;

    // Get the height and the width of the face tile. This has to be multiplied
    // by two, cause the value is related to half tile. (half positioning)
    m_tileFaceWidth = m_tiles->qWidth() * 2.0;
    m_tileFaceHeight = m_tiles->qHeight() * 2.0;
    m_tileFaceWidthScaled = m_tileFaceWidth * m_faceScale;
    m_tileFaceHeightScaled = m_tileFaceHeight * m_faceScale;

    m_borderWidthPixel = m_borderWidthFrac * m_width;
    m_titleHeightPixel = m_titleHeightFrac * m_height;

    maxTilesRow = static_cast<int>(
        (m_width - 2 * m_borderWidthPixel) / m_tileFaceWidthScaled
    );
    maxTilesCol = static_cast<int>(
        (m_height - 2 * m_borderWidthPixel - m_titleHeightPixel) /
        m_tileFaceHeightScaled
    );

    m_tileSpaceRow = ((m_width - 2 * m_borderWidthPixel) - 
        maxTilesRow * m_tileFaceWidthScaled) / (maxTilesRow - 1);
    m_tileSpaceCol = ((m_height - 
        2 * m_borderWidthPixel - m_titleHeightPixel) -
        maxTilesCol * m_tileFaceHeightScaled) / (maxTilesCol - 1);

    m_maxTilesRow = maxTilesRow;
    m_maxTilesCol = maxTilesCol;
}

113
114
115
void GameRemovedTiles::paint(QPainter *painter, const QStyleOptionGraphicsItem *, 
    QWidget *)
{
116
117
    updateTileCalculations();

118
    // General painter settings.
119
    painter->setRenderHint(QPainter::Antialiasing);
120
121

    // Paint the background.
122
123
124
125
    painter->setOpacity(0.5);
    QPainterPath path;
    path.addRoundedRect(QRectF(0, 0, m_width, m_height), 10, 10);
    painter->fillPath(path, Qt::black);
126
127
128
129

    // Paint the title text.
    painter->setPen(Qt::white);
    QFont font(painter->font());
130
    font.setPointSize(m_titleHeightPixel * 0.15);
131
132
    painter->setFont(font);
    painter->drawText(
133
134
        QRectF(m_borderWidthPixel, m_borderWidthPixel, m_width, m_titleHeightPixel),
        i18n("Removed tiles")
135
136
137
    );

    // Exit if no tileset has been set to this object.
138
    if (m_tiles == nullptr || m_itemFaces->isEmpty()) {
139
140
141
142
143
144
        return;
    }

    // Paint all the tiles.
    painter->setPen(QPen(Qt::white, 10));

145
146
    unsigned int row = 0;
    unsigned int col = 0;
147
148
149
150
151
    int start = m_itemFaces->size() - (m_maxTilesCol * m_maxTilesRow * 2);
    if (start < 0) {
        start *= 0;
    }
    for (int pos = start; pos < m_itemFaces->size() - 1; pos+=2) {
152
153
154
        if (col >= m_maxTilesRow) {
            row++;
            col = 0;
155
        }
156
157
158

        // Get the pixmap of the face.
        QPixmap face;
159
        face = m_tiles->tileface(m_itemFaces->at(pos));
160
161
162
163
164
165
166
167
168
169
170
171
172
        face = face.scaledToHeight(
            m_tileFaceHeightScaled, Qt::SmoothTransformation
        );

        // Paint the background of the face.
        QPainterPath pixPath;
        pixPath.addRoundedRect(
            QRectF(
                m_borderWidthPixel + col * m_tileSpaceRow + col * m_tileFaceWidth, 
                m_titleHeightPixel + row * m_tileSpaceCol + row * m_tileFaceHeight,
                m_tileFaceWidth, m_tileFaceHeight
            ), 10, 10
        );
173
        painter->setOpacity(1.0 - (m_itemFaces->size() - pos) / 100.0);
174
175
176
        painter->fillPath(pixPath, Qt::white);

        // Paint the pixmap of the face.
177
        painter->setOpacity(1.0 - (m_itemFaces->size() - pos) / 100.0);
178
179
180
181
182
183
184
185
        painter->drawPixmap(
            QPointF(
                m_borderWidthPixel + col * m_tileSpaceRow + col * m_tileFaceWidth,
                m_titleHeightPixel + row * m_tileSpaceCol + row * m_tileFaceHeight
            ), face
        );

        col++;
186
    }
187
188
}

189
190
191
192
193
194
195
196
void GameRemovedTiles::undo()
{
    if (m_itemFaces->size() >= 2) {
        m_itemFaces->removeLast();
        m_itemFaces->removeLast();
    }
}

197
198
199
200
201
void GameRemovedTiles::reset()
{
    m_itemFaces->clear();
}

202
203
204
205
206
207
208
209
210
QRectF GameRemovedTiles::boundingRect() const
{
    return QRectF(QPointF(0.0, 0.0), QSizeF(m_width, m_height));
}

QRectF GameRemovedTiles::rect() const
{
    return boundingRect();
}
211
212
213
214
215
216
217
218
219
220

void GameRemovedTiles::addItem(const POSITION & itemPos) 
{
    m_itemFaces->append(itemPos.f);
}

void GameRemovedTiles::removeLastItem()
{
    m_itemFaces->removeLast();
}