From a8f37f041dce3f16917b6659d3ca97492f178f4d Mon Sep 17 00:00:00 2001 From: Sébastien Dailly Date: Sun, 3 Jan 2021 05:42:35 +0100 Subject: Communication with webworker --- script.it/state.ml | 69 +++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 53 insertions(+), 16 deletions(-) (limited to 'script.it/state.ml') diff --git a/script.it/state.ml b/script.it/state.ml index 5a1ef8f..cfde0b0 100755 --- a/script.it/state.ml +++ b/script.it/state.ml @@ -1,12 +1,8 @@ open StdLabels open Brr -let expected_host = Blog.Hash_host.expected_host - let backgroundColor = Blog.Nord.nord0 -let timer, tick = Elements.Timer.create () - type mode = | Edit | Selection of int @@ -22,10 +18,21 @@ type button_events = [ `Delete | `Export ] +type render_event = + [ + `Rendering of Layer.Paths.printer + ] + +type worker_event = + [ `Basic of Jv.t + | `Complete of (int * (Path.Fixed.path array)) + ] type events = [ canva_events | button_events + | render_event + | worker_event | `Point of float * (float * float) | `Width of float | `Angle of float @@ -41,12 +48,13 @@ type state = ; current : Path.Path_Builder.t ; width : float ; angle : float + ; rendering : Layer.Paths.printer } -let insert_or_replace state ((x, y) as p) path = +let insert_or_replace state ((x, y) as p) stamp path = let width = state.width and angle = state.angle in - let point = Path.Point.create ~x ~y ~angle ~width in + let point = Path.Point.create ~x ~y ~angle ~width ~stamp in match Path.Path_Builder.peek path with | None -> Path.Path_Builder.add_point @@ -97,14 +105,15 @@ let update_selection id state f = { state with paths } let do_action - : events -> state -> state - = fun event state -> + : Brr_webworkers.Worker.t -> Elements.Timer.t -> events -> state -> state + = fun worker timer event state -> match event, state.mode with - | `Point (_delay, point), Edit -> + | `Point (delay, point), Edit -> (* Add the point in the list *) let current = insert_or_replace state point + delay state.current in { state with current } @@ -115,16 +124,17 @@ let do_action let width = state.width and angle = state.angle in + let stamp = 0. in let point = match check_selection p state.paths with | None -> (* Start a new path with the point clicked *) - Path.Point.create ~x ~y ~angle ~width + Path.Point.create ~x ~y ~angle ~width ~stamp | Some (p, _) -> (* If the point is close to an existing path, we use the closest point in the path instead *) let x, y = Gg.V2.to_tuple p in - Path.Point.create ~x ~y ~angle ~width + Path.Point.create ~x ~y ~angle ~width ~stamp in let current = Path.Path_Builder.add_point @@ -150,6 +160,7 @@ let do_action end | `Out point, Edit -> + let stamp = Elements.Timer.delay timer in Elements.Timer.stop timer; begin match Path.Path_Builder.peek2 state.current with (* If there is at last two points selected, handle this as a curve @@ -162,14 +173,20 @@ let do_action | Some (p, _) -> Gg.V2.to_tuple p in *) - let current = insert_or_replace state point state.current in + let current = insert_or_replace state point stamp state.current in let paths = let last = Path.Fixed.to_fixed (module Path.Path_Builder) current in + + let id = Path.Fixed.id last + and path = Path.Fixed.path last in + let () = Brr_webworkers.Worker.post worker (`Complete (id, path)) in last::state.paths and current = Path.Path_Builder.empty in + + { state with mode = Out ; paths; current } @@ -198,8 +215,7 @@ let do_action | `Export, _ -> let my_host = Uri.host @@ Window.location @@ G.window in - - if (Hashtbl.hash my_host) = expected_host then ( + if (Hashtbl.hash my_host) = Blog.Hash_host.expected_host then ( (* Convert the path into an sVG element *) let svg = Layer.Svg.svg ~at:Brr.At.[ @@ -208,11 +224,11 @@ let do_action (List.map state.paths ~f:(fun path -> - Path.to_svg + Layer.Paths.to_svg ~color:backgroundColor (module Path.Fixed) path - `Fill + state.rendering )) in let content = El.prop Elements.Prop.outerHTML svg in @@ -248,6 +264,26 @@ let do_action | `Delete, Out -> state + | `Rendering rendering, _ -> + { state with rendering} + + + | `Basic t, _ -> + Console.(log [t]); + state + + | `Complete (id, paths), _ -> + let paths = List.map state.paths + ~f:(fun path -> + let id' = Path.Fixed.id path in + match id = id' with + | false -> path + | true -> + Path.Fixed.update path paths + ) in + { state with paths } + + (* Some non possible cases *) | `Out _, Out | `Point _, Out @@ -263,4 +299,5 @@ let init = ; mode = Out ; angle = 30. ; width = 10. + ; rendering = `Fill } -- cgit v1.2.3