-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUIScreen.cpp
155 lines (130 loc) · 2.42 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
#include "UIScreen.h"
#include "Game.h"
#include "Font.h"
#include "Button.h"
UIScreen::UIScreen(Game* game) : mGame(game)
,mFont(nullptr)
,mTitle(nullptr)
,mTitlePos(Vector2::Zero)
,mState(EActive)
,mNextButtonPos(Vector2(0, 0))
{
game->PushUI(this);
}
UIScreen::~UIScreen()
{
if (mFont)
{
mFont->Unload();
delete mFont;
}
if (mButtonSelected)
{
SDL_DestroyTexture(mButtonSelected);
}
if (mButtonUnSelected)
{
SDL_DestroyTexture(mButtonUnSelected);
}
if (!mButtons.empty())
{
for (auto& b : mButtons)
{
delete b;
}
mButtons.clear();
}
}
void UIScreen::Update(float deltaTime)
{
}
void UIScreen::Draw(SDL_Renderer* renderer)
{
// Iterate through mButtons and draw each button
if (!mButtons.empty())
{
for (auto const& b : mButtons)
{
if (b->GetHighlighted())
{
b->Draw(b->GetSelected(), renderer);
}
else
{
b->Draw(b->GetUnSelected(), renderer);
}
}
}
}
void UIScreen::ProcessInput(const uint8_t* keys)
{
if (!mButtons.empty())
{
// Get mouse position
int x, y;
uint32_t state = SDL_GetMouseState(&x, &y);
Vector2 mousePos(static_cast<float>(x), static_cast<float>(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)
{
}
void UIScreen::AddButton(const std::string& name, std::function<void()> onClick)
{
Vector2 dims(64, 64);
int a, b;
SDL_QueryTexture(mButtonSelected, NULL, NULL, &a, &b);
dims.x = static_cast<float>(a);
dims.y = static_cast<float>(b);
Button* button = new Button(name, mFont, onClick, mNextButtonPos, dims);
mNextButtonPos.y += b + 10;
mButtons.emplace_back(button);
}
void UIScreen::LoadSelectedTex(const std::string path)
{
mButtonSelected = mGame->GetTexture(path);
}
void UIScreen::LoadUnSelectedTex(const std::string path)
{
mButtonUnSelected = mGame->GetTexture(path);
}
void UIScreen::DrawTexture(SDL_Texture* texture, const Vector2& offset, float scale)
{
}