progress with resizing (triangle and button are always horizontally centred if windowWidth is greater than windowHeight, or windowHeight is greather than windowWidth)

This commit is contained in:
2024-08-02 13:35:48 +01:00
parent 7a584fea3c
commit 6c444696d4
2 changed files with 154 additions and 61 deletions

BIN
dotscape

Binary file not shown.

View File

@@ -2,18 +2,72 @@ structure AppUpdate =
struct struct
open AppType open AppType
(* 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
local local
fun helpGetTrianglesVector (lst, acc) = fun helpGetTrianglesVector
(lst, acc, windowWidth, windowHeight, halfWidth, halfHeight) =
case lst of case lst of
{x1, y1, x2, y2, x3, y3} :: tl => {x1, y1, x2, y2, x3, y3} :: tl =>
let val vec = #[x1, y1, x2, y2, x3, y3] let
in helpGetTrianglesVector (tl, vec :: acc) 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)
end end
| [] => acc | [] => acc
in in
fun getTrianglesVector (app: app_type) = fun getTrianglesVector (app: app_type) =
let val lst = helpGetTrianglesVector (#triangles app, []) let
in Vector.concat lst 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
end end
end end
@@ -29,8 +83,17 @@ struct
local local
fun getVerticalClickPos fun getVerticalClickPos
(yClickPoints, idx, horizontalPos, mouseX, mouseY, r, g, b, windowWidth, ( yClickPoints
windowHeight) = , idx
, horizontalPos
, mouseX
, mouseY
, r
, g
, b
, windowWidth
, windowHeight
) =
if idx = Vector.length yClickPoints then if idx = Vector.length yClickPoints then
(#[], 0.0, 0.0) (#[], 0.0, 0.0)
else else
@@ -39,9 +102,18 @@ struct
in in
if mouseY < curVerticalPos - 7.0 orelse mouseY > curVerticalPos + 7.0 then if mouseY < curVerticalPos - 7.0 orelse mouseY > curVerticalPos + 7.0 then
getVerticalClickPos getVerticalClickPos
(yClickPoints, idx + 1, horizontalPos, mouseX, mouseY, r, g, b, ( yClickPoints
windowWidth, windowHeight) , idx + 1
else , horizontalPos
, mouseX
, mouseY
, r
, g
, b
, windowWidth
, windowHeight
)
else
let let
(* calculate normalised device coordinates *) (* calculate normalised device coordinates *)
val halfWidth = Real32.fromInt (windowWidth div 2) val halfWidth = Real32.fromInt (windowWidth div 2)
@@ -115,8 +187,18 @@ struct
end end
end end
fun getHorizontalClickPos (xClickPoints, yClickPoints, idx, mouseX, mouseY, fun getHorizontalClickPos
r, g, b, windowWidth, windowHeight) = ( xClickPoints
, yClickPoints
, idx
, mouseX
, mouseY
, r
, g
, b
, windowWidth
, windowHeight
) =
if idx = Vector.length xClickPoints then if idx = Vector.length xClickPoints then
(#[], 0.0, 0.0) (#[], 0.0, 0.0)
else else
@@ -125,12 +207,30 @@ struct
in in
if mouseX < curPos - 7.0 orelse mouseX > curPos + 7.0 then if mouseX < curPos - 7.0 orelse mouseX > curPos + 7.0 then
getHorizontalClickPos getHorizontalClickPos
(xClickPoints, yClickPoints, idx + 1, mouseX, mouseY, r, g, b, ( xClickPoints
windowWidth, windowHeight) , yClickPoints
, idx + 1
, mouseX
, mouseY
, r
, g
, b
, windowWidth
, windowHeight
)
else else
getVerticalClickPos getVerticalClickPos
(yClickPoints, 0, curPos, mouseX, mouseY, r, g, b, windowWidth, ( yClickPoints
windowHeight) , 0
, curPos
, mouseX
, mouseY
, r
, g
, b
, windowWidth
, windowHeight
)
end end
in in
(* (*
@@ -140,8 +240,18 @@ struct
* an empty vector is returned. * an empty vector is returned.
*) *)
fun getClickPos (mouseX, mouseY, r, g, b, model: app_type) = fun getClickPos (mouseX, mouseY, r, g, b, model: app_type) =
getHorizontalClickPos (#xClickPoints model, #yClickPoints model, 0, getHorizontalClickPos
mouseX, mouseY, r, g, b, #windowWidth model, #windowHeight model) ( #xClickPoints model
, #yClickPoints model
, 0
, mouseX
, mouseY
, r
, g
, b
, #windowWidth model
, #windowHeight model
)
end end
fun getFirstTriangleStageVector (x1, y1, drawVec, model) = fun getFirstTriangleStageVector (x1, y1, drawVec, model) =
@@ -152,29 +262,11 @@ struct
val halfWidth = Real32.fromInt (windowWidth div 2) val halfWidth = Real32.fromInt (windowWidth div 2)
val halfHeight = Real32.fromInt (windowHeight div 2) val halfHeight = Real32.fromInt (windowHeight div 2)
val x1px = val x1px = calcRelativeX (x1, windowWidth, windowHeight, halfWidth)
if windowWidth > windowHeight then
let
val difference = windowWidth - windowHeight
val offset = Real32.fromInt (difference div 2)
in
x1 * (halfWidth - offset)
end
else
x1 * halfWidth
val left = (x1px - 5.0) / halfWidth val left = (x1px - 5.0) / halfWidth
val right = (x1px + 5.0) / halfWidth val right = (x1px + 5.0) / halfWidth
val y1px = val y1px = calcRelativeY (y1, windowWidth, windowHeight, halfHeight)
if windowHeight > windowWidth then
let
val difference = windowHeight - windowWidth
val offset = Real32.fromInt (difference div 2)
in
y1 * (halfHeight - offset)
end
else
y1 * halfHeight
val top = (y1px + 5.0) / halfHeight val top = (y1px + 5.0) / halfHeight
val bottom = (y1px - 5.0) / halfHeight val bottom = (y1px - 5.0) / halfHeight
@@ -191,21 +283,22 @@ struct
val halfWidth = Real32.fromInt (windowWidth div 2) val halfWidth = Real32.fromInt (windowWidth div 2)
val halfHeight = Real32.fromInt (windowHeight div 2) val halfHeight = Real32.fromInt (windowHeight div 2)
val x1px = x1 * halfWidth
val x1px = calcRelativeX (x1, windowWidth, windowHeight, halfWidth)
val left = (x1px - 5.0) / halfWidth val left = (x1px - 5.0) / halfWidth
val right = (x1px + 5.0) / halfWidth val right = (x1px + 5.0) / halfWidth
val y1px = y1 * halfHeight val y1px = calcRelativeY (y1, windowWidth, windowHeight, halfHeight)
val top = (y1px + 5.0) / halfHeight val top = (y1px + 5.0) / halfHeight
val bottom = (y1px - 5.0) / halfHeight val bottom = (y1px - 5.0) / halfHeight
val firstVec = ltrbToVertex (left, top, right, bottom, 0.0, 0.0, 1.0) val firstVec = ltrbToVertex (left, top, right, bottom, 0.0, 0.0, 1.0)
val x2px = x2 * halfWidth val x2px = calcRelativeX (x2, windowWidth, windowHeight, halfWidth)
val left = (x2px - 5.0) / halfWidth val left = (x2px - 5.0) / halfWidth
val right = (x2px + 5.0) / halfWidth val right = (x2px + 5.0) / halfWidth
val y2px = y2 * halfHeight val y2px = calcRelativeY (y2, windowWidth, windowHeight, halfHeight)
val top = (y2px + 5.0) / halfHeight val top = (y2px + 5.0) / halfHeight
val bottom = (y2px - 5.0) / halfHeight val bottom = (y2px - 5.0) / halfHeight
@@ -227,9 +320,8 @@ struct
fun mouseMoveOrRelease (model: app_type, mouseX, mouseY) = fun mouseMoveOrRelease (model: app_type, mouseX, mouseY) =
let let
val {xClickPoints, yClickPoints, ...} = model val {xClickPoints, yClickPoints, ...} = model
val (drawVec, _, _) = getClickPos val (drawVec, _, _) = getClickPos (mouseX, mouseY, 1.0, 0.0, 0.0, model)
(mouseX, mouseY, 1.0, 0.0, 0.0, model)
val drawVec = getTriangleStageVector (model, drawVec) val drawVec = getTriangleStageVector (model, drawVec)
val drawMsg = DRAW_BUTTON drawVec val drawMsg = DRAW_BUTTON drawVec
in in
@@ -238,7 +330,7 @@ struct
fun mouseLeftClick (model: app_type, mouseX, mouseY) = fun mouseLeftClick (model: app_type, mouseX, mouseY) =
let let
val {xClickPoints, yClickPoints, ...} = model val {xClickPoints, yClickPoints, ...} = model
val (buttonVec, hpos, vpos) = getClickPos val (buttonVec, hpos, vpos) = getClickPos
(mouseX, mouseY, 0.0, 0.0, 1.0, model) (mouseX, mouseY, 0.0, 0.0, 1.0, model)
in in
@@ -256,8 +348,8 @@ struct
end end
| FIRST {x1, y1} => | FIRST {x1, y1} =>
let let
val drawVec = getFirstTriangleStageVector (x1, y1, buttonVec, val drawVec =
model) getFirstTriangleStageVector (x1, y1, buttonVec, model)
val drawMsg = DRAW_BUTTON drawVec val drawMsg = DRAW_BUTTON drawVec
val newTriangleStage = SECOND val newTriangleStage = SECOND
@@ -282,28 +374,29 @@ struct
fun resizeWindow (model, mouseX, mouseY, width, height) = fun resizeWindow (model, mouseX, mouseY, width, height) =
let let
val msg = String.concat [ val msg = String.concat
"resized window. ", [ "resized window. "
"width = ", , "width = "
Int.toString width, , Int.toString width
" height = ", , " height = "
Int.toString height, , Int.toString height
"\n" , "\n"
] ]
val _ = print msg val _ = print msg
val model = AppType.withWindowResize (model, width, height) val model = AppType.withWindowResize (model, width, height)
val drawVec = getTrianglesVector model
val drawMsg = DRAW_TRIANGLES_AND_RESET_BUTTONS drawVec
in in
(model, NO_DRAW, mouseX, mouseY) (model, drawMsg, mouseX, mouseY)
end end
in in
fun update (model: app_type, mouseX, mouseY, inputMsg) = fun update (model: app_type, mouseX, mouseY, inputMsg) =
case inputMsg of case inputMsg of
MOUSE_MOVE {x = mouseX, y = mouseY} => MOUSE_MOVE {x = mouseX, y = mouseY} =>
mouseMoveOrRelease (model, mouseX, mouseY) mouseMoveOrRelease (model, mouseX, mouseY)
| MOUSE_LEFT_RELEASE => | MOUSE_LEFT_RELEASE => mouseMoveOrRelease (model, mouseX, mouseY)
mouseMoveOrRelease (model, mouseX, mouseY) | MOUSE_LEFT_CLICK => mouseLeftClick (model, mouseX, mouseY)
| MOUSE_LEFT_CLICK =>
mouseLeftClick (model, mouseX, mouseY)
| RESIZE_WINDOW {width, height} => | RESIZE_WINDOW {width, height} =>
resizeWindow (model, mouseX, mouseY, width, height) resizeWindow (model, mouseX, mouseY, width, height)
end end