aboutsummaryrefslogtreecommitdiff
path: root/script.it/state.ml
diff options
context:
space:
mode:
Diffstat (limited to 'script.it/state.ml')
-rwxr-xr-xscript.it/state.ml114
1 files changed, 73 insertions, 41 deletions
diff --git a/script.it/state.ml b/script.it/state.ml
index c147c2c..403efbe 100755
--- a/script.it/state.ml
+++ b/script.it/state.ml
@@ -23,7 +23,7 @@ type render_event =
type worker_event =
[ `Basic of Jv.t
- | `Complete of Path.Fixed.t
+ | `Complete of Outline.t
]
type events =
@@ -42,7 +42,7 @@ type events =
*)
type state =
{ mode : mode
- ; paths : Path.Fixed.t list
+ ; paths : Outline.t list
; current : Path.Path_Builder.t
; width : float
; angle : float
@@ -78,29 +78,31 @@ let insert_or_replace state ((x, y) as p) stamp path =
(** Update the path in the selection with the given function applied to
every point *)
-let update_path_selection id paths f =
- List.map paths
- ~f:(fun path ->
- let id' = Path.Fixed.id path in
- match id = id' with
- | false -> path
- | true -> Path.Fixed.map path f
- )
+let update_path_selection
+ : int -> Outline.t list -> (Path.Point.t -> Path.Point.t) -> Outline.t list
+ = fun id outlines f ->
+ List.map outlines
+ ~f:(fun outline ->
+ let id' = outline.Outline.id in
+ match id = id' with
+ | false -> outline
+ | true -> {outline with path = Path.Fixed.map outline.path f}
+ )
let update_point_selection state path_id point f =
let paths = List.map state.paths
~f:(fun p ->
- match Path.Fixed.id p = path_id with
+ match p.Outline.id = path_id with
| false -> p
| true ->
- Path.Fixed.map
- p
- (fun p ->
- if (Path.Point.id p = Path.Point.id point) then
- f p
- else
- p
- )
+ { p with path = Path.Fixed.map
+ p.path
+ (fun p ->
+ if (Path.Point.id p = Path.Point.id point) then
+ f p
+ else
+ p
+ ) }
) in
{ state with paths }
@@ -129,7 +131,7 @@ let delete state worker =
let paths = List.filter
state.paths
~f:(fun p ->
- Path.Fixed.id p != id
+ p.Outline.id != id
) in
{ state with paths ; mode = Out}
@@ -137,7 +139,7 @@ let delete state worker =
List.iter
state.paths
~f:(fun p ->
- let id' = Path.Fixed.id p in
+ let id' = p.Outline.id in
match id' = id with
| false -> ()
| true ->
@@ -165,30 +167,46 @@ let tick (delay, point) state =
{ state with current }
| _ -> state
-let angle angle state =
+let angle worker angle state =
match state.mode with
(* Change angle for the whole path *)
| Selection (Path s) ->
let state = { state with angle } in
let paths = update_path_selection s state.paths (fun p -> Path.Point.set_angle p angle) in
+ (* Update the event to the worker *)
+ let outline = List.find paths
+ ~f:(fun o -> o.Outline.id = s) in
+ post worker (`Back outline);
{state with paths }
(* Change angle localy *)
| Selection (Point (s, point)) ->
let state = update_point_selection state s point
(fun p -> Path.Point.set_angle p angle) in
+ (* Update the event to the worker *)
+ let outline = List.find state.paths
+ ~f:(fun o -> o.Outline.id = s) in
+ post worker (`Back outline);
{ state with angle }
| _ ->
{ state with angle}
-let width width state =
+let width worker width state =
match state.mode with
| Selection (Path s) ->
let state = { state with width } in
let paths = update_path_selection s state.paths (fun p -> Path.Point.set_width p width) in
+ (* Update the event to the worker *)
+ let outline = List.find paths
+ ~f:(fun o -> o.Outline.id = s) in
+ post worker (`Back outline);
{state with paths }
| Selection (Point (s, point)) ->
let state = update_point_selection state s point
(fun p -> Path.Point.set_width p width) in
+ (* Update the event to the worker *)
+ let outline = List.find state.paths
+ ~f:(fun o -> o.Outline.id = s) in
+ post worker (`Back outline);
{ state with width }
| _ ->
{ state with width }
@@ -234,12 +252,12 @@ let do_action
{ state with
mode = Out }
| dist, Some selection ->
- let _, path, _, _ = selection in
- if Path.Fixed.id path != id then
+ let _, outline, _, _ = selection in
+ if outline.Outline.id != id then
select_segment position selection state dist
else
(* On the same segment, check for a point *)
- let selection = Selection.select_point path (Gg.V2.of_tuple position) in
+ let selection = Selection.select_point outline (Gg.V2.of_tuple position) in
match selection with
| Path _ ->
{ state with mode = Selection selection }
@@ -263,9 +281,21 @@ let do_action
let current = insert_or_replace state point stamp state.current in
let paths =
- let last = Path.Fixed.to_fixed
+
+ let path = Path.Fixed.to_fixed
(module Path.Path_Builder)
- current
+ current in
+
+ (* Create a copy from the path with all the interior points *)
+ let back = Path.Fixed.map
+ path
+ (fun pt -> Path.Point.copy pt @@ Path.Point.get_coord' pt) in
+
+ let last =
+ { Outline.path = path
+ ; Outline.back = back
+ ; Outline.id = Outline.get_id
+ }
in
let () = post worker (`Complete last) in
@@ -298,14 +328,17 @@ let do_action
else
let point' = Path.Point.copy point mouse_v2 in
List.iter state.paths
- ~f:(fun path ->
- let id' = Path.Fixed.id path in
+ ~f:(fun outline ->
+ let id' = outline.Outline.id in
match id = id' with
| false -> ()
| true ->
Option.iter
- (fun p -> post worker (`Complete p))
- (Path.Fixed.replace_point path point')
+ (fun p ->
+
+ let outline = {outline with path = p} in
+ post worker (`Complete outline))
+ (Path.Fixed.replace_point outline.Outline.path point')
);
{ state with mode = Selection (Point (id, point')) }
| `Delete, _ ->
@@ -325,7 +358,7 @@ let do_action
Layer.Paths.to_svg
~color:Blog.Nord.nord0
(module Layer.Paths.ReprFixed)
- (path, path)
+ (path.Outline.path, path.Outline.path)
state.rendering
)) in
@@ -347,9 +380,9 @@ let do_action
state
| `Angle value , _ ->
- angle value state
+ angle worker value state
| `Width value, _ ->
- width value state
+ width worker value state
| `Rendering rendering, _ ->
@@ -361,14 +394,13 @@ let do_action
state
| `Complete path, _ ->
- let id = Path.Fixed.id path in
+ let id = path.Outline.id in
let paths = List.map state.paths
- ~f:(fun path' ->
- let id' = Path.Fixed.id path' in
+ ~f:(fun line ->
+ let id' = line.Outline.id in
match id = id' with
- | false -> path'
- | true ->
- path
+ | false -> line
+ | true -> path
) in
{ state with paths }