-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgui.hpp
executable file
·137 lines (105 loc) · 3.38 KB
/
gui.hpp
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
#ifndef GUI_HPP
#define GUI_HPP
#include <vector>
#include <utility>
#include <string>
class GuiStyle
{
public:
sf::Color bodyCol;
sf::Color bodyHighlightCol;
sf::Color borderCol;
sf::Color borderHighlightCol;
sf::Color textCol;
sf::Color textHighlightCol;
sf::Font* font;
float borderSize;
GuiStyle(sf::Font* font, float borderSize,
sf::Color bodyCol, sf::Color borderCol, sf::Color textCol,
sf::Color bodyHighlightCol, sf::Color borderHighlightCol, sf::Color textHighlightCol)
{
this->bodyCol = bodyCol;
this->borderCol = borderCol;
this->textCol = textCol;
this->bodyHighlightCol = bodyHighlightCol;
this->borderHighlightCol = borderHighlightCol;
this->textHighlightCol = textHighlightCol;
this->font = font;
this->borderSize = borderSize;
}
GuiStyle() { }
};
class GuiEntry
{
public:
/* Handles appearance of the entry */
sf::RectangleShape shape;
/* String returned when the entry is activated */
std::string message;
/* Text displayed on the entry */
sf::Text text;
GuiEntry(const std::string& message, sf::RectangleShape shape, sf::Text text)
{
this->message = message;
this->shape = shape;
this->text = text;
}
GuiEntry() { }
};
class Gui : public sf::Transformable, public sf::Drawable
{
private:
/* If true the menu entries will be horizontally, not vertically, adjacent */
bool horizontal;
GuiStyle style;
sf::Vector2f dimensions;
int padding;
public:
std::vector<GuiEntry> entries;
bool visible;
/* Constructor */
Gui(sf::Vector2f dimensions, int padding, bool horizontal,
GuiStyle& style, std::vector<std::pair<std::string, std::string>> entries)
{
visible = false;
this->horizontal = horizontal;
this->style = style;
this->dimensions = dimensions;
this->padding = padding;
/* Construct the background shape */
sf::RectangleShape shape;
shape.setSize(dimensions);
shape.setFillColor(style.bodyCol);
shape.setOutlineThickness(-style.borderSize);
shape.setOutlineColor(style.borderCol);
/* Construct each gui entry */
for(auto entry : entries)
{
/* Construct the text */
sf::Text text;
text.setString(entry.first);
text.setFont(*style.font);
text.setColor(style.textCol);
text.setCharacterSize(dimensions.y-style.borderSize-padding);
this->entries.push_back(GuiEntry(entry.second, shape, text));
}
}
sf::Vector2f getSize();
/* Return the entry that the mouse is hovering over. Returns
* -1 if the mouse if outside of the Gui */
int getEntry(const sf::Vector2f mousePos);
/* Change the text of an entry */
void setEntryText(int entry, std::string text);
/* Change the entry dimensions */
void setDimensions(sf::Vector2f dimensions);
/* Draw the menu */
virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const;
void show();
void hide();
/* Highlights an entry of the menu */
void highlight(const int entry);
/* Return the message bound to the entry */
std::string activate(const int entry);
std::string activate(const sf::Vector2f mousePos);
};
#endif /* GUI_HPP */