-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmapcanvas.cpp
More file actions
528 lines (460 loc) · 15.7 KB
/
mapcanvas.cpp
File metadata and controls
528 lines (460 loc) · 15.7 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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
#include "mapcanvas.h"
#include "mainwindow.h"
#include "project.h"
#include "toolboxpanel.h"
#include <QMenu>
#include <QMouseEvent>
#include <QAction>
#include <QScrollBar>
#include <QMessageBox>
#include <QLabel>
#include <math.h>
extern Project project;
#define CANVASX_TO_COLUMN(x) (((x-TILEPAD*((x/TILE_W)/scaling))/TILE_W)/scaling)
#define CANVASY_TO_ROW(y) (((y-TILEPAD*((y/TILE_H)/scaling))/TILE_H)/scaling)
#define TILEPAD (draw_tilegrid? 1:0) //You can change these values to manage how thick the grid is, even if it's off
#define CANVAS_HISTORY_MAX 32
MapCanvas::MapCanvas(QScrollArea* parent, int width, int height)
{
size= QSize(width, height);
setScene(&scene);
setParent(parent);
parent->setWidget(this);
setStyleSheet("background-image: url(:/ui/bgtile2);"
"border: "+QString::number(CANVAS_BORDER_W)+"px solid #666;");
setMouseTracking(true);
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
Clear(0);
Redraw();
show();
}
void MapCanvas::Clear(int bgtile)
{
tiles.clear();
for (int i=0; i<size.width()*size.height(); i++)
tiles+= Tile(bgtile);
//Redraw();
}
void MapCanvas::Resize(int width, int height)
{
QList<Tile> new_tiles;
//Try to adapt the old tile list into a new size
for (int iy=0; iy<height; iy++)
{
for (int ix=0; ix<width; ix++)
{
if (iy >= size.height() || ix >= size.width())
{
Tile ttile;
ttile.tileset_offset= 0;
ttile.hflip= 0;
ttile.vflip= 0;
ttile.palette_index= 0;
new_tiles+= ttile;
continue;
}
else
new_tiles+= tiles[ix+iy*size.width()];
}
}
size= QSize(width, height);
tiles= new_tiles;
tiles_history.clear();
history_current_index= 0;
UpdateHistory();
//UpdateScaling();
Redraw();
}
void MapCanvas::Plot(int row, int column, Tile tile)
{
if (row<0 || row>=size.height())
return;
if (column<0 || column>=size.width())
return;
tiles[column+row*size.width()]= tile;
}
MapCanvas::~MapCanvas()
{
close();
}
QSize MapCanvas::Size()
{
return size;
}
void MapCanvas::ZoomIn()
{
if (scaling >= CANVAS_MAX_SCALING)
return;
scaling++;
Redraw();
}
void MapCanvas::ZoomOut()
{
if (scaling <= 1)
return;
scaling--;
Redraw();
}
void MapCanvas::Redraw()
{
scene.clear();
UpdateScaling();
for (int iy=0; iy<size.height(); iy++)
{
for (int ix=0; ix<size.width(); ix++)
{
RedrawTile(iy, ix);
}
}
}
void MapCanvas::RedrawTile(int row, int column)
{
if (row<0 || row>=size.height())
return;
if (column<0 || column>=size.width())
return;
QBrush bru;
bru.setColor(QColor::fromRgb(255,0,0));
bru.setStyle(Qt::DiagCrossPattern);
QPen pen;
pen.setColor(QColor::fromRgb(128,128,128));
pen.setWidth(1);
pen.setStyle(Qt::SolidLine);
Tile* ttile= &tiles[column+row*size.width()];
if (ttile->tileset_offset >= project.tileset.tiles.count() || ttile->tileset_offset < 0)
{
//Tile index is outside the bounds of the tileset
scene.addRect(column*TILE_W*scaling+TILEPAD*column, row*TILE_H*scaling+TILEPAD*row,
TILE_W*scaling+TILEPAD, TILE_H*scaling+TILEPAD, pen, bru);
}
else
{
QPixmap pix;
pix= QPixmap::fromImage(ttile->RenderImage(&project.tileset, project.tileset.isSubPalettedFormat()));
QGraphicsPixmapItem* item= new QGraphicsPixmapItem(pix);
item->setX(column*TILE_W*scaling+TILEPAD*column);
item->setY(row*TILE_H*scaling+TILEPAD*row);
item->setScale(scaling);
scene.addItem(item);
}
}
void MapCanvas::UpdateScaling()
{
setMinimumSize(size.width()*TILE_W*scaling+(CANVAS_BORDER_W*2)+TILEPAD*size.width(),
size.height()*TILE_H*scaling+(CANVAS_BORDER_W*2)+TILEPAD*size.height());
setMaximumSize(this->minimumSize());
scene.setSceneRect(QRect(0,0,size.width()*(TILE_W+TILEPAD)*scaling,size.height()*(TILE_H+TILEPAD)*scaling));
}
void MapCanvas::OpenContextMenu(QPoint screen_pos, QPoint canvas_pos)
{
int tilex= CANVASX_TO_COLUMN(canvas_pos.x());
int tiley= CANVASY_TO_ROW(canvas_pos.y());
int tilen= tilex+tiley*size.width();
if (tilen >= tiles.count())
{
QMessageBox::critical(this, "Error", "Cannot open menu, index out fo range.\r\tThis is a bug!");
return;
}
if (context_menu)
delete context_menu;
context_menu= new QMenu();
context_menu->addAction("Clear tile with background");
connect(context_menu->actions().last(), &QAction::triggered, this, &MapCanvas::onMenuClearWithBgTile_triggered);
context_menu_palette_sel= context_menu->addMenu("Palette index: "+QString::number(tiles[tilen].palette_index)+" (change)");
for (int i=0; i<PALETTE_H; i++)
{
context_menu_palette_sel->addAction(""+QString::number(i));
}
connect(context_menu_palette_sel, &QMenu::triggered, this, &MapCanvas::onMenuChangePal_triggered);
context_menu_palette_sel->setEnabled(project.tileset.isSubPalettedFormat());
context_menu->addAction("Flip tile horizontally");
context_menu->actions().last()->setCheckable(true);
context_menu->actions().last()->setChecked(tiles[tilen].hflip);
connect(context_menu->actions().last(), &QAction::triggered, this, &MapCanvas::onMenuHFlip_triggered);
context_menu->addAction("Flip tile vertically");
context_menu->actions().last()->setCheckable(true);
context_menu->actions().last()->setChecked(tiles[tilen].vflip);
connect(context_menu->actions().last(), &QAction::triggered, this, &MapCanvas::onMenuVFlip_triggered);
context_menu->setWindowModality(Qt::ApplicationModal);
context_menu->setGeometry(QRect(screen_pos,context_menu->sizeHint()));
context_menu->show();
}
void MapCanvas::keyPressEvent(QKeyEvent* event)
{
if (event->modifiers() == Qt::ControlModifier)
this->setCursor(Qt::PointingHandCursor);
}
void MapCanvas::keyReleaseEvent(QKeyEvent* event)
{
if (event->modifiers() == Qt::NoModifier)
this->setCursor(Qt::ArrowCursor);
}
void MapCanvas::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 picked= CANVASX_TO_COLUMN(event->pos().x())+CANVASY_TO_ROW(event->pos().y())*size.width();
if (picked >= tiles.count())
return;
if (mouse_down_button != Qt::RightButton)
{
//QMessageBox::information(project.main_window, "Picked tile", QString::number(picked%size.width())+", "+QString::number(picked/size.width()));
project.tileset_selected_tile= tiles[picked].tileset_offset;
if (project.main_window)
project.main_window->dckTilePicker->Update();
}
if (mouse_down_button != Qt::LeftButton)
{
project.tileset_selected_bgtile= tiles[picked].tileset_offset;
if (project.main_window)
project.main_window->dckTilePicker->Update();
}
}
else
{
if (mouse_down_button != Qt::RightButton)
mouseMoveEvent(event);
}
mouse_has_moved= false;
}
void MapCanvas::mouseMoveEvent(QMouseEvent *event)
{
event->accept();
if ((mouse_down_button == Qt::RightButton || mouse_down_button == Qt::LeftButton) && !(event->modifiers()& Qt::ControlModifier))
for (int iy=-floor((float)project.pen_size/2); iy<ceil((float)project.pen_size/2); iy++)
{
for (int ix=-floor((float)project.pen_size/2); ix<ceil((float)project.pen_size/2); ix++)
{
int tilex= CANVASX_TO_COLUMN(event->pos().x())+ix;
int tiley= CANVASY_TO_ROW(event->pos().y())+iy;
ManagedPlot(tilex, tiley);
}
}
if (mouse_down_button == Qt::MiddleButton)
{
#if QT_VERSION_MAJOR > 5
((QScrollArea*)parent())->scroll(event->globalPosition().x()-mouse_last_global_pos.x(),
event->globalPosition().y()-mouse_last_global_pos.y());
mouse_last_global_pos= event->globalPosition();
#else
((QScrollArea*)parent())->scroll(event->globalPos().x()-mouse_last_global_pos.x(),
event->globalPos().y()-mouse_last_global_pos.y());
mouse_last_global_pos= event->globalPos();
#endif
}
mouse_has_moved= true;
}
void MapCanvas::mouseReleaseEvent(QMouseEvent *event)
{
event->accept();
if (mouse_down_button == Qt::MiddleButton)
this->setCursor(Qt::ArrowCursor);
if (!mouse_has_moved && mouse_down_button == Qt::RightButton && !(event->modifiers()& Qt::ControlModifier))
#if QT_VERSION_MAJOR > 5
OpenContextMenu(event->globalPosition().toPoint(), event->pos());
#else
OpenContextMenu(event->globalPos(), event->pos());
#endif
else if (mouse_down_button == Qt::RightButton || mouse_down_button == Qt::LeftButton)
UpdateHistory();
mouse_down_button= Qt::NoButton;
mouse_has_moved= false;
}
void MapCanvas::ManagedPlot(int tilex, int tiley)
{
if (tilex < 0 || tilex >= size.width())
return;
if (tiley < 0 || tiley >= size.height())
return;
Tile ttile= tiles[tilex+tiley*size.width()];
switch (mouse_down_button)
{
case Qt::LeftButton:
if (project.selected_tools & ToolBoxPanel::tool_OffsetPen
&& project.tileset_selected_tile >= 0)
ttile.tileset_offset= project.tileset_selected_tile;
if (project.selected_tools & ToolBoxPanel::tool_HFlipPen)
ttile.hflip= false;
if (project.selected_tools & ToolBoxPanel::tool_VFlipPen)
ttile.vflip= false;
if (project.selected_tools & ToolBoxPanel::tool_PalettePen)
ttile.palette_index= project.paltable_current_row;
Plot(tiley, tilex, ttile);
RedrawTile(tiley, tilex);
break;
case Qt::RightButton:
if (project.selected_tools & ToolBoxPanel::tool_OffsetPen
&& project.tileset_selected_tile >= 0)
ttile.tileset_offset= project.tileset_selected_bgtile;
if (project.selected_tools & ToolBoxPanel::tool_HFlipPen)
ttile.hflip= true;
if (project.selected_tools & ToolBoxPanel::tool_VFlipPen)
ttile.vflip= true;
if (project.selected_tools & ToolBoxPanel::tool_PalettePen
&& project.tileset.palette.count() > 0)
ttile.palette_index= project.tileset.tiles[ttile.tileset_offset].pixelIndex(0,0)/PALETTE_W;
Plot(tiley, tilex, ttile);
RedrawTile(tiley, tilex);
break;
default:
break;
}
}
void MapCanvas::onMenuClearWithBgTile_triggered()
{
if (project.tileset_selected_bgtile < 0 || project.tileset_selected_bgtile >= project.tileset.tiles.count())
return;
Tile ttile;
ttile.tileset_offset= project.tileset_selected_bgtile;
ttile.hflip= false;
ttile.vflip= false;
ttile.palette_index= project.tileset.tiles[ttile.tileset_offset].pixelIndex(0,0)/PALETTE_W;
int xt= CANVASX_TO_COLUMN(mouse_last_pos.x());
int yt= CANVASY_TO_ROW(mouse_last_pos.y());
Plot(yt, xt, ttile);
RedrawTile(yt, xt);
UpdateHistory();
}
void MapCanvas::onMenuHFlip_triggered()
{
int xt= CANVASX_TO_COLUMN(mouse_last_pos.x());
int yt= CANVASY_TO_ROW(mouse_last_pos.y());
Tile* tile= &tiles[xt+yt*size.width()];
tile->hflip = !tile->hflip;
RedrawTile(yt, xt);
UpdateHistory();
}
void MapCanvas::onMenuVFlip_triggered()
{
int xt= CANVASX_TO_COLUMN(mouse_last_pos.x());
int yt= CANVASY_TO_ROW(mouse_last_pos.y());
Tile* tile= &tiles[xt+yt*size.width()];
tile->vflip = !tile->vflip;
RedrawTile(yt, xt);
}
void MapCanvas::onMenuChangePal_triggered(QAction* selected_action)
{
int xt= CANVASX_TO_COLUMN(mouse_last_pos.x());
int yt= CANVASY_TO_ROW(mouse_last_pos.y());
Tile* tile= &tiles[xt+yt*size.width()];
tile->palette_index= std::strtol(selected_action->text().toLocal8Bit(), NULL, 10);
RedrawTile(yt, xt);
UpdateHistory();
}
QImage MapCanvas::GetImage()
{
QImage timg= QImage(size.width()*TILE_W, size.height()*TILE_H, QImage::Format_Indexed8);
timg.fill(0);
timg.setColorTable(project.tileset.palette);
//Manual rendering of tiles onto a QImage
for (int iyt=0; iyt<size.height(); iyt++)
for (int ixt=0; ixt<size.width(); ixt++)
{
for (int iy=0; iy<TILE_H; iy++)
{
unsigned char* timg_scanline= timg.scanLine(iy+iyt*TILE_H);
int tm_ti= ixt+iyt*size.width();
unsigned char* tile_scanline;
if (!tiles[tm_ti].vflip)
tile_scanline= project.tileset.tiles[tiles[tm_ti].tileset_offset].scanLine(iy);
else
tile_scanline= project.tileset.tiles[tiles[tm_ti].tileset_offset].scanLine(TILE_H-iy-1);
for (int ix=0; ix<TILE_W; ix++)
{
unsigned char pixel= tile_scanline[ix];
if (project.tileset.isSubPalettedFormat())
pixel= tiles[tm_ti].palette_index*PALETTE_W+pixel%PALETTE_W;
if (!tiles[tm_ti].hflip)
timg_scanline[ix+ixt*TILE_W]= pixel;
else
timg_scanline[TILE_W-(ix+1)+ixt*TILE_W]= pixel;
}
}
}
return timg;
}
void MapCanvas::wheelEvent(QWheelEvent *event)
{
event->accept();
if (event->modifiers() == Qt::ControlModifier)
{
if (event->angleDelta().y() > 16)
ZoomIn();
if (event->angleDelta().y() < -16)
ZoomOut();
}
else
{
//This usually works with touchpads
((QScrollArea*)parent())->scroll(event->pixelDelta().x(), event->pixelDelta().y());
}
}
#if QT_VERSION_MAJOR > 5
void MapCanvas::enterEvent(QEnterEvent* event)
#else
void MapCanvas::enterEvent(QEvent* event)
#endif
{
if (!project.statusbar)
return;
project.statusbar->showMessage("Click and drag to draw with the selected tile || Right mouse button for context menu || Ctrl+click to pick the tile under the mouse");
}
void MapCanvas::leaveEvent(QEvent* event)
{
if (!project.statusbar)
return;
project.statusbar->clearMessage();
}
void MapCanvas::UpdateHistory()
{
if (history_current_index >= CANVAS_HISTORY_MAX)
{
history_current_index--;
tiles_history.removeFirst();
}
//Remove all future snapshots
if (history_current_index < tiles_history.count()-1 && tiles_history.count() >= 0)
{
for (int i=0; i<=tiles_history.count()-history_current_index; i++)
tiles_history.removeLast();
}
tiles_history+= tiles;
history_current_index++;
}
void MapCanvas::Undo()
{
if (tiles_history.isEmpty())
return;
if (history_current_index <= 0)
{
history_current_index= 0;
return;
}
history_current_index--;
tiles= tiles_history[history_current_index];
Redraw();
}
void MapCanvas::Redo()
{
if (tiles_history.isEmpty() || history_current_index < 0)
return;
if (history_current_index >= tiles_history.count()-1)
{
history_current_index= tiles_history.count()-1;
return;
}
history_current_index++;
tiles= tiles_history[history_current_index];
Redraw();
}