-
Notifications
You must be signed in to change notification settings - Fork 0
/
myscene.cpp
executable file
·550 lines (467 loc) · 17.8 KB
/
myscene.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
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
529
530
531
532
533
534
535
536
537
538
539
540
#include "myscene.h"
#include "mainwindow.h"
int ZValueCursorImage;
MyScene::MyScene(int newPixle, QString nonePath, int sceneWidth)
{
//set values
ZValueCursorImage = 11;
m_pixle = newPixle;
m_none = nonePath;
m_sceneWidth = sceneWidth;
m_shift = false;
m_shiftLeftCorner = QPointF(-1,-1);
m_background.append(this->addPixmap(NULL));
m_cursorImage = this->addRect(0,0,0,0);
// set shift rectangle
QPen *myPen = new QPen();
myPen->setColor(Qt::green);
myPen->setWidth(2);
m_shiftRect = this->addRect(0,0,1,1,*myPen,Qt::lightGray);
m_shiftRect->setZValue(ZValueCursorImage);
m_shiftRect->setOpacity(0.4f);
m_shiftRect->setVisible(false);
//paint grid into scene
makeGrid();
}
MyScene::~MyScene()
{
delete m_cursorImage;
delete m_shiftRect;
}
void MyScene::makeGrid()
{
//Make a grid with a little opacity
QPen gPen(Qt::lightGray);
for(int i = m_pixle ; i < m_sceneWidth; i = i +m_pixle)
{
m_grid.append(this->addLine(i,0,i,600, gPen));
m_grid.last()->setOpacity(0.2f);
}
for(int i = m_pixle; i < 600; i = i +m_pixle)
{
m_grid.append(this->addLine(0,i,m_sceneWidth,i,gPen));
m_grid.last()->setOpacity(0.2f);
}
}
void MyScene::showHideGrid(bool show)
{
//show or hide grid lines
if(show){
for(int index = 0; index < m_grid.length();index++)
m_grid[index]->show();
}
else{
for(int index = 0; index < m_grid.length();index++)
m_grid[index]->hide();
}
}
void MyScene::removeNone(){
//remove none images from list
for(int index = 0; index < m_images.length();index++){
if(m_images[index]->data(0).toString() == m_none){
this->removeItem(m_images[index]);
m_images.removeAt(index);
--index;
}
}
}
void MyScene::paintImagesRect(QPointF leftCorner, QPointF rightCorner){
//put pictures into the scene into the rectangle (leftCorner x rightCorner)
int helper;
//set rights corners
if(leftCorner.x() > rightCorner.x()){
helper = rightCorner.x();
rightCorner.setX(leftCorner.x());
leftCorner.setX(helper);
}
if(leftCorner.y() > rightCorner.y()){
helper = rightCorner.y();
rightCorner.setY(leftCorner.y());
leftCorner.setY(helper);
}
//take all space and detect if can put a new item here
for(int y = leftCorner.y();y < rightCorner.y();y = y + m_cursorImage->rect().height()){
for(int x = leftCorner.x();x < rightCorner.x();x = x + m_cursorImage->rect().width()){
int index;
bool canPut = true;
//if there is no picture in the way add picture else none
//take all pictures from list of added pictures and check if there can put
for(index = 0; index < m_images.length();index++){
if(m_images[index]->data(1)!=m_zValue)
continue;
if(m_images[index]->pos().x() > x &&
m_images[index]->pos().x() < x + m_cursorImage->rect().width()
|| (m_images[index]->pos().x() + m_images[index]->pixmap().width() > x
&& m_images[index]->pos().x() < x)
|| m_images[index]->pos().x() == x){
if(m_images[index]->pos().y() > y &&
m_images[index]->pos().y() < y + m_cursorImage->rect().height()
|| (m_images[index]->pos().y() + m_images[index]->pixmap().height() > y
&& m_images[index]->pos().y() < y)
|| m_images[index]->pos().y() == y){
canPut = false;
break;
}
}
}
//can i put a picture put else none
if(canPut){
m_images.append(this->addPixmap(m_imagePath));
m_images.last()->setZValue(m_zValue);
m_images.last()->setData(0, m_imagePath);
m_images.last()->setPos(QPoint(x,y));
m_images.last()->setData(1,m_zValue);
//if the layer where is picture is invisible set invisible to picture
if(m_visibleLayer!=m_zValue && m_visibleLayer!=0)
m_images.last()->setVisible(false);
}
}
}
m_saved = false;
}
void MyScene::removeImagesRect(QPointF leftCorner, QPointF rightCorner){
//remove all pictures in the rectangle(leftCorner x rightCorner)
int helper;
//set rights corners
if(leftCorner.x() > rightCorner.x()){
helper = rightCorner.x();
rightCorner.setX(leftCorner.x());
leftCorner.setX(helper);
}
if(leftCorner.y() > rightCorner.y()){
helper = rightCorner.y();
rightCorner.setY(leftCorner.y());
leftCorner.setY(helper);
}
//remove all pictures in area and right ZValue
for(int y = leftCorner.y();y < rightCorner.y();y += m_pixle){
for(int x = leftCorner.x();x < rightCorner.x();x += m_pixle){
for(int index = 0; index < m_images.length(); index++){
if(m_images[index]->data(1)!=m_zValue)
continue;
if(m_images[index]->pos() == QPointF(x,y)){
this->removeItem(m_images[index]);
m_images.removeAt(index);
break;
}
}
}
}
m_saved = false;
}
void MyScene::setImage(QString path){
//set path to Image in resources and set the crusorRectangel
m_imagePath = path;
QPen *myPen = new QPen();
myPen->setWidth(2);
QPixmap newImage;
//if user use rubber set specific values else normal
if(m_imagePath==":/images/rubber"){
myPen->setColor(Qt::red);
newImage.load(m_none);
m_shiftRect->setPen(*myPen);
m_eraser = true;
}
else{
myPen->setColor(Qt::green);
newImage.load(m_imagePath);
m_shiftRect->setPen(*myPen);
m_eraser = false;
}
//set cursor image information and hide shift rectangle
m_cursorImage->setRect(0,0,newImage.width(),newImage.height());
m_cursorImage->setBrush(Qt::white);
m_cursorImage->setPen(*myPen);
m_cursorImage->setOpacity(0.4f);
m_cursorImage->setZValue(ZValueCursorImage);
shiftRectangle(false);
}
void MyScene::mousePressEvent(QGraphicsSceneMouseEvent *event){
if(event->button() == Qt::LeftButton){
//if eraser is active erase images else add images
if(m_eraser){
//erase image
if(m_shift){
removeImagesRect(m_shiftLeftCorner,m_cursor);
}
else{
removeImagesRect(m_cursor,m_cursor);
}
}
else{
if(m_shift){
paintImagesRect(m_shiftLeftCorner,m_cursor);
}
else{
int index;
bool canPut = true;
//check if can put selected item on a selected place
for(index = 0; index < m_images.length();index++){
if(m_images[index]->data(1)!=m_zValue)
continue;
if(m_images[index]->pos().x() > m_cursorImage->pos().x() &&
m_images[index]->pos().x() < m_cursorImage->pos().x() + m_cursorImage->rect().width()
|| (m_images[index]->pos().x() + m_images[index]->pixmap().width() > m_cursorImage->pos().x()
&& m_images[index]->pos().x() < m_cursorImage->pos().x()
)
|| m_images[index]->pos().x() == m_cursorImage->pos().x()){
if(m_images[index]->pos().y() > m_cursorImage->pos().y() &&
m_images[index]->pos().y() < m_cursorImage->pos().y() + m_cursorImage->rect().height()
|| (m_images[index]->pos().y() + m_images[index]->pixmap().height() > m_cursorImage->pos().y()
&& m_images[index]->pos().y() < m_cursorImage->pos().y()
)
|| m_images[index]->pos().y() == m_cursorImage->pos().y()){
canPut = false;
break;
}
}
}
/*
// alternative algorithm
m_cursorImage->setVisible(false);
if(this->itemAt(m_cursorImage->pos().x(),m_cursorImage->pos().y())->zValue()!=0){
canPut=false;
QMessageBox::information(0,"prd", QString::number(this->itemAt(m_cursorImage->pos().x(),m_cursorImage->pos().y())->zValue()));
}
m_cursorImage->setVisible(true);
*/
if(canPut){
m_images.append(this->addPixmap(m_imagePath));
m_images.last()->setZValue(m_zValue);
m_images.last()->setData(1,m_zValue);
m_images.last()->setData(0, m_imagePath);
m_images.last()->setPos(m_cursor);
if(m_visibleLayer!=m_zValue && m_visibleLayer!=0)
m_images.last()->setVisible(false);
}
}
}
m_saved = false;
}
}
void MyScene::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
//changes position of cursorImage depends on cursor position
m_cursor.setX(event->scenePos().x());
m_cursor.setY(event->scenePos().y());
//if shift rectangle is visible set rectangle else set cursor image
if(m_shiftRect->isVisible()){
//rectangle size increase or decrease by cursor image size
// if image is 40x40 the size of rectangle increase/decrease by size 40
//size increase by steps
int intRemainder = ((int(m_cursor.x()) - int(m_shiftLeftCorner.x())) % int(m_cursorImage->rect().width()));
if(m_cursor.x()>m_shiftLeftCorner.x())
m_cursor.setX((int(m_cursor.x()) - intRemainder)+m_cursorImage->rect().width());
else
m_cursor.setX((int(m_cursor.x()) - intRemainder)-m_cursorImage->rect().width());
intRemainder =(int(int(m_cursor.y()) - int(m_shiftLeftCorner.y())) % int(m_cursorImage->rect().height()));
if(m_cursor.y()>m_shiftLeftCorner.y())
m_cursor.setY((int(m_cursor.y()) - intRemainder)+m_cursorImage->rect().height());
else
m_cursor.setY((int(m_cursor.y()) - intRemainder)-m_cursorImage->rect().height());
//control if curosr isnt out of screen
if(m_cursor.x()>m_sceneWidth)
m_cursor.setX(m_cursor.x()-m_cursorImage->rect().width());
else if(m_cursor.x()<0)
m_cursor.setX(m_cursor.x()+m_cursorImage->rect().width());
if(m_cursor.y()>600)
m_cursor.setY(m_cursor.y()-m_cursorImage->rect().height());
else if(m_cursor.y()<0)
m_cursor.setY(m_cursor.y()+m_cursorImage->rect().height());
//set rectangle rights points
QRect rectangle;
if(m_shiftLeftCorner.x() < m_cursor.x()){
if(m_shiftLeftCorner.y() < m_cursor.y())
rectangle.setCoords(m_shiftLeftCorner.x(),m_shiftLeftCorner.y(),m_cursor.x(), m_cursor.y());
else
rectangle.setCoords(m_shiftLeftCorner.x(),m_cursor.y(),m_cursor.x(), m_shiftLeftCorner.y());
}
else{
if(m_shiftLeftCorner.y() < m_cursor.y())
rectangle.setCoords(m_cursor.x(),m_shiftLeftCorner.y(),m_shiftLeftCorner.x(), m_cursor.y());
else
rectangle.setCoords(m_cursor.x(),m_cursor.y(),m_shiftLeftCorner.x(), m_shiftLeftCorner.y());
}
m_shiftRect->setRect(rectangle);
}
else{
//if shift rectangle is invisible set cursor image
//move by steps
if((int(m_cursor.x()) % m_pixle) != 0)
m_cursor.setX(int(m_cursor.x()) - (int(m_cursor.x()) % m_pixle));
if((int(m_cursor.y()) % m_pixle) != 0)
m_cursor.setY(int(m_cursor.y()) - (int(m_cursor.y()) % m_pixle));
//picture cant get out of screen
if(m_cursor.x()<0)
m_cursor.setX(0);
if(m_cursor.y()<0)
m_cursor.setY(0);
if((m_cursor.x())+m_cursorImage->rect().width()>m_sceneWidth)
m_cursor.setX(m_sceneWidth-m_cursorImage->rect().width());
if((m_cursor.y())+m_cursorImage->rect().height()>600)
m_cursor.setY(600-m_cursorImage->rect().height());
m_cursorImage->setPos(m_cursor);
}
}
void MyScene::keyPressEvent(QKeyEvent *event)
{
//set that shift is active
if(event->key() == Qt::Key_Shift)
shiftRectangle(true);
}
void MyScene::keyReleaseEvent(QKeyEvent *event)
{
//set that shift is inactive
if(event->key() == Qt::Key_Shift)
shiftRectangle(false);
}
void MyScene::showHideBackgroudImage(bool show){
//show hide bacground
if(show){
for(int index = 0; index < m_background.length();index++)
m_background[index]->setVisible(true);
}
else{
for(int index = 0; index < m_background.length();index++)
m_background[index]->setVisible(false);
}
}
void MyScene::setBackground(QString backgroundPath, bool repeat){
//set new background
QPixmap newBackground;
newBackground.load(backgroundPath);
for(int i = 0; i < m_background.length(); i++)
this->removeItem(m_background[i]);
m_background.clear();
if(repeat){
for(int number = 0;number < int(m_sceneWidth / newBackground.width())+1;number++){
m_background.append(this->addPixmap(newBackground));
m_background.last()->setData(0,backgroundPath);
m_background[number]->setPos(int(newBackground.width()*number), 0);
m_background[number]->setZValue(-1);
}
}
else{
m_background.append(this->addPixmap(newBackground));
m_background.last()->setData(0,backgroundPath);
m_background.last()->setZValue(-1);
}
m_saved = false;
}
void MyScene::shiftRectangle(bool shift){
//set shift rectangle or unset and set cursor image
if(shift){
m_shiftLeftCorner = m_cursor;
m_shiftRect->setVisible(true);
m_cursorImage->setVisible(false);
m_shiftRect->setRect(m_cursor.x(), m_cursor.y(),m_cursorImage->rect().width(),m_cursorImage->rect().height());
m_shift = true;
}
else{
m_shiftRect->setVisible(false);
m_cursorImage->setVisible(true);
m_shift = false;
m_shiftLeftCorner = QPointF(-1,-1);
}
}
void MyScene::setItemZValue(int zValue){
m_zValue = zValue;
}
void MyScene::showLayer(int layer){
//hide images in unselected layer
//show all images if layer is 0
if(layer==0){
for(int i = 0; i<m_images.length();i++)
m_images[i]->setVisible(true);
}
else{
for(int i = 0; i<m_images.length();i++){
if(m_images[i]->data(1).toInt()==layer)
m_images[i]->setVisible(true);
else
m_images[i]->setVisible(false);
}
}
m_visibleLayer=layer;
}
void MyScene::clearlyNewScene(int sceneWidth, QString mapName, QString info){
m_mapName = mapName;
m_info = info;
m_sceneWidth = sceneWidth;
//remove all images from scene and list of images
for(int i = 0; i<m_images.length();i++)
this->removeItem(m_images[i]);
m_images.clear();
//reset grid
for(int i = 0; i<m_grid.length();i++)
this->removeItem(m_grid[i]);
m_grid.clear();
makeGrid();
m_saved = false;
}
void MyScene::fill(){
//fill images into scene or remove all images if rubber
if(m_imagePath==":/images/rubber")
removeImagesRect(QPoint(0,0),QPoint(m_sceneWidth,600));
else
paintImagesRect(QPoint(0,0),QPoint(m_sceneWidth,600));
}
bool MyScene::saveMap(QString mapPath){
//map informations
m_map.setMap(m_mapName,m_sceneWidth,m_info);
//background informations
bool repeat;
if(m_background.count() == 1)
repeat = false;
else
repeat = true;
//define folder with images
QString folderName = mapPath.split("/").last();
folderName.remove(".map",Qt::CaseSensitive);
folderName.append("_images");
m_map.setBackground(m_background.first(),repeat,folderName);
removeNone();
//items informations
m_map.setItems(m_images,folderName);
//save map
return m_map.saveFile(mapPath);
}
void MyScene::setInfo(QString mapName, QString info){
//set informations about map
m_mapName = mapName;
m_info = info;
m_saved = false;
}
bool MyScene::loadMap(QString mapPath){
//load map
if(m_map.loadFile(mapPath))
{
//if map has been loaded clean scene andset new values
clearlyNewScene(m_map.getWidth(),m_map.getName(),m_map.getInfo());
//set new background
setBackground(m_map.getBgPath(), m_map.getBgRepeat());
//paint images
for(int i = 0 ; i < m_map.getItemsCount();i++){
m_images.append(this->addPixmap(m_map.getItemPath(i)));
m_images.last()->setZValue(m_map.getItemZ(i));
m_images.last()->setData(1, m_map.getItemZ(i));
m_images.last()->setData(0, m_map.getItemPath(i));
m_images.last()->setPos(QPointF(m_map.getItemX(i),m_map.getItemY(i)));
}
//return true if OK
return true;
}
else
return false;
}
bool MyScene::getBgRepeat(){
//return if background is repeated
if(m_background.count()==1)
return false;
else
return true;
}
void MyScene::setBgIndividualy(QString bgPath, int repeat){
//set background
setBackground(bgPath, repeat);
}