summaryrefslogtreecommitdiff
path: root/gui.c
blob: 0fe1f69b5f5128acdc50d1994449ae58a90499da (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
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
#include "gui.h"
#include "memory.h"
#include "plat.h"
#include "render.h"
#include "standard.h"

void* gui_alloc(GUI* g, int size) {
	GUI_El* e = arena_alloc(g->a, size);
	if (g->prev)
		g->prev->next = e;
	if (!g->first)
		g->first = e;
	g->prev = e;
	e->next = 0;
	return e;
}

void gui_begin(GUI* g, const App* app, Arena* a) {
	g->a = a;
	g->app = app;
	g->pad = 2;
	g->first = 0;
	g->cursor[0] = 10;
	g->cursor[1] = 10;
}

void gui_get_layout(GUI* g, Rect* r) {
	r->x = g->cursor[0];
	r->y = g->cursor[1];
	g->cursor[1] += r->h;
}

int gui_text_width(const char* text, int len) {
	(void)text;
	return len * 10;
}

int gui_text_height(void) {
	return 10;
}

int mouse_over(const App* app, const Rect* r) {
	return
		app->mx > r->x &&
		app->my > r->y &&
		app->mx < r->x + r->w &&
		app->my < r->y + r->h;
}

Rect gui_viewport(GUI* g) {
	Rect r = { 0 };
	r.w = g->app->w;
	r.h = g->app->h;
	return r;
}

Rect gui_cut_left(Rect* a, int v) {
	Rect r = * a;
	a->x += v;
	a->w -= v;
	r.w -= a->w;
	return r;
}

Rect gui_cut_right(Rect* a, int v) {
	Rect r = *a;
	a->w -= v;
	r.x += v;
	r.w -= v;
	return r;
}

Rect gui_cut_down(Rect* a, int v) {
	Rect r = *a;
	a->h -= v;
	r.y += v;
	r.h -= v;
	return r;
}

Rect gui_cut_up(Rect* a, int v) {
	Rect r = * a;
	a->y += v;
	a->h -= v;
	r.h -= a->h;
	return r;
}

int gui_btn(GUI* g, Rect r, const char* text) {
	int len = string_len(text);
	GUI_Btn* e = gui_alloc(g, sizeof *e);
	e->el.type = gui_el_type_btn;
	e->el.r = r;
	e->text = arena_alloc_aligned(g->a, len + 1, 1);
	string_copy(e->text, text);
	e->hover = mouse_over(g->app, &e->el.r);
	e->active = e->hover &&
		g->app->mbtn_states[mbtn_left] & key_state_pressed;
	return e->hover &&
		g->app->mbtn_states[mbtn_left] & key_state_just_released;
}

void gui_scrollable(
	GUI* g,
	Rect r,
	GUI_Scroll_State* state,
	GUI_Scrollable_Draw_Fn fn
) {
	GUI_Scrollable* e = gui_alloc(g, sizeof *e);
	e->el.type = gui_el_type_scrollable;
	e->el.r = r;
	e->ss = state;
	e->draw = fn;
}

void rect_cfit(Rect* r) {
	r->x++;
	r->y++;
	r->w -= 2;
	r->h -= 2;
}

void draw_container(Renderer* r, const Rect* rect, int active) {
	const Colour bg   = make_colour(0xc3c3c3, 0xff);
	Colour out1       = make_colour(0xffffff, 0xff);
	Colour out2       = make_colour(0x000000, 0xff);
	Rect t = *rect;
	if (active) {
		Colour t = out1;
		out1 = out2;
		out2 = t;
	}
	ren_rect(r, bg, rect);
	t.h = 1;
	ren_rect(r, out1, &t);
	t.w = 1;
	t.h = rect->h;
	ren_rect(r, out1, &t);
	t.h--;
	t.x += rect->w - 1;
	t.y++;
	ren_rect(r, out2, &t);
	t.y = rect->y + rect->h - 1;
	t.x = rect->x + 1;
	t.w = rect->w - 1;
	t.h = 1;
	ren_rect(r, out2, &t);
}

void draw_btn(GUI* g, Renderer* r, const GUI_Btn* btn) {
	const Colour txt  = make_colour(0x000000, 0xff);
	int tp[2];
	Rect rect = btn->el.r;
	(void)g;
	tp[0] =
		rect.x + rect.w / 2 -
		gui_text_width(btn->text, string_len(btn->text)) / 2;
	tp[1] =
		rect.y + rect.h / 2 -
		gui_text_height() / 2;
	if (btn->active) {
		tp[0]++;
		tp[1]++;
	}
	draw_container(r, &rect, btn->active);
	rect_cfit(&rect);
	ren_clip(r, &rect);
	ren_text(r, txt, tp[0], tp[1], btn->text);
	ren_clipr(r);
}

void draw_scrollable(
	GUI* g,
	Renderer* r,
	const GUI_Scrollable* s
) {
	Rect rect = s->el.r;
	draw_container(r, &s->el.r, 0);
	rect_cfit(&rect);
	ren_clip(r, &rect);
	s->draw(g, r, &rect, s->ss);
	ren_clipr(r);
}

void gui_end(GUI* g, Renderer* r) {
	GUI_El* el = g->first;
	while (el) {
		switch (el->type) {
			case gui_el_type_btn:
				draw_btn(g, r, (GUI_Btn*)el);
				break;
			case gui_el_type_scrollable:
				draw_scrollable(g, r, (GUI_Scrollable*)el);
				break;
		}
		el = el->next;
	}
}