-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathimgui.cpp
413 lines (338 loc) · 13.1 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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
#include "imgui.h"
#include "pixie.h"
#include "font.h"
#include <string.h>
#include <assert.h>
#include <algorithm>
using namespace Pixie;
struct State
{
enum Flags
{
Flags_Started = 1 << 0,
};
bool HasStarted() { return (flags & Flags_Started) != 0; }
int GetNextId() { return nextId++; }
int flags;
int nextId;
int hoverId;
int focusId;
int keyboardCursorPosition;
float keyRepeatTimer;
float keyRepeatTime;
float cursorBlinkTimer;
uint32_t defaultTextColour;
Window* window;
Font* font;
};
static State s_state = { 0 };
void ImGui::Begin(Window* window, Font* font)
{
assert(window);
assert(font);
s_state.flags = State::Flags_Started;
s_state.nextId = 1;
s_state.hoverId = 0;
s_state.window = window;
s_state.font = font;
s_state.defaultTextColour = MAKE_RGB(200, 200, 200);
}
void ImGui::End()
{
s_state.flags = 0;
if (s_state.window->HasMouseGoneDown(Pixie::MouseButton_Left))
{
// If mouse has gone down over empty space, clear the current focus.
if (s_state.hoverId == 0)
{
s_state.focusId = 0;
s_state.keyboardCursorPosition = 0;
}
}
s_state.window = 0;
}
void ImGui::Label(const char* text, int x, int y, uint32_t colour)
{
assert(text);
assert(s_state.HasStarted());
s_state.font->DrawColour(text, x, y, colour, s_state.window);
}
bool ImGui::Button(const char* label, int x, int y, int width, int height)
{
assert(s_state.HasStarted());
Window* window = s_state.window;
int id = s_state.GetNextId();
const uint32_t NormalColour = MAKE_RGB(32, 50, 77);
const uint32_t HoverColour = MAKE_RGB(39, 73, 114);
const uint32_t PressedColour = MAKE_RGB(22, 40, 67);
const uint32_t BorderColour = MAKE_RGB(68, 79, 103);
const uint32_t FocusBorderColour = MAKE_RGB(200, 200, 229);
int mouseX = window->GetMouseX();
int mouseY = window->GetMouseY();
bool hover = mouseX >= x && mouseX <= x + width && mouseY >= y && mouseY <= y + height;
bool pressed = false;
if (hover)
{
s_state.hoverId = id;
// Mouse has just gone down over this element, so give it focus.
if (window->HasMouseGoneDown(Pixie::MouseButton_Left))
s_state.focusId = id;
// If mouse is still down over this element and it has focus, then it is pressed.
pressed = window->IsMouseDown(Pixie::MouseButton_Left) && s_state.focusId == id;
}
uint32_t buttonColour = pressed ? PressedColour : hover ? HoverColour : NormalColour;
uint32_t borderColour = s_state.focusId == id ? FocusBorderColour : BorderColour;
FilledRect(x, y, width, height, buttonColour, borderColour);
if (label)
{
Font* font = s_state.font;
int textX = x + ((width - font->GetStringWidth(label)) >> 1);
int textY = y + ((height - font->GetCharacterHeight()) >> 1);
Label(label, textX, textY, s_state.defaultTextColour);
}
return hover && s_state.focusId == id && window->HasMouseGoneUp(Pixie::MouseButton_Left);
}
void ImGui::Input(char* text, int textBufferLength, int x, int y, int width, int height)
{
assert(text);
assert(s_state.HasStarted());
Window* window = s_state.window;
int id = s_state.GetNextId();
const int LeftMargin = 8;
const uint32_t NormalColour = MAKE_RGB(64, 68, 71);
const uint32_t HoverColour = MAKE_RGB(74, 78, 81);
const uint32_t BorderColour = MAKE_RGB(104, 108, 111);
const uint32_t FocusBorderColour = MAKE_RGB(200, 200, 200);
const uint32_t CursorColour = MAKE_RGB(220, 220, 220);
const int CursorWidth = 8;
const float KeyRepeatTimeInit = 0.2f;
const float KeyRepeatTimeRepeat = 0.05f;
const float CursorBlinkTime = 1.0f;
int mouseX = window->GetMouseX();
int mouseY = window->GetMouseY();
bool hover = mouseX >= x && mouseX <= x + width && mouseY >= y && mouseY <= y + height;
bool pressed = false;
int textLength = (int)strlen(text);
int textX = x + LeftMargin;
if (hover)
{
s_state.hoverId = id;
// Mouse has just gone down over this element, so give it focus.
if (window->HasMouseGoneDown(Pixie::MouseButton_Left))
{
if (s_state.focusId != id)
{
s_state.keyRepeatTimer = 0.0f;
s_state.cursorBlinkTimer = 0.0f;
s_state.focusId = id;
}
// Move the cursor to whereever the user clicked.
s_state.keyboardCursorPosition = std::min((mouseX - textX) / s_state.font->GetCharacterWidth(), textLength);
// Also force the cursor to be visible.
s_state.cursorBlinkTimer = CursorBlinkTime;
}
// If mouse is still down over this element and it has focus, then it is pressed.
pressed = window->IsMouseDown(Pixie::MouseButton_Left) && s_state.focusId == id;
}
uint32_t boxColour = pressed || hover || s_state.focusId == id ? HoverColour : NormalColour;
uint32_t borderColour = s_state.focusId == id ? FocusBorderColour : BorderColour;
// Draw the input field.
FilledRect(x, y, width, height, boxColour, borderColour);
int textY = y + ((height - s_state.font->GetCharacterHeight()) >> 1);
Label(text, textX, textY, s_state.defaultTextColour);
if (s_state.focusId == id)
{
float delta = window->GetDelta();
// Input field has focus, draw the keyboard cursor and process input.
s_state.cursorBlinkTimer -= delta;
if (s_state.cursorBlinkTimer >= CursorBlinkTime * 0.5f)
FilledRect(textX + (s_state.keyboardCursorPosition * s_state.font->GetCharacterWidth()), textY + s_state.font->GetCharacterHeight() - 2, CursorWidth, 2, CursorColour, CursorColour);
if (s_state.cursorBlinkTimer <= 0.0f || window->IsAnyKeyDown())
s_state.cursorBlinkTimer = CursorBlinkTime;
// TODO: Not sure if this is the right way to do this. The timer should probably be per-key.
if (window->HasAnyKeyGoneDown())
{
s_state.keyRepeatTimer = 0.0f;
s_state.keyRepeatTime = KeyRepeatTimeInit;
}
s_state.keyRepeatTimer -= delta;
if (s_state.keyRepeatTimer <= 0.0f)
{
s_state.keyRepeatTimer = s_state.keyRepeatTime;
s_state.keyRepeatTime = KeyRepeatTimeRepeat;
if (window->IsKeyDown(Pixie::Key_Left))
{
s_state.keyboardCursorPosition = std::max(s_state.keyboardCursorPosition - 1, 0);
}
else if (window->IsKeyDown(Pixie::Key_Right))
{
s_state.keyboardCursorPosition = std::min(s_state.keyboardCursorPosition + 1, textLength);
}
else if (window->IsKeyDown(Pixie::Key_Backspace))
{
// Move cursor back.
s_state.keyboardCursorPosition--;
if (s_state.keyboardCursorPosition >= 0)
{
// Copy everything after the current position to the current position.
int position = s_state.keyboardCursorPosition;
int copyAmount = (int)strlen(text + position + 1) + 1;
memcpy(text + position, text + position + 1, copyAmount);
}
else
{
s_state.keyboardCursorPosition = 0;
}
}
else if (window->IsKeyDown(Pixie::Key_Delete))
{
// Delete the current character.
int position = s_state.keyboardCursorPosition;
if (position < textLength)
{
// Copy everything after the current position to the current position.
int copyAmount = (int)strlen(text + position + 1) + 1;
memcpy(text + position, text + position + 1, copyAmount);
}
}
else if (window->IsKeyDown(Pixie::Key_Home))
{
s_state.keyboardCursorPosition = 0;
}
else if (window->IsKeyDown(Pixie::Key_End))
{
s_state.keyboardCursorPosition = textLength;
}
}
// Process remaining ASCII input.
const char* inputCharacters = window->GetInputCharacters();
if (*inputCharacters)
{
for ( ; *inputCharacters; inputCharacters++)
{
if (s_state.keyboardCursorPosition == textBufferLength - 1)
break;
// Overwrite the character at the current position.
int position = s_state.keyboardCursorPosition;
assert(position >= 0 && position < textBufferLength - 1);
text[position] = *inputCharacters;
// If at the end of the string, add a null because we've just extended the string.
if (position == textLength)
{
position++;
assert(position >= 0 && position < textBufferLength);
text[position] = 0;
}
// Move the cursor.
s_state.keyboardCursorPosition = std::min(s_state.keyboardCursorPosition + 1, textBufferLength);
}
// We have consumed the input so remove it from the buffer.
window->ClearInputCharacters();
}
}
}
bool ImGui::Checkbox(const char* label, bool checked, int x, int y)
{
assert(label);
assert(s_state.HasStarted());
const int TextLeftMargin = 8;
const int BoxSize = 18;
const int CheckSize = 8;
Font* font = s_state.font;
int charHeight = font->GetCharacterHeight();
int textY = y + ((BoxSize - charHeight) >> 1) + 1;
Label(label, x + BoxSize + TextLeftMargin, textY, s_state.defaultTextColour);
bool wasChecked = checked;
if (Button(0, x, y, BoxSize, BoxSize))
checked = !checked;
if (wasChecked)
{
// Draw check mark.
int checkX = x + ((BoxSize - CheckSize) >> 1);
int checkY = y + ((BoxSize - CheckSize) >> 1);
Window* window = s_state.window;
int windowWidth = window->GetWidth();
uint32_t* pixels = window->GetPixels();
y = checkY;
for (int yy = y*windowWidth, x = 0; y < checkY + CheckSize; y++, x++, yy += windowWidth)
{
pixels[checkX + x + yy] = MAKE_RGB(255, 255, 255);
pixels[checkX + CheckSize - x - 1 + yy] = MAKE_RGB(255, 255, 255);
}
}
return checked;
}
bool ImGui::RadioButton(const char* label, bool checked, int x, int y)
{
assert(label);
assert(s_state.HasStarted());
const int TextLeftMargin = 8;
const int BoxSize = 18;
const int CheckSize = 8;
const uint32_t FontColour = s_state.defaultTextColour;
Font* font = s_state.font;
int charHeight = font->GetCharacterHeight();
int textY = y + ((BoxSize - charHeight) >> 1) + 1;
Label(label, x + BoxSize + TextLeftMargin, textY, FontColour);
bool wasChecked = checked;
if (Button(0, x, y, BoxSize, BoxSize))
checked = !checked;
if (wasChecked)
{
// Draw radio button mark.
int checkX = x + ((BoxSize - CheckSize) >> 1);
int checkY = y + ((BoxSize - CheckSize) >> 1);
FilledRect(checkX, checkY, CheckSize, CheckSize, FontColour, FontColour);
}
return checked;
}
void ImGui::Rect(int x, int y, int width, int height, uint32_t borderColour)
{
assert(s_state.HasStarted());
Window* window = s_state.window;
uint32_t* pixels = window->GetPixels();
int windowWidth = window->GetWidth();
int windowHeight = window->GetHeight();
pixels += x + (y*windowWidth);
for (int j = 0, ypos = y; j < height && ypos < windowHeight; j++, ypos++)
{
int left = x;
if (left >= 0 && left < windowWidth)
*pixels = borderColour;
int right = x + width - 1;
if (right >= 0 && right < windowWidth)
*(pixels + width - 1) = borderColour;
pixels += windowWidth;
}
pixels = window->GetPixels() + x + (y*windowWidth);
for (int i = 0, xpos = x; i < width; i++, xpos++)
{
int top = y;
if (top >= 0 && top < windowHeight)
*pixels = borderColour;
int bottom = y + height - 1;
if (bottom >= 0 && bottom < windowHeight)
*(pixels + ((height-1)*windowWidth)) = borderColour;
pixels++;
}
}
void ImGui::FilledRect(int x, int y, int width, int height, uint32_t colour, uint32_t borderColour)
{
assert(s_state.HasStarted());
Window* window = s_state.window;
uint32_t* pixels = window->GetPixels();
int windowWidth = window->GetWidth();
int windowHeight = window->GetHeight();
pixels += x + (y*windowWidth);
for (int j = 0, ypos = y; j < height && ypos < windowHeight; j++, ypos++)
{
for (int i = 0, xpos = x; i < width; i++, xpos++)
{
if (xpos < 0 || xpos >= windowWidth || ypos < 0 || ypos >= windowHeight)
continue;
*pixels = (i == 0 || i == width - 1 || j == 0 || j == height - 1) ? borderColour : colour;
pixels++;
}
pixels += windowWidth - width;
}
}