aboutsummaryrefslogtreecommitdiff
path: root/apps/paint/main.c
blob: 464a925cb0cf6b893f8e61b4526a8ce5dafdd7d0 (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
// MIT License, Copyright (c) 2021 Marvin Borner

#include <libgui/gui.h>

#define COLOR_SIZE 50
#define TOOLBAR_MARGIN 2

static const u32 colors[] = { COLOR_BLACK, COLOR_RED,	  COLOR_GREEN, COLOR_YELLOW,
			      COLOR_BLUE,  COLOR_MAGENTA, COLOR_CYAN,  COLOR_WHITE };
static u32 current_color = COLOR_BLACK;
static u32 size = 5;

static void mousemove(struct gui_event_mouse *event)
{
	static vec2 last = { 0 };

	if (event->scroll && !(event->scroll == -1 && size <= 1))
		size += event->scroll;

	if (event->but.left) {
		gui_draw_line(event->win, event->widget, GUI_LAYER_FG, last, event->pos, size,
			      current_color);
		gui_redraw_widget(event->win, event->widget);
	}
	last = event->pos;
}

static void color_click(struct gui_event_mouse *event)
{
	current_color = colors[event->widget - 2];
}

// TODO: Simplify using predefined widgets and utilities
int main(void)
{
	u32 win = gui_new_window(APPNAME);
	vec2 win_size = gui_window_size(win);

	u32 toolbar = gui_new_widget(win, GUI_MAIN, vec2(0, 0),
				     vec2(win_size.x, COLOR_SIZE + 2 * TOOLBAR_MARGIN));
	gui_fill(win, toolbar, GUI_LAYER_BG, COLOR_WHITE);

	u32 color_count = COUNT(colors);
	for (u32 i = 0; i < color_count; i++) {
		u32 color = gui_new_widget(win, toolbar,
					   vec2(TOOLBAR_MARGIN + i * (COLOR_SIZE + TOOLBAR_MARGIN),
						TOOLBAR_MARGIN),
					   vec2(COLOR_SIZE, COLOR_SIZE));
		gui_fill(win, color, GUI_LAYER_FG, colors[i]);
		gui_draw_border(win, color, GUI_LAYER_FG, 2, COLOR_BLACK);
		gui_listen_widget(win, color, GUI_LISTEN_MOUSECLICK, (u32)color_click);
	}

	u32 canvas =
		gui_new_widget(win, GUI_MAIN, vec2(0, COLOR_SIZE + 2 * TOOLBAR_MARGIN),
			       vec2(win_size.x, win_size.y - (COLOR_SIZE + 2 * TOOLBAR_MARGIN)));
	gui_fill(win, canvas, GUI_LAYER_BG, COLOR_WHITE);
	gui_listen_widget(win, canvas, GUI_LISTEN_MOUSEMOVE, (u32)mousemove);

	gui_redraw_window(win);
	gui_loop();
	return 0;
}