forked from tsweeney256/Pokedex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimgui.cpp
219 lines (189 loc) · 4.52 KB
/
imgui.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
#include <SDL.h>
#include <memory>
#include <iostream>
#include "imgui.hpp"
namespace imgui {
static const SDL_Color CYAN = {0, 255, 255, 255};
static const SDL_Color RED = {255, 0, 0, 255};
static const SDL_Color GREEN = {0, 255, 0, 255};
void UIState::begin()
{
hotItem = 0;
}
void UIState::end()
{
if (!mouseDown) {
activeItem = 0;
} else {
if (activeItem == 0) {
activeItem = -1;
}
}
keysEntered = "";
m_currentKeyIndex = 0;
scrollWheel = 0;
}
void UIState::beginFrame(SDL_Texture *texture)
{
m_renderBackend->setRenderTarget(texture);
}
void UIState::endFrame(SDL_Texture *)
{
m_renderBackend->setRenderTarget(nullptr);
}
void UIState::handleEvent(const SDL_Event &sdlEvent)
{
switch(sdlEvent.type) {
case SDL_MOUSEMOTION:
mouseX = sdlEvent.motion.x;
mouseY = sdlEvent.motion.y;
break;
case SDL_MOUSEBUTTONDOWN:
if (sdlEvent.button.button == SDL_BUTTON_LEFT) {
mouseDown = true;
}
break;
case SDL_MOUSEBUTTONUP:
if (sdlEvent.button.button == SDL_BUTTON_LEFT) {
mouseDown = false;
}
break;
case SDL_MOUSEWHEEL:
scrollWheel += sdlEvent.wheel.y;
break;
case SDL_TEXTINPUT:
keysEntered += sdlEvent.text.text;
break;
case SDL_KEYDOWN:
if (sdlEvent.key.keysym.sym == SDLK_BACKSPACE) {
--m_currentKeyIndex;
} else if (sdlEvent.key.keysym.sym == SDLK_v &&
(sdlEvent.key.keysym.mod == KMOD_LCTRL ||
sdlEvent.key.keysym.mod == KMOD_RCTRL)) {
keysEntered = SDL_GetClipboardText();
}
break;
case SDL_TEXTEDITING:
m_cursor = sdlEvent.edit.start;
break;
}
}
bool UIState::mouseHit(int x, int y, int w, int h)
{
if (mouseX < x || mouseY < y || mouseX >= x + w || mouseY >= y + h)
return false;
return true;
}
bool UIState::clickedSprite(int id, const Sprite &sprite) {
if (mouseOverSprite(sprite)) {
hotItem = id;
if (activeItem == 0 && mouseDown) {
activeItem = id;
}
}
if (hotItem == id && activeItem == id && !mouseDown) {
return true;
}
return false;
}
bool UIState::mouseOverSprite(const Sprite &sprite) {
SDL_Rect rect = sprite.rect();
return mouseHit(rect.x, rect.y, rect.w, rect.h);
}
void UIState::setRenderBackend(std::unique_ptr<RenderBackend> &&renderBackend)
{
m_renderBackend = std::move(renderBackend);
}
bool UIState::button(int id, int x, int y, int w, int h)
{
SDL_Color color = CYAN;
if (mouseHit(x, y, w, h)) {
hotItem = id;
if (activeItem == 0 && mouseDown) {
activeItem = id;
}
}
if (hotItem == id) {
if (activeItem == id) {
color = GREEN;
} else {
color = RED;
}
}
m_renderBackend->drawRect(x, y, w, h, color);
if (hotItem == id && activeItem == id && !mouseDown) {
return true;
}
return false;
}
inline void popBackString(std::string &text) {
while (text.size()) {
char c = text.back();
text.pop_back();
// compatible with utf-8
if ((c & 0xC0) != 0x80)
break;
}
}
bool UIState::textField(int id, int x, int y, int w, int h, std::string &text)
{
SDL_Color color = {128, 0, 128, 255};
if (mouseHit(x, y, w, h)) {
hotItem = id;
if (activeItem == 0 && mouseDown) {
activeItem = id;
}
}
text += keysEntered;
int index = m_currentKeyIndex;
while (index < 0) {
popBackString(text);
index++;
}
const std::string tempString = text + std::string("|");
SDL_Color white = {255, 255, 255, 255};
m_renderBackend->drawRect(x, y, w, h, color);
m_renderBackend->drawText(x, y, w, white, tempString);
if (keysEntered.size()) {
return true;
}
return false;
}
bool UIState::scrollBar(int id, int x, int y, int h, int max, float *val)
{
const SDL_Color color = {225, 0, 0, 255};
const int width = 16, height = 24;
int ypos = (h * (*val)) / max;
if (mouseHit(x, y + ypos, width, height)) {
hotItem = id;
if (activeItem == 0 && mouseDown) {
activeItem = id;
}
}
m_renderBackend->drawRect(x, y + ypos, width, height, color);
// todo only update based on scroll-wheel when window is active
if (activeItem == id) {
int mousePos = mouseY - y;
if (mousePos < 0) {
mousePos = 0;
} else if (mousePos > h) {
mousePos = h;
}
float tempValue = (mousePos*max) / h;
if (*val != tempValue) {
*val = tempValue;
return true;
}
} else if (scrollWheel) {
int tempValue = *val - (scrollWheel*max)/h;
if (tempValue > max) {
tempValue = max;
} else if (tempValue < 0) {
tempValue = 0;
}
*val = tempValue;
return true;
}
return false;
}
} // namespace imgui