refactor a little bit, making signature of app-update.sml simpler to use by storing mouseX and mouseY inside the app_type

This commit is contained in:
2024-08-08 21:58:50 +01:00
parent 89f4ae3b13
commit 4243e4c679
6 changed files with 109 additions and 31 deletions

BIN
dotscape

Binary file not shown.

View File

@@ -8,7 +8,7 @@ struct
open AppType open AppType
fun helpFromWidthAndHeight fun helpFromWidthAndHeight
(windowWidth, windowHeight, wStart, wFinish, hStart, hFinish) = (windowWidth, windowHeight, wStart, wFinish, hStart, hFinish) : app_type =
let let
val xClickPoints = ClickPoints.generate (wStart, wFinish) val xClickPoints = ClickPoints.generate (wStart, wFinish)
val yClickPoints = ClickPoints.generate (hStart, hFinish) val yClickPoints = ClickPoints.generate (hStart, hFinish)
@@ -24,6 +24,8 @@ struct
, yClickPoints = yClickPoints , yClickPoints = yClickPoints
, graphLines = graphLines , graphLines = graphLines
, undo = [] , undo = []
, mouseX = 0.0
, mouseY = 0.0
} }
end end

View File

@@ -24,6 +24,8 @@ sig
, yClickPoints: Real32.real vector , yClickPoints: Real32.real vector
, graphLines: Real32.real vector , graphLines: Real32.real vector
, undo: (Real32.real * Real32.real) list , undo: (Real32.real * Real32.real) list
, mouseX: Real32.real
, mouseY: Real32.real
} }
end end
@@ -61,5 +63,7 @@ struct
, yClickPoints: Real32.real vector , yClickPoints: Real32.real vector
, graphLines: Real32.real vector , graphLines: Real32.real vector
, undo: (Real32.real * Real32.real) list , undo: (Real32.real * Real32.real) list
, mouseX: Real32.real
, mouseY: Real32.real
} }
end end

View File

@@ -1,7 +1,7 @@
signature APP_UPDATE = signature APP_UPDATE =
sig sig
val update: AppType.app_type * Real32.real * Real32.real * InputMessage.t val update: AppType.app_type * InputMessage.t
-> AppType.app_type * DrawMessage.t * Real32.real * Real32.real -> AppType.app_type * DrawMessage.t
end end
structure AppUpdate :> APP_UPDATE = structure AppUpdate :> APP_UPDATE =
@@ -10,9 +10,18 @@ struct
open DrawMessage open DrawMessage
open InputMessage open InputMessage
fun mouseMoveOrRelease (model: app_type, mouseX, mouseY) = fun mouseMoveOrRelease (model: app_type) =
let let
val {xClickPoints, yClickPoints, windowWidth, windowHeight, ...} = model val
{ xClickPoints
, yClickPoints
, windowWidth
, windowHeight
, mouseX
, mouseY
, ...
} = model
val (drawVec, _, _) = ClickPoints.getClickPosition val (drawVec, _, _) = ClickPoints.getClickPosition
( mouseX ( mouseX
, mouseY , mouseY
@@ -27,12 +36,21 @@ struct
val drawVec = TriangleStage.toVector (model, drawVec) val drawVec = TriangleStage.toVector (model, drawVec)
val drawMsg = DRAW_BUTTON drawVec val drawMsg = DRAW_BUTTON drawVec
in in
(model, drawMsg, mouseX, mouseY) (model, drawMsg)
end end
fun mouseLeftClick (model: app_type, mouseX, mouseY) = fun mouseLeftClick (model: app_type) =
let let
val {xClickPoints, yClickPoints, windowWidth, windowHeight, ...} = model val
{ xClickPoints
, yClickPoints
, windowWidth
, windowHeight
, mouseX
, mouseY
, ...
} = model
val (buttonVec, hpos, vpos) = ClickPoints.getClickPosition val (buttonVec, hpos, vpos) = ClickPoints.getClickPosition
( mouseX ( mouseX
, mouseY , mouseY
@@ -57,7 +75,7 @@ struct
val model = val model =
AppWith.newTriangleStage (model, newTriangleStage, newUndoTuple) AppWith.newTriangleStage (model, newTriangleStage, newUndoTuple)
in in
(model, drawMsg, mouseX, mouseY) (model, drawMsg)
end end
| FIRST {x1, y1} => | FIRST {x1, y1} =>
let let
@@ -70,7 +88,7 @@ struct
val model = val model =
AppWith.newTriangleStage (model, newTriangleStage, newUndoTuple) AppWith.newTriangleStage (model, newTriangleStage, newUndoTuple)
in in
(model, drawMsg, mouseX, mouseY) (model, drawMsg)
end end
| SECOND {x1, y1, x2, y2} => | SECOND {x1, y1, x2, y2} =>
let let
@@ -80,13 +98,13 @@ struct
val drawVec = Triangles.toVector model val drawVec = Triangles.toVector model
val drawMsg = DRAW_TRIANGLES_AND_RESET_BUTTONS drawVec val drawMsg = DRAW_TRIANGLES_AND_RESET_BUTTONS drawVec
in in
(model, drawMsg, mouseX, mouseY) (model, drawMsg)
end end
else else
(model, NO_DRAW, mouseX, mouseY) (model, NO_DRAW)
end end
fun resizeWindow (model, mouseX, mouseY, width, height) = fun resizeWindow (model, width, height) =
let let
val model = AppWith.windowResize (model, width, height) val model = AppWith.windowResize (model, width, height)
val triangles = Triangles.toVector model val triangles = Triangles.toVector model
@@ -95,15 +113,15 @@ struct
RESIZE_TRIANGLES_BUTTONS_AND_GRAPH RESIZE_TRIANGLES_BUTTONS_AND_GRAPH
{triangles = triangles, graphLines = graphLines} {triangles = triangles, graphLines = graphLines}
in in
(model, drawMsg, mouseX, mouseY) (model, drawMsg)
end end
fun undoAction (model, mouseX, mouseY) = fun undoAction model =
case #triangleStage model of case #triangleStage model of
FIRST {x1, y1} => FIRST {x1, y1} =>
(* Change FIRST to NO_TRIANGLE and clear buttons. *) (* Change FIRST to NO_TRIANGLE and clear buttons. *)
let val model = AppWith.replaceTriangleStage (model, NO_TRIANGLE) let val model = AppWith.replaceTriangleStage (model, NO_TRIANGLE)
in (model, CLEAR_BUTTONS, mouseX, mouseY) in (model, CLEAR_BUTTONS)
end end
| SECOND {x1, y1, x2, y2} => | SECOND {x1, y1, x2, y2} =>
(* Change FIRST to SECOND and redraw buttons. *) (* Change FIRST to SECOND and redraw buttons. *)
@@ -115,7 +133,7 @@ struct
TriangleStage.firstToVector (x1, y1, Vector.fromList [], model) TriangleStage.firstToVector (x1, y1, Vector.fromList [], model)
val drawMsg = DRAW_BUTTON drawVec val drawMsg = DRAW_BUTTON drawVec
in in
(model, drawMsg, mouseX, mouseY) (model, drawMsg)
end end
| NO_TRIANGLE => | NO_TRIANGLE =>
(case #triangles model of (case #triangles model of
@@ -135,19 +153,21 @@ struct
DRAW_TRIANGLES_AND_BUTTONS DRAW_TRIANGLES_AND_BUTTONS
{triangles = newTriangleVec, buttons = drawVec} {triangles = newTriangleVec, buttons = drawVec}
in in
(model, drawMsg, mouseX, mouseY) (model, drawMsg)
end end
| [] => | [] =>
(* Can't undo, because there are no actions to undo. *) (* Can't undo, because there are no actions to undo. *)
(model, NO_DRAW, mouseX, mouseY)) (model, NO_DRAW))
fun update (model: app_type, mouseX, mouseY, inputMsg) = fun update (model: app_type, inputMsg) =
case inputMsg of case inputMsg of
MOUSE_MOVE {x = mouseX, y = mouseY} => MOUSE_MOVE {x = mouseX, y = mouseY} =>
mouseMoveOrRelease (model, mouseX, mouseY) let val model = AppWith.mousePosition (model, mouseX, mouseY)
| MOUSE_LEFT_RELEASE => mouseMoveOrRelease (model, mouseX, mouseY) in mouseMoveOrRelease model
| MOUSE_LEFT_CLICK => mouseLeftClick (model, mouseX, mouseY) end
| MOUSE_LEFT_RELEASE => mouseMoveOrRelease model
| MOUSE_LEFT_CLICK => mouseLeftClick model
| RESIZE_WINDOW {width, height} => | RESIZE_WINDOW {width, height} =>
resizeWindow (model, mouseX, mouseY, width, height) resizeWindow (model, width, height)
| UNDO_ACTION => undoAction (model, mouseX, mouseY) | UNDO_ACTION => undoAction model
end end

View File

@@ -22,6 +22,9 @@ sig
* Real32.real * Real32.real
* (Real32.real * Real32.real) * (Real32.real * Real32.real)
-> AppType.app_type -> AppType.app_type
val mousePosition: AppType.app_type * Real32.real * Real32.real
-> AppType.app_type
end end
structure AppWith :> APP_WITH = structure AppWith :> APP_WITH =
@@ -40,7 +43,10 @@ struct
, windowHeight , windowHeight
, graphLines , graphLines
, undo , undo
, mouseX
, mouseY
} = app } = app
val newUndo = xyTuple :: undo val newUndo = xyTuple :: undo
in in
{ triangleStage = newTriangleStage { triangleStage = newTriangleStage
@@ -51,6 +57,8 @@ struct
, windowHeight = windowHeight , windowHeight = windowHeight
, graphLines = graphLines , graphLines = graphLines
, undo = newUndo , undo = newUndo
, mouseX = mouseX
, mouseY = mouseY
} }
end end
@@ -65,6 +73,8 @@ struct
, windowHeight , windowHeight
, graphLines , graphLines
, undo , undo
, mouseX
, mouseY
} = app } = app
in in
{ triangleStage = newTriangleStage { triangleStage = newTriangleStage
@@ -75,6 +85,8 @@ struct
, windowHeight = windowHeight , windowHeight = windowHeight
, graphLines = graphLines , graphLines = graphLines
, undo = undo , undo = undo
, mouseX = mouseX
, mouseY = mouseY
} }
end end
@@ -90,6 +102,8 @@ struct
, windowHeight , windowHeight
, graphLines , graphLines
, undo , undo
, mouseX
, mouseY
} = app } = app
in in
{ triangleStage = newTriangleStage { triangleStage = newTriangleStage
@@ -100,6 +114,8 @@ struct
, windowHeight = windowHeight , windowHeight = windowHeight
, graphLines = graphLines , graphLines = graphLines
, undo = undo , undo = undo
, mouseX = mouseX
, mouseY = mouseY
} }
end end
@@ -115,6 +131,8 @@ struct
, windowHeight , windowHeight
, graphLines , graphLines
, undo , undo
, mouseX
, mouseY
} = app } = app
val newTriangle = {x1 = x1, y1 = y1, x2 = x2, y2 = y2, x3 = x3, y3 = y3} val newTriangle = {x1 = x1, y1 = y1, x2 = x2, y2 = y2, x3 = x3, y3 = y3}
@@ -123,12 +141,14 @@ struct
in in
{ triangleStage = NO_TRIANGLE { triangleStage = NO_TRIANGLE
, triangles = newTriangles , triangles = newTriangles
, undo = newUndo
, xClickPoints = xClickPoints , xClickPoints = xClickPoints
, yClickPoints = yClickPoints , yClickPoints = yClickPoints
, windowWidth = windowWidth , windowWidth = windowWidth
, windowHeight = windowHeight , windowHeight = windowHeight
, graphLines = graphLines , graphLines = graphLines
, undo = newUndo , mouseX = mouseX
, mouseY = mouseY
} }
end end
@@ -145,6 +165,8 @@ struct
, triangles , triangles
, triangleStage , triangleStage
, undo , undo
, mouseX
, mouseY
} = app } = app
val xClickPoints = ClickPoints.generate (wStart, wFinish) val xClickPoints = ClickPoints.generate (wStart, wFinish)
val yClickPoints = ClickPoints.generate (hStart, hFinish) val yClickPoints = ClickPoints.generate (hStart, hFinish)
@@ -160,6 +182,8 @@ struct
, windowWidth = windowWidth , windowWidth = windowWidth
, windowHeight = windowHeight , windowHeight = windowHeight
, undo = undo , undo = undo
, mouseX = mouseX
, mouseY = mouseY
} }
end end
@@ -185,4 +209,33 @@ struct
helpWindowResize helpWindowResize
(app, windowWidth, windowHeight, 0, windowWidth, hStart, hFinish) (app, windowWidth, windowHeight, 0, windowWidth, hStart, hFinish)
end end
fun mousePosition (app: app_type, mouseX, mouseY) =
let
val
{ mouseX = _
, mouseY = _
, triangles
, triangleStage
, xClickPoints
, yClickPoints
, windowWidth
, windowHeight
, graphLines
, undo
} = app
in
{ mouseX = mouseX
, mouseY = mouseY
, triangles = triangles
, triangleStage = triangleStage
, xClickPoints = xClickPoints
, yClickPoints = yClickPoints
, windowWidth = windowWidth
, windowHeight = windowHeight
, graphLines = graphLines
, undo = undo
}
end
end end

View File

@@ -4,18 +4,17 @@ struct
open DrawMessage open DrawMessage
local local
fun loop (inputMailbox, drawMailbox, mouseX, mouseY, model) = fun loop (inputMailbox, drawMailbox, model) =
let let
val inputMsg = Mailbox.recv inputMailbox val inputMsg = Mailbox.recv inputMailbox
val (model, drawMsg, mouseX, mouseY) = val (model, drawMsg) = AppUpdate.update (model, inputMsg)
AppUpdate.update (model, mouseX, mouseY, inputMsg)
val _ = Mailbox.send (drawMailbox, drawMsg) val _ = Mailbox.send (drawMailbox, drawMsg)
in in
loop (inputMailbox, drawMailbox, mouseX, mouseY, model) loop (inputMailbox, drawMailbox, model)
end end
in in
fun update (inputMailbox, drawMailbox, initial) = fun update (inputMailbox, drawMailbox, initial) =
loop (inputMailbox, drawMailbox, 0.0, 0.0, initial) loop (inputMailbox, drawMailbox, initial)
end end
fun draw fun draw