From 228eceeed40b0f86e75a394fe8d65e6e93ca2370 Mon Sep 17 00:00:00 2001 From: Sébastien Dailly Date: Tue, 12 Jan 2021 13:41:00 +0100 Subject: Move path, some refactoring --- script.it/script.ml | 120 +++++++++++++++++++++++++++++++--------------------- 1 file changed, 72 insertions(+), 48 deletions(-) (limited to 'script.it/script.ml') diff --git a/script.it/script.ml b/script.it/script.ml index 3859cc9..73e5ac3 100755 --- a/script.it/script.ml +++ b/script.it/script.ml @@ -3,7 +3,9 @@ open Note open Brr open Brr_note - +let post + : Brr_webworkers.Worker.t -> Worker_messages.to_worker -> unit + = Brr_webworkers.Worker.post (** Create the element in the page, and the event handler *) let canva @@ -47,7 +49,7 @@ let canva let click = Brr_note_kit.Mouse.left_down mouse - |> E.map (fun c -> `Click c) in + |> E.map (fun c -> `MouseDown c) in let up = Brr_note_kit.Mouse.left_up mouse @@ -190,8 +192,21 @@ let backgroundColor = Blog.Nord.nord0 let white = Jstr.v "#eceff4" let green = Jstr.v "#a3be8c" +let draw_point point context = + let module Cd2d = Brr_canvas.C2d in + let x, y = Gg.V2.to_tuple @@ Path.Point.get_coord point in + Cd2d.stroke_rect + ~x:(x -. 5.) + ~y:(y -. 5.) + ~w:10. + ~h:10. + context + (** Redraw the canva on update *) let on_change canva mouse_position timer state = + let pos = S.rough_value mouse_position in + let pos_v2 = Option.map Gg.V2.of_tuple pos in + let module Cd2d = Brr_canvas.C2d in let w, h = Brr_canvas.Canvas.(float_of_int @@ w canva, float_of_int @@ h canva) in @@ -213,7 +228,6 @@ let on_change canva mouse_position timer state = be far away in the past, and would give to the user a sensation of lag. *) - let pos = S.rough_value mouse_position in let current = begin match state.State.mode, pos with | Edit, Some point -> @@ -252,52 +266,63 @@ let on_change canva mouse_position timer state = Layer.Paths.to_canva (module Path.Fixed) (p, path.Outline.back) context state.rendering ); + (* Draw the selected path, and operate the modifications directly as a preview *) let () = match state.mode with - | Selection (Path id) -> - Cd2d.set_stroke_style context (Cd2d.color white); - List.iter - state.paths - ~f:(fun path -> - if id = path.Outline.id then - let p = path.Outline.path in - Layer.Paths.to_canva (module Path.Fixed) (p, path.Outline.back) context `Line - ) - | Selection (Point (id, point)) -> - (* As before, mark the selected path *) + | Selection t -> Cd2d.set_stroke_style context (Cd2d.color white); - - List.iter - state.paths - ~f:(fun outline -> - if id = outline.Outline.id then - let path = begin match pos with - | Some pos -> - - let pos_v2 = Gg.V2.of_tuple pos in - if Elements.Timer.delay timer < 0.3 then - outline.Outline.path - else - let point' = Path.Point.copy point pos_v2 in - begin match Path.Fixed.replace_point outline.Outline.path point' with - | None -> outline.Outline.path - | Some p -> p - end - | None -> outline.Outline.path end in - Layer.Paths.to_canva (module Path.Fixed) (path, outline.Outline.back) context `Line - ); - - (* Now draw the selected point *) - let x, y = Gg.V2.to_tuple @@ Path.Point.get_coord point in - Cd2d.stroke_rect - ~x:(x -. 5.) - ~y:(y -. 5.) - ~w:10. - ~h:10. - context; + begin match pos_v2, Selection.find_selection t state.paths with + (* The selected element does not exist, just do nothing *) + | _, None -> () + + (* There is no click on the canva, print the line *) + | None, Some (Path outline) -> + Layer.Paths.to_canva + (module Path.Fixed) + (outline.path, outline.back) + context + `Line; + + (* The user is modifiying the path *) + | Some pos_v2, Some (Path outline) -> + (* Translate the path *) + let delta = Gg.V2.(pos_v2 - state.State.mouse_down_position) in + let path = Path.Fixed.map + outline.Outline.path + (fun pt -> Path.Point.get_coord pt + |> Gg.V2.add delta + |> Path.Point.copy pt) in + Layer.Paths.to_canva + (module Path.Fixed) + (path, path) + context + `Line; + + (* The user is modifiying the point *) + | Some pos_v2, Some (Point (outline, point)) when Elements.Timer.delay timer > 0.3 -> + let point' = Path.Point.copy point pos_v2 in + let path = begin match Path.Fixed.replace_point outline.Outline.path point' with + | None -> outline.Outline.path + | Some p -> p + end in + + Layer.Paths.to_canva + (module Path.Fixed) + (path, path) + context + `Line; + draw_point point context + + | _, Some (Point (outline, point)) -> + Layer.Paths.to_canva + (module Path.Fixed) + (outline.path, outline.back) + context + `Line; + draw_point point context + + end | _ -> () in - - () let spawn_worker () = @@ -387,12 +412,11 @@ let page_main id = let _ = E.select [ E.map (fun _ -> ()) (S.changes mouse_position) - ; E.map (fun _ -> ()) (S.changes parameters.angle) - ; E.map (fun _ -> ()) (S.changes parameters.width) ; E.map (fun _ -> ()) parameters.rendering ; E.map (fun _ -> ()) worker_event ; parameters.delete ] - |> fun ev -> E.log ev (fun _ -> on_change canva mouse_position timer (S.value state) ) + |> fun ev -> E.log ev (fun _ -> + on_change canva mouse_position timer (S.value state) ) |> Option.iter Logr.hold in -- cgit v1.2.3