summaryrefslogtreecommitdiff
path: root/gui.h
blob: e94267a90f87750941b59c21307673398531adf5 (plain)
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
ui_t beginui(prge_window_t window, input_t input, shader_t shader, rect_t rect, arena_t *arena, f32 padding)
{
	assert(arena);
	assert(shader.id);
	assert(window.width && window.height);

	v2 start = {rect.start.x, rect.end.y};
	ui_t ui = {window, input, shader, rect, ui_list, start, arena, padding};
	glUseProgram(ui.shader.id);
	mat4 projection = ortho(0.0f, ui.window.width, 0.0f, ui.window.height, -1.0f, 1.0f);
	shader_set_mat4(ui.shader, SHADER_PROJECTION_MATRIX, projection);
	glDisable(GL_DEPTH_TEST);

	f32 width = rect.end.x - rect.start.x;
	f32 height = rect.end.y - rect.start.y;
	mesh_t background = gen_quad_mesh(ui.arena, DEFAULT_TRANSFORM, width, height);
	v2 half_width_height = {width/2.0f, height/2.0f};
	start = v2_add(rect.start, half_width_height);
	mat4 world = mat4_make_transl(v3_from_v2(start));
	shader_set_mat4(ui.shader, SHADER_MODEL_MATRIX, world);
	v4 color = {0.149f, 0.4f, 0.42f, 1.0f};
	shader_set_v4(ui.shader, "color", color);
	draw_mesh(background);
	clear_mesh(&background);
	
	return ui;
}

i32 button(ui_t *ui, const char *text, f32 width, f32 height)
{
	if (width <= 0.0f || height <= 0.0f)
		return 0;

	v4 color = {0.075f, 0.525f, 0.549f, 1.0f};
	i32 pressed = 0;

	v2 last = {ui->last.x + ui->padding, ui->last.y - height - ui->padding};
	rect_t rect = {last, v2_add(last, (v2){width, height})};
	if (in_rect(ui->input.mouse.pos, rect) && !ui->input.mouse.capture) {
		color = (v4){0.3f, 0.61f, 0.63f, 1.0f};
		if (is_key_pressed(ui->input.mouse.left)) {
			pressed = 1;
			printf("%s\n", text);
		}
	}

	v2 center = {width/2.0f, height/2.0f};
	v2 start = v2_add(last, center);
	ui->last.x += width + 2.0f*ui->padding;
	mat4 world = mat4_make_transl(v3_from_v2(start));

	shader_set_mat4(ui->shader, SHADER_MODEL_MATRIX, world);
	shader_set_v4(ui->shader, "color", color);

	mesh_t quad = gen_quad_mesh(ui->arena, DEFAULT_TRANSFORM, width, height);

	draw_mesh(quad);
	clear_mesh(&quad);

	return pressed;
}

void endui(void)
{
	glEnable(GL_DEPTH_TEST);
	glUseProgram(0);
}