-
Notifications
You must be signed in to change notification settings - Fork 1
/
tileview.cpp
256 lines (235 loc) · 7.11 KB
/
tileview.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
#include "tileview.h"
#include <QWidget>
#include <QPainter>
#include <QScrollArea>
#include <QScrollBar>
#include <QResizeEvent>
#include <QPaintEvent>
int TileView::_cols(int w) {
return (w-TILE_SPACE)/(TILE_PITCH);
}
int TileView::_col(int x) {
// this includes left border/space and excludes right
return x/TILE_PITCH;
}
int TileView::_row(int y) {
// this includes top border/space and excludes bottom
return y/TILE_PITCH;
}
TileView::TileView(QWidget *parent)
: QWidget(parent)
{
this->setMinimumWidth(MIN_H);
}
TileView::~TileView()
{
clear();
}
void TileView::clear()
{
_tiles.clear();
_colorMaps.clear();
_colorMapMap.clear();
if (_pixels) delete[] _pixels;
_pixels = NULL;
if (_image) delete _image;
_image = NULL;
this->repaint();
this->setMinimumWidth(MIN_H);
}
void TileView::add(const AbstractTile& block)
{
// TODO: extract data from block instead of storing the block
_tiles.append(block);
_layoutChanged = true;
}
void TileView::set(int index, const AbstractTile& block)
{
// TODO: extract data from block instead of storing the block
_tiles[index] = block;
_layoutChanged = true;
}
int TileView::addColorMap(const ColorMap &map)
{
int index = _colorMaps.length();
_colorMaps.append(map);
if (index==0) _mapsChanged = true;
return index;
}
ColorMap TileView::colorMap(int index) const
{
if (index>_colorMaps.length()) return ColorMap();
return _colorMaps[index];
}
ColorMap TileView::itemColorMap(int index) const
{
if (index>_tiles.length()) return ColorMap();
auto it = _colorMapMap.find(index);
if (it == _colorMapMap.end())
return colorMap(0);
return colorMap(it.value());
}
void TileView::setColorMap(int index, const ColorMap &map)
{
bool mapInUse = index<_colorMaps.length(); // TODO: actually find out if it's in use
bool needRefresh = mapInUse && (map != _colorMaps[index]);
if (index > _colorMaps.length()) return;
else if (index == _colorMaps.length()) addColorMap(map);
else _colorMaps[index] = map;
if (needRefresh) {
_mapsChanged = true;
repaint();
}
}
int TileView::selected() const
{
return _selected;
}
void TileView::setSelected(int index)
{
if (index == _selected) return;
int newx = itemX(index);
int newy = itemY(index);
int oldx = itemX(_selected);
int oldy = itemY(_selected);
if (_selected>=0 && index>=0) {
int minx = ((oldx<newx) ? oldx : newx);
int miny = ((oldy<newy) ? oldy : newy);
int maxx = ((oldx>newx) ? oldx : newx)+TILE_OUTER_SIZE;
int maxy = ((oldy>newy) ? oldy : newy)+TILE_OUTER_SIZE;
_selected = index;
repaint(minx, miny, maxx-minx, maxy-miny);
} else if (index >= 0) {
_selected = index;
repaint(newx, newy, TILE_OUTER_SIZE, TILE_OUTER_SIZE);
} else if (_selected >= 0) {
_selected = index;
repaint(oldx, oldy, TILE_OUTER_SIZE, TILE_OUTER_SIZE);
}
}
int TileView::itemX(int index) const
{
int cols = _cols(width());
int col = index%cols;
return col*TILE_PITCH;
}
int TileView::itemY(int index) const
{
int cols = _cols(width());
int row = index/cols;
return row*TILE_PITCH;
}
int TileView::itemIndex(int x, int y) const
{
int cols = _cols(width());
int row = _row(y);
int col = _col(x);
if (col>=cols) return -1;
int idx = row*cols + col;
if (idx>=_tiles.size()) return -1;
return idx;
}
void TileView::setBackground(QRgb color)
{
if (_bgColor==color || (_bgColor>>24==0 && color>>24==0)) return;
_bgColor = color;
repaint();
}
void TileView::mousePressEvent(QMouseEvent * ev)
{
#if QT_VERSION_MAJOR < 6
int index = itemIndex(ev->x(), ev->y());
#else
int index = itemIndex(ev->position().x(), ev->position().y());
#endif
setSelected(index);
emit(selectionChanged(index));
}
void TileView::resizeEvent(QResizeEvent *ev)
{
int oldcols = _cols(ev->oldSize().width());
int newcols = _cols(ev->size().width());
if ( oldcols != newcols ) {
int newrows =(_tiles.count()+newcols-1)/newcols;
int newh = TILE_SPACE+TILE_PITCH*newrows;
this->setMinimumHeight(newh);
_layoutChanged = true;
}
}
void TileView::paintEvent(QPaintEvent* ev)
{
int w = width();
int h = height();
int cols = _cols(w);
if (_colorMaps.empty() || _tiles.empty()) {
QPainter painter(this);
painter.eraseRect(ev->rect());
return;
}
if (!_pixels || !_image || _layoutChanged || _mapsChanged) {
qDebug("reshape: %dx%d\n", w, h);
int rows =(_tiles.count()+cols-1)/cols;
int newh = TILE_SPACE+TILE_PITCH*rows;
if (newh < MIN_H) newh=MIN_H;
if (newh > this->minimumHeight()) {
this->setMinimumHeight(newh);
repaint();
return;
} else {
this->setMinimumHeight(newh);
}
_layoutChanged = false;
_mapsChanged = false;
int x = TILE_SPACE;
int y = TILE_SPACE;
if (_pixels) delete[] _pixels;
_pixels = new QRgb[w*h];
QRgb* pixels = _pixels;
memset(pixels, 0, w*h*sizeof(QRgb));
const ColorMap& map = _colorMaps[_colorMapMap[0]];
for (int i=0; i<_tiles.size(); i++) {
const AbstractTile& block = _tiles[i];
QByteArray src = block.getPixels();
if (src.length() < block.size * block.size) {
qWarning("Expected %d, got %lld pixels for tile %d",
block.size*block.size, (long long)src.length(), i);
} else {
for (int ys=0; ys<block.size; ys++) {
for (int xs=0; xs<block.size; xs++) {
int xd = x + 2*(xs);
int yd = y + 2*(ys);
uint8_t c = src[xs+ys*block.size];
pixels[(xd+0) + (yd+0)*w] = map.c[c];
pixels[(xd+0) + (yd+1)*w] = map.c[c];
pixels[(xd+1) + (yd+0)*w] = map.c[c];
pixels[(xd+1) + (yd+1)*w] = map.c[c];
}
}
}
x += TILE_PITCH;
if (x+TILE_PITCH > w) {
x = TILE_SPACE;
y += TILE_PITCH;
}
if (y+TILE_SIZE > h) break; // out of space
}
if (_image) delete _image;
_image = new QImage((uchar*)_pixels, w, h, QImage::Format_ARGB32);
}
QPainter painter(this);
if (_bgColor>>24) painter.fillRect(0,0,width(),height(),_bgColor);
if (_selected>=0) {
int row = _selected/cols;
int col = _selected%cols;
int x1=col*TILE_PITCH, x2=x1+TILE_PITCH, y1=row*TILE_PITCH, y2=y1+TILE_PITCH;
painter.setPen(QColor(0xff66aaff));
painter.fillRect(x1, y1, TILE_OUTER_SIZE, TILE_OUTER_SIZE,
QColor(0xff66aaff));
painter.setPen(QPen(Qt::black, 1, Qt::DashLine));
painter.drawLine(x1, y1, x2, y1);
painter.drawLine(x1, y2, x2, y2);
painter.drawLine(x1, y1, x1, y2);
painter.drawLine(x2, y1, x2, y2);
}
painter.drawImage(0,0, *_image);
}