summaryrefslogtreecommitdiff
path: root/ui.c
blob: a7d1070caeaa70fc43b087f816435659e197aeaf (plain) (blame)
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
#include "config.h"
#include "render.h"
#include "plat.h"
#include "rcache.h"
#include "ui.h"

Rectangle rectcut_left(Rectangle* l, int a) {
	Rectangle r;
	r.x = l->x;
	r.y = l->y;
	r.w = a;
	r.h = l->h;
	l->x += a;
	l->w -= a;
	return r;
}

Rectangle rectcut_right(Rectangle* l, int a) {
	Rectangle r;
	r.x = l->x + l->w - a;
	r.y = l->y;
	r.w = a;
	r.h = l->h;
	l->w -= a;
	return r;
}

Rectangle rectcut_top(Rectangle* l, int a) {
	Rectangle r;
	r.x = l->x;
	r.y = l->y;
	r.w = l->w;
	r.h = a;
	l->y += a;
	l->h -= a;
	return r;
}

Rectangle rectcut_bottom(Rectangle* l, int a) {
	Rectangle r;
	r.x = l->x;
	r.y = l->y + l->h - a;
	r.w = l->w;
	r.h = a;
	l->h -= a;
	return r;
}

Rectangle shrink_rect(const Rectangle* l, int a) {
	Rectangle r;
	int a2 = a * 2;
	r.x = l->x + a;
	r.y = l->y + a;
	r.w = l->w - a2;
	r.h = l->h - a2;
	return r;
}

Rectangle centre_rect(
	const Rectangle* l,
	const Rectangle* t
) {
	Rectangle r;
	r.x = l->x + l->w / 2 - t->w / 2;
	r.y = l->y + l->h / 2 - t->h / 2;
	r.w = t->w;
	r.h = t->h;
	return r;
}

void init_ui(UI* u, Font* f) {
	u->font = f;
}

void ui_begin(UI* u, Rectangle* l) {
	(void)u;
	l->x = 0;
	l->y = 0;
	l->w = get_render_w();
	l->h = get_render_h();
}

void ui_end(UI* u) {
	(void)u;
}

void ui_label(
	UI* u,
	const Rectangle* l,
	const char* text
) {
	rc_add_cmd_rfont_text_col(
		u->font,
		l->x,
		l->y,
		text,
		theme_text_colour
	);
}

int ui_button(
	UI* u,
	const Rectangle* r,
	const char* label
) {
	Rectangle o, t;
	Colour c;
	int h;
	o = shrink_rect(r, theme_outline_width);
	h = point_rect_overlap(
		&o,
		get_mouse_x(),
		get_mouse_y()
	);
	if (h) {
		if (mbtn_pressed(mbtn_left))
			c = theme_active_colour;
		else
			c = theme_hovered_colour;
	} else
		c = theme_background_colour;
	rc_add_cmd_rect(r, theme_outline_colour);
	rc_add_cmd_rect(&o, c);
	t = text_rect(u->font, label);
	o = centre_rect(&o, &t);
	rc_add_cmd_rfont_text_col(
		u->font,
		o.x,
		o.y,
		label,
		theme_text_colour
	);
	return h && mbtn_just_released(mbtn_left);
}

void ui_container(
	UI* u,
	const Rectangle* l,
	Rectangle* r,
	const Rectangle* c
) {
	Rectangle o, cl;
	(void)u;
	rc_add_cmd_rect(l, theme_outline_colour);
	o = shrink_rect(l, theme_outline_width);
	rc_add_cmd_rect(&o, theme_background_colour);
	cl = o;
	cl.w += cl.x;
	cl.h += cl.y;
	rc_add_cmd_clip(&cl);
	*r = shrink_rect(&o, theme_padding);
}