-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathQTileSelector.cpp
334 lines (280 loc) · 9.86 KB
/
QTileSelector.cpp
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
#include "QTileSelector.h"
#include <QtGui/QMouseEvent>
#include <QtGui/QPainter>
#include <QtGui/QResizeEvent>
#include <QDebug>
#include <QDKEdit.h>
QTileSelector::QTileSelector(QWidget *parent) :
QWidget(parent), pixSrc(NULL)
{
arrangedTiles = QImage(this->rect().width(), this->rect().height(), QImage::Format_ARGB32);
QPainter painter;
painter.begin(&arrangedTiles);
painter.fillRect(this->rect(), Qt::white);
painter.end();
mouseOverTile = QRect(0, 0, 0, 0);
//setMouseTracking(true);
}
void QTileSelector::changeTilePixmap(QPixmap tilePixmap)
{
tiles = tilePixmap;
updateImage();
}
void QTileSelector::setTilePixmap(QPixmap tilePixmap, QSize size, float scale, int count, QStringList names)
{
tiles = tilePixmap;
orgTileSize = size;
tileCount = count;
tileNames = names;
scaleFactor = scale;
tileSize = orgTileSize * scale;
updateImage();
}
bool QTileSelector::event(QEvent *e)
{
if (e->type() == QEvent::ToolTip)
{
if (groups.isEmpty())
{
QHelpEvent *he = static_cast<QHelpEvent*>(e);
int xTile= he->x() / tileSize.width();
int yTile = he->y() / tileSize.height();
if ((xTile >= arrangedTilesDimension.width()) || (yTile >= arrangedTilesDimension.height()))
{
setToolTip("");
return QWidget::event(e);
}
int num = yTile*imageDimension.width()+xTile;
if (num < tileCount)
setToolTip(tileNames.value(num, QString("TileNumber: 0x%1").arg(num, 0, 16)));
else
setToolTip("");
}
else
{
QHelpEvent *he = static_cast<QHelpEvent*>(e);
if ((he->x() >= allTilesRect.width()) || (he->y() >= allTilesRect.height()))
{
setToolTip("");
return QWidget::event(e);
}
int tile = -1;
QRect tileRect;
QMap<int, QRect>::const_iterator it = tileRects.constBegin();
while (it != tileRects.constEnd())
{
tileRect = it.value();
if ((he->x() >= tileRect.x()) && (he->y() >= tileRect.y()) &&
(he->x() < tileRect.x() + tileRect.width()) && (he->y() < tileRect.y() + tileRect.height()))
{
tile = it.key();
break;
}
++it;
}
if (tile != -1)
setToolTip(tileNames.value(tile, QString("TileNumber: 0x%1").arg(tile, 0, 16)));
else
setToolTip("");
}
}
return QWidget::event(e);
}
void QTileSelector::groupTiles(QTileGroups grouping, QSpacings spacings)
{
groups = grouping;
space = spacings;
updateImage();
}
void QTileSelector::setTilePixSrc(QDKEdit *src)
{
pixSrc = src;
updateImage();
}
void QTileSelector::updateImage()
{
int pixmapX, pixmapY;
pixmapX = tiles.width() / orgTileSize.width();
pixmapY = tiles.height() / orgTileSize.height();
imageDimension.setWidth(this->rect().width() / tileSize.width());
imageDimension.setHeight(this->rect().height() / tileSize.height());
if (groups.isEmpty())
allTilesRect = QRect(0, 0, imageDimension.width()*tileSize.width(), ((tileCount / imageDimension.width()) + 1)*tileSize.height());
else
allTilesRect = this->rect();
arrangedTilesDimension.setWidth(allTilesRect.width() / tileSize.width());
arrangedTilesDimension.setHeight(allTilesRect.height() / tileSize.height());
int x, x2, y, y2;
QPainter painter;
arrangedTiles = QImage(this->rect().width(), this->rect().height(), QImage::Format_ARGB32);
arrangedTiles.fill(Qt::white);
painter.begin(&arrangedTiles);
painter.fillRect(allTilesRect, Qt::white);
if (groups.empty())
{
for (int i = 0; i < tileCount; i++)
{
x = (i % imageDimension.width()) * tileSize.width();
y = (i / imageDimension.width()) * tileSize.height();
x2 = (i % pixmapX) * orgTileSize.width();
y2 = (i / pixmapX) * orgTileSize.height();
QRect target(x, y, tileSize.width(), tileSize.height());
QRect source(x2, y2, orgTileSize.width(), orgTileSize.height());
painter.drawPixmap(target, tiles, source);
}
}
else
{
QVector<int> tilelist;
int vPos, hPos, x, y;
QString title;
QPixmap *pix;
vPos = space.top;
hPos = space.left;
tileRects.clear();
QTileGroups::const_iterator it = groups.constBegin();
while (it != groups.constEnd())
{
title = it.key();
tilelist = it.value();
++it;
hPos = space.left;
painter.drawText(QRect(hPos, vPos, allTilesRect.width(), tileSize.height()), Qt::AlignVCenter, title);
vPos += tileSize.height() + space.afterText;
for (int i = 0; i < tilelist.size(); i++)
{
x = (tilelist[i] % pixmapX) * orgTileSize.width();
y = (tilelist[i] / pixmapX) * orgTileSize.height();
if (pixSrc)
pix = pixSrc->getTilePixmap(tilelist[i]);
else
{
pix = new QPixmap(tileSize);
QPainter p;
p.begin(pix);
x2 = (tilelist[i] % pixmapX) * orgTileSize.width();
y2 = (tilelist[i] / pixmapX) * orgTileSize.height();
QRect source(x2, y2, orgTileSize.width(), orgTileSize.height());
p.drawPixmap(pix->rect(), tiles, source);
p.end();
}
QSize pixSize = pix->size();
pixSize.scale(tileSize, Qt::KeepAspectRatio);
QRect target(hPos+(tileSize.width() - pixSize.width())/2, vPos+(tileSize.height() - pixSize.height())/2, pixSize.width(), pixSize.height());
tileRects.insert(tilelist[i], QRect(hPos, vPos, tileSize.width(), tileSize.height()));
painter.drawPixmap(target, *pix);
delete pix;
hPos += tileSize.width() + space.hSpace;
if (hPos + tileSize.width() + space.right > arrangedTiles.width())
{
hPos = space.left;
vPos += tileSize.height() + space.vSpace;
}
}
if (hPos == space.left)
vPos -= tileSize.height() + space.vSpace;
vPos += tileSize.height() + space.beforeText;
}
}
painter.end();
update();
}
void QTileSelector::paintEvent(QPaintEvent *)
{
QPainter painter(this);
painter.drawImage(QPoint(0, 0), arrangedTiles);
//draw selection
painter.setPen(Qt::black);
painter.drawRect(mouseOverTile.x(), mouseOverTile.y(), mouseOverTile.width(), mouseOverTile.height());
//draw selected tile
painter.setPen(Qt::red);
painter.drawRect(selectedTile.x(), selectedTile.y(), selectedTile.width(), selectedTile.height());
}
void QTileSelector::mouseMoveEvent(QMouseEvent *e)
{
if (groups.isEmpty())
{
//check if selection rect has moved
int xTile= e->x() / tileSize.width();
int yTile = e->y() / tileSize.height();
QRect newSelection;
if ((xTile >= arrangedTilesDimension.width()) || (yTile >= arrangedTilesDimension.height()))
newSelection = QRect(0, 0, 0, 0);
else
newSelection = QRect(xTile * tileSize.width(), yTile * tileSize.height(), tileSize.width()-1, tileSize.height()-1);
if (mouseOverTile != newSelection)
{
mouseOverTile = newSelection;
update();
}
//check whether left or right mouse button has been pressed
if (e->buttons() != Qt::LeftButton)
return;
if (newSelection != selectedTile)
{
int newTileNumber = xTile + (yTile * imageDimension.width());
if (newTileNumber >= tileCount)
return;
selectedTile = newSelection;
selectedTileNumber = newTileNumber;
update();
emit tileSelected(selectedTileNumber);
}
}
else
{
QRect newSelection;
int tile = -1;
QRect tileRect;
//check if mouse is inside rect
if ((e->x() >= allTilesRect.width()) || (e->y() >= allTilesRect.height()))
newSelection = QRect(0, 0, 0, 0);
else
{
QMap<int, QRect>::const_iterator it = tileRects.constBegin();
while (it != tileRects.constEnd())
{
tileRect = it.value();
if ((e->x() >= tileRect.x()) && (e->y() >= tileRect.y()) &&
(e->x() < tileRect.x() + tileRect.width()) && (e->y() < tileRect.y() + tileRect.height()))
{
tile = it.key();
break;
}
++it;
}
if (tile != -1)
newSelection = tileRect;
}
if (mouseOverTile != newSelection)
{
mouseOverTile = newSelection;
update();
}
//check whether left or right mouse button has been pressed
if (e->buttons() != Qt::LeftButton)
return;
if (newSelection != selectedTile)
{
if ((tile >= tileCount) || (tile == -1))
return;
selectedTile = newSelection;
selectedTileNumber = tile;
update();
emit tileSelected(selectedTileNumber);
}
}
}
void QTileSelector::getMouse(bool enable)
{
setMouseTracking(enable);
}
void QTileSelector::mousePressEvent(QMouseEvent *e)
{
mouseMoveEvent(e);
}
void QTileSelector::resizeEvent(QResizeEvent *e)
{
if (e->oldSize() != QSize(-1, -1))
updateImage();
}