attempt a little refactoring (but note that this refactoring is untested)
This commit is contained in:
@@ -15,21 +15,31 @@ 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) =
|
||||||
|
case ClickPoints.getClickPositionFromMouse model of
|
||||||
|
SOME (xpos, ypos) =>
|
||||||
let
|
let
|
||||||
val (dotVec, hpos, vpos) =
|
val dotVec = ClickPoints.getDrawVec (xpos, ypos, 0.0, 0.0, 1.0, model)
|
||||||
ClickPoints.getClickPosition (0.0, 0.0, 1.0, model)
|
val {windowWidth, windowHeight, ...} = model
|
||||||
|
val halfWidth = Real32.fromInt (windowWidth div 2)
|
||||||
|
val halfHeight = Real32.fromInt (windowHeight div 2)
|
||||||
|
val hpos =
|
||||||
|
Ndc.centreAlignX (xpos, windowWidth, windowHeight, halfWidth)
|
||||||
|
val vpos =
|
||||||
|
Ndc.centreAlignY (ypos, windowWidth, windowHeight, halfHeight)
|
||||||
val newUndoTuple = (hpos, vpos)
|
val newUndoTuple = (hpos, vpos)
|
||||||
in
|
in
|
||||||
if Vector.length dotVec > 0 then
|
(case #triangleStage model of
|
||||||
case #triangleStage model of
|
|
||||||
NO_TRIANGLE =>
|
NO_TRIANGLE =>
|
||||||
let
|
let
|
||||||
val drawVec = TriangleStage.toVector (model, dotVec)
|
val drawVec = TriangleStage.toVector (model, dotVec)
|
||||||
@@ -37,19 +47,22 @@ struct
|
|||||||
|
|
||||||
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
|
||||||
|
(model, newTriangleStage, newUndoTuple)
|
||||||
in
|
in
|
||||||
(model, DRAW drawMsg)
|
(model, DRAW drawMsg)
|
||||||
end
|
end
|
||||||
| FIRST {x1, y1} =>
|
| FIRST {x1, y1} =>
|
||||||
let
|
let
|
||||||
val drawVec = TriangleStage.firstToVector (x1, y1, dotVec, model)
|
val drawVec =
|
||||||
|
TriangleStage.firstToVector (x1, y1, dotVec, model)
|
||||||
val drawMsg = DRAW_DOT drawVec
|
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
|
||||||
|
(model, newTriangleStage, newUndoTuple)
|
||||||
in
|
in
|
||||||
(model, DRAW drawMsg)
|
(model, DRAW drawMsg)
|
||||||
end
|
end
|
||||||
@@ -62,10 +75,9 @@ struct
|
|||||||
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
|
end
|
||||||
else
|
| NONE => (model, NO_MAILBOX)
|
||||||
(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
|
||||||
|
|||||||
@@ -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
|
||||||
|
SOME (Vector.sub (clickPoints, idx))
|
||||||
|
end
|
||||||
|
|
||||||
|
fun getClickPositionFromMouse (app: AppType.app_type) =
|
||||||
|
case getClickPos (#xClickPoints app, #mouseX app, 0) of
|
||||||
|
SOME xPos =>
|
||||||
|
(case getClickPos (#yClickPoints app, #mouseY app, 0) of
|
||||||
|
SOME yPos => SOME (xPos, yPos)
|
||||||
|
| NONE => NONE)
|
||||||
|
| NONE => NONE
|
||||||
|
|
||||||
|
fun getDrawVec (xpos, ypos, r, g, b, app: AppType.app_type) =
|
||||||
let
|
let
|
||||||
|
val {windowWidth, windowHeight, ...} = app
|
||||||
|
|
||||||
(* calculate normalised device coordinates *)
|
(* calculate normalised device coordinates *)
|
||||||
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 hpos = horizontalPos - halfWidth
|
val hpos = xpos - halfWidth
|
||||||
val vpos = ~(curVerticalPos - halfHeight)
|
val vpos = ~(ypos - halfHeight)
|
||||||
|
|
||||||
(* coordinates to form small box around clicked area *)
|
(* coordinates to form small box around clicked area *)
|
||||||
val left = (hpos - 5.0) / halfWidth
|
val left = (hpos - 5.0) / halfWidth
|
||||||
val right = (hpos + 5.0) / halfWidth
|
val right = (hpos + 5.0) / halfWidth
|
||||||
val bottom = (vpos - 5.0) / halfHeight
|
val bottom = (vpos - 5.0) / halfHeight
|
||||||
val top = (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
|
in
|
||||||
(*
|
Ndc.ltrbToVertex (left, top, right, bottom, r, g, b)
|
||||||
* ...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
|
|
||||||
|
|
||||||
fun getHorizontalClickPos
|
|
||||||
( xClickPoints
|
|
||||||
, yClickPoints
|
|
||||||
, idx
|
|
||||||
, mouseX
|
|
||||||
, mouseY
|
|
||||||
, r
|
|
||||||
, 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
|
|
||||||
|
|
||||||
(*
|
|
||||||
* 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
|
|
||||||
val
|
|
||||||
{ xClickPoints
|
|
||||||
, yClickPoints
|
|
||||||
, mouseX
|
|
||||||
, mouseY
|
|
||||||
, windowWidth
|
|
||||||
, windowHeight
|
|
||||||
, ...
|
|
||||||
} = app
|
|
||||||
in
|
|
||||||
getHorizontalClickPos
|
|
||||||
( xClickPoints
|
|
||||||
, yClickPoints
|
|
||||||
, 0
|
|
||||||
, mouseX
|
|
||||||
, mouseY
|
|
||||||
, r
|
|
||||||
, g
|
|
||||||
, b
|
|
||||||
, windowWidth
|
|
||||||
, windowHeight
|
|
||||||
)
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user