-
Notifications
You must be signed in to change notification settings - Fork 0
/
MapController.cpp
131 lines (123 loc) · 3.38 KB
/
MapController.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
#include "MapController.hpp"
#include "MapView.hpp"
void MapController::preceedMouse(sf::Vector2i clickPos, const sf::Event & event)
{
if (event.type == sf::Event::MouseWheelMoved)
{
if (!mouseDeltaActive)
mouseDeltaActive = true;
if (sf::Keyboard::isKeyPressed(sf::Keyboard::LControl))
mouseDelta += event.mouseWheel.delta * 25;
else
mouseDelta += event.mouseWheel.delta;
if (mouseDelta < 0)
mouseDelta = 0;
if (mouseDelta > mapHandler->getMineUpperbound())
mouseDelta = mapHandler->getMineUpperbound();
viewHandler->popup("Next generated map will have " + std::to_string(mouseDelta) + "mines!", 3000);
}
if (event.type == sf::Event::MouseButtonReleased)
{
if (mapHandler->isWin() || mapHandler->isLose())
{
mouseCount = 0;
return;
}
if (clickPos.x < 0 || clickPos.y < 0)
{
mouseCount = 0;
return;
}
clickPos.x /= viewHandler->getSquareSize() + viewHandler->getMargin();
clickPos.y /= viewHandler->getSquareSize() + viewHandler->getMargin();
if (clickPos.x >= mapHandler->getSizeX() ||
clickPos.y >= mapHandler->getSizeY())
{
mouseCount = 0;
return;
}
viewHandler->removeFocus();
switch (mouseCount)
{
case LPM:
mapHandler->reveal(clickPos.y, clickPos.x);
break;
case PPM:
mapHandler->mark(clickPos.y, clickPos.x);
break;
case LPM_PPM:
mapHandler->revealDoubleClick(clickPos.y, clickPos.x);
break;
}
if (mouseCount != LPM_PPM)
mouseCount = 0;
else if (mouseCount == LPM_PPM)
mouseCount = BLOCKED;
}
}
void MapController::continiousMouse(sf::Vector2i pos)
{
if (sf::Mouse::isButtonPressed(sf::Mouse::Left) || sf::Mouse::isButtonPressed(sf::Mouse::Right))
{
viewHandler->hideNotifications();
viewHandler->removeFocus();
if (mapHandler->isWin() || mapHandler->isLose())
return;
if (pos.x < 0 || pos.y < 0)
return;
if (mouseCount == BLOCKED)
return;
pos.x /= viewHandler->getSquareSize() + viewHandler->getMargin();
pos.y /= viewHandler->getSquareSize() + viewHandler->getMargin();
if (pos.x >= mapHandler->getSizeX() ||
pos.y >= mapHandler->getSizeY())
return;
if (sf::Mouse::isButtonPressed(sf::Mouse::Left) && mouseCount != LPM)
{
if (mouseCount == 0)
mouseCount = LPM;
else
mouseCount = LPM_PPM;
}
else if (sf::Mouse::isButtonPressed(sf::Mouse::Right) && mouseCount != PPM)
{
if (mouseCount == 0 || mouseCount == PPM)
mouseCount = PPM;
else
mouseCount = LPM_PPM;
}
if (mouseCount == LPM)
{
if (mapHandler->getStatus(pos.y, pos.x) == Tile::HIDDEN)
viewHandler->addToFocusBuffor(sf::Vector2i(pos.x, pos.y));
}
else if (mouseCount == LPM_PPM)
{
for (int y = -1; y < 2; y++)
for (int x = -1; x < 2; x++)
if (mapHandler->getStatus(pos.y + y, pos.x + x) == Tile::HIDDEN)
viewHandler->addToFocusBuffor(sf::Vector2i(pos.x + x, pos.y + y));
}
viewHandler->addFocus();
}
// UNBLOCK
else if (mouseCount != 0)
mouseCount = 0;
}
void MapController::preceedKeyboard(const sf::Event & event)
{
if (event.type == sf::Event::KeyReleased)
{
if (event.key.code == sf::Keyboard::Space)
{
if (mouseDeltaActive)
mapHandler->setMineNumber(mouseDelta);
mapHandler->generateMap();
viewHandler->reload();
viewHandler->popup(
"Map " + std::to_string(mapHandler->getSizeX()) +
"x" + std::to_string(mapHandler->getSizeY()) + " with " +
std::to_string(mapHandler->getMineCount()) + " mines generated!", 3000);
}
}
}