-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGUI.h
More file actions
187 lines (177 loc) · 4.32 KB
/
GUI.h
File metadata and controls
187 lines (177 loc) · 4.32 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
181
182
183
184
185
186
187
#pragma once
#include <iostream>
#include <vector>
#include <SDL.h>
#include <SDL_ttf.h>
#include <SDL_image.h>
#include <intrin.h>
#include "draw.h"
#include "global.h"
struct Position
{
int x, y;
};
struct Size
{
int w, h;
};
enum Relativity
{
TopLeft,
Top,
TopRight,
Left,
Center,
Right,
BottomLeft,
Bottom,
BottomRight
};
namespace GUIColor
{
static SDL_Color Red{ 255,0,0,255 };
static SDL_Color Green{ 0,255,0,255 };
static SDL_Color Blue{ 0,0,255,255 };
static SDL_Color White{ 255,255,255,255 };
static SDL_Color Black{ 0,0,0,255 };
static SDL_Color Gray{ 45,45,45,255 };
static SDL_Color Yellow{ 255,255,0,255 };
static SDL_Color Cyan{ 0,255,255,255 };
static SDL_Color Purple{ 255,0,255,255 };
static SDL_Color Transparent{ 0,0,0,0 };
};
class GUIElement
{
public:
GUIElement() {}
~GUIElement() {}
virtual void OnClick(std::function<void()> func) { m_func = func; };
virtual void Click()
{
if (m_bClickable && m_func)
m_func();
}
SDL_Rect orig;
SDL_Rect rect;
Relativity rel;
bool m_bClickable = false;
bool m_bEnabled = true;
std::function<void()> m_func;
std::string m_sKey;
virtual void SetRect() = 0;
};
int getIndex(std::vector<GUIElement*> v, std::string K);
class GUI
{
public:
GUI(const GUI&) = delete;
static GUI& Get()
{
static GUI instance;
return instance;
}
static void Add(GUIElement* element) { Get().gui.push_back(element); }
//static void Remove(std::string _key) { Get().gui.erase(Get().gui.begin() + getIndex(Get().gui, _key)); }
static std::vector<GUIElement*>& Elements() { return Get().gui; }
private:
GUI() {}
std::vector<GUIElement*> gui;
};
class Button : public GUIElement
{
public:
Button() = delete;
Button(std::string _key, std::string _text, TTF_Font* m_Font, SDL_Rect _rect, Relativity _rel, SDL_Color m_colText, SDL_Color m_colBackground, bool defaultEnabled);
~Button();
SDL_Texture* m_Texture;
void SetRect();
private:
std::string m_sText;
};
class TextInput : public GUIElement
{
public:
TextInput() = delete;
TextInput(std::string _key, std::string _text, std::string _textEmpty, TTF_Font* _font, SDL_Rect _rect, Relativity _rel, SDL_Color _textColor, SDL_Color _textColorEmpty, SDL_Color _backgroundColor, bool defaultEnabled);
~TextInput();
SDL_Texture* m_Texture;
std::string m_sText;
std::string m_sTextEmpty;
void OnClick(std::function<void()> func) { m_func = func; };
void Click()
{
if (m_func)
m_func();
}
void Update();
void SetRect();
private:
TTF_Font* m_Font;
SDL_Color m_colText, m_colTextEmpty, m_colBackground;
};
class Checkbox : public GUIElement
{
public:
Checkbox() = delete;
Checkbox(std::string _key, std::string _text, TTF_Font* m_Font, SDL_Rect _rect, Relativity _rel, SDL_Color m_colText, SDL_Color m_colBackground, bool defaultState, bool defaultEnabled);
~Checkbox();
void Click() override
{
m_bChecked ? Uncheck() : Check();
m_bChecked = !m_bChecked;
}
void Check()
{
if (m_func)
m_func();
}
void Uncheck()
{
if (m_func2)
m_func2();
}
void OnCheck(std::function<void()> func) { m_func = func; };
void OnUncheck(std::function<void()> func) { m_func2 = func; };
void SetRect();
SDL_Texture* m_texChecked;
SDL_Texture* m_texUnChecked;
bool m_bChecked;
private:
std::string m_sText;
std::function<void()> m_func2;
};
class Slider : public GUIElement
{
public:
Slider() = delete;
Slider(std::string _key, std::string _text, TTF_Font* m_Font, SDL_Rect _rect, Relativity _rel, SDL_Color m_colText, SDL_Color m_colBackground, int minValue, int maxValue, int defaultValue, bool defaultEnabled);
void ChangeValue(int val)
{
m_iValue = val;
}
void SetRect();
SDL_Texture* m_Texture;
int m_iValue;
private:
std::string m_sText;
};
class Block : public GUIElement
{
public:
Block() = delete;
Block(std::string _key, SDL_Rect _rect, Relativity _rel, SDL_Color m_colBackground, bool defaultEnabled);
~Block();
void SetRect();
SDL_Texture* m_Texture;
};
class Label : public GUIElement
{
public:
Label() = delete;
Label(std::string _key, std::string _text, TTF_Font* m_Font, SDL_Rect _rect, Relativity _rel, SDL_Color m_colText, bool defaultEnabled);
~Label();
void SetRect();
SDL_Texture* m_Texture;
private:
std::string m_sText;
};