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 +++++++++++++++++++---------- drawing/drawing.ha | 17 ++++++++++++++--- 2 files changed, 33 insertions(+), 13 deletions(-) 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 = { diff --git a/drawing/drawing.ha b/drawing/drawing.ha index a7d9e1f..54116a3 100644 --- a/drawing/drawing.ha +++ b/drawing/drawing.ha @@ -103,9 +103,19 @@ export fn circle_hollow(picture: *picture, c: pos, r: i32, color: u32) void = { export fn stroke(picture: *picture, c0: pos, c1: pos, r: i32, color: u32) void = { const dx = c1.0 - c0.0; const dy = c1.1 - c0.1; - const count = (abs(dx)+abs(dy))/r; - const sx = dx: f64 / (count-1): f64; - const sy = dy: f64 / (count-1): f64; + const d = math::sqrtf64((dx*dx+dy*dy):f64):i32; + const count = d/r + 1; + if (count == 0) { + circle(picture, c1, r, color); + return; + } else if (count == 1) { + circle(picture, c0, r, color); + circle(picture, c1, r, color); + return; + }; + fmt::printfln("count: {}",count)!; + const sx = dx: f64 / (count): f64; + const sy = dy: f64 / (count): f64; for (let i = 0i32; i < count; i += 1) { const cx = c0.0 + math::roundf64(i:f64*sx):i32; @@ -113,6 +123,7 @@ export fn stroke(picture: *picture, c0: pos, c1: pos, r: i32, color: u32) void = circle(picture, (cx, cy), r, color); }; + circle(picture, c1, r, color); }; // ehh should check bounding box instead of doing all pictures maybe -- cgit v1.2.3