-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOptionSelector.cpp
More file actions
149 lines (123 loc) · 3.03 KB
/
OptionSelector.cpp
File metadata and controls
149 lines (123 loc) · 3.03 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
#include "OptionSelector.h"
#include "Event.h"
#include "EventQueue.h"
#include "TextureData.h"
#include "FontData.h"
#include <SFML/Graphics/RenderTarget.hpp>
OptionSelector::OptionSelector(const std::vector<std::string> &optionStrings) :
sf::Drawable(),
options_(),
selectorArrow_(),
selectedOptionIndex_(0)
{
const unsigned int fontSize = 16;
const float gap = 2.0f;
const float lineHeight = fontSize * 0.5f + gap;
float x = 25.0f;
float startY = 32.0f - ((optionStrings.size() - 1) * lineHeight + gap) * 0.5f;
for (size_t i = 0; i < optionStrings.size(); ++i)
{
options_.push_back(
Option(
optionStrings[i],
sf::Vector2f(x, startY + lineHeight * i),
16));
}
options_[selectedOptionIndex_].startShaking();
this->updateSelectorPosition_();
}
void OptionSelector::update(float deltaTime)
{
for (Option &option : options_)
{
option.update(deltaTime);
}
selectorArrow_.update(deltaTime);
}
void OptionSelector::updateOnMouseMove(const sf::Vector2f &worldMousePosition)
{
for (size_t i = 0; i < options_.size(); ++i)
{
options_[i].updateOnMouseMove(worldMousePosition);
if (options_[i].isHovered())
{
this->setSelectedOptionIndex_(i);
break;
}
}
}
void OptionSelector::updateOnMouseButtonPress(sf::Mouse::Button button)
{
if (button != sf::Mouse::Button::Left
|| !options_[selectedOptionIndex_].isHovered())
{
return;
}
this->selectOption_();
}
void OptionSelector::updateOnKeyPress(sf::Keyboard::Key key)
{
switch (key)
{
case sf::Keyboard::Key::W:
case sf::Keyboard::Key::Up:
this->decrementSelectedOptionIndex_();
break;
case sf::Keyboard::Key::S:
case sf::Keyboard::Key::Down:
this->incrementSelectedOptionIndex_();
break;
case sf::Keyboard::Key::Space:
case sf::Keyboard::Key::Enter:
this->selectOption_();
break;
}
}
void OptionSelector::draw(sf::RenderTarget &target, sf::RenderStates states) const
{
for (const Option &option : options_)
{
target.draw(option, states);
}
target.draw(selectorArrow_, states);
}
void OptionSelector::selectOption_()
{
EventQueue::getInstance().send(
new EventPlaySound(
2));
}
void OptionSelector::decrementSelectedOptionIndex_()
{
this->setSelectedOptionIndex_(
(selectedOptionIndex_ == 0)
? options_.size() - 1
: selectedOptionIndex_ - 1);
}
void OptionSelector::incrementSelectedOptionIndex_()
{
this->setSelectedOptionIndex_(
(selectedOptionIndex_ + 1) % options_.size());
}
void OptionSelector::updateSelectorPosition_()
{
const sf::Vector2f &selectedOptionPosition = options_[selectedOptionIndex_].getPosition();
selectorArrow_.setPosition(
sf::Vector2f(
selectedOptionPosition.x - 5.0f,
selectedOptionPosition.y + options_[selectedOptionIndex_].getSize().y * 0.5f));
}
void OptionSelector::setSelectedOptionIndex_(size_t index)
{
if (selectedOptionIndex_ == index)
{
return;
}
options_[selectedOptionIndex_].stopShaking();
selectedOptionIndex_ = index;
options_[selectedOptionIndex_].startShaking();
this->updateSelectorPosition_();
EventQueue::getInstance().send(
new EventPlaySound(
1));
}