-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtile-viewer.cpp
208 lines (163 loc) · 5.91 KB
/
tile-viewer.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
#include "tile-viewer.h"
#include <QtGlobal>
#include <QMouseEvent>
#include <QPainter>
#include "utilities.h"
static constexpr int TILE_WIDTH_SCALED = TileManager::TILE_WIDTH * 2;
static constexpr int TILE_HEIGHT_SCALED = TileManager::TILE_HEIGHT * 2;
TileViewer::TileViewer(const TileManager &tile_manager)
: tile_manager(tile_manager)
, timer(this)
{
setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
connect(&tile_manager, &TileManager::pixmapsChanged, this,
[this]()
{
selected.resize(this->tile_manager.total_tiles());
update();
}
);
connect(&timer, &QTimer::timeout, this,
[this]()
{
selection_flip_flop = !selection_flip_flop;
update();
}
);
// Flash the selection every two seconds.
timer.start(1000 * 2);
}
void TileViewer::setSelection(const bool scroll_to_selection, const std::function<void(QVector<bool> &selection)> &callback)
{
callback(selected);
if (scroll_to_selection)
{
int width, height;
getGridDimensions(width, height);
const int row_offset = scroll() % width;
const auto to_row = [&width, &row_offset](const int tile_index)
{
return (tile_index - row_offset) / width;
};
const int scroll_row = to_row(scroll());
const int first_tile_row = to_row(selected.indexOf(true));
const int last_tile_row = to_row(selected.lastIndexOf(true));
const int upper_bound_delta = 2;
const int lower_bound_delta = height - 1 - 2;
const int upper_bound = scroll_row + upper_bound_delta;
const int lower_bound = scroll_row + lower_bound_delta;
if (first_tile_row < upper_bound || last_tile_row > lower_bound)
{
int y;
if (last_tile_row > lower_bound)
y = qMin(first_tile_row - upper_bound_delta, last_tile_row - lower_bound_delta);
else //if (first_tile_row < upper_bound)
y = qMax(first_tile_row - upper_bound_delta, last_tile_row - lower_bound_delta);
setScroll(row_offset + y * width);
}
}
update();
}
void TileViewer::getGridDimensions(int &columns, int &rows)
{
const int space_width = vertical_orientation ? height() : width();
const int space_height = vertical_orientation ? width() : height();
const int tile_width = vertical_orientation ? TILE_HEIGHT_SCALED : TILE_WIDTH_SCALED;
const int tile_height = vertical_orientation ? TILE_WIDTH_SCALED : TILE_HEIGHT_SCALED;
columns = space_width / tile_width;
rows = qMax(1, qMin(Utilities::DivideCeiling(space_height, tile_height), Utilities::DivideCeiling(tile_manager.total_tiles(), columns)));
}
void TileViewer::paintEvent(QPaintEvent* const event)
{
QWidget::paintEvent(event);
int tiles_per_row, total_rows;
getGridDimensions(tiles_per_row, total_rows);
QBrush brush;
brush.setStyle(Qt::SolidPattern);
QPainter painter(this);
painter.setPen(Qt::NoPen);
const int tiles_to_do = tile_manager.total_tiles() - m_scroll;
const int rows_to_do = qMin(total_rows, Utilities::DivideCeiling(tiles_to_do, tiles_per_row));
const auto do_visible_rows = [this, tiles_per_row, tiles_to_do, rows_to_do](const std::function<void(int, int, int)> &callback)
{
int current_tile = m_scroll;
for (int y = 0; y < rows_to_do; ++y)
{
const int length_of_row = qMin(tiles_per_row, tiles_to_do - y * tiles_per_row);
callback(current_tile, y, length_of_row);
current_tile += length_of_row;
}
};
const auto do_visible_tiles = [this, &do_visible_rows](const std::function<void(int, const QRect&)> &callback)
{
do_visible_rows([this, &callback](const int tile_index, const int y, const int length_of_row)
{
for (int x = 0; x < length_of_row; ++x)
{
const int actual_x = vertical_orientation ? y : x;
const int actual_y = vertical_orientation ? x : y;
callback(tile_index + x, QRect(actual_x * TILE_WIDTH_SCALED, actual_y * TILE_HEIGHT_SCALED, TILE_WIDTH_SCALED, TILE_HEIGHT_SCALED));
}
}
);
};
brush.setColor(Qt::black);
painter.setBrush(brush);
// Initialise colour values to 0.
do_visible_rows(
[this, &painter](const int tile_index, const int y, const int length_of_row)
{
Q_UNUSED(tile_index);
if (vertical_orientation)
painter.drawRect(QRect{y * TILE_WIDTH_SCALED, 0, TILE_WIDTH_SCALED, length_of_row * TILE_HEIGHT_SCALED});
else
painter.drawRect(QRect{0, y * TILE_HEIGHT_SCALED, length_of_row * TILE_WIDTH_SCALED, TILE_HEIGHT_SCALED});
}
);
brush.setColor(Qt::white);
painter.setBrush(brush);
// Set all bits of the pixels in the selection to 1.
// This alternates, creating a flashing effect.
do_visible_tiles(
[this, &painter](const int tile_index, const QRect &rect)
{
if (selected[tile_index] && selection_flip_flop)
painter.drawRect(rect.marginsAdded(QMargins(4, 4, 4, 4)));
}
);
painter.setCompositionMode(QPainter::RasterOp_SourceXorDestination);
do_visible_tiles(
[this, &painter](const int tile_index, const QRect &rect)
{
// XOR the selection.
if (selected[tile_index])
painter.drawRect(rect);
// XOR the selection with the tile's pixmap.
painter.drawPixmap(rect, tile_manager.pixmaps(tile_index, palette_line, TileManager::PixmapType::WITH_BACKGROUND));
}
);
}
void TileViewer::mousePressEvent(QMouseEvent* const event)
{
QWidget::mousePressEvent(event);
int tiles_per_row, total_rows;
getGridDimensions(tiles_per_row, total_rows);
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
const int tile_x = event->x() / TILE_WIDTH_SCALED;
const int tile_y = event->y() / TILE_HEIGHT_SCALED;
#else
const int tile_x = event->position().x() / TILE_WIDTH_SCALED;
const int tile_y = event->position().y() / TILE_HEIGHT_SCALED;
#endif
const int tile_index = m_scroll + tile_y * tiles_per_row + tile_x;
if (tile_index >= selected.size())
return;
clearSelection();
setSelection(false,
[tile_index](QVector<bool> &selection)
{
selection[tile_index] = true;
}
);
emit tileSelected(tile_index);
}