-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcanvas.cpp
More file actions
89 lines (76 loc) · 2.42 KB
/
canvas.cpp
File metadata and controls
89 lines (76 loc) · 2.42 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
#include "canvas.h"
#include "simulator.h"
#include <QPainter>
#include <QPaintEvent>
#include <QMessageBox>
#include<QDebug>
Canvas::Canvas() : QOpenGLWidget()
{
trigger = false;
whiteBackground = new QBrush(Qt::white);
black = new QPen(Qt::black);
setFixedSize(fixed_size-2,fixed_size-2);
setAutoFillBackground(true);
paintEvent(nullptr);
}
void Canvas::paintEvent(QPaintEvent *event)
{
QPainter painter;
painter.begin(this);
painter.setRenderHint(QPainter::Antialiasing);
painter.fillRect(event->rect(), Qt::white);
updateGrid(&painter);
paintGrid(&painter, Data::grid_size);
painter.end();
emit paintEnd();
if (trigger) {
emit paintEndedThread();
}
}
void Canvas::paintGrid(QPainter *painter, int n){
double pas = fixed_size/(n);
for (int i = 0 ; i<n+1 ; i++){
painter->drawLine(0, pas*i, fixed_size, pas*i);
for (int j = 0; j<n+1; j++) {
painter->drawLine(pas*i, 0, pas*i, fixed_size);
}
}
}
void Canvas::updateGrid(QPainter *painter){
double pas = fixed_size/Data::grid_size;
for (int i = 0 ; i < Data::grid_size; i++) {
for (int j = 0 ; j < Data::grid_size; j++) {
painter->fillRect(i*pas, j*pas, pas, pas, Data::state_colors[Data::grid_state[i][j]]);
}
}
}
void Canvas::mousePressEvent(QMouseEvent *event) {
if (!Data::isStarted) {
int pas = fixed_size/Data::grid_size;
Data::grid_state[event->x()/pas][event->y()/pas] = Data::currently_selected_state;
switch (Data::currently_selected_state){
case Data::STATE_GROUND:
Data::setupGround(event->x()/pas, event->y()/pas);
break;
case Data::STATE_GRASS:
Data::setupGrass(event->x()/pas, event->y()/pas);
break;
case Data::STATE_TREES:
Data::setupTree(event->x()/pas, event->y()/pas);
break;
case Data::STATE_WATER:
Data::setupWater(event->x()/pas, event->y()/pas);
break;
case Data::STATE_ON_FIRE:
Data::setupFire(event->x()/pas, event->y()/pas);
}
repaint();
}
}
int Canvas::caseNumber(int xpos, int ypos, int grid_size, int widget_size) {
return xpos/(widget_size/grid_size)+ypos/(widget_size/grid_size)*grid_size;
}
void Canvas::refresh() {
trigger = true;
repaint();
}