-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtilecanvas.cpp
More file actions
246 lines (207 loc) · 6.37 KB
/
tilecanvas.cpp
File metadata and controls
246 lines (207 loc) · 6.37 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
#include "tilecanvas.h"
#include "mainwindow.h"
#include "tile.h"
#include "project.h"
#include <QMenu>
#include <QMouseEvent>
#include <QAction>
#include <QScrollBar>
#include <QMessageBox>
#define TILECANVASX_TO_PIXEL(x) (((x-x/(image.width()*scaling)*TILECANVAS_TILEPAD)/scaling)%image.width())
#define TILECANVASY_TO_PIXEL(y) (((y-y/(image.height()*scaling)*TILECANVAS_TILEPAD)/scaling)%image.height())
#define TILECANVAS_TILEPAD (draw_tilegrid? 1:0)
#define TILECANVAS_HISTORY_MAX 32
extern Project project;
TileCanvas::TileCanvas(QScrollArea* parent, int tile_id, int w, int h)
{
image= QImage(w, h, QImage::Format_Indexed8);
setScene(&scene);
setParent(parent);
parent->setWidget(this);
setStyleSheet("background-image: url(:/ui/bgtile2);"
"border: "+QString::number(TILECANVAS_BORDER_W)+"px solid black;");
setMouseTracking(true);
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
if (tile_id < 0 || tile_id >= project.tileset.tiles.count())
{
QMessageBox::critical(this, "Error", "Target tile id is out of bounds");
return;
}
image= project.tileset.tiles[tile_id];
this->tile_id= tile_id;
Redraw();
show();
//parent->scroll(image.width()*scaling/2, image.height()*scaling/2);
}
TileCanvas::~TileCanvas() {};
void TileCanvas::Clear(int color)
{ image.fill(color); }
void TileCanvas::Plot(int x, int y, int color)
{
if (x<0 || x>=image.width())
return;
if (y<0 || y>=image.height())
return;
image.setPixel(x, y, color);
Redraw();
}
void TileCanvas::UpdateHistory() {} //Todo
void TileCanvas::Undo() {} //Todo
void TileCanvas::Redo() {} //Todo
void TileCanvas::ZoomIn()
{
if (scaling >= TILECANVAS_MAX_SCALING)
{
scaling= TILECANVAS_MAX_SCALING;
return;
}
scaling+=4;
Redraw();
}
void TileCanvas::ZoomOut()
{
if (scaling <= 4)
{
scaling= 4;
return;
}
scaling-=4;
Redraw();
}
void TileCanvas::Redraw()
{
UpdateScaling();
image.setColorTable(project.tileset.tiles[tile_id].colorTable());
//Check if an update is necessary
if (image == last_image && scaling == last_scaling)
return;
else
last_image= image, last_scaling= scaling;
scene.clear();
QPixmap pix;
pix= QPixmap::fromImage(image);
int iterx= fill_screen? scene.width()/scaling : 1;
int itery= fill_screen? scene.height()/scaling : 1;
for (int iy=0; iy<itery; iy++)
{
for (int ix=0; ix<iterx; ix++)
{
QGraphicsPixmapItem* item= new QGraphicsPixmapItem(pix);
item->setX(ix*(image.width()*scaling+TILECANVAS_TILEPAD));
item->setY(iy*(image.height()*scaling+TILECANVAS_TILEPAD));
item->setScale(scaling);
scene.addItem(item);
}
}
}
void TileCanvas::mousePressEvent(QMouseEvent* event)
{
//event->accept();
mouse_down_button= event->button();
mouse_last_pos= event->pos();
#if QT_VERSION_MAJOR > 5
mouse_last_global_pos= event->globalPosition();
#else
mouse_last_global_pos= event->globalPos();
#endif
if (mouse_down_button == Qt::MiddleButton)
this->setCursor(Qt::ClosedHandCursor);
if (event->modifiers()& Qt::ControlModifier)
{
int color_picked= image.pixelIndex(TILECANVASX_TO_PIXEL(event->pos().x()), TILECANVASY_TO_PIXEL(event->pos().y()));
if (mouse_down_button != Qt::RightButton)
{
//QMessageBox::information(project.main_window, "Picked tile", QString::number(picked%size.width())+", "+QString::number(picked/size.width()));
project.paltable_current_column = color_picked%PALETTE_W;
project.paltable_current_row = color_picked/PALETTE_W;
if (project.main_window)
project.main_window->dckPaletteEdit->on_tblPalette_cellClicked(project.paltable_current_row, project.paltable_current_column);
}
}
else
{
if (mouse_down_button != Qt::RightButton)
mouseMoveEvent(event);
}
mouse_has_moved= false;
}
void TileCanvas::mouseMoveEvent(QMouseEvent* event)
{
event->accept();
if (mouse_down_button == Qt::LeftButton)
{
int tilex= TILECANVASX_TO_PIXEL(event->pos().x());
int tiley= TILECANVASY_TO_PIXEL(event->pos().y());
Plot(tilex, tiley, project.paltable_current_column+project.paltable_current_row*PALETTE_W);
}
}
void TileCanvas::mouseReleaseEvent(QMouseEvent* event)
{
event->accept();
UpdateSourceTile();
mouse_down_button= Qt::NoButton;
mouse_has_moved= false;
}
void TileCanvas::wheelEvent(QWheelEvent* event) {} //Todo
#if QT_VERSION_MAJOR > 5
void TileCanvas::enterEvent(QEnterEvent* event)
#else
void TileCanvas::enterEvent(QEvent* event)
#endif
{
if (!project.statusbar)
return;
project.statusbar->showMessage("Click and drag to draw with the selected color || Ctrl+click to pick the pixel color under the mouse");
}
void TileCanvas::leaveEvent(QEvent* event)
{
if (!project.statusbar)
return;
project.statusbar->clearMessage();
}
void TileCanvas::keyPressEvent(QKeyEvent* event)
{
if (event->modifiers() == Qt::ControlModifier)
this->setCursor(Qt::PointingHandCursor);
}
void TileCanvas::keyReleaseEvent(QKeyEvent* event)
{
if (event->modifiers() == Qt::NoModifier)
this->setCursor(Qt::ArrowCursor);
}
void TileCanvas::UpdateScaling()
{
if (!fill_screen)
{
setMinimumSize(image.width()*scaling+(TILECANVAS_BORDER_W*2),
image.height()*scaling+(TILECANVAS_BORDER_W*2));
setMaximumSize(this->minimumSize());
scene.setSceneRect(QRect(0,0,image.width()*scaling,image.height()*scaling));
}
else
{
setMinimumSize(((QScrollArea*)parent())->size());
setMaximumSize(this->minimumSize());
scene.setSceneRect(QRect(0,0,this->maximumWidth(),this->maximumHeight()));
}
}
void TileCanvas::UpdateTileId(int new_tile_id)
{
tile_id= new_tile_id;
UpdateMyTile();
}
void TileCanvas::UpdateSourceTile()
{
if (tile_id < 0 || tile_id >= project.tileset.tiles.count())
{
QMessageBox::critical(this, "Error", "Target tile id is out of bounds");
return;
}
project.tileset.tiles[tile_id]= image;
}
void TileCanvas::UpdateMyTile()
{
image= project.tileset.tiles[tile_id];
Redraw();
}