-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgui.cpp
More file actions
executable file
·135 lines (108 loc) · 3.13 KB
/
Copy pathgui.cpp
File metadata and controls
executable file
·135 lines (108 loc) · 3.13 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
#include <SFML/Graphics.hpp>
#include <string>
#include "gui.hpp"
sf::Vector2f Gui::getSize()
{
return sf::Vector2f(this->dimensions.x, this->dimensions.y * this->entries.size());
}
int Gui::getEntry(const sf::Vector2f mousePos)
{
/* If there are no entries then outside the menu */
if(entries.size() == 0) return -1;
if(!this->visible) return -1;
for(int i = 0; i < this->entries.size(); ++i)
{
/* Translate point to use the entry's local coordinates*/
sf::Vector2f point = mousePos;
point += this->entries[i].shape.getOrigin();
point -= this->entries[i].shape.getPosition();
if(point.x < 0 || point.x > this->entries[i].shape.getScale().x*this->dimensions.x) continue;
if(point.y < 0 || point.y > this->entries[i].shape.getScale().y*this->dimensions.y) continue;
return i;
}
return -1;
}
void Gui::setEntryText(int entry, std::string text)
{
if(entry >= entries.size() || entry < 0) return;
entries[entry].text.setString(text);
return;
}
void Gui::setDimensions(sf::Vector2f dimensions)
{
this->dimensions = dimensions;
for(auto& entry : entries)
{
entry.shape.setSize(dimensions);
entry.text.setCharacterSize(dimensions.y-style.borderSize-padding);
}
return;
}
void Gui::draw(sf::RenderTarget& target, sf::RenderStates states) const
{
if(!visible) return;
/* Draw each entry of the menu */
for(auto entry : this->entries)
{
/* Draw the entry */
target.draw(entry.shape);
target.draw(entry.text);
}
return;
}
void Gui::show()
{
sf::Vector2f offset(0.0f, 0.0f);
this->visible = true;
/* Draw each entry of the menu */
for(auto& entry : this->entries)
{
/* Set the origin of the entry */
sf::Vector2f origin = this->getOrigin();
origin -= offset;
entry.shape.setOrigin(origin);
entry.text.setOrigin(origin);
/* Compute the position of the entry */
entry.shape.setPosition(this->getPosition());
entry.text.setPosition(this->getPosition());
if(this->horizontal) offset.x += this->dimensions.x;
else offset.y += this->dimensions.y;
}
return;
}
void Gui::hide()
{
this->visible = false;
return;
}
/* Highlights an entry of the menu */
void Gui::highlight(const int entry)
{
for(int i = 0; i < entries.size(); ++i)
{
if(i == entry)
{
entries[i].shape.setFillColor(style.bodyHighlightCol);
entries[i].shape.setOutlineColor(style.borderHighlightCol);
entries[i].text.setColor(style.textHighlightCol);
}
else
{
entries[i].shape.setFillColor(style.bodyCol);
entries[i].shape.setOutlineColor(style.borderCol);
entries[i].text.setColor(style.textCol);
}
}
return;
}
/* Return the message bound to the entry */
std::string Gui::activate(const int entry)
{
if(entry == -1) return "null";
return entries[entry].message;
}
std::string Gui::activate(sf::Vector2f mousePos)
{
int entry = this->getEntry(mousePos);
return this->activate(entry);
}