-
Notifications
You must be signed in to change notification settings - Fork 0
/
undo.go
42 lines (36 loc) · 858 Bytes
/
undo.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package main
type Undoable interface {
undo(sb *Sandbox, ui *UiState)
redo(sb *Sandbox, ui *UiState)
}
type UndoRedoSystem struct {
history []Undoable
doneCount uint32
// Whether any undoables have been done or undone since last save
dirty bool
}
func NewUndoRedoSystem() UndoRedoSystem {
return UndoRedoSystem{}
}
func (s *UndoRedoSystem) Redo(sb *Sandbox, ui *UiState) {
if s.doneCount < uint32(len(s.history)) {
s.history[s.doneCount].redo(sb, ui)
s.doneCount++
s.dirty = true
}
}
func (s *UndoRedoSystem) Undo(sb *Sandbox, ui *UiState) bool {
if s.doneCount == 0 {
return false
}
s.doneCount--
s.history[s.doneCount].undo(sb, ui)
s.dirty = true
return true
}
func (s *UndoRedoSystem) Append(undoable Undoable) {
s.history = s.history[:s.doneCount]
s.history = append(s.history, undoable)
s.doneCount++
s.dirty = true
}