add redo stack, and adjust functions to clear it when new triangle or triangle_stage is added; when an undo function is called, the undoHd becomes the redoHd, and undo becomes undoTl, which is expected behaviour. Still need to implement REDO_ACTION though.

This commit is contained in:
2024-08-08 23:10:38 +01:00
parent 4243e4c679
commit 10b3617689
5 changed files with 126 additions and 84 deletions

BIN
dotscape

Binary file not shown.

View File

@@ -24,6 +24,7 @@ struct
, yClickPoints = yClickPoints , yClickPoints = yClickPoints
, graphLines = graphLines , graphLines = graphLines
, undo = [] , undo = []
, redo = []
, mouseX = 0.0 , mouseX = 0.0
, mouseY = 0.0 , mouseY = 0.0
} }

View File

@@ -24,6 +24,7 @@ sig
, yClickPoints: Real32.real vector , yClickPoints: Real32.real vector
, graphLines: Real32.real vector , graphLines: Real32.real vector
, undo: (Real32.real * Real32.real) list , undo: (Real32.real * Real32.real) list
, redo: (Real32.real * Real32.real) list
, mouseX: Real32.real , mouseX: Real32.real
, mouseY: Real32.real , mouseY: Real32.real
} }
@@ -63,6 +64,7 @@ struct
, yClickPoints: Real32.real vector , yClickPoints: Real32.real vector
, graphLines: Real32.real vector , graphLines: Real32.real vector
, undo: (Real32.real * Real32.real) list , undo: (Real32.real * Real32.real) list
, redo: (Real32.real * Real32.real) list
, mouseX: Real32.real , mouseX: Real32.real
, mouseY: Real32.real , mouseY: Real32.real
} }

View File

@@ -1,6 +1,6 @@
signature APP_UPDATE = signature APP_UPDATE =
sig sig
val update: AppType.app_type * InputMessage.t val update: AppType.app_type * InputMessage.t
-> AppType.app_type * DrawMessage.t -> AppType.app_type * DrawMessage.t
end end
@@ -73,7 +73,7 @@ struct
val newTriangleStage = FIRST {x1 = hpos, y1 = vpos} val newTriangleStage = FIRST {x1 = hpos, y1 = vpos}
val model = val model =
AppWith.newTriangleStage (model, newTriangleStage, newUndoTuple) AppWith.addTriangleStage (model, newTriangleStage, newUndoTuple)
in in
(model, drawMsg) (model, drawMsg)
end end
@@ -86,13 +86,13 @@ struct
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.newTriangleStage (model, newTriangleStage, newUndoTuple) AppWith.addTriangleStage (model, newTriangleStage, newUndoTuple)
in in
(model, drawMsg) (model, drawMsg)
end end
| SECOND {x1, y1, x2, y2} => | SECOND {x1, y1, x2, y2} =>
let let
val model = AppWith.newTriangle val model = AppWith.addTriangle
(model, x1, y1, x2, y2, hpos, vpos, newUndoTuple) (model, x1, y1, x2, y2, hpos, vpos, newUndoTuple)
val drawVec = Triangles.toVector model val drawVec = Triangles.toVector model
@@ -120,31 +120,33 @@ struct
case #triangleStage model of case #triangleStage model of
FIRST {x1, y1} => FIRST {x1, y1} =>
(* Change FIRST to NO_TRIANGLE and clear buttons. *) (* Change FIRST to NO_TRIANGLE and clear buttons. *)
let val model = AppWith.replaceTriangleStage (model, NO_TRIANGLE) let val model = AppWith.undoTriangleStage (model, NO_TRIANGLE, (x1, y1))
in (model, CLEAR_BUTTONS) in (model, CLEAR_BUTTONS)
end end
| SECOND {x1, y1, x2, y2} => | SECOND {x1, y1, x2, y2} =>
(* Change FIRST to SECOND and redraw buttons. *) (* Change FIRST to SECOND and redraw buttons. *)
let let
val newTriangleStage = FIRST {x1 = x1, y1 = y1} val newTriangleStage = FIRST {x1 = x1, y1 = y1}
val model = AppWith.replaceTriangleStage (model, newTriangleStage) val model =
AppWith.undoTriangleStage (model, newTriangleStage, (x2, y2))
val drawVec = val emptyVec: Real32.real vector = Vector.fromList []
TriangleStage.firstToVector (x1, y1, Vector.fromList [], model) val drawVec = TriangleStage.firstToVector (x1, y1, emptyVec, model)
val drawMsg = DRAW_BUTTON drawVec val drawMsg = DRAW_BUTTON drawVec
in in
(model, drawMsg) (model, drawMsg)
end end
| NO_TRIANGLE => | NO_TRIANGLE =>
(case #triangles model of (case #triangles model of
{x1, y1, x2, y2, ...} :: trianglesTl => {x1, y1, x2, y2, x3, y3} :: trianglesTl =>
(* Have to slice off (x3, y3) from triangle head, (* Have to slice off (x3, y3) from triangle head,
* turn (x1, y1, x2, y2) into a triangleStage, * turn (x1, y1, x2, y2) into a triangleStage,
* and redraw both triangle and triangleStage. *) * and redraw both triangle and triangleStage. *)
let let
val triangleStage = SECOND {x1 = x1, y1 = y1, x2 = x2, y2 = y2} val triangleStage = SECOND {x1 = x1, y1 = y1, x2 = x2, y2 = y2}
val model = val model =
AppWith.undoTriangle (model, triangleStage, trianglesTl) AppWith.undoTriangle
(model, triangleStage, trianglesTl, (x3, y3))
val newTriangleVec = Triangles.toVector model val newTriangleVec = Triangles.toVector model
val drawVec = TriangleStage.secondToVector val drawVec = TriangleStage.secondToVector
@@ -167,7 +169,6 @@ struct
end end
| MOUSE_LEFT_RELEASE => mouseMoveOrRelease model | MOUSE_LEFT_RELEASE => mouseMoveOrRelease model
| MOUSE_LEFT_CLICK => mouseLeftClick model | MOUSE_LEFT_CLICK => mouseLeftClick model
| RESIZE_WINDOW {width, height} => | RESIZE_WINDOW {width, height} => resizeWindow (model, width, height)
resizeWindow (model, width, height)
| UNDO_ACTION => undoAction model | UNDO_ACTION => undoAction model
end end

View File

@@ -2,17 +2,30 @@ signature APP_WITH =
sig sig
val windowResize: AppType.app_type * int * int -> AppType.app_type val windowResize: AppType.app_type * int * int -> AppType.app_type
val newTriangleStage: val mousePosition: AppType.app_type * Real32.real * Real32.real
-> AppType.app_type
val undoTriangleStage:
AppType.app_type * AppType.triangle_stage * (Real32.real * Real32.real) AppType.app_type * AppType.triangle_stage * (Real32.real * Real32.real)
-> AppType.app_type -> AppType.app_type
val replaceTriangleStage: AppType.app_type * AppType.triangle_stage
-> AppType.app_type
val undoTriangle: val undoTriangle:
AppType.app_type * AppType.triangle_stage * AppType.triangle list AppType.app_type
* AppType.triangle_stage
* AppType.triangle list
* (Real32.real * Real32.real)
-> AppType.app_type -> AppType.app_type
val newTriangle: (*
* 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 AppType.app_type
* Real32.real * Real32.real
* Real32.real * Real32.real
@@ -22,17 +35,15 @@ sig
* Real32.real * Real32.real
* (Real32.real * Real32.real) * (Real32.real * Real32.real)
-> AppType.app_type -> AppType.app_type
val mousePosition: AppType.app_type * Real32.real * Real32.real
-> AppType.app_type
end end
structure AppWith :> APP_WITH = structure AppWith :> APP_WITH =
struct struct
open AppType open AppType
fun newTriangleStage (* add to undo, clear redo *)
(app: app_type, newTriangleStage: triangle_stage, xyTuple) : app_type = fun addTriangleStage
(app: app_type, newTriangleStage: triangle_stage, newUndoHd) : app_type =
let let
val val
{ triangleStage = _ { triangleStage = _
@@ -43,84 +54,28 @@ struct
, windowHeight , windowHeight
, graphLines , graphLines
, undo , undo
, redo = _
, mouseX , mouseX
, mouseY , mouseY
} = app } = app
val newUndo = xyTuple :: undo val newUndo = newUndoHd :: undo
in in
{ triangleStage = newTriangleStage { triangleStage = newTriangleStage
, triangles = triangles
, xClickPoints = xClickPoints
, yClickPoints = yClickPoints
, windowWidth = windowWidth
, windowHeight = windowHeight
, graphLines = graphLines
, undo = newUndo , undo = newUndo
, mouseX = mouseX , redo = []
, mouseY = mouseY
}
end
fun replaceTriangleStage (app: app_type, newTriangleStage) =
let
val
{ triangleStage = _
, triangles
, xClickPoints
, yClickPoints
, windowWidth
, windowHeight
, graphLines
, undo
, mouseX
, mouseY
} = app
in
{ triangleStage = newTriangleStage
, triangles = triangles , triangles = triangles
, xClickPoints = xClickPoints , xClickPoints = xClickPoints
, yClickPoints = yClickPoints , yClickPoints = yClickPoints
, windowWidth = windowWidth , windowWidth = windowWidth
, windowHeight = windowHeight , windowHeight = windowHeight
, graphLines = graphLines , graphLines = graphLines
, undo = undo
, mouseX = mouseX , mouseX = mouseX
, mouseY = mouseY , mouseY = mouseY
} }
end end
fun undoTriangle fun addTriangle (app: app_type, x1, y1, x2, y2, x3, y3, newUndoHd) : app_type =
(app: app_type, newTriangleStage: triangle_stage, trianglesTl) : app_type =
let
val
{ triangleStage = _
, triangles = _
, xClickPoints
, yClickPoints
, windowWidth
, windowHeight
, graphLines
, undo
, mouseX
, mouseY
} = app
in
{ triangleStage = newTriangleStage
, triangles = trianglesTl
, xClickPoints = xClickPoints
, yClickPoints = yClickPoints
, windowWidth = windowWidth
, windowHeight = windowHeight
, graphLines = graphLines
, undo = undo
, mouseX = mouseX
, mouseY = mouseY
}
end
fun newTriangle (app: app_type, x1, y1, x2, y2, x3, y3, newUndoTuple) :
app_type =
let let
val val
{ triangles { triangles
@@ -131,17 +86,96 @@ struct
, windowHeight , windowHeight
, graphLines , graphLines
, undo , undo
, redo = _
, mouseX , mouseX
, mouseY , mouseY
} = app } = 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
val newUndo = newUndoTuple :: undo val newUndo = newUndoHd :: undo
in in
{ triangleStage = NO_TRIANGLE { triangleStage = NO_TRIANGLE
, triangles = newTriangles , triangles = newTriangles
, undo = newUndo , undo = newUndo
, redo = []
, xClickPoints = xClickPoints
, yClickPoints = yClickPoints
, windowWidth = windowWidth
, windowHeight = windowHeight
, graphLines = graphLines
, mouseX = mouseX
, mouseY = mouseY
}
end
(* add to redo, pop one from undo *)
fun undoTriangleStage (app: app_type, newTriangleStage, newRedoHd) =
let
val
{ triangleStage = _
, triangles
, xClickPoints
, yClickPoints
, windowWidth
, windowHeight
, graphLines
, undo
, redo
, mouseX
, mouseY
} = app
val newUndo =
case undo of
hd :: tl => tl
| empty => empty
val newRedo = newRedoHd :: redo
in
{ triangleStage = newTriangleStage
, triangles = triangles
, undo = newUndo
, redo = newRedo
, xClickPoints = xClickPoints
, yClickPoints = yClickPoints
, windowWidth = windowWidth
, windowHeight = windowHeight
, graphLines = graphLines
, mouseX = mouseX
, mouseY = mouseY
}
end
fun undoTriangle
(app: app_type, newTriangleStage: triangle_stage, trianglesTl, newRedoHd) :
app_type =
let
val
{ triangleStage = _
, triangles = _
, xClickPoints
, yClickPoints
, windowWidth
, windowHeight
, graphLines
, undo
, redo
, mouseX
, mouseY
} = app
val newUndo =
case undo of
hd :: tl => tl
| empty => empty
val newRedo = newRedoHd :: redo
in
{ triangleStage = newTriangleStage
, triangles = trianglesTl
, undo = newUndo
, redo = newRedo
, xClickPoints = xClickPoints , xClickPoints = xClickPoints
, yClickPoints = yClickPoints , yClickPoints = yClickPoints
, windowWidth = windowWidth , windowWidth = windowWidth
@@ -165,9 +199,11 @@ struct
, triangles , triangles
, triangleStage , triangleStage
, undo , undo
, redo
, mouseX , mouseX
, mouseY , mouseY
} = app } = app
val xClickPoints = ClickPoints.generate (wStart, wFinish) val xClickPoints = ClickPoints.generate (wStart, wFinish)
val yClickPoints = ClickPoints.generate (hStart, hFinish) val yClickPoints = ClickPoints.generate (hStart, hFinish)
val graphLines = val graphLines =
@@ -182,6 +218,7 @@ struct
, windowWidth = windowWidth , windowWidth = windowWidth
, windowHeight = windowHeight , windowHeight = windowHeight
, undo = undo , undo = undo
, redo = redo
, mouseX = mouseX , mouseX = mouseX
, mouseY = mouseY , mouseY = mouseY
} }
@@ -223,8 +260,8 @@ struct
, windowHeight , windowHeight
, graphLines , graphLines
, undo , undo
, redo
} = app } = app
in in
{ mouseX = mouseX { mouseX = mouseX
, mouseY = mouseY , mouseY = mouseY
@@ -236,6 +273,7 @@ struct
, windowHeight = windowHeight , windowHeight = windowHeight
, graphLines = graphLines , graphLines = graphLines
, undo = undo , undo = undo
, redo = redo
} }
end end
end end