-
Notifications
You must be signed in to change notification settings - Fork 1
/
paletteview.cpp
70 lines (60 loc) · 2.05 KB
/
paletteview.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
#include "paletteview.h"
PaletteView::PaletteView(QGraphicsScene* ogscene) : QGraphicsView(ogscene) {
setWindowTitle("Palette");
setHorizontalScrollBarPolicy(Qt::ScrollBarPolicy::ScrollBarAlwaysOff);
setVerticalScrollBarPolicy(Qt::ScrollBarPolicy::ScrollBarAlwaysOff);
}
QPoint PaletteView::convertPointToTile(const QPointF& point) {
int intx = point.toPoint().x();
int inty = point.toPoint().y();
// this might *seem* like a useless calculation but in reality without this it doesn't work properly
// yay for rounding
return QPoint(intx - (intx % 16), inty - (inty % 16));
}
void PaletteView::mousePressEvent(QMouseEvent *event) {
if (event->button() == Qt::RightButton) {
return;
}
QImage m = currentItem->pixmap().toImage();
QPainter p{&m};
p.setBrush(QBrush(Qt::BrushStyle::SolidPattern));
QColor color = QColorDialog::getColor(m.pixelColor(event->position().toPoint()), this);
if (!color.isValid())
return;
QPoint colorIndex = convertPointToTile(event->position());
SpritePaletteCreator::changePaletteColor(color, colorIndex);
emit paletteChanged();
p.fillRect(QRect{colorIndex, QSize(16, 16)}, color);
currentItem->setPixmap(QPixmap::fromImage(m));
event->accept();
}
void PaletteView::open() {
m_open = true;
show();
raise();
}
void PaletteView::updateForChange(const QPixmap& image) {
if (currentItem) {
scene()->removeItem(currentItem);
currentItem->~QGraphicsPixmapItem();
}
currentItem = new QGraphicsPixmapItem(image);
currentItem->setAcceptHoverEvents(true);
currentItem->setFlag(QGraphicsItem::ItemIgnoresTransformations);
scene()->addItem(currentItem);
setFixedSize(256, 128);
}
void PaletteView::close(QCloseEvent* event) {
closeEvent(event);
}
QGraphicsPixmapItem* PaletteView::getCurrentItem() {
return currentItem;
}
void PaletteView::closeEvent(QCloseEvent* event) {
m_open = false;
event->accept();
}
PaletteView::~PaletteView() {
qDebug() << "PaletteView destructor called";
delete currentItem;
}