attempt a little refactoring (but note that this refactoring is untested)

This commit is contained in:
2024-09-16 21:49:05 +01:00
parent d21f31778a
commit 9153217d5f
3 changed files with 94 additions and 222 deletions

BIN
dotscape

Binary file not shown.

View File

@@ -15,57 +15,69 @@ struct
fun mouseMoveOrRelease (model: app_type) = fun mouseMoveOrRelease (model: app_type) =
let let
val (drawVec, _, _) = ClickPoints.getClickPosition (1.0, 0.0, 0.0, model) val drawVec =
val drawVec = TriangleStage.toVector (model, drawVec) case ClickPoints.getClickPositionFromMouse model of
SOME (xpos, ypos) =>
ClickPoints.getDrawVec (xpos, ypos, 1.0, 0.0, 0.0, model)
| NONE => Vector.fromList []
val drawMsg = DRAW_DOT drawVec val drawMsg = DRAW_DOT drawVec
in in
(model, DRAW drawMsg) (model, DRAW drawMsg)
end end
fun mouseLeftClick (model: app_type) = fun mouseLeftClick (model: app_type) =
let case ClickPoints.getClickPositionFromMouse model of
val (dotVec, hpos, vpos) = SOME (xpos, ypos) =>
ClickPoints.getClickPosition (0.0, 0.0, 1.0, model) let
val newUndoTuple = (hpos, vpos) val dotVec = ClickPoints.getDrawVec (xpos, ypos, 0.0, 0.0, 1.0, model)
in val {windowWidth, windowHeight, ...} = model
if Vector.length dotVec > 0 then val halfWidth = Real32.fromInt (windowWidth div 2)
case #triangleStage model of val halfHeight = Real32.fromInt (windowHeight div 2)
NO_TRIANGLE => val hpos =
let Ndc.centreAlignX (xpos, windowWidth, windowHeight, halfWidth)
val drawVec = TriangleStage.toVector (model, dotVec) val vpos =
val drawMsg = DRAW_DOT drawVec Ndc.centreAlignY (ypos, windowWidth, windowHeight, halfHeight)
val newUndoTuple = (hpos, vpos)
in
(case #triangleStage model of
NO_TRIANGLE =>
let
val drawVec = TriangleStage.toVector (model, dotVec)
val drawMsg = DRAW_DOT drawVec
val newTriangleStage = FIRST {x1 = hpos, y1 = vpos} val newTriangleStage = FIRST {x1 = hpos, y1 = vpos}
val model = val model =
AppWith.addTriangleStage (model, newTriangleStage, newUndoTuple) AppWith.addTriangleStage
in (model, newTriangleStage, newUndoTuple)
(model, DRAW drawMsg) in
end (model, DRAW drawMsg)
| FIRST {x1, y1} => end
let | FIRST {x1, y1} =>
val drawVec = TriangleStage.firstToVector (x1, y1, dotVec, model) let
val drawMsg = DRAW_DOT drawVec val drawVec =
TriangleStage.firstToVector (x1, y1, dotVec, model)
val drawMsg = DRAW_DOT drawVec
val newTriangleStage = SECOND val newTriangleStage = SECOND
{x1 = x1, y1 = y1, x2 = hpos, y2 = vpos} {x1 = x1, y1 = y1, x2 = hpos, y2 = vpos}
val model = val model =
AppWith.addTriangleStage (model, newTriangleStage, newUndoTuple) AppWith.addTriangleStage
in (model, newTriangleStage, newUndoTuple)
(model, DRAW drawMsg) in
end (model, DRAW drawMsg)
| SECOND {x1, y1, x2, y2} => end
let | SECOND {x1, y1, x2, y2} =>
val model = AppWith.addTriangle let
(model, x1, y1, x2, y2, hpos, vpos, newUndoTuple) val model = AppWith.addTriangle
(model, x1, y1, x2, y2, hpos, vpos, newUndoTuple)
val drawVec = Triangles.toVector model val drawVec = Triangles.toVector model
val drawMsg = DRAW_TRIANGLES_AND_RESET_DOTS drawVec val drawMsg = DRAW_TRIANGLES_AND_RESET_DOTS drawVec
in in
(model, DRAW drawMsg) (model, DRAW drawMsg)
end end)
else end
(model, NO_MAILBOX) | NONE => (model, NO_MAILBOX)
end
fun resizeWindow (model, width, height) = fun resizeWindow (model, width, height) =
let let
@@ -182,9 +194,7 @@ struct
in in
(model, DRAW drawMsg) (model, DRAW drawMsg)
end) end)
| [] => | [] => (* Nothing to redo. *) (model, NO_MAILBOX)
(* Nothing to redo. *)
(model, NO_MAILBOX)
fun toggleGraph (model: app_type) = fun toggleGraph (model: app_type) =
if #showGraph model then if #showGraph model then

View File

@@ -1,9 +1,16 @@
signature CLICK_POINTS = signature CLICK_POINTS =
sig sig
val generate: int * int -> Real32.real vector val generate: int * int -> Real32.real vector
val getClickPosition: val getClickPositionFromMouse: AppType.app_type
Real32.real * Real32.real * Real32.real * AppType.app_type -> (Real32.real * Real32.real) option
-> Real32.real vector * Real32.real * Real32.real val getDrawVec:
Real32.real
* Real32.real
* Real32.real
* Real32.real
* Real32.real
* AppType.app_type
-> Real32.real vector
end end
structure ClickPoints :> CLICK_POINTS = structure ClickPoints :> CLICK_POINTS =
@@ -25,188 +32,43 @@ struct
* *) * *)
val range = 15.0 val range = 15.0
fun getVerticalClickPos fun getClickPos (clickPoints, mousePos, idx) =
( yClickPoints if idx = Vector.length clickPoints then
, idx NONE
, horizontalPos
, mouseX
, mouseY
, r
, g
, b
, windowWidth
, windowHeight
) =
if idx = Vector.length yClickPoints then
(#[], 0.0, 0.0)
else else
let let
val curVerticalPos = Vector.sub (yClickPoints, idx) val curPos = Vector.sub (clickPoints, idx)
in in
if if mousePos < curPos - range orelse mousePos > curPos + range then
mouseY < curVerticalPos - range orelse mouseY > curVerticalPos + range getClickPos (clickPoints, mousePos, idx + 1)
then
getVerticalClickPos
( yClickPoints
, idx + 1
, horizontalPos
, mouseX
, mouseY
, r
, g
, b
, windowWidth
, windowHeight
)
else else
let SOME (Vector.sub (clickPoints, idx))
(* calculate normalised device coordinates *)
val halfWidth = Real32.fromInt (windowWidth div 2)
val halfHeight = Real32.fromInt (windowHeight div 2)
val hpos = horizontalPos - halfWidth
val vpos = ~(curVerticalPos - halfHeight)
(* coordinates to form small box around clicked area *)
val left = (hpos - 5.0) / halfWidth
val right = (hpos + 5.0) / halfWidth
val bottom = (vpos - 5.0) / halfHeight
val top = (vpos + 5.0) / halfHeight
(* normalised device coordinates of drawVec should be relative
* to actual windowWidth and windowHeight,
* even if not a square, to display cursor position... *)
val drawVec = Ndc.ltrbToVertex (left, top, right, bottom, r, g, b)
in
(*
* ...however, normalised device coordinate of hpos and vpos
* should be relative to the vertical centre
* (if height is greater than width)
* or horizontal centre
* (if width is greater than height).
*
* So, for example, a 900x1000 resolution
* will have clickable points from 50...950,
* in increments of 50.
* Because we always want to show canvas as a square
* with an aspect ratio of 1:1.
* For displaying the click position on the screen, drawVec
* which uses actual windowWidth and windowHeight, is fine.
* However, we want to attach the meaning "start" to 50
* and "end" to 950,
* so the hpos and vpos stored in the app's triangle list
* subtracts the offset 50 if needed,
* allowing us to treat the coordinates as a 900x900 square.
*
* We may not actually want to render a square to the screen
* if the screen's aspect ratio is not 1:1,
* but it's the responsibility of the rendering code
* which turns triangles into OpenGL vectors
* to do that.
* *)
if windowWidth = windowHeight then
let
val hpos = hpos / halfWidth
val vpos = vpos / halfHeight
in
(drawVec, hpos, vpos)
end
else if windowWidth > windowHeight then
let
val difference = windowWidth - windowHeight
val offset = Real32.fromInt (difference div 2)
val hpos = hpos / (halfWidth - offset)
val vpos = vpos / halfHeight
in
(drawVec, hpos, vpos)
end
else
(* windowHeight > windowWidth *)
let
val difference = windowHeight - windowWidth
val offset = Real32.fromInt (difference div 2)
val hpos = hpos / halfWidth
val vpos = vpos / (halfHeight - offset)
in
(drawVec, hpos, vpos)
end
end
end end
fun getHorizontalClickPos fun getClickPositionFromMouse (app: AppType.app_type) =
( xClickPoints case getClickPos (#xClickPoints app, #mouseX app, 0) of
, yClickPoints SOME xPos =>
, idx (case getClickPos (#yClickPoints app, #mouseY app, 0) of
, mouseX SOME yPos => SOME (xPos, yPos)
, mouseY | NONE => NONE)
, r | NONE => NONE
, g
, b
, windowWidth
, windowHeight
) =
if idx = Vector.length xClickPoints then
(#[], 0.0, 0.0)
else
let
val curPos = Vector.sub (xClickPoints, idx)
in
if mouseX < curPos - range orelse mouseX > curPos + range then
getHorizontalClickPos
( xClickPoints
, yClickPoints
, idx + 1
, mouseX
, mouseY
, r
, g
, b
, windowWidth
, windowHeight
)
else
getVerticalClickPos
( yClickPoints
, 0
, curPos
, mouseX
, mouseY
, r
, g
, b
, windowWidth
, windowHeight
)
end
(* fun getDrawVec (xpos, ypos, r, g, b, app: AppType.app_type) =
* This function returns a vector containing the position data of the
* clicked square.
* If a square wasn't found at the clicked position,
* an empty vector is returned.
*)
fun getClickPosition (r, g, b, app: AppType.app_type) =
let let
val val {windowWidth, windowHeight, ...} = app
{ xClickPoints
, yClickPoints (* calculate normalised device coordinates *)
, mouseX val halfWidth = Real32.fromInt (windowWidth div 2)
, mouseY val halfHeight = Real32.fromInt (windowHeight div 2)
, windowWidth val hpos = xpos - halfWidth
, windowHeight val vpos = ~(ypos - halfHeight)
, ...
} = app (* coordinates to form small box around clicked area *)
val left = (hpos - 5.0) / halfWidth
val right = (hpos + 5.0) / halfWidth
val bottom = (vpos - 5.0) / halfHeight
val top = (vpos + 5.0) / halfHeight
in in
getHorizontalClickPos Ndc.ltrbToVertex (left, top, right, bottom, r, g, b)
( xClickPoints
, yClickPoints
, 0
, mouseX
, mouseY
, r
, g
, b
, windowWidth
, windowHeight
)
end end
end end