aboutsummaryrefslogtreecommitdiff
path: root/client/paintui/paintui.ha
blob: 4f656c4d404baf569aeaf143f7f98ea5366df27f (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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// paintui handles turning sdl input things
// into drawing instructions
// it might have some kind of state machine
// for doing smooth lines between discrete mouse positions

// all mouse pos stuff in this module is in world coordinates
// someone else is responsible for doing the transform from
// screen to world coords.

use drawing;
use drawing::{pos};

export const sizes: [_]u8 = [
	1, 2, 4, 8, 16, 32, 48, 64, 80, 96, 112, 128
];

const colors: [_]u32 = [
	0xffffff, // white
	0xff4040, // red
	0xff8000, // orange
	0xc0c040, // yellow 
	0x00c000, // green
	0x00c0c0, // teal
	0x4040ff, // blue
	0xc000c0, // purple
 	0x808080, // grey
	0x000000, // black
];

export type state = struct {
	// last known mouse position, in world coordinates
	last_mouse_pos: pos,
	// last known mouse pressed-ness
	last_mouse_pressed: bool,

	size_idx: size,
	color: u32
};

fn abs(x: i32) i32 = if(x < 0) -x else x;

export fn tick(
	pstate: *state,
	mouse_pos: pos, mouse_pressed: bool
) (void | drawing::op) = {

	// always update last_mouse_pos and last_mouse_pressed
	defer {
		pstate.last_mouse_pos = mouse_pos;
		pstate.last_mouse_pressed = mouse_pressed;
	};

	const radius = sizes[pstate.size_idx];

	const newly_pressed = mouse_pressed && !pstate.last_mouse_pressed;
	const position_moved = (mouse_pos.0 != pstate.last_mouse_pos.0
							|| mouse_pos.1 != pstate.last_mouse_pos.1);

	if (!mouse_pressed) return void;
	return if (newly_pressed) drawing::op_circle {
		pos = mouse_pos,
		radius = radius,
		color = pstate.color,
	} else if (position_moved) drawing::op_stroke {
			pos0 = pstate.last_mouse_pos,
			pos1 = mouse_pos,
			radius = radius,
			color = pstate.color,
	} else void;
};

export fn mousewheel(pstate: *state, amt: i32) void = {
	if (amt < 0 && pstate.size_idx > 0)
		pstate.size_idx -= 1
	else if (amt > 0 && pstate.size_idx < len(sizes) - 1)
		pstate.size_idx += 1;
};

export fn key(pstate: *state, key: uint) void = {
	assert(key <= 9, "what what what what what");
	pstate.color = colors[key];
};