-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpalettegraphicsview.cpp
More file actions
414 lines (357 loc) · 11.6 KB
/
Copy pathpalettegraphicsview.cpp
File metadata and controls
414 lines (357 loc) · 11.6 KB
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
#include "palettegraphicsview.h"
PaletteGraphicsView::PaletteGraphicsView(QWidget *parent) : QGraphicsView(parent)
{
m_graphicsScene = new QGraphicsScene(this);
this->setScene(m_graphicsScene);
this->setMouseTracking(true);
this->setViewportUpdateMode(ViewportUpdateMode::FullViewportUpdate);
this->setAlignment(Qt::AlignTop | Qt::AlignTop);
QBrush brush(QColor(240,240,240));
this->setBackgroundBrush(brush);
m_is256ColorMode = false;
m_highlight = Q_NULLPTR;
setHighlightImage();
m_highlight->setZValue(1000);
m_highlight->setVisible(false);
m_index = -1;
m_mouseEventEnabled = true;
m_mousePos = QPoint(0,0);
m_timer = new QTimer(this);
connect(m_timer, SIGNAL(timeout()), SLOT(updateInfo()));
m_infoWindow = new PaletteInfoWindow();
m_infoWindow->setAttribute(Qt::WA_ShowWithoutActivating);
}
PaletteGraphicsView::~PaletteGraphicsView()
{
clear();
}
void PaletteGraphicsView::addPalette(Palette palette, bool is256Color, int insertAt)
{
if (m_is256ColorMode != is256Color)
{
m_is256ColorMode = is256Color;
setHighlightImage();
}
Q_ASSERT(palette.size() == 16 || palette.size() == 256);
palette.push_back(c_unselected); // 16/256
palette[0] |= 0xFF000000; // undo transparency on first color
QImage* image = new QImage(c_width, m_is256ColorMode ? c_size * c_size : c_size, QImage::Format_Indexed8);
image->setColorTable(palette);
for (int y = 0; y < image->height(); y++)
{
for (int x = 0; x < image->width(); x++)
{
if (x == 0 || x % (c_size - 1) == 0 || y == 0 || y % c_size == c_size - 1)
{
// Border
image->setPixel(x, y, palette.size() - 1);
}
else
{
// Color
image->setPixel(x, y, (y / c_size) * 16 + (x / (c_size - 1)));
}
}
}
if (insertAt == -1 || insertAt > m_images.size())
{
m_images.push_back(image);
}
else
{
m_images.insert(insertAt, image);
}
QGraphicsPixmapItem* item = m_graphicsScene->addPixmap(QPixmap::fromImage(m_is256ColorMode ? get256ColorImage(image) : *image));
if (insertAt == -1 || insertAt > m_pixmapItems.size())
{
m_pixmapItems.push_back(item);
if (m_is256ColorMode && m_images.size() > 1)
{
// Only allow one 256 color is display at a time
item->setPos(0, 0);
item->setVisible(false);
}
else
{
item->setPos(0, (m_images.size() - 1) * c_size);
}
}
else
{
m_pixmapItems.insert(insertAt, item);
if (m_is256ColorMode && m_images.size() > 1)
{
// Only allow one 256 color is display at a time
item->setPos(0, 0);
item->setVisible(false);
}
else
{
item->setPos(0, insertAt * c_size);
// Shift everything after this index down
for (int i = insertAt + 1 ; i < m_pixmapItems.size(); i++)
{
QGraphicsPixmapItem* t = m_pixmapItems[i];
t->moveBy(0, c_size);
}
}
}
m_graphicsScene->setSceneRect(0, 0, c_width, m_is256ColorMode ? c_size * c_size : m_images.size() * c_size);
}
void PaletteGraphicsView::setPaletteSelected(int index)
{
Q_ASSERT(index >= 0 && index < m_images.size());
if (index >= 0 && index < m_images.size())
{
m_index = index;
m_highlight->setVisible(true);
if (m_is256ColorMode)
{
// Display the current 256 color
for (int i = 0; i < m_pixmapItems.size(); i++)
{
m_pixmapItems[i]->setVisible(i == index);
}
}
else
{
m_highlight->setPos(0, index * c_size);
// Scroll to palette if it is off-screen
QScrollBar* vScrollBar = this->verticalScrollBar();
int height = this->size().height() - 2;
int yScroll = vScrollBar->value();
int selectedTopY = index * 16;
int selectedBaseY = (index + 1) * 16;
if (selectedBaseY > height + yScroll)
{
vScrollBar->setValue(selectedBaseY - height);
}
else if (selectedTopY < yScroll)
{
vScrollBar->setValue(selectedTopY);
}
}
}
}
void PaletteGraphicsView::deletePalette(int index)
{
Q_ASSERT(index >= 0 && index < m_images.size());
if (index >= 0 && index < m_images.size())
{
delete m_images[index];
m_images.erase(m_images.begin() + index);
QGraphicsPixmapItem* item = m_pixmapItems[index];
m_graphicsScene->removeItem(item);
delete item;
m_pixmapItems.erase(m_pixmapItems.begin() + index);
if (!m_is256ColorMode)
{
m_graphicsScene->setSceneRect(0, 0, c_width, m_images.size() * 16);
// Shift everything after this index up
for (int i = index ; i < m_pixmapItems.size(); i++)
{
QGraphicsPixmapItem* item = m_pixmapItems[i];
item->moveBy(0, -c_size);
}
// Highlight is outside limit, move it up
if (m_index == m_pixmapItems.size())
{
m_index--;
m_highlight->moveBy(0, -c_size);
}
}
}
}
void PaletteGraphicsView::clear()
{
for (QImage* image : m_images)
{
delete image;
}
m_images.clear();
m_index = -1;
for (QGraphicsPixmapItem* item : m_pixmapItems)
{
m_graphicsScene->removeItem(item);
delete item;
}
m_pixmapItems.clear();
m_graphicsScene->setSceneRect(0, 0, 0, 0);
m_highlight->setVisible(false);
}
void PaletteGraphicsView::replaceColor(int paletteIndex, int colorIndex, QRgb color)
{
QImage* image = m_images[paletteIndex];
Palette palette = image->colorTable();
// Update color (covert it back and forth to round for GBA color)
color = BNSprite::ClampRGB(color);
palette[colorIndex] = color;
image->setColorTable(palette);
QGraphicsPixmapItem* item = m_pixmapItems[paletteIndex];
item->setPixmap(QPixmap::fromImage(m_is256ColorMode ? get256ColorImage(image) : *image));
}
void PaletteGraphicsView::replacePalette(int paletteIndex, Palette palette)
{
if (paletteIndex >= m_images.size()) return;
Q_ASSERT(palette.size() == 16 || palette.size() == 256);
palette.push_back(c_unselected); // 16/256
palette[0] |= 0xFF000000; // undo transparency on first color
QImage* image = m_images[paletteIndex];
image->setColorTable(palette);
QGraphicsPixmapItem* item = m_pixmapItems[paletteIndex];
item->setPixmap(QPixmap::fromImage(m_is256ColorMode ? get256ColorImage(image) : *image));
}
void PaletteGraphicsView::swapPalette(int id1, int id2)
{
qSwap(m_images[id1], m_images[id2]);
qSwap(m_pixmapItems[id1], m_pixmapItems[id2]);
QPointF tempPos = m_pixmapItems[id1]->pos();
m_pixmapItems[id1]->setPos(m_pixmapItems[id2]->pos());
m_pixmapItems[id2]->setPos(tempPos);
}
void PaletteGraphicsView::closeInfo()
{
m_infoWindow->close();
m_mousePos = QPoint(0,0);
}
QPoint PaletteGraphicsView::getPixelPos(QPoint pos)
{
return pos + QPoint(this->horizontalScrollBar()->value(), this->verticalScrollBar()->value());
}
void PaletteGraphicsView::setHighlightImage()
{
QImage image = QImage((c_size - 1) * 16 + 1, m_is256ColorMode ? c_size * 16 : c_size, QImage::Format_RGBA8888);
for (int y = 0; y < image.height(); y++)
{
for (int x = 0; x < image.width(); x++)
{
if (x <= 1 || x >= image.width() - 2 || y <= 1 || y >= image.height() - 2)
{
image.setPixel(x, y, qRgb(255, 0, 0));
}
else
{
image.setPixel(x, y, 0);
}
}
}
if (m_highlight)
{
m_highlight->setPixmap(QPixmap::fromImage(image));
}
else
{
m_highlight = m_graphicsScene->addPixmap(QPixmap::fromImage(image));
}
}
QImage PaletteGraphicsView::get256ColorImage(const QImage *imageIndexed)
{
Q_ASSERT(imageIndexed);
QImage image = imageIndexed->convertToFormat(QImage::Format_ARGB32);
for (int y = 0; y < image.height(); y++)
{
for (int x = 0; x < image.width(); x++)
{
if (x == 0 || x % (c_size - 1) == 0 || y == 0 || y % c_size == c_size - 1)
{
image.setPixel(x, y, c_unselected);
}
}
}
return image;
}
void PaletteGraphicsView::enterEvent(QEvent *event)
{
m_timer->start(10);
}
void PaletteGraphicsView::leaveEvent(QEvent *event)
{
m_timer->stop();
closeInfo();
}
void PaletteGraphicsView::mousePressEvent(QMouseEvent *event)
{
if (m_mouseEventEnabled)
{
QPoint pos = getPixelPos(m_mousePos);
if (pos.x() != 0 && pos.x() <= (c_size - 1) * 16)
{
int row = pos.y() / c_size;
int column = (pos.x() - 1) / (c_size - 1);
if (column > 15
|| (!m_is256ColorMode && row >= m_images.size())
|| (m_is256ColorMode && row >= 16))
{
return;
}
int paletteIndex = m_is256ColorMode ? m_index : row;
int colorIndex = m_is256ColorMode ? row * 16 + column : column;
if (paletteIndex < 0)
{
return;
}
if (event->button() == Qt::LeftButton)
{
QImage const* image = m_images[paletteIndex];
Palette const palette = image->colorTable();
QRgb const color = palette[colorIndex];
QColor const newColor = QColorDialog::getColor(QColor(color), this, "Pick a Color");
if (newColor.isValid())
{
replaceColor(paletteIndex, colorIndex, newColor.rgb());
emit colorChanged(paletteIndex, colorIndex, newColor.rgb());
}
}
else if (event->button() == Qt::RightButton)
{
emit paletteContextMenuRequested(paletteIndex, colorIndex, this->mapToGlobal(event->pos()));
}
}
}
QGraphicsView::mousePressEvent(event);
}
void PaletteGraphicsView::mouseMoveEvent(QMouseEvent *event)
{
m_mousePos = event->pos();
updateInfo();
QGraphicsView::mouseMoveEvent(event);
}
void PaletteGraphicsView::updateInfo()
{
if (this->verticalScrollBar()->mapFromGlobal(QCursor::pos()).x() >= 0)
{
closeInfo();
return;
}
QPoint pos = getPixelPos(m_mousePos);
if (pos.x() != 0 && pos.x() <= (c_size - 1) * 16)
{
int row = pos.y() / c_size;
int column = (pos.x() - 1) / (c_size - 1);
if (column > 15
|| (!m_is256ColorMode && row >= m_images.size())
|| (m_is256ColorMode && row >= 16))
{
closeInfo();
return;
}
int paletteIndex = m_is256ColorMode ? m_index : row;
int colorIndex = m_is256ColorMode ? row * 16 + column : column;
if (paletteIndex < 0)
{
closeInfo();
return;
}
QImage* image = m_images[paletteIndex];
Palette const palette = image->colorTable();
QColor color = palette[colorIndex];
m_infoWindow->setColor(color, paletteIndex, colorIndex);
m_infoWindow->show();
m_infoWindow->raise();
m_infoWindow->move(QCursor::pos() + QPoint(2,2));
}
else
{
closeInfo();
}
}