-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMapEditorSelect.cpp
195 lines (172 loc) · 5.31 KB
/
MapEditorSelect.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
#include "MapEditorSelect.h"
#include "Menu.h"
#include "Styles.h"
MapEditorSelect::MapEditorSelect(StateManager &stateManager) : StateScreen(stateManager)
{
using namespace sf;
text.setFont(assetManager->getFont("game font"));
text.setFillColor(Color::White);
startButton.setShortcut(sf::Keyboard::Enter);
startButton.setBaseColor(TRStyles::btnBaseColor);
startButton.setHighlightColor(TRStyles::btnHLColor);
startButton.setSize(Vector2f(200, 100));
startButton.setPosition(Vector2f(2048 / 2 - 100, 1000));
cursor = 0;
std::string path = "BeatMaps";
for (const auto& entry : fs::directory_iterator(path))
{
maps.push_back(entry.path());
}
}
MapEditorSelect::~MapEditorSelect()
{
}
void MapEditorSelect::drawOptions(sf::RenderTexture& window, std::string option, int x, int y, bool isHighlight)
{
using namespace sf;
if (isHighlight)
{
RectangleShape rect(Vector2f(window.getSize().x, 150));
rect.setPosition(x, y - 150/4);
rect.setFillColor(Color(0, 186, 211));
window.draw(rect);
text.setFillColor(Color::Black);
}
else
{
text.setFillColor(Color::White);
}
text.setPosition(x, y);
text.setCharacterSize(60);
text.setString(option);
window.draw(text);
}
void MapEditorSelect::drawOptions(sf::RenderWindow& window, std::string options, int x, int y, bool isHighlight)
{
using namespace sf;
if (isHighlight)
{
RectangleShape rect(Vector2f(800, 150));
rect.setPosition(x, y - 150/4);
rect.setFillColor(Color::White);
window.draw(rect);
text.setFillColor(Color::Black);
}
else
{
text.setFillColor(Color::White);
}
text.setPosition(x, y);
text.setCharacterSize(60);
text.setString(options);
window.draw(text);
}
void MapEditorSelect::tick(const float & dt, sf::RenderWindow& window)
{
}
void MapEditorSelect::render(sf::RenderWindow& window)
{
using namespace sf;
RenderTexture mapsTexture;
mapsTexture.create(1000, 800);
mapsTexture.clear(Color::Transparent);
mapsTexture.display();
startButton.setHighlight(startButton.mouseInButton(window));
text.setFillColor(Color::White);
text.setCharacterSize(80);
text.setString("Select map to edit");
text.setPosition(1024 - text.getLocalBounds().width / 2, 50);
window.draw(text);
int size = maps.size();
for (int i = 0; i < size; ++i)
{
drawOptions(mapsTexture, maps[i].filename().string(), 0, mapsTexture.getSize().y/2 - 150/2 + i * 150 + mapRenderOffset, cursor == i);
}
Sprite sprite(mapsTexture.getTexture());
sprite.setPosition(2048/2 - 800/2, 150);
window.draw(sprite);
if (mouseInBox(window, 20, 20, 40, 40)) // back button
{
window.draw(assetManager->getDrawable("back button hl"));
}
else
{
window.draw(assetManager->getDrawable("back button"));
}
window.draw(startButton);
}
void MapEditorSelect::keyEvent(const float & dt, sf::Event event)
{
using namespace sf;
using namespace std;
if (event.type != Event::KeyPressed) return;
Keyboard::Key key = event.key.code;
switch (key)
{
case Keyboard::Key::Return:
stateManager.addState(std::unique_ptr<StateScreen>(new BeatMapEditor(stateManager, fs::absolute(maps[cursor]).string())));
break;
case Keyboard::Key::Escape:
stateManager.addState(std::unique_ptr<StateScreen>(new Menu(stateManager)));
break;
case Keyboard::Key::Up:
cursor = clamp(cursor - 1, 0, (int)maps.size() - 1);
mapRenderOffset = -cursor * 150;
break;
case Keyboard::Key::Down:
cursor = clamp(cursor + 1, 0, (int)maps.size() - 1);
mapRenderOffset = -cursor * 150;
break;
}
}
void MapEditorSelect::mouseEvent(const float & dt, sf::RenderWindow& window, sf::Event event)
{
using namespace sf;
if (event.type == Event::MouseButtonPressed && mouseInBox(window, 2048/2 - 800/2, 100, 1000, 800))
{
isPressed = true;
Vector2i pixelPos = Mouse::getPosition(window);
Vector2f mouseViewPos = window.mapPixelToCoords(pixelPos);
pressedPosition = Vector2f(mouseViewPos.x, mouseViewPos.y);
prevMapRenderOffset = mapRenderOffset;
}
else if (isPressed && Mouse::isButtonPressed(Mouse::Left))
{
Vector2i pixelPos = Mouse::getPosition(window);
Vector2f mouseViewPos = window.mapPixelToCoords(pixelPos);
mapRenderOffset = prevMapRenderOffset;
mapRenderOffset += mouseViewPos.y - pressedPosition.y;
cursor = std::clamp( - (mapRenderOffset - 150/2) / 150, 0, (int)maps.size() - 1);
}
else if (event.type == Event::MouseButtonReleased)
{
isPressed = false;
mapRenderOffset = -cursor * 150;
prevMapRenderOffset = mapRenderOffset;
}
else if (event.type == Event::MouseButtonPressed && event.mouseButton.button == Mouse::Left)
{
if (startButton.mouseInButton(window))
{
stateManager.addState(std::unique_ptr<StateScreen>(new BeatMapEditor(stateManager, fs::absolute(maps[cursor]).string())));
}
else if (mouseInBox(window, 20, 20, 40, 40)) // back button
{
stateManager.addState(std::unique_ptr<StateScreen>(new Menu(stateManager)));
}
else if (mouseInBox(window, 20, 20, 40, 40)) // back button
{
stateManager.addState(std::unique_ptr<StateScreen>(new Menu(stateManager)));
}
}
}
void MapEditorSelect::mouseScrollEvent(const float& dt, sf::RenderWindow& window, sf::Event event)
{
using namespace std;
if (event.type == sf::Event::MouseWheelScrolled && mouseInBox(window, 2048/2 - 800/2, 150, 1000, 800))
{
cursor = clamp(cursor - (int) event.mouseWheelScroll.delta, 0, (int)maps.size() - 1);
mapRenderOffset = -cursor * 150;
prevMapRenderOffset = mapRenderOffset;
}
}