Skip to content

Commit

Permalink
Add 'undo' function
Browse files Browse the repository at this point in the history
  • Loading branch information
cjbrigato committed Sep 30, 2024
1 parent 8006738 commit 9963037
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
1 change: 1 addition & 0 deletions examples/paint/canvas.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ func CanvasWidget() g.Widget {
inpos := image.Point{mousepos.X - scr.X, mousepos.Y - scr.Y}
if imgui.IsMouseClickedBool(imgui.MouseButtonLeft) {
was_drawing = true
canvas.UndoIndexes = append(canvas.UndoIndexes, len(canvas.DrawCommands))
lastTo = image.Point{0, 0}
buffer = append(buffer, DrawCommand{Tool: current_tool, Color: current_color, BrushSize: brush_size, From: inpos, To: inpos})
lastTo = inpos
Expand Down
12 changes: 11 additions & 1 deletion examples/paint/toolbar.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,17 @@ func ButtonColorMaker() *g.RowWidget {
current_tool = 1
}
imgui.SameLine()
imgui.ImageButton("##undo_tool", undoButtonImg.Texture().ID(), sz.Mul(1.7))
if imgui.ImageButton("##undo_tool", undoButtonImg.Texture().ID(), sz.Mul(1.7)) {
if len(canvas.UndoIndexes) > 0 {
lastUndoIndex := canvas.UndoIndexes[len(canvas.UndoIndexes)-1]
uind := canvas.UndoIndexes[:len(canvas.UndoIndexes)-1]
dc := canvas.DrawCommands[:lastUndoIndex]
canvas.Backend.ForceRelease()
canvas, _ = NewCanvas(canvasDetectedHeight)
canvas.UndoIndexes = uind
canvas.DrawCommands = dc
}
}
imgui.SameLine()
if imgui.ImageButton("##clear_tool", clearButtonImg.Texture().ID(), sz.Mul(1.7)) {
canvas.Backend.ForceRelease()
Expand Down

0 comments on commit 9963037

Please sign in to comment.