Files
sml-projects/functional-core/app-with.sml

280 lines
6.7 KiB
Standard ML
Raw Normal View History

signature APP_WITH =
sig
val windowResize: AppType.app_type * int * int -> AppType.app_type
2024-08-08 21:35:48 +01:00
val mousePosition: AppType.app_type * Real32.real * Real32.real
-> AppType.app_type
val undoTriangleStage:
2024-08-08 21:35:48 +01:00
AppType.app_type * AppType.triangle_stage * (Real32.real * Real32.real)
-> AppType.app_type
val undoTriangle:
AppType.app_type
* AppType.triangle_stage
* AppType.triangle list
* (Real32.real * Real32.real)
2024-08-08 21:35:48 +01:00
-> AppType.app_type
(*
* add functions clear the redo stack,
* as they are meant to be called after a click action,
* and also add new click position to undo stack.
*)
val addTriangleStage:
AppType.app_type * AppType.triangle_stage * (Real32.real * Real32.real)
-> AppType.app_type
val addTriangle:
AppType.app_type
* Real32.real
* Real32.real
* Real32.real
* Real32.real
* Real32.real
* Real32.real
2024-08-08 21:35:48 +01:00
* (Real32.real * Real32.real)
-> AppType.app_type
end
structure AppWith :> APP_WITH =
2024-08-08 00:18:03 +01:00
struct
open AppType
(* add to undo, clear redo *)
fun addTriangleStage
(app: app_type, newTriangleStage: triangle_stage, newUndoHd) : app_type =
2024-08-08 21:35:48 +01:00
let
val
{ triangleStage = _
, triangles
, xClickPoints
, yClickPoints
, windowWidth
, windowHeight
, graphLines
, undo
, redo = _
, mouseX
, mouseY
2024-08-08 21:35:48 +01:00
} = app
val newUndo = newUndoHd :: undo
2024-08-08 21:35:48 +01:00
in
{ triangleStage = newTriangleStage
, undo = newUndo
, redo = []
2024-08-08 21:35:48 +01:00
, triangles = triangles
, xClickPoints = xClickPoints
, yClickPoints = yClickPoints
, windowWidth = windowWidth
, windowHeight = windowHeight
, graphLines = graphLines
, mouseX = mouseX
, mouseY = mouseY
2024-08-08 21:35:48 +01:00
}
end
fun addTriangle (app: app_type, x1, y1, x2, y2, x3, y3, newUndoHd) : app_type =
2024-08-08 00:18:03 +01:00
let
val
{ triangles
, triangleStage = _
2024-08-08 00:18:03 +01:00
, xClickPoints
, yClickPoints
, windowWidth
, windowHeight
, graphLines
, undo
, redo = _
, mouseX
, mouseY
2024-08-08 00:18:03 +01:00
} = app
val newTriangle = {x1 = x1, y1 = y1, x2 = x2, y2 = y2, x3 = x3, y3 = y3}
val newTriangles = newTriangle :: triangles
val newUndo = newUndoHd :: undo
2024-08-08 00:18:03 +01:00
in
{ triangleStage = NO_TRIANGLE
, triangles = newTriangles
, undo = newUndo
, redo = []
2024-08-08 00:18:03 +01:00
, xClickPoints = xClickPoints
, yClickPoints = yClickPoints
, windowWidth = windowWidth
, windowHeight = windowHeight
, graphLines = graphLines
, mouseX = mouseX
, mouseY = mouseY
2024-08-08 00:18:03 +01:00
}
end
(* add to redo, pop one from undo *)
fun undoTriangleStage (app: app_type, newTriangleStage, newRedoHd) =
2024-08-08 21:35:48 +01:00
let
val
{ triangleStage = _
, triangles
2024-08-08 21:35:48 +01:00
, xClickPoints
, yClickPoints
, windowWidth
, windowHeight
, graphLines
, undo
, redo
, mouseX
, mouseY
2024-08-08 21:35:48 +01:00
} = app
val newUndo =
case undo of
hd :: tl => tl
| empty => empty
val newRedo = newRedoHd :: redo
2024-08-08 21:35:48 +01:00
in
{ triangleStage = newTriangleStage
, triangles = triangles
, undo = newUndo
, redo = newRedo
2024-08-08 21:35:48 +01:00
, xClickPoints = xClickPoints
, yClickPoints = yClickPoints
, windowWidth = windowWidth
, windowHeight = windowHeight
, graphLines = graphLines
, mouseX = mouseX
, mouseY = mouseY
2024-08-08 21:35:48 +01:00
}
end
fun undoTriangle
(app: app_type, newTriangleStage: triangle_stage, trianglesTl, newRedoHd) :
2024-08-08 21:35:48 +01:00
app_type =
2024-08-08 00:18:03 +01:00
let
val
{ triangleStage = _
, triangles = _
2024-08-08 00:18:03 +01:00
, xClickPoints
, yClickPoints
, windowWidth
, windowHeight
, graphLines
, undo
, redo
, mouseX
, mouseY
2024-08-08 00:18:03 +01:00
} = app
val newUndo =
case undo of
hd :: tl => tl
| empty => empty
val newRedo = newRedoHd :: redo
2024-08-08 00:18:03 +01:00
in
{ triangleStage = newTriangleStage
, triangles = trianglesTl
, undo = newUndo
, redo = newRedo
2024-08-08 00:18:03 +01:00
, xClickPoints = xClickPoints
, yClickPoints = yClickPoints
, windowWidth = windowWidth
, windowHeight = windowHeight
, graphLines = graphLines
, mouseX = mouseX
, mouseY = mouseY
2024-08-08 00:18:03 +01:00
}
end
fun helpWindowResize
(app: app_type, windowWidth, windowHeight, wStart, wFinish, hStart, hFinish) :
app_type =
let
val
{ xClickPoints = _
, yClickPoints = _
, windowWidth = _
, windowHeight = _
, graphLines = _
, triangles
, triangleStage
, undo
, redo
, mouseX
, mouseY
} = app
val xClickPoints = ClickPoints.generate (wStart, wFinish)
val yClickPoints = ClickPoints.generate (hStart, hFinish)
val graphLines =
GraphLines.generate
(windowWidth, windowHeight, xClickPoints, yClickPoints)
in
{ xClickPoints = xClickPoints
, yClickPoints = yClickPoints
, graphLines = graphLines
, triangles = triangles
, triangleStage = triangleStage
, windowWidth = windowWidth
, windowHeight = windowHeight
, undo = undo
, redo = redo
, mouseX = mouseX
, mouseY = mouseY
}
end
fun windowResize (app: app_type, windowWidth, windowHeight) =
if windowWidth = windowHeight then
helpWindowResize
(app, windowWidth, windowHeight, 0, windowWidth, 0, windowHeight)
else if windowWidth > windowHeight then
let
val difference = windowWidth - windowHeight
val wStart = difference div 2
val wFinish = wStart + windowHeight
in
helpWindowResize
(app, windowWidth, windowHeight, wStart, wFinish, 0, windowHeight)
end
else
2024-08-08 00:18:03 +01:00
let
val difference = windowHeight - windowWidth
val hStart = difference div 2
val hFinish = hStart + windowWidth
2024-08-08 00:18:03 +01:00
in
helpWindowResize
(app, windowWidth, windowHeight, 0, windowWidth, hStart, hFinish)
2024-08-08 00:18:03 +01:00
end
fun mousePosition (app: app_type, mouseX, mouseY) =
let
val
{ mouseX = _
, mouseY = _
, triangles
, triangleStage
, xClickPoints
, yClickPoints
, windowWidth
, windowHeight
, graphLines
, undo
, redo
} = app
in
{ mouseX = mouseX
, mouseY = mouseY
, triangles = triangles
, triangleStage = triangleStage
, xClickPoints = xClickPoints
, yClickPoints = yClickPoints
, windowWidth = windowWidth
, windowHeight = windowHeight
, graphLines = graphLines
, undo = undo
, redo = redo
}
end
2024-08-08 00:18:03 +01:00
end