add clickPoints to app-type so we are able to change those points at runtime
This commit is contained in:
@@ -15,21 +15,27 @@ sig
|
|||||||
, y3: Real32.real
|
, y3: Real32.real
|
||||||
}
|
}
|
||||||
|
|
||||||
type app_type = {triangleStage: triangle_stage, triangles: triangle list}
|
type app_type =
|
||||||
|
{ triangles: triangle list
|
||||||
|
, triangleStage: triangle_stage
|
||||||
|
, clickPoints: Real32.real vector
|
||||||
|
}
|
||||||
|
|
||||||
val initial: app_type
|
val getInitial: int * int -> app_type
|
||||||
|
|
||||||
|
val genClickPoints: int * int -> Real32.real vector
|
||||||
|
|
||||||
val withTriangleStage: app_type * triangle_stage -> app_type
|
val withTriangleStage: app_type * triangle_stage -> app_type
|
||||||
|
|
||||||
val addTriangleAndResetStage :
|
val addTriangleAndResetStage:
|
||||||
app_type *
|
|
||||||
Real32.real *
|
|
||||||
Real32.real *
|
|
||||||
Real32.real *
|
|
||||||
Real32.real *
|
|
||||||
Real32.real *
|
|
||||||
Real32.real ->
|
|
||||||
app_type
|
app_type
|
||||||
|
* Real32.real
|
||||||
|
* Real32.real
|
||||||
|
* Real32.real
|
||||||
|
* Real32.real
|
||||||
|
* Real32.real
|
||||||
|
* Real32.real
|
||||||
|
-> app_type
|
||||||
end
|
end
|
||||||
|
|
||||||
structure AppType :> APP_TYPE =
|
structure AppType :> APP_TYPE =
|
||||||
@@ -57,24 +63,48 @@ struct
|
|||||||
| SECOND of
|
| SECOND of
|
||||||
{x1: Real32.real, y1: Real32.real, x2: Real32.real, y2: Real32.real}
|
{x1: Real32.real, y1: Real32.real, x2: Real32.real, y2: Real32.real}
|
||||||
|
|
||||||
type app_type = {triangles: triangle list, triangleStage: triangle_stage}
|
type app_type =
|
||||||
|
{ triangles: triangle list
|
||||||
|
, triangleStage: triangle_stage
|
||||||
|
, clickPoints: Real32.real vector
|
||||||
|
}
|
||||||
|
|
||||||
val initial = {triangles = [], triangleStage = NO_TRIANGLE}
|
fun genClickPoints (windowWidth, windowHeight) =
|
||||||
|
let
|
||||||
|
val w = Real32.fromInt windowWidth / 40.0
|
||||||
|
val h = Real32.fromInt windowHeight / 40.0
|
||||||
|
in
|
||||||
|
Vector.tabulate (41, fn idx => Real32.fromInt idx * w)
|
||||||
|
end
|
||||||
|
|
||||||
|
fun getInitial (windowWidth, windowHeight) =
|
||||||
|
{ triangles = []
|
||||||
|
, triangleStage = NO_TRIANGLE
|
||||||
|
, clickPoints = genClickPoints (windowWidth, windowHeight)
|
||||||
|
}
|
||||||
|
|
||||||
fun withTriangleStage (app: app_type, newTriangleStage: triangle_stage) :
|
fun withTriangleStage (app: app_type, newTriangleStage: triangle_stage) :
|
||||||
app_type =
|
app_type =
|
||||||
let val {triangles, triangleStage = _} = app
|
let
|
||||||
in {triangles = triangles, triangleStage = newTriangleStage}
|
val {triangles, triangleStage = _, clickPoints = clickPoints} = app
|
||||||
|
in
|
||||||
|
{ triangles = triangles
|
||||||
|
, triangleStage = newTriangleStage
|
||||||
|
, clickPoints = clickPoints
|
||||||
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
fun addTriangleAndResetStage (app: app_type, x1, y1, x2, y2, x3, y3) :
|
fun addTriangleAndResetStage (app: app_type, x1, y1, x2, y2, x3, y3) :
|
||||||
app_type =
|
app_type =
|
||||||
let
|
let
|
||||||
val {triangles, triangleStage = _} = app
|
val {triangles, triangleStage = _, clickPoints = clickPoints} = 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}
|
||||||
val newTriangles = newTriangle :: triangles
|
val newTriangles = newTriangle :: triangles
|
||||||
in
|
in
|
||||||
{triangles = newTriangles, triangleStage = NO_TRIANGLE}
|
{ triangles = newTriangles
|
||||||
|
, triangleStage = NO_TRIANGLE
|
||||||
|
, clickPoints = clickPoints
|
||||||
|
}
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -28,18 +28,8 @@ struct
|
|||||||
]
|
]
|
||||||
|
|
||||||
local
|
local
|
||||||
fun genClickPoints (windowWidth, windowHeight) =
|
fun getVerticalClickPos
|
||||||
let
|
(clickPoints, idx, horizontalPos, mouseX, mouseY, r, g, b) =
|
||||||
val w = Real32.fromInt windowWidth / 40.0
|
|
||||||
val h = Real32.fromInt windowHeight / 40.0
|
|
||||||
in
|
|
||||||
Vector.tabulate (41, fn idx => Real32.fromInt idx * w)
|
|
||||||
end
|
|
||||||
|
|
||||||
val clickPoints =
|
|
||||||
genClickPoints (Constants.windowWidth, Constants.windowHeight)
|
|
||||||
|
|
||||||
fun getVerticalClickPos (idx, horizontalPos, mouseX, mouseY, r, g, b) =
|
|
||||||
if idx = Vector.length clickPoints then
|
if idx = Vector.length clickPoints then
|
||||||
(#[], 0.0, 0.0)
|
(#[], 0.0, 0.0)
|
||||||
else
|
else
|
||||||
@@ -48,7 +38,7 @@ 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
|
||||||
(idx + 1, horizontalPos, mouseX, mouseY, r, g, b)
|
(clickPoints, idx + 1, horizontalPos, mouseX, mouseY, r, g, b)
|
||||||
else
|
else
|
||||||
let
|
let
|
||||||
val halfWidth = Real32.fromInt (Constants.windowWidth div 2)
|
val halfWidth = Real32.fromInt (Constants.windowWidth div 2)
|
||||||
@@ -69,7 +59,7 @@ struct
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
fun getHorizontalClickPos (idx, mouseX, mouseY, r, g, b) =
|
fun getHorizontalClickPos (clickPoints, idx, mouseX, mouseY, r, g, b) =
|
||||||
if idx = Vector.length clickPoints then
|
if idx = Vector.length clickPoints then
|
||||||
(#[], 0.0, 0.0)
|
(#[], 0.0, 0.0)
|
||||||
else
|
else
|
||||||
@@ -77,9 +67,11 @@ struct
|
|||||||
val curPos = Vector.sub (clickPoints, idx)
|
val curPos = Vector.sub (clickPoints, idx)
|
||||||
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 (idx + 1, mouseX, mouseY, r, g, b)
|
getHorizontalClickPos
|
||||||
|
(clickPoints, idx + 1, mouseX, mouseY, r, g, b)
|
||||||
else
|
else
|
||||||
getVerticalClickPos (0, curPos, mouseX, mouseY, r, g, b)
|
getVerticalClickPos
|
||||||
|
(clickPoints, 0, curPos, mouseX, mouseY, r, g, b)
|
||||||
end
|
end
|
||||||
in
|
in
|
||||||
(*
|
(*
|
||||||
@@ -88,9 +80,8 @@ struct
|
|||||||
* If a square wasn't found at the clicked position,
|
* If a square wasn't found at the clicked position,
|
||||||
* an empty vector is returned.
|
* an empty vector is returned.
|
||||||
*)
|
*)
|
||||||
fun getClickPos (mouseX, mouseY, r, g, b) =
|
fun getClickPos (clickPoints, mouseX, mouseY, r, g, b) =
|
||||||
getHorizontalClickPos
|
getHorizontalClickPos (clickPoints, 0, mouseX, mouseY, r, g, b)
|
||||||
(0, mouseX, mouseY, r, g, b)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
fun getFirstTriangleStageVector (x1, y1, drawVec) =
|
fun getFirstTriangleStageVector (x1, y1, drawVec) =
|
||||||
@@ -154,7 +145,8 @@ struct
|
|||||||
case inputMsg of
|
case inputMsg of
|
||||||
MOUSE_MOVE {x = mouseX, y = mouseY} =>
|
MOUSE_MOVE {x = mouseX, y = mouseY} =>
|
||||||
let
|
let
|
||||||
val (drawVec, _, _) = getClickPos (mouseX, mouseY, 1.0, 0.0, 0.0)
|
val (drawVec, _, _) = getClickPos
|
||||||
|
(#clickPoints model, mouseX, mouseY, 1.0, 0.0, 0.0)
|
||||||
val drawVec = getTriangleStageVector (model, drawVec)
|
val drawVec = getTriangleStageVector (model, drawVec)
|
||||||
val drawMsg = DRAW_BUTTON drawVec
|
val drawMsg = DRAW_BUTTON drawVec
|
||||||
in
|
in
|
||||||
@@ -162,7 +154,8 @@ struct
|
|||||||
end
|
end
|
||||||
| MOUSE_LEFT_RELEASE =>
|
| MOUSE_LEFT_RELEASE =>
|
||||||
let
|
let
|
||||||
val (drawVec, _, _) = getClickPos (mouseX, mouseY, 1.0, 0.0, 0.0)
|
val (drawVec, _, _) = getClickPos
|
||||||
|
(#clickPoints model, mouseX, mouseY, 1.0, 0.0, 0.0)
|
||||||
val drawVec = getTriangleStageVector (model, drawVec)
|
val drawVec = getTriangleStageVector (model, drawVec)
|
||||||
val drawMsg = DRAW_BUTTON drawVec
|
val drawMsg = DRAW_BUTTON drawVec
|
||||||
in
|
in
|
||||||
@@ -171,7 +164,7 @@ struct
|
|||||||
| MUSE_LEFT_CLICK =>
|
| MUSE_LEFT_CLICK =>
|
||||||
let
|
let
|
||||||
val (buttonVec, hpos, vpos) = getClickPos
|
val (buttonVec, hpos, vpos) = getClickPos
|
||||||
(mouseX, mouseY, 0.0, 0.0, 1.0)
|
(#clickPoints model, mouseX, mouseY, 0.0, 0.0, 1.0)
|
||||||
in
|
in
|
||||||
if Vector.length buttonVec > 0 then
|
if Vector.length buttonVec > 0 then
|
||||||
(case #triangleStage model of
|
(case #triangleStage model of
|
||||||
|
|||||||
@@ -15,7 +15,13 @@ struct
|
|||||||
end
|
end
|
||||||
in
|
in
|
||||||
fun update (inputMailbox, drawMailbox) =
|
fun update (inputMailbox, drawMailbox) =
|
||||||
loop (inputMailbox, drawMailbox, 0.0, 0.0, AppType.initial)
|
loop
|
||||||
|
( inputMailbox
|
||||||
|
, drawMailbox
|
||||||
|
, 0.0
|
||||||
|
, 0.0
|
||||||
|
, AppType.getInitial (Constants.windowWidth, Constants.windowHeight)
|
||||||
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
fun draw
|
fun draw
|
||||||
@@ -35,7 +41,8 @@ struct
|
|||||||
val _ = Gles3.clear ()
|
val _ = Gles3.clear ()
|
||||||
|
|
||||||
val _ = AppDraw.drawGraphLines graphDrawObject
|
val _ = AppDraw.drawGraphLines graphDrawObject
|
||||||
val _ = AppDraw.drawTriangles (triangleDrawObject, triangleDrawLength)
|
val _ =
|
||||||
|
AppDraw.drawTriangles (triangleDrawObject, triangleDrawLength)
|
||||||
val _ = AppDraw.drawButton (buttonDrawObject, buttonDrawLength)
|
val _ = AppDraw.drawButton (buttonDrawObject, buttonDrawLength)
|
||||||
|
|
||||||
val _ = Glfw.pollEvents ()
|
val _ = Glfw.pollEvents ()
|
||||||
@@ -70,9 +77,11 @@ struct
|
|||||||
end
|
end
|
||||||
| DRAW_TRIANGLES_AND_RESET_BUTTONS triangleVec =>
|
| DRAW_TRIANGLES_AND_RESET_BUTTONS triangleVec =>
|
||||||
let
|
let
|
||||||
val _ = AppDraw.uploadTrianglesVector (triangleDrawObject, triangleVec)
|
val _ =
|
||||||
|
AppDraw.uploadTrianglesVector
|
||||||
|
(triangleDrawObject, triangleVec)
|
||||||
val triangleDrawLength = Vector.length triangleVec div 2
|
val triangleDrawLength = Vector.length triangleVec div 2
|
||||||
(* buttons are reset by setting buttonDrawLength to 0 *)
|
(* buttons are reset by setting buttonDrawLength to 0 *)
|
||||||
in
|
in
|
||||||
draw
|
draw
|
||||||
( drawMailbox
|
( drawMailbox
|
||||||
|
|||||||
Reference in New Issue
Block a user