@@ -34,43 +34,50 @@ data MouseMsg
3434
3535data Msg
3636 = OperatorClicked OperatorId
37- | CursorMoved
37+ | OperatorsChanged (Array Operator )
38+
39+ data DirtyState = Clean | Dirty
3840
3941-- ------------------------------------------------------------------------------
4042
41- evalModel :: MouseMsg -> Model -> Model
43+ -- | The `DirtyState` in the result indicates if any ops were modified.
44+ evalModel :: MouseMsg -> Model -> DirtyState /\ Model
4245evalModel msg model = case msg of
43- MouseIsOut _ -> model { mouseOver = Nothing }
44- MouseIsOver x k -> model { mouseOver = Just (x /\ k) }
45- MousePos p -> model { mousePos = p }
46- MouseDown p -> model { mousePos = p
47- , mousePressed = true
48- , dragStart = case model.mouseOver of
49- Nothing -> DragStartedOnBackground model.mousePos
50- Just (op /\ opPos) -> DragStartedOnOperator model.mousePos op opPos
51- }
52- MouseUp p -> (dropGhost model) { mousePos = p
53- , mousePressed = false
54- , dragStart = DragNotStarted
55- }
46+ MouseIsOut _ -> Clean /\ model { mouseOver = Nothing }
47+ MouseIsOver x k -> Clean /\ model { mouseOver = Just (x /\ k) }
48+ MousePos p -> Clean /\ model { mousePos = p }
49+ MouseDown p -> Clean /\ model { mousePos = p
50+ , mousePressed = true
51+ , dragStart = case model.mouseOver of
52+ Nothing -> DragStartedOnBackground model.mousePos
53+ Just (op /\ opPos) -> DragStartedOnOperator model.mousePos op opPos
54+ }
55+ MouseUp p -> opsModified /\ model' { mousePos = p
56+ , mousePressed = false
57+ , dragStart = DragNotStarted
58+ }
59+ where
60+ opsModified /\ model' = dropGhost model
5661
5762-- ------------------------------------------------------------------------------
5863
59- dropGhost :: Model -> Model
64+ -- | The `DirtyState` in the result indicates if any ops were modified.
65+ dropGhost :: Model -> DirtyState /\ Model
6066dropGhost model = case model.dragStart of
61- DragStartedOnOperator _ op _ ->
62- let scale = model.config.scale
63- dd = dragDelta model
64- ddScreen = snap scale <$> dd
65- ddModel = (_/scale) <$> ddScreen
66- opxyw = op.pos - ddModel
67- (cw /\ ch) = model.config.width /\ model.config.height
68- isValid = isPositive && isBounded
69- isPositive = (_x opxyw >= zero) && (_y opxyw >= zero)
70- isBounded = (_x opxyw < (cw / scale)) && (_y opxyw < (ch / scale))
71- -- TODO ^ add condition for w
72- (ox /\ ow) = if _z opxyw > zero then _x opxyw /\ _z opxyw else (_x opxyw + _z opxyw) /\ (- _z opxyw)
73- modOp o = o { pos = vec3 ox (_y opxyw) ow }
74- newOps = modifyOperator op.identifier modOp model.ops
75- in if isValid then model { ops = newOps } else model
76- _ -> model
67+ DragStartedOnOperator _ op _ -> if isValid then Dirty /\ model { ops = newOps }
68+ else Clean /\ model
69+ where
70+ scale = model.config.scale
71+ dd = dragDelta model
72+ ddScreen = snap scale <$> dd
73+ ddModel = (_/scale) <$> ddScreen
74+ opxyw = op.pos - ddModel
75+ (cw /\ ch) = model.config.width /\ model.config.height
76+ isValid = isPositive && isBounded
77+ isPositive = (_x opxyw >= zero) && (_y opxyw >= zero)
78+ isBounded = (_x opxyw < (cw / scale)) && (_y opxyw < (ch / scale))
79+ -- TODO ^ add condition for w
80+ (ox /\ ow) = if _z opxyw > zero then _x opxyw /\ _z opxyw else (_x opxyw + _z opxyw) /\ (- _z opxyw)
81+ modOp o = o { pos = vec3 ox (_y opxyw) ow }
82+ newOps = modifyOperator op.identifier modOp model.ops
83+ _ -> Clean /\ model
0 commit comments