-
Notifications
You must be signed in to change notification settings - Fork 0
/
spritecanvas.cpp
427 lines (371 loc) · 10.4 KB
/
spritecanvas.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
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
#include "spritecanvas.h"
#include <QInputDialog>
#include <QDebug>
#include <QQueue>
using namespace std;
/**
* Constructor.
* @param parent - Parent widget
*/
SpriteCanvas::SpriteCanvas(QWidget *parent)
: QWidget(parent)
{
bool ok;
gridSize = QInputDialog::getInt(this, tr("Pixel image size"),tr("Grid Size:"), 32, 32, 128, 32, &ok);
image = QImage(gridSize, gridSize, QImage::Format_ARGB32);
onionSkinImage = QImage(gridSize, gridSize, QImage::Format_ARGB32);
//unionSkinImage = QImage(grideSize, gridSize, QImage::Format_ARGB32);
drawingPencil = true;
scribbling = false;
myPenWidth = 1;
myPenColor = Qt::red;
QWidget::setMouseTracking(true);
lastPoint = QPoint(0,0);
lastHighlightedColor = QColor(0,0,0,0);
clear_Image();
}
/*******************************************************
****************** PUBLIC FUNCTIONS *******************
*******************************************************/
/**
* Sets the pen width.
* @param newWidth - Size of the pen
*/
void SpriteCanvas::setPenWidth(int newWidth)
{
myPenWidth = newWidth;
}
/**
* Sets the pen color.
* @param newColor - Color
*/
void SpriteCanvas::setPenColor(const QColor &newColor)
{
myPenColor = newColor;
}
/**
* Sets the grid size.
* @param newGridSize - Grid size
*/
void SpriteCanvas::setGridSize(int newGridSize)
{
newGridSize = gridSize;
}
/**
* Clear the undo stack, delete the undo_cached image.
*/
void SpriteCanvas::clearUndoStack() {
lastHighlighted = QPoint(0,0);
lastHighlightedColor = QColor(0,0,0,0);
currentColor = QColor(0,0,0,0);
undo_stack.clear();
undo_cache = QImage();
emit enableUndoButton(false);
}
/**
* Returns the current image on canvas.
* @return - Image
*/
QImage SpriteCanvas::getImage(){
return image;
}
/*******************************************************
****************** PRIVATE FUNCTIONS ******************
*******************************************************/
/**
* Highlight the point where the mouse is hovering.
* @param currentPoint - Position on the canvas.
*/
void SpriteCanvas::highlightPosition(const QPoint ¤tPoint)
{
QPainter painter(&image);
if(lastPoint != currentPoint)
{
image.setPixelColor(lastPoint,lastHighlightedColor);
painter.setPen(QPen(QColor(250,250,250,150), myPenWidth, Qt::SolidLine, Qt::RoundCap,
Qt::RoundJoin));
painter.drawPoint(currentPoint);
lastHighlightedColor = currentColor;
lastPoint = currentPoint;
update();
//emit updatePreview(&tempImage);
}
}
/**
* Draws a box/square.
* @param releasePoint - The ending cordinates.
*/
void SpriteCanvas::drawBox(const QPoint &releasePoint)
{
QPainter painter(&image);
painter.setPen(QPen(myPenColor, myPenWidth, Qt::SolidLine, Qt::RoundCap,
Qt::RoundJoin));
painter.drawRect(QRect(lastPoint,QPoint(releasePoint.x()-1,releasePoint.y()-1)));
update();
//emit updatePreview(&image);
}
/**
* Draws a line
* @param endPoint - The end position on canvas
*/
void SpriteCanvas::drawLineTo(const QPoint &endPoint)
{
QPainter painter(&image);
if(drawingEraser)
{
image.setPixelColor(endPoint,QColor(0,0,0,0));
lastHighlightedColor = QColor(0,0,0,0);
}
else
{
painter.setPen(QPen(myPenColor, myPenWidth, Qt::SolidLine, Qt::RoundCap,
Qt::RoundJoin));
painter.drawLine(lastPoint, endPoint);
lastHighlightedColor = myPenColor;
}
modified = true;
update();
lastPoint = endPoint;
}
void SpriteCanvas::fillPath(const QPoint ¤tPoint)
{
if(lastHighlightedColor == myPenColor)
{
return;
}
QPainter painter(&image);
painter.setPen(QPen(myPenColor, myPenWidth, Qt::SolidLine, Qt::RoundCap,
Qt::RoundJoin));
painter.drawPoint(currentPoint);
update();
QQueue<QPoint> pixels;
pixels.enqueue(currentPoint);
while(pixels.isEmpty() == 0)
{
QPoint newPoint = pixels.dequeue();
QPoint left((newPoint.x()-1), newPoint.y());
if(left.x() >-1 && left.x() < image.width() && image.pixelColor(left) == lastHighlightedColor)
{
pixels.enqueue(left);
painter.drawPoint(left);
update();
}
QPoint right((newPoint.x()+1), newPoint.y());
if(right.x() > -1 && right.x() < image.width() && image.pixelColor(right) == lastHighlightedColor)
{
pixels.enqueue(right);
painter.drawPoint(right);
update();
}
QPoint up((newPoint.x()), (newPoint.y()-1));
if(up.y() > -1 && up.y() < image.height() && image.pixelColor(up) == lastHighlightedColor)
{
pixels.enqueue(up);
painter.drawPoint(up);
update();
}
QPoint down((newPoint.x()), (newPoint.y()+1));
if(down.y() > -1 && down.y() < image.height() && image.pixelColor(down) == lastHighlightedColor)
{
pixels.enqueue(down);
painter.drawPoint(down);
update();
}
}
lastHighlightedColor = myPenColor;
}
/**
* Helper function to calculate point.
* @param event - Mouse press event.
* @return - The calculated point
*/
QPoint SpriteCanvas::calculatePoint(QMouseEvent *event)
{
return QPoint((event->x()-3)/float(width()-6)*gridSize, (event->y()-3)/float(height()-6)*gridSize);
}
/*******************************************************
************************ SLOTS ************************
*******************************************************/
/**
* Create a new blank image replacing the old one.
*/
void SpriteCanvas::clear_Image()
{
image.fill(QColor(255,255,255,0));
onionSkinImage.fill(QColor(255,255,255,0));
modified = true;
update();
QImage tempImage(image.scaled(131,91));
emit updatePreview(&tempImage);
}
/**
* Set the image of canvas.
* @param newImage - The image to be set.
*/
void SpriteCanvas::set_Image(QImage &newImage) {
clear_Image();
image = QImage(newImage.scaled(gridSize, gridSize));
setFocus();
update();
QImage tempImage(newImage.scaled(131,91));
emit updatePreview(&tempImage);
clearUndoStack();
undo_stack.append(image.copy());
}
/**
* Undoes the last thing that was drawn.
*/
void SpriteCanvas::undo() {
undoWait = true;
if (!undo_stack.isEmpty()) {
undo_stack.removeLast();
clear_Image();
if (!undo_stack.isEmpty()) {
image = undo_stack.last().copy();
} else {
if (!undo_cache.isNull()) {
image = undo_cache.copy();
}
emit enableUndoButton(false);
}
}
QImage tempImage(image.scaled(131,91));
emit updatePreview(&tempImage);
}
void SpriteCanvas::on_onion_skin_frame(QImage & image)
{
onionSkinImage = image;
unsigned int rgb;
for(int y=0;y<onionSkinImage.height();y++){
for(int x=0;x<onionSkinImage.width();x++){
rgb=onionSkinImage.pixel(x,y);
if(rgb == 16777215)
{
continue;
}
onionSkinImage.setPixel(x,y,qRgba(qRed(rgb),qGreen(rgb),qBlue(rgb),120));
}
}
update();
}
void SpriteCanvas::on_onion_skin_clear()
{
onionSkinImage.fill(QColor(255,255,255,0));
update();
}
/*******************************************************
********************** PROTECTED **********************
*******************************************************/
/**
* @brief SpriteCanvas::mousePressEvent
* @param event
*/
/* Track the mouse when the left button is pressed.
* @param event - Mouse press event.
*/
void SpriteCanvas::mousePressEvent(QMouseEvent *event)
{
if(event->button() == Qt::LeftButton)
{
lastPoint = calculatePoint(event);
scribbling = true;
startClick = lastPoint;
if(drawingPencil)
drawLineTo(lastPoint);
}
}
/**
* Keep tracking the mouse press.
* @param event - Mouse move event.
*/
void SpriteCanvas::mouseMoveEvent(QMouseEvent *event)
{
QPoint mousePoint = calculatePoint(event);
if(event->buttons() == Qt::LeftButton && drawingPencil)
drawLineTo(mousePoint);
if(!scribbling)
{
currentColor = image.pixelColor(mousePoint);
image.setPixelColor(mousePoint,currentColor);
highlightPosition(mousePoint);
}
}
/**
* Calculate what was drawn when the mouse press is released.
* @param event - Mouse press release
*/
void SpriteCanvas::mouseReleaseEvent(QMouseEvent *event)
{
if (event->button() == Qt::LeftButton && drawingPencil) {
startClick= calculatePoint(event);
drawLineTo(calculatePoint(event));
}
else if(event->button() == Qt::LeftButton && drawingSquare)
{
drawBox(calculatePoint(event));
lastHighlightedColor = penColor();
}
else if(event->button() == Qt::LeftButton && filling)
{
fillPath(calculatePoint(event));
neighbors.clear();
visited.clear();
}
// Store a copy of image to undo stack
undo_stack.append(image.copy());
// Only keep 5 undos
if (undo_stack.size() > 5) {
undo_cache = undo_stack.first().copy();
undo_stack.removeFirst();
}
// Enable the undo button
emit enableUndoButton(true);
scribbling = false;
QImage tempImage(image.scaled(131,91));
emit updatePreview(&tempImage);
}
/**
* Point click event, draw where the mouse was clicked.
* @param event - Paint event
*/
void SpriteCanvas::paintEvent(QPaintEvent *event)
{
QStyleOption opt;
opt.init(this);
QPainter p(this);
style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing);
painter.drawImage(this->rect().marginsAdded(QMargins(-3,-3,-3,-3)), onionSkinImage);
painter.drawImage(this->rect().marginsAdded(QMargins(-3,-3,-3,-3)), image);
}
/**
* Resize the current image on canvas.
* @param event 0 Resize event
*/
void SpriteCanvas::resizeEvent(QResizeEvent *event)
{
update();
QImage tempImage(image.scaled(131,91));
emit updatePreview(&tempImage);
QWidget::resizeEvent(event);
}
/**
* Leave Event
* @param event
*/
void SpriteCanvas::leaveEvent(QEvent *event)
{
image.setPixelColor(lastPoint,lastHighlightedColor);
}
/**
* Delete key press event.
* @param event Key event
*/
void SpriteCanvas::keyPressEvent(QKeyEvent *event)
{
if(event->key() == Qt::Key_Backspace)
{
emit deleteKeyPressed(event);
}
}