-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMenu.cpp
89 lines (78 loc) · 2.57 KB
/
Menu.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
#include "stdafx.h"
#include "Menu.h"
Menu::Menu(float width, float height, sf::Font &font) : selectedItemIndex(0), visible(false)
{
menu.push_back(MenuItem(sf::Vector2f(width / 2, height / (ITEMS_COUNT) * 1), sf::Vector2f(120.f, 22.f), font, "Continue"));
menu.push_back(MenuItem(sf::Vector2f(width / 2, height / (ITEMS_COUNT) * 2), sf::Vector2f(110.f, 22.f), font, "Options"));
menu.push_back(MenuItem(sf::Vector2f(width / 2, height / (ITEMS_COUNT) * 3), sf::Vector2f(54.f, 22.f), font, "Exit"));
setSelectedItem(0);
}
void Menu::draw(sf::RenderWindow &window) const {
for (MenuItem const &mi : menu)
mi.draw(window);
}
void Menu::moveUp() {
if (selectedItemIndex - 1 >= 0)
{
menu[selectedItemIndex].setColor(sf::Color::White);
selectedItemIndex--;
menu[selectedItemIndex].setColor(sf::Color::Red);
}
else {
menu[selectedItemIndex].setColor(sf::Color::White);
selectedItemIndex = ITEMS_COUNT - 1;
menu[selectedItemIndex].setColor(sf::Color::Red);
}
}
void Menu::moveDown() {
if (selectedItemIndex < ITEMS_COUNT - 1)
{
menu[selectedItemIndex].setColor(sf::Color::White);
selectedItemIndex++;
menu[selectedItemIndex].setColor(sf::Color::Red);
}
else {
menu[selectedItemIndex].setColor(sf::Color::White);
selectedItemIndex = 0;
menu[selectedItemIndex].setColor(sf::Color::Red);
}
}
sf::Uint8 Menu::update(sf::RenderWindow &window)
{
//SELECTIONS
sf::Vector2f MousePos(window.mapPixelToCoords(sf::Mouse::getPosition(window)));
if (menu[0].rc.getGlobalBounds().contains(MousePos)) {
setSelectedItem(0);
}
else if (menu[1].rc.getGlobalBounds().contains(MousePos)) {
setSelectedItem(1);
}
else if (menu[2].rc.getGlobalBounds().contains(MousePos)) {
setSelectedItem(2);
}
//EVENTS
if ((!sf::Mouse::isButtonPressed(sf::Mouse::Left) || !menu[selectedItemIndex].rc.getGlobalBounds().contains(window.mapPixelToCoords(sf::Mouse::getPosition(window)))) && !sf::Keyboard::isKeyPressed(sf::Keyboard::Return))
return 0;
switch (selectedItemIndex) {
//Continue
case 0:
visible = false;
return 1;
//Options
case 1:
return 2;
//Exit
case 2:
return 3;
}
}
void Menu::updatePos(sf::Vector2f pos, float height) {
menu[0].setPosition(pos.x - 60, pos.y - height / (ITEMS_COUNT * 2));
menu[1].setPosition(pos.x - 55, pos.y);
menu[2].setPosition(pos.x - 27, pos.y + height / (ITEMS_COUNT * 2));
}
void Menu::setSelectedItem(sf::Uint8 index) {
menu[selectedItemIndex].setColor(sf::Color::White);
selectedItemIndex = index;
menu[selectedItemIndex].setColor(sf::Color::Red);
}