-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrid.cpp
166 lines (120 loc) · 4.26 KB
/
grid.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
#include <algorithm>
#include <iostream>
#include <vector>
#include <QDebug>
#include <QtGui>
#include <QtWidgets>
#include "grid.h"
#include "pathfinding.h"
//todo: private aaaand declared on the header.
int DEFAULT_MOUSE_STATE = 0;
int MOUSE_DRAG_STATE = 1;
AAGrid::AAGrid() {
int currentMouseState = DEFAULT_MOUSE_STATE;
paintingState = STATES::BLANK;
currentScene = new QGraphicsScene;
setScene(currentScene);
this->setRenderHint(QPainter::HighQualityAntialiasing, true);
this->setRenderHint(QPainter::SmoothPixmapTransform, true);
this->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
this->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
this->scene()->setSceneRect(0, 0, WIDTH, HEIGHT);
for (int x = currentScene->sceneRect().left(); x < currentScene->sceneRect().right(); x = x + NODE_SIZE)
{
for (int y = currentScene->sceneRect().top(); y < currentScene->sceneRect().bottom(); y = y + NODE_SIZE)
{
AAGrid::createNode(x, y, NODE_SIZE);
}
}
// Once all nodes are creater we iterate over them to assign their neighbours.
for (auto i : hashesNodeMapping)
setNeighbours(i.second);
};
void AAGrid::createNode(int inPosX, int inPosY, int inGridSize)
{
auto* aaNode = new AANode(inPosX, inPosY, inGridSize);
scene()->addItem( aaNode );
hashesNodeMapping.insert({aaNode->hashCode(), aaNode});
}
void AAGrid::setGoalNode(AANode *inNode) {
if (inNode->getCurrentState() == STATES::GOAL)
return;
inNode->switchGoalNode();
if (goalNodes.size() == 2){
AANode* nodeToRemove = goalNodes.at( 0 );
nodeToRemove->setCurrentState( STATES::BLANK );
goalNodes.erase( goalNodes.begin() + 0 );
}
goalNodes.push_back(inNode);
}
void AAGrid::setNeighbours(AANode* inNode) {
std::vector<AANode*> neighbourNodes;
for (int x = -1; x <= 1; x++) {
for (int y = -1; y <= 1; y++) {
if (x== 0 && y == 0)
continue;
int neighbourX = inNode->posX + (x * NODE_SIZE);
int neighbourY = inNode->posY + (y * NODE_SIZE);
std::string hashString = AANode::hashCode(neighbourX, neighbourY);
auto nodeIterator = hashesNodeMapping.find(hashString);
if(nodeIterator == hashesNodeMapping.end())
continue;
AANode* neighbourNode = nodeIterator->second;
neighbourNodes.push_back(neighbourNode);
}
}
inNode->neighbourNodes = neighbourNodes;
}
void AAGrid::mousePressEvent(QMouseEvent *event) {
AANode * selectedNode = (AANode*)this->scene()->itemAt( this->mapToScene( event->pos() ) ,
QTransform() );
if (selectedNode == nullptr)
return;
if (event->button() == Qt::LeftButton)
{
if (event->modifiers() == Qt::NoModifier )
{
selectedNode->switchWallState();
currentMouseState = MOUSE_DRAG_STATE;
paintingState = selectedNode->getCurrentState();
}
else if (event->modifiers() == Qt::AltModifier )
{
setGoalNode( selectedNode );
}
}
}
void AAGrid::mouseMoveEvent(QMouseEvent *event) {
if (currentMouseState != MOUSE_DRAG_STATE)
return;
AANode * selectedNode = (AANode*)this->scene()->itemAt( this->mapToScene( event->pos() ) ,
QTransform() );
if (selectedNode == nullptr)
return;
if(std::find(nodesToSwitch.begin(), nodesToSwitch.end(), selectedNode) != nodesToSwitch.end())
return;
if (selectedNode->getCurrentState() != paintingState)
return
selectedNode->switchWallState();
nodesToSwitch.push_back( selectedNode );
}
void AAGrid::mouseReleaseEvent(QMouseEvent *event) {
currentMouseState = DEFAULT_MOUSE_STATE;
nodesToSwitch.clear();
}
void AAGrid::resetNodes() {
for (auto const& pair: hashesNodeMapping) {
pair.second->reset();
}
}
void AAGrid::keyPressEvent(QKeyEvent *event) {
if (event->key() == Qt::Key_Control)
{
resetNodes();
}
if (event->key() == Qt::Key_Shift)
{
if (goalNodes.size() == 2)
findPath(goalNodes.at(0), goalNodes.at(1));
}
}