-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathgame_menu_impl.cpp
More file actions
180 lines (157 loc) · 5.25 KB
/
game_menu_impl.cpp
File metadata and controls
180 lines (157 loc) · 5.25 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
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
#include "game_menu_impl.h"
#include "SFML/Graphics/Color.hpp"
#include "SFML/Window/Event.hpp"
#include <iostream>
#include <string>
game_menu::MENU *create_menu_context(sf::RenderWindow &wnd,
game_menu::MenuConfig &config) {
return reinterpret_cast<game_menu::MENU *>(new game_menu::Menu(wnd, config));
}
void menu_destroy_context(game_menu::MENU *menu) {
delete reinterpret_cast<game_menu::Menu *>(menu);
}
void menu_handle_event(game_menu::MENU *menu, sf::Event &event) {
reinterpret_cast<game_menu::Menu *>(menu)->handleEvent(event);
}
void menu_render(game_menu::MENU *menu) {
reinterpret_cast<game_menu::Menu *>(menu)->render();
}
namespace game_menu {
void Menu::handleEvent(sf::Event &event) {
auto max_items = _items.size();
if (event.type == sf::Event::KeyPressed) {
if (event.key.code == sf::Keyboard::Up ||
event.key.code == sf::Keyboard::Down) {
changeCurrSelectedItem(event.key.code == sf::Keyboard::Up);
} else if (event.key.code == sf::Keyboard::Return) {
performCurrSelectedItemAction();
}
}
else if (event.type == sf::Event::MouseMoved) {
sf::Vector2f mousePos(event.mouseMove.x, event.mouseMove.y);
for (int i = 0; i < _items.size(); ++i) {
if (_items[i].textObj.getGlobalBounds().contains(mousePos)) {
_currently_selected_item = i;
break;
}
}
}
else if (event.type == sf::Event::MouseButtonPressed) {
if (event.mouseButton.button == sf::Mouse::Left) {
sf::Vector2f mousePos(event.mouseButton.x, event.mouseButton.y);
for (int i = 0; i < _items.size(); ++i) {
if (_items[i].textObj.getGlobalBounds().contains(mousePos)) {
_items[i].data.action(_window);
break;
}
}
}
}
else if (event.type == sf::Event::MouseWheelScrolled) {
if (event.mouseWheelScroll.wheel == sf::Mouse::VerticalWheel) {
changeCurrSelectedItem(event.mouseWheelScroll.delta > 0.0);
}
}
} // handleEvent(...)
void Menu::render() {
setMenu();
drawMenu();
} // render(...)
sf::Text Menu::writeText(std::string str, sf::Font *font, unsigned int size,
float x, float y, const Color color) {
sf::Color textColor(color);
sf::Text text;
text.setString(str);
text.setFont(*font);
text.setFillColor(textColor);
text.setCharacterSize(size);
sf::FloatRect textRect = text.getLocalBounds();
text.setOrigin(textRect.width / 2.0f, 0);
if (x - textRect.width / 2.0f < 0) {
x = textRect.width / 2 + _style.PaddingTitle.left;
}
if (x + textRect.width / 2.0f > _window.getSize().x) {
x = _window.getSize().x - textRect.width / 2 + _style.PaddingTitle.left;
}
text.setPosition(sf::Vector2f(x, y));
_window.draw(text);
return text;
} // writeText(...)
void Menu::setMenu() {
// std::cout << "screen size:" << window.getSize().x << " " <<
// window.getSize().y
// << std::endl;
/* Setting title of menu */
{
/* Small scope just to be able to freely use the variable names */
float offset_coefficient = 0.5;
switch (_style.TitleAlign) {
case Align::Center:
offset_coefficient = 0.5;
break;
case Align::Left:
offset_coefficient = 0.25;
break;
case Align::Right:
offset_coefficient = 0.75;
break;
}
float x = (float)_window.getSize().x * offset_coefficient ,
y = _style.PaddingTitle.top;
_title_location.x = (x + _style.PaddingTitle.left);
_title_location.y = y;
}
float menu_screen_height =
_title_location.y + _style.PaddingItems.top;
float block_height =
(float)_style.ItemFontSize * _style.MenuItemScaleFactor;
float offset_coefficient = 0.5;
switch (_style.ItemAlign) {
case Align::Center:
offset_coefficient = 0.5;
break;
case Align::Left:
offset_coefficient = 0.25;
break;
case Align::Right:
offset_coefficient = 0.75;
break;
}
float x = (float)_window.getSize().x * offset_coefficient +
_style.PaddingItems.left;
float y = menu_screen_height +
block_height + _style.PaddingItems.top;
/* Calculating Menu item locations */
for (int8_t i = 0; i < _items.size(); ++i) {
coordinates crd;
crd.x = x;
crd.y = y;
_items[i].location = crd;
y += block_height;
}
} // setMenu()
void Menu::drawMenu() {
writeText(_title, _style.ItemFont, _style.TitleFontSize, _title_location.x,
_title_location.y, _style.colorScheme.titleColor);
game_menu::Color color(_style.colorScheme.itemColor);
for (int i = 0; i < _items.size(); ++i) {
if (i == _currently_selected_item) {
color = _style.colorScheme.selectedColor;
} else {
color = _style.colorScheme.itemColor;
}
_items[i].textObj = writeText(_items[i].data.name, _style.ItemFont, _style.ItemFontSize,
_items[i].location.x, _items[i].location.y, color);
}
} // drawMenu()
void Menu::changeCurrSelectedItem(const bool moveUp) {
const auto maxItems = _items.size();
_currently_selected_item = moveUp
? (_currently_selected_item + maxItems - 1) % maxItems
: (_currently_selected_item + 1) % maxItems;
} // changeCurrSelectedItem()
void Menu::performCurrSelectedItemAction()
{
_items[_currently_selected_item].data.action(_window);
} // performCurrSelectedItemAction()
} // namespace game_menu