-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathUIScreen.cpp
236 lines (214 loc) · 4.69 KB
/
UIScreen.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
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
// Ahmed S. Tolba 2015-2018
#include "UIScreen.h"
#include "Texture.h"
#include "Shader.h"
#include "Game.h"
#include "Renderer.h"
#include "Font.h"
UIScreen::UIScreen(Game* game)
:mGame(game)
,mTitle(nullptr)
,mBackground(nullptr)
,mTitlePos(0.0f, 300.0f)
,mNextButtonPos(0.0f, 200.0f)
,mBGPos(0.0f, 250.0f)
,mState(EActive)
{
// Add to UI Stack
mGame->PushUI(this);
mFont = mGame->GetFont("Assets/Carlito-Regular.ttf");
mButtonOn = mGame->GetRenderer()->GetTexture("Assets/ButtonYellow.png");
mButtonOff = mGame->GetRenderer()->GetTexture("Assets/ButtonBlue.png");
}
UIScreen::~UIScreen()
{
if (mTitle)
{
mTitle->Unload();
delete mTitle;
}
for (auto b : mButtons)
{
delete b;
}
mButtons.clear();
}
void UIScreen::Update(float deltaTime)
{
}
void UIScreen::Draw(Shader* shader)
{
// Draw background (if exists)
if (mBackground)
{
DrawTexture(shader, mBackground, mBGPos);
}
// Draw title (if exists)
if (mTitle)
{
DrawTexture(shader, mTitle, mTitlePos);
}
// Draw buttons
for (auto b : mButtons)
{
// Draw background of button
Texture* tex = b->GetHighlighted() ? mButtonOn : mButtonOff;
DrawTexture(shader, tex, b->GetPosition());
// Draw text of button
DrawTexture(shader, b->GetNameTex(), b->GetPosition());
}
// Override in subclasses to draw any textures
}
void UIScreen::ProcessInput(const uint8_t* keys)
{
// Do we have buttons?
if (!mButtons.empty())
{
// Get position of mouse
int x, y;
SDL_GetMouseState(&x, &y);
// Convert to (0,0) center coordinates
Vector2 mousePos(static_cast<float>(x), static_cast<float>(y));
mousePos.x -= mGame->GetRenderer()->GetScreenWidth() * 0.5f;
mousePos.y = mGame->GetRenderer()->GetScreenHeight() * 0.5f - mousePos.y;
// Highlight any buttons
for (auto b : mButtons)
{
if (b->ContainsPoint(mousePos))
{
b->SetHighlighted(true);
}
else
{
b->SetHighlighted(false);
}
}
}
}
void UIScreen::HandleKeyPress(int key)
{
switch (key)
{
case SDL_BUTTON_LEFT:
if (!mButtons.empty())
{
for (auto b : mButtons)
{
if (b->GetHighlighted())
{
b->OnClick();
break;
}
}
}
break;
default:
break;
}
}
void UIScreen::Close()
{
mState = EClosing;
}
void UIScreen::SetTitle(const std::string& text,
const Vector3& color,
int pointSize)
{
// Clear out previous title texture if it exists
if (mTitle)
{
mTitle->Unload();
delete mTitle;
mTitle = nullptr;
}
mTitle = mFont->RenderText(text, color, pointSize);
}
void UIScreen::AddButton(const std::string& name, std::function<void()> onClick)
{
Vector2 dims(static_cast<float>(mButtonOn->GetWidth()),
static_cast<float>(mButtonOn->GetHeight()));
Button* b = new Button(name, mFont, onClick, mNextButtonPos, dims);
mButtons.emplace_back(b);
// Update position of next button
// Move down by height of button plus padding
mNextButtonPos.y -= mButtonOff->GetHeight() + 20.0f;
}
void UIScreen::DrawTexture(class Shader* shader, class Texture* texture,
const Vector2& offset, float scale)
{
// Scale the quad by the width/height of texture
Matrix4 scaleMat = Matrix4::CreateScale(
static_cast<float>(texture->GetWidth()) * scale,
static_cast<float>(texture->GetHeight()) * scale,
1.0f);
// Translate to position on screen
Matrix4 transMat = Matrix4::CreateTranslation(
Vector3(offset.x, offset.y, 0.0f));
// Set world transform
Matrix4 world = scaleMat * transMat;
shader->SetMatrixUniform("uWorldTransform", world);
// Set current texture
texture->SetActive();
// Draw quad
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, nullptr);
}
void UIScreen::SetRelativeMouseMode(bool relative)
{
if (relative)
{
SDL_SetRelativeMouseMode(SDL_TRUE);
// Make an initial call to get relative to clear out
SDL_GetRelativeMouseState(nullptr, nullptr);
}
else
{
SDL_SetRelativeMouseMode(SDL_FALSE);
}
}
Button::Button(const std::string& name, Font* font,
std::function<void()> onClick,
const Vector2& pos, const Vector2& dims)
:mOnClick(onClick)
,mNameTex(nullptr)
,mFont(font)
,mPosition(pos)
,mDimensions(dims)
,mHighlighted(false)
{
SetName(name);
}
Button::~Button()
{
if (mNameTex)
{
mNameTex->Unload();
delete mNameTex;
}
}
void Button::SetName(const std::string& name)
{
mName = name;
if (mNameTex)
{
mNameTex->Unload();
delete mNameTex;
mNameTex = nullptr;
}
mNameTex = mFont->RenderText(mName);
}
bool Button::ContainsPoint(const Vector2& pt) const
{
bool no = pt.x < (mPosition.x - mDimensions.x / 2.0f) ||
pt.x > (mPosition.x + mDimensions.x / 2.0f) ||
pt.y < (mPosition.y - mDimensions.y / 2.0f) ||
pt.y > (mPosition.y + mDimensions.y / 2.0f);
return !no;
}
void Button::OnClick()
{
// Call attached handler, if it exists
if (mOnClick)
{
mOnClick();
}
}