aboutsummaryrefslogtreecommitdiff
path: root/state.ml
diff options
context:
space:
mode:
authorSébastien Dailly <sebastien@chimrod.com>2020-12-30 11:41:01 +0100
committerSébastien Dailly <sebastien@chimrod.com>2020-12-30 11:41:01 +0100
commite25b7797708c19cbaef68c14ebef8738de44c2d9 (patch)
tree6d778a7bca390c496ee95cfb337f2f26fe0aa5c3 /state.ml
parentfae31bdb659b4b14322136e045ea565d38bbd04f (diff)
Refactor
Diffstat (limited to 'state.ml')
-rwxr-xr-xstate.ml62
1 files changed, 29 insertions, 33 deletions
diff --git a/state.ml b/state.ml
index e41c328..35fc2ed 100755
--- a/state.ml
+++ b/state.ml
@@ -1,10 +1,6 @@
open StdLabels
open Brr
-module Path_Builder = Paths.Path_Builder
-module SVGRepr = Path.WireFramePrinter.Make(Layer.Svg)
-module SVG_Fixed_Printer = Path_Builder.DrawFixed(SVGRepr)
-
let expected_host = Blog.Hash_host.expected_host
let backgroundColor = Blog.Nord.nord0
@@ -13,10 +9,10 @@ let timer, tick = Elements.Timer.create ()
type mode =
| Edit
- | Selection of Path_Builder.fixedPath
+ | Selection of Paths.Fixed.t
| Out
-type current = Path_Builder.t
+type current = Paths.Path_Builder.t
(** Events *)
@@ -44,7 +40,7 @@ type events =
*)
type state =
{ mode : mode
- ; paths : Path_Builder.fixedPath list
+ ; paths : Paths.Fixed.t list
; current : current
; width : float
; angle : float
@@ -54,9 +50,9 @@ let insert_or_replace state ((x, y) as p) path =
let width = state.width
and angle = state.angle in
let point = Path.Point.create ~x ~y ~angle ~width in
- match Path_Builder.peek path with
+ match Paths.Path_Builder.peek path with
| None ->
- Path_Builder.add_point
+ Paths.Path_Builder.add_point
point
path
| Some p1 ->
@@ -66,15 +62,15 @@ let insert_or_replace state ((x, y) as p) path =
let dist = (norm (p1' - (of_tuple p))) in
if dist < 5. then (
- path, None
+ path
) else (
- Path_Builder.add_point
+ Paths.Path_Builder.add_point
point
path
)
let check_selection
- : (float * float) -> Path_Builder.fixedPath list -> Path_Builder.fixedPath option
+ : (float * float) -> Paths.Fixed.t list -> Paths.Fixed.t option
= fun position paths ->
let point = Gg.V2.of_tuple position in
(* If the user click on a curve, select it *)
@@ -86,7 +82,7 @@ let check_selection
| Some p -> Some p
| None ->
(* TODO : Add a method in the point module *)
- begin match Path_Builder.distance point path with
+ begin match Paths.Fixed.distance point path with
| Some p when p < 20. ->
Some path
| _ -> None
@@ -96,12 +92,12 @@ let check_selection
(** Update the path in the selection with the given function applied to
every point *)
let update_selection s state f =
- let s = Path_Builder.map_point s f
- and id = Path_Builder.id s in
+ let s = Paths.Fixed.map_point s f
+ and id = Paths.Fixed.id s in
let paths = List.map state.paths
~f:(fun path ->
- let id' = Path_Builder.id path in
+ let id' = Paths.Fixed.id path in
match id = id' with
| false -> path
| true -> s
@@ -114,14 +110,11 @@ let do_action
match event, state.mode with
| `Point (_delay, point), Edit ->
(* Add the point in the list *)
- let current, fixed_path = insert_or_replace
+ let current = insert_or_replace
state
point
state.current in
- let paths = match fixed_path with
- | None -> state.paths
- | Some p -> p::state.paths in
- { state with current; paths }
+ { state with current }
(* Click anywhere while in Out mode, we switch in edition *)
| `Click _, Out ->
@@ -145,22 +138,25 @@ let do_action
| `Out point, Edit ->
Elements.Timer.stop timer;
- begin match Path_Builder.peek2 state.current with
+ begin match Paths.Path_Builder.peek2 state.current with
(* If there is at last two points selected, handle this as a curve
creation *)
| Some _ ->
- let current, fixed_path = insert_or_replace state point state.current in
- let paths = match fixed_path with
- | None -> Path_Builder.to_fixed current::state.paths
- | Some p -> p::state.paths
- and current = Path_Builder.empty in
+ let current = insert_or_replace state point state.current in
+ let paths =
+ let last = Paths.Fixed.to_fixed
+ (module Paths.Path_Builder)
+ current
+ in
+ last::state.paths
+ and current = Paths.Path_Builder.empty in
{ state with
mode = Out
; paths; current }
(* Else, check if there is a curve undre the cursor, and remove it *)
| None ->
- let current = Path_Builder.empty in
+ let current = Paths.Path_Builder.empty in
begin match check_selection point state.paths with
| None ->
{ state with
@@ -174,8 +170,8 @@ let do_action
end
end
| `Delete, Selection s ->
- let id = Path_Builder.id s in
- let paths = List.filter state.paths ~f:(fun p -> Path_Builder.id p != id) in
+ let id = Paths.Fixed.id s in
+ let paths = List.filter state.paths ~f:(fun p -> Paths.Fixed.id p != id) in
{ state with paths ; mode = Out}
@@ -191,8 +187,8 @@ let do_action
; v (Jstr.v "xmlns:xlink") (Jstr.v "http://www.w3.org/1999/xlink") ]
(List.map state.paths
~f:(fun path ->
- let repr = SVGRepr.create_path (fun _ -> ()) in
- let path = SVGRepr.get @@ SVG_Fixed_Printer.draw path repr in
+ let repr = Paths.SVGRepr.create_path (fun _ -> ()) in
+ let path = Paths.SVGRepr.get @@ Paths.SVG_Printer.draw path repr in
Layer.Svg.path
~at:Brr.At.[
@@ -245,7 +241,7 @@ let do_action
let init =
{ paths = []
- ; current = Path_Builder.empty
+ ; current = Paths.Path_Builder.empty
; mode = Out
; angle = 30.
; width = 10.