From 9bb892b35308919ff93d22e12387e6bdc0a64223 Mon Sep 17 00:00:00 2001 From: ubq323 Date: Mon, 15 Apr 2024 22:12:38 +0100 Subject: get smooth brush strokes working to my present satisfaction --- client/paintui/paintui.ha | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) (limited to 'client') diff --git a/client/paintui/paintui.ha b/client/paintui/paintui.ha index 81fa7ca..4f656c4 100644 --- a/client/paintui/paintui.ha +++ b/client/paintui/paintui.ha @@ -37,27 +37,36 @@ export type state = struct { 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; }; - // if mouse down and (position moved OR newly pressed) - return if (mouse_pressed - && (mouse_pos.0 != pstate.last_mouse_pos.0 - || mouse_pos.1 != pstate.last_mouse_pos.1 - || (!pstate.last_mouse_pressed))) - drawing::op_stroke { + 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, - pos0 = (0,0), - radius = sizes[pstate.size_idx], + radius = radius, color = pstate.color, - } - else void; + } else void; }; export fn mousewheel(pstate: *state, amt: i32) void = { -- cgit v1.2.3