2024-07-30 19:04:36 +01:00
|
|
|
structure AppUpdate =
|
|
|
|
|
struct
|
2024-07-31 12:00:07 +01:00
|
|
|
open AppType
|
2024-07-30 19:04:36 +01:00
|
|
|
|
2024-08-02 13:35:48 +01:00
|
|
|
(* This function adjusts the x position to be centre-aligned to the grid
|
|
|
|
|
* if windowWidth is greater than height
|
|
|
|
|
* (where screen size does not have 1:1 aspect ratio). *)
|
|
|
|
|
fun calcRelativeX (x, windowWidth, windowHeight, halfWidth) =
|
|
|
|
|
if windowWidth > windowHeight then
|
|
|
|
|
let
|
|
|
|
|
val difference = windowWidth - windowHeight
|
|
|
|
|
val offset = Real32.fromInt (difference div 2)
|
|
|
|
|
in
|
|
|
|
|
x * (halfWidth - offset)
|
|
|
|
|
end
|
|
|
|
|
else
|
|
|
|
|
x * halfWidth
|
|
|
|
|
|
|
|
|
|
(* Similar to calcRelativeX, except it centre-aligns the y-point
|
|
|
|
|
* when windowHeight is greater than windowWidth. *)
|
|
|
|
|
fun calcRelativeY (y, windowWidth, windowHeight, halfHeight) =
|
|
|
|
|
if windowHeight > windowWidth then
|
|
|
|
|
let
|
|
|
|
|
val difference = windowHeight - windowWidth
|
|
|
|
|
val offset = Real32.fromInt (difference div 2)
|
|
|
|
|
in
|
|
|
|
|
y * (halfHeight - offset)
|
|
|
|
|
end
|
|
|
|
|
else
|
|
|
|
|
y * halfHeight
|
|
|
|
|
|
2024-07-30 19:04:36 +01:00
|
|
|
local
|
2024-08-02 13:35:48 +01:00
|
|
|
fun helpGetTrianglesVector
|
|
|
|
|
(lst, acc, windowWidth, windowHeight, halfWidth, halfHeight) =
|
2024-07-31 12:00:07 +01:00
|
|
|
case lst of
|
|
|
|
|
{x1, y1, x2, y2, x3, y3} :: tl =>
|
2024-08-02 13:35:48 +01:00
|
|
|
let
|
|
|
|
|
val x1 = calcRelativeX (x1, windowWidth, windowHeight, halfWidth)
|
|
|
|
|
val x2 = calcRelativeX (x2, windowWidth, windowHeight, halfWidth)
|
|
|
|
|
val x3 = calcRelativeX (x3, windowWidth, windowHeight, halfWidth)
|
|
|
|
|
|
|
|
|
|
val y1 = calcRelativeY (y1, windowWidth, windowHeight, halfHeight)
|
|
|
|
|
val y2 = calcRelativeY (y2, windowWidth, windowHeight, halfHeight)
|
|
|
|
|
val y3 = calcRelativeY (y3, windowWidth, windowHeight, halfHeight)
|
|
|
|
|
|
|
|
|
|
val vec =
|
|
|
|
|
#[ x1 / halfWidth
|
|
|
|
|
, y1 / halfHeight
|
|
|
|
|
, x2 / halfWidth
|
|
|
|
|
, y2 / halfHeight
|
|
|
|
|
, x3 / halfWidth
|
|
|
|
|
, y3 / halfHeight
|
|
|
|
|
]
|
|
|
|
|
val acc = vec :: acc
|
|
|
|
|
in
|
|
|
|
|
helpGetTrianglesVector
|
|
|
|
|
(tl, acc, windowWidth, windowHeight, halfWidth, halfHeight)
|
2024-07-31 12:00:07 +01:00
|
|
|
end
|
|
|
|
|
| [] => acc
|
|
|
|
|
in
|
|
|
|
|
fun getTrianglesVector (app: app_type) =
|
2024-08-02 13:35:48 +01:00
|
|
|
let
|
|
|
|
|
val windowWidth = #windowWidth app
|
|
|
|
|
val windowHeight = #windowHeight app
|
|
|
|
|
val halfWidth = Real32.fromInt (windowWidth div 2)
|
|
|
|
|
val halfHeight = Real32.fromInt (windowHeight div 2)
|
|
|
|
|
val lst = helpGetTrianglesVector
|
|
|
|
|
(#triangles app, [], windowWidth, windowHeight, halfWidth, halfHeight)
|
|
|
|
|
in
|
|
|
|
|
Vector.concat lst
|
2024-07-31 12:00:07 +01:00
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
2024-08-02 03:09:59 +01:00
|
|
|
fun getFirstTriangleStageVector (x1, y1, drawVec, model) =
|
2024-07-31 21:21:45 +01:00
|
|
|
let
|
2024-08-02 03:09:59 +01:00
|
|
|
val windowWidth = #windowWidth model
|
|
|
|
|
val windowHeight = #windowHeight model
|
|
|
|
|
|
|
|
|
|
val halfWidth = Real32.fromInt (windowWidth div 2)
|
|
|
|
|
val halfHeight = Real32.fromInt (windowHeight div 2)
|
2024-07-31 20:58:33 +01:00
|
|
|
|
2024-08-02 13:35:48 +01:00
|
|
|
val x1px = calcRelativeX (x1, windowWidth, windowHeight, halfWidth)
|
2024-07-31 21:21:45 +01:00
|
|
|
val left = (x1px - 5.0) / halfWidth
|
|
|
|
|
val right = (x1px + 5.0) / halfWidth
|
2024-07-31 20:58:33 +01:00
|
|
|
|
2024-08-02 13:35:48 +01:00
|
|
|
val y1px = calcRelativeY (y1, windowWidth, windowHeight, halfHeight)
|
2024-07-31 21:21:45 +01:00
|
|
|
val top = (y1px + 5.0) / halfHeight
|
|
|
|
|
val bottom = (y1px - 5.0) / halfHeight
|
2024-07-31 20:58:33 +01:00
|
|
|
|
2024-07-31 21:21:45 +01:00
|
|
|
val firstVec = ltrbToVertex (left, top, right, bottom, 0.0, 0.0, 1.0)
|
|
|
|
|
in
|
|
|
|
|
Vector.concat [firstVec, drawVec]
|
|
|
|
|
end
|
|
|
|
|
|
2024-08-02 03:09:59 +01:00
|
|
|
fun getSecondTriangleStageVector (x1, y1, x2, y2, drawVec, model) =
|
2024-07-31 21:21:45 +01:00
|
|
|
let
|
2024-08-02 03:09:59 +01:00
|
|
|
val windowWidth = #windowWidth model
|
|
|
|
|
val windowHeight = #windowHeight model
|
|
|
|
|
|
|
|
|
|
val halfWidth = Real32.fromInt (windowWidth div 2)
|
|
|
|
|
val halfHeight = Real32.fromInt (windowHeight div 2)
|
2024-07-31 20:58:33 +01:00
|
|
|
|
2024-08-02 13:35:48 +01:00
|
|
|
|
|
|
|
|
val x1px = calcRelativeX (x1, windowWidth, windowHeight, halfWidth)
|
2024-07-31 21:21:45 +01:00
|
|
|
val left = (x1px - 5.0) / halfWidth
|
|
|
|
|
val right = (x1px + 5.0) / halfWidth
|
2024-07-31 20:58:33 +01:00
|
|
|
|
2024-08-02 13:35:48 +01:00
|
|
|
val y1px = calcRelativeY (y1, windowWidth, windowHeight, halfHeight)
|
2024-07-31 21:21:45 +01:00
|
|
|
val top = (y1px + 5.0) / halfHeight
|
|
|
|
|
val bottom = (y1px - 5.0) / halfHeight
|
2024-07-31 20:58:33 +01:00
|
|
|
|
2024-07-31 21:21:45 +01:00
|
|
|
val firstVec = ltrbToVertex (left, top, right, bottom, 0.0, 0.0, 1.0)
|
2024-07-31 20:58:33 +01:00
|
|
|
|
2024-08-02 13:35:48 +01:00
|
|
|
val x2px = calcRelativeX (x2, windowWidth, windowHeight, halfWidth)
|
2024-07-31 21:21:45 +01:00
|
|
|
val left = (x2px - 5.0) / halfWidth
|
|
|
|
|
val right = (x2px + 5.0) / halfWidth
|
2024-07-31 20:58:33 +01:00
|
|
|
|
2024-08-02 13:35:48 +01:00
|
|
|
val y2px = calcRelativeY (y2, windowWidth, windowHeight, halfHeight)
|
2024-07-31 21:21:45 +01:00
|
|
|
val top = (y2px + 5.0) / halfHeight
|
|
|
|
|
val bottom = (y2px - 5.0) / halfHeight
|
2024-07-31 20:58:33 +01:00
|
|
|
|
2024-07-31 21:21:45 +01:00
|
|
|
val secVec = ltrbToVertex (left, top, right, bottom, 0.0, 0.0, 1.0)
|
|
|
|
|
in
|
|
|
|
|
Vector.concat [firstVec, secVec, drawVec]
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
fun getTriangleStageVector (model: app_type, drawVec) =
|
|
|
|
|
case #triangleStage model of
|
|
|
|
|
NO_TRIANGLE => drawVec
|
2024-08-02 03:09:59 +01:00
|
|
|
| FIRST {x1, y1} => getFirstTriangleStageVector (x1, y1, drawVec, model)
|
2024-07-31 21:21:45 +01:00
|
|
|
| SECOND {x1, y1, x2, y2} =>
|
2024-08-02 03:09:59 +01:00
|
|
|
getSecondTriangleStageVector (x1, y1, x2, y2, drawVec, model)
|
2024-07-31 20:58:33 +01:00
|
|
|
|
2024-08-02 00:05:24 +01:00
|
|
|
local
|
|
|
|
|
open DrawMessage
|
|
|
|
|
open InputMessage
|
|
|
|
|
|
2024-08-02 03:09:59 +01:00
|
|
|
fun mouseMoveOrRelease (model: app_type, mouseX, mouseY) =
|
2024-08-02 00:05:24 +01:00
|
|
|
let
|
2024-08-02 13:35:48 +01:00
|
|
|
val {xClickPoints, yClickPoints, ...} = model
|
|
|
|
|
val (drawVec, _, _) = getClickPos (mouseX, mouseY, 1.0, 0.0, 0.0, model)
|
2024-08-02 00:05:24 +01:00
|
|
|
val drawVec = getTriangleStageVector (model, drawVec)
|
|
|
|
|
val drawMsg = DRAW_BUTTON drawVec
|
|
|
|
|
in
|
|
|
|
|
(model, drawMsg, mouseX, mouseY)
|
|
|
|
|
end
|
|
|
|
|
|
2024-08-02 03:09:59 +01:00
|
|
|
fun mouseLeftClick (model: app_type, mouseX, mouseY) =
|
2024-08-02 00:05:24 +01:00
|
|
|
let
|
2024-08-02 13:35:48 +01:00
|
|
|
val {xClickPoints, yClickPoints, ...} = model
|
2024-08-02 00:05:24 +01:00
|
|
|
val (buttonVec, hpos, vpos) = getClickPos
|
2024-08-02 03:09:59 +01:00
|
|
|
(mouseX, mouseY, 0.0, 0.0, 1.0, model)
|
2024-08-02 00:05:24 +01:00
|
|
|
in
|
|
|
|
|
if Vector.length buttonVec > 0 then
|
|
|
|
|
case #triangleStage model of
|
|
|
|
|
NO_TRIANGLE =>
|
|
|
|
|
let
|
|
|
|
|
val drawVec = getTriangleStageVector (model, buttonVec)
|
|
|
|
|
val drawMsg = DRAW_BUTTON drawVec
|
|
|
|
|
|
|
|
|
|
val newTriangleStage = FIRST {x1 = hpos, y1 = vpos}
|
|
|
|
|
val model = AppType.withTriangleStage (model, newTriangleStage)
|
|
|
|
|
in
|
|
|
|
|
(model, drawMsg, mouseX, mouseY)
|
|
|
|
|
end
|
|
|
|
|
| FIRST {x1, y1} =>
|
|
|
|
|
let
|
2024-08-02 13:35:48 +01:00
|
|
|
val drawVec =
|
|
|
|
|
getFirstTriangleStageVector (x1, y1, buttonVec, model)
|
2024-08-02 00:05:24 +01:00
|
|
|
val drawMsg = DRAW_BUTTON drawVec
|
|
|
|
|
|
|
|
|
|
val newTriangleStage = SECOND
|
|
|
|
|
{x1 = x1, y1 = y1, x2 = hpos, y2 = vpos}
|
|
|
|
|
val model = AppType.withTriangleStage (model, newTriangleStage)
|
|
|
|
|
in
|
|
|
|
|
(model, drawMsg, mouseX, mouseY)
|
|
|
|
|
end
|
|
|
|
|
| SECOND {x1, y1, x2, y2} =>
|
|
|
|
|
let
|
|
|
|
|
val model = AppType.addTriangleAndResetStage
|
|
|
|
|
(model, x1, y1, x2, y2, hpos, vpos)
|
|
|
|
|
|
|
|
|
|
val drawVec = getTrianglesVector model
|
|
|
|
|
val drawMsg = DRAW_TRIANGLES_AND_RESET_BUTTONS drawVec
|
|
|
|
|
in
|
|
|
|
|
(model, drawMsg, mouseX, mouseY)
|
|
|
|
|
end
|
|
|
|
|
else
|
|
|
|
|
(model, NO_DRAW, mouseX, mouseY)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
fun resizeWindow (model, mouseX, mouseY, width, height) =
|
|
|
|
|
let
|
2024-08-02 03:09:59 +01:00
|
|
|
val model = AppType.withWindowResize (model, width, height)
|
2024-08-02 13:35:48 +01:00
|
|
|
|
2024-08-03 04:40:53 +01:00
|
|
|
val triangles = getTrianglesVector model
|
|
|
|
|
val graphLines = #graphLines model
|
2024-08-03 07:10:38 +01:00
|
|
|
val drawMsg =
|
|
|
|
|
RESIZE_TRIANGLES_BUTTONS_AND_GRAPH
|
|
|
|
|
{triangles = triangles,
|
|
|
|
|
graphLines = graphLines}
|
2024-08-02 00:05:24 +01:00
|
|
|
in
|
2024-08-02 13:35:48 +01:00
|
|
|
(model, drawMsg, mouseX, mouseY)
|
2024-08-02 00:05:24 +01:00
|
|
|
end
|
|
|
|
|
in
|
2024-08-02 03:09:59 +01:00
|
|
|
fun update (model: app_type, mouseX, mouseY, inputMsg) =
|
2024-07-31 12:00:07 +01:00
|
|
|
case inputMsg of
|
|
|
|
|
MOUSE_MOVE {x = mouseX, y = mouseY} =>
|
2024-08-02 00:05:24 +01:00
|
|
|
mouseMoveOrRelease (model, mouseX, mouseY)
|
2024-08-02 13:35:48 +01:00
|
|
|
| MOUSE_LEFT_RELEASE => mouseMoveOrRelease (model, mouseX, mouseY)
|
|
|
|
|
| MOUSE_LEFT_CLICK => mouseLeftClick (model, mouseX, mouseY)
|
2024-08-02 00:05:24 +01:00
|
|
|
| RESIZE_WINDOW {width, height} =>
|
|
|
|
|
resizeWindow (model, mouseX, mouseY, width, height)
|
|
|
|
|
end
|
2024-07-30 19:04:36 +01:00
|
|
|
end
|