a little refactoring

This commit is contained in:
2024-08-02 00:05:24 +01:00
parent 13063ea2c6
commit b01637ba3b
3 changed files with 102 additions and 100 deletions

BIN
dotscape

Binary file not shown.

View File

@@ -137,13 +137,11 @@ struct
| SECOND {x1, y1, x2, y2} => | SECOND {x1, y1, x2, y2} =>
getSecondTriangleStageVector (x1, y1, x2, y2, drawVec) getSecondTriangleStageVector (x1, y1, x2, y2, drawVec)
fun update (model, mouseX, mouseY, inputMsg) = local
let
open DrawMessage open DrawMessage
open InputMessage open InputMessage
in
case inputMsg of fun mouseMoveOrRelease (model, mouseX, mouseY) =
MOUSE_MOVE {x = mouseX, y = mouseY} =>
let let
val (drawVec, _, _) = getClickPos val (drawVec, _, _) = getClickPos
(#clickPoints model, mouseX, mouseY, 1.0, 0.0, 0.0) (#clickPoints model, mouseX, mouseY, 1.0, 0.0, 0.0)
@@ -152,43 +150,32 @@ struct
in in
(model, drawMsg, mouseX, mouseY) (model, drawMsg, mouseX, mouseY)
end end
| MOUSE_LEFT_RELEASE =>
let fun mouseLeftClick (model, mouseX, mouseY) =
val (drawVec, _, _) = getClickPos
(#clickPoints model, mouseX, mouseY, 1.0, 0.0, 0.0)
val drawVec = getTriangleStageVector (model, drawVec)
val drawMsg = DRAW_BUTTON drawVec
in
(model, drawMsg, mouseX, mouseY)
end
| MOUSE_LEFT_CLICK =>
let let
val (buttonVec, hpos, vpos) = getClickPos val (buttonVec, hpos, vpos) = getClickPos
(#clickPoints model, 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
NO_TRIANGLE => NO_TRIANGLE =>
let let
val drawVec = getTriangleStageVector (model, buttonVec) val drawVec = getTriangleStageVector (model, buttonVec)
val drawMsg = DRAW_BUTTON drawVec val drawMsg = DRAW_BUTTON drawVec
val newTriangleStage = FIRST {x1 = hpos, y1 = vpos} val newTriangleStage = FIRST {x1 = hpos, y1 = vpos}
val model = val model = AppType.withTriangleStage (model, newTriangleStage)
AppType.withTriangleStage (model, newTriangleStage)
in in
(model, drawMsg, mouseX, mouseY) (model, drawMsg, mouseX, mouseY)
end end
| FIRST {x1, y1} => | FIRST {x1, y1} =>
let let
val drawVec = val drawVec = getFirstTriangleStageVector (x1, y1, buttonVec)
getFirstTriangleStageVector (x1, y1, buttonVec)
val drawMsg = DRAW_BUTTON drawVec val drawMsg = DRAW_BUTTON 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 = AppType.withTriangleStage (model, newTriangleStage)
AppType.withTriangleStage (model, newTriangleStage)
in in
(model, drawMsg, mouseX, mouseY) (model, drawMsg, mouseX, mouseY)
end end
@@ -201,15 +188,30 @@ struct
val drawMsg = DRAW_TRIANGLES_AND_RESET_BUTTONS drawVec val drawMsg = DRAW_TRIANGLES_AND_RESET_BUTTONS drawVec
in in
(model, drawMsg, mouseX, mouseY) (model, drawMsg, mouseX, mouseY)
end) end
else else
(model, NO_DRAW, mouseX, mouseY) (model, NO_DRAW, mouseX, mouseY)
end end
| RESIZE_WINDOW {width, height} =>
fun resizeWindow (model, mouseX, mouseY, width, height) =
let let
val low = Int.min (width, height)
val high = Int.min (width, height)
val difference = high - low
val offset = difference div 2
val _ = print "resized window \n" val _ = print "resized window \n"
in in
(model, NO_DRAW, mouseX, mouseY) (model, NO_DRAW, mouseX, mouseY)
end end
in
fun update (model, mouseX, mouseY, inputMsg) =
case inputMsg of
MOUSE_MOVE {x = mouseX, y = mouseY} =>
mouseMoveOrRelease (model, mouseX, mouseY)
| MOUSE_LEFT_RELEASE => mouseMoveOrRelease (model, mouseX, mouseY)
| MOUSE_LEFT_CLICK => mouseLeftClick (model, mouseX, mouseY)
| RESIZE_WINDOW {width, height} =>
resizeWindow (model, mouseX, mouseY, width, height)
end end
end end

View File

@@ -94,6 +94,7 @@ struct
end end
fun drawButton (buttonDrawObject: draw_object, buttonDrawLength) = fun drawButton (buttonDrawObject: draw_object, buttonDrawLength) =
if buttonDrawLength > 0 then
let let
val {vertexBuffer, program, ...} = buttonDrawObject val {vertexBuffer, program, ...} = buttonDrawObject
val _ = Gles3.bindBuffer vertexBuffer val _ = Gles3.bindBuffer vertexBuffer
@@ -106,6 +107,8 @@ struct
in in
() ()
end end
else
()
fun initTriangles () = fun initTriangles () =
let let
@@ -114,12 +117,7 @@ struct
val {vertexBuffer, program, ...} = triangleDrawObject val {vertexBuffer, program, ...} = triangleDrawObject
val _ = Gles3.bindBuffer vertexBuffer val _ = Gles3.bindBuffer vertexBuffer
val _ = val _ = Gles3.bufferData (#[], 0, Gles3.STATIC_DRAW ())
Gles3.bufferData
( #[]
, 0
, Gles3.STATIC_DRAW ()
)
val _ = Gles3.vertexAttribPointer (0, 2, 2, 0) val _ = Gles3.vertexAttribPointer (0, 2, 2, 0)
val _ = Gles3.enableVertexAttribArray 0 val _ = Gles3.enableVertexAttribArray 0
in in
@@ -136,15 +134,17 @@ struct
end end
fun drawTriangles (triangleDrawObject: draw_object, triangleDrawLength) = fun drawTriangles (triangleDrawObject: draw_object, triangleDrawLength) =
if triangleDrawLength > 0 then
let let
val {vertexBuffer, program, ...} = triangleDrawObject val {vertexBuffer, program, ...} = triangleDrawObject
val _ = Gles3.bindBuffer vertexBuffer val _ = Gles3.bindBuffer vertexBuffer
val _ = Gles3.vertexAttribPointer (0, 2, 2, 0) val _ = Gles3.vertexAttribPointer (0, 2, 2, 0)
val _ = Gles3.enableVertexAttribArray 0 val _ = Gles3.enableVertexAttribArray 0
val _ = Gles3.useProgram program val _ = Gles3.useProgram program
val _ = Gles3.drawArrays val _ = Gles3.drawArrays (Gles3.TRIANGLES (), 0, triangleDrawLength)
(Gles3.TRIANGLES (), 0, triangleDrawLength)
in in
() ()
end end
else
()
end end