-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathui.h
More file actions
63 lines (54 loc) · 1.58 KB
/
ui.h
File metadata and controls
63 lines (54 loc) · 1.58 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
/* Just helper functions for relative positioning of UI elements. */
#ifndef UI_H
#define UI_H
#include "window.h"
/* Offset+relative -> `Vector2`. */
static Vector2 ui_rel(Window window, Vector4 rel) {
return (Vector2) {
.x = rel.x + rel.y * window.length,
.y = rel.z + rel.w * window.length
};
}
/* Offset+relative positioning and scaling as `Rectangle`. */
static Rectangle ui_rect(Window window, Vector4 rel_pos, Vector4 rel_scale, bool center) {
Vector2 pos = ui_rel(window, rel_pos);
Vector2 scale = ui_rel(window, rel_scale);
return (Rectangle) {
.x = center
? pos.x - scale.x / 2
: pos.x,
.y = center
? pos.y - scale.y / 2
: pos.y,
.width = scale.x,
.height = scale.y
};
}
/* Scaling for text. */
typedef struct UIText {
const char *text;
Font font;
Vector2 position,
origin;
int scale,
spacing;
} UIText;
static UIText ui_text(Window window, Font font, const char *text, int spacing,
Vector4 rel_pos, Vector4 rel_scale) {
Vector2 pos = ui_rel(window, rel_pos);
Vector2 scale = ui_rel(window, rel_scale);
// Boom bam bosh
UIText template = {
.text = text,
.font = font,
.position = pos,
.origin = (Vector2) { scale.x / 2, scale.y / 2 },
.scale = 1,
.spacing = spacing
};
// Adjust scale to fit
while (MeasureTextEx(font, text, template.scale, spacing).x < scale.x)
template.scale += 1;
return template;
}
#endif