-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathui.c
More file actions
113 lines (106 loc) · 3.02 KB
/
Copy pathui.c
File metadata and controls
113 lines (106 loc) · 3.02 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
#include "common.h"
static Font g_font = {0};
#define FONT_BTN 15
#define FONT_LABEL 14
#define BTN_RADIUS 0.28f
#define BTN_SEGS 8
#define COL_WHITE CLITERAL(Color){255, 255, 255, 255}
void SetAppFont(Font f)
{
g_font = f;
}
void DrawTxt(const char *text, int x, int y, int size, Color col)
{
if (g_font.texture.id > 0)
DrawTextEx(g_font, text,
(Vector2){(float)x, (float)y},
(float)size, 1.0f, col);
else
DrawText(text, x, y, size, col);
}
int MeasureTxt(const char *text, int size)
{
if (g_font.texture.id > 0)
return ((int)MeasureTextEx(g_font, text, (float)size, 1.0f).x);
return (MeasureText(text, size));
}
bool UiButton(Rectangle r, const char *label, bool selected)
{
Vector2 mp;
bool over;
bool down;
Color bg;
Color fg;
Color border;
int tw;
mp = GetMousePosition();
over = CheckCollisionPointRec(mp, r);
down = over && IsMouseButtonDown(MOUSE_BUTTON_LEFT);
bg = COL_SURFACE2;
fg = COL_TEXT_DIM;
border = COL_BORDER;
if (over && !down && !selected)
{ bg = COL_SURFACE3; fg = COL_TEXT; border = COL_BORDER_FOCUS; }
if (down)
{ bg = COL_PRIMARY_ACTIVE; fg = COL_WHITE; border = COL_BORDER_FOCUS; }
if (selected)
{ bg = COL_PRIMARY; fg = COL_WHITE; border = COL_PRIMARY; }
DrawRectangleRounded(r, BTN_RADIUS, BTN_SEGS, bg);
DrawRectangleRoundedLinesEx(r, BTN_RADIUS, BTN_SEGS, 1.5f, border);
tw = MeasureTxt(label, FONT_BTN);
DrawTxt(label,
(int)(r.x + r.width / 2.0f - tw / 2.0f),
(int)(r.y + r.height / 2.0f - FONT_BTN / 2.0f - 1),
FONT_BTN, fg);
return (over && IsMouseButtonReleased(MOUSE_BUTTON_LEFT));
}
void UiSlider(Rectangle r, const char *label, int *val, int lo, int hi)
{
float pct;
int track_y;
int knob_x;
Vector2 mp;
bool drag;
Color knob_col;
char buf[16];
float t;
if (label && label[0] != '\0')
DrawTxt(label, (int)r.x, (int)r.y - 17, FONT_LABEL, COL_MUTED);
if (hi > lo)
pct = (float)(*val - lo) / (float)(hi - lo);
else
pct = 0.0f;
track_y = (int)(r.y + r.height / 2.0f);
knob_x = (int)(r.x + pct * r.width);
DrawRectangleRounded(
(Rectangle){r.x, (float)(track_y - 3), r.width, 6},
1.0f, 4, COL_SURFACE3);
if (knob_x - (int)r.x > 2)
DrawRectangleRounded(
(Rectangle){r.x, (float)(track_y - 3),
(float)(knob_x - (int)r.x), 6},
1.0f, 4, COL_PRIMARY);
mp = GetMousePosition();
drag = IsMouseButtonDown(MOUSE_BUTTON_LEFT)
&& CheckCollisionPointRec(mp,
(Rectangle){r.x - 10, r.y - 10, r.width + 20, r.height + 20});
knob_col = COL_PRIMARY;
if (CheckCollisionPointCircle(
mp, (Vector2){(float)knob_x, (float)track_y}, 11.0f))
knob_col = COL_PRIMARY_HOVER;
if (drag)
knob_col = COL_PRIMARY_ACTIVE;
DrawCircle(knob_x, track_y, 10.0f, CLITERAL(Color){0, 0, 0, 30});
DrawCircle(knob_x, track_y, 9.0f, COL_WHITE);
DrawCircle(knob_x, track_y, 7.0f, knob_col);
if (drag)
{
t = (mp.x - r.x) / r.width;
if (t < 0.0f) t = 0.0f;
if (t > 1.0f) t = 1.0f;
*val = lo + (int)(t * (float)(hi - lo) + 0.5f);
}
snprintf(buf, sizeof(buf), "%d", *val);
DrawTxt(buf, (int)(r.x + r.width + 12),
track_y - FONT_LABEL / 2 - 1, FONT_LABEL, COL_TEXT_DIM);
}