blob: 5b0297bab47b8a3940c0440b5e95af5f9579933e (
plain)
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 model
type UndoStack struct {
states []Entry
pos int
}
type Entry struct {
Target *Map
State *Map
}
func (u *UndoStack) Push(target *Map) {
keep := u.pos + 1
if keep > len(u.states) {
keep = len(u.states)
}
u.states = append(u.states[:keep], Entry{Target: target, State: target.Clone()})
u.pos = len(u.states) - 1
if len(u.states) > 100 {
u.states = u.states[1:]
u.pos--
}
}
func (u *UndoStack) Undo() *Entry {
if u.pos <= 0 {
return nil
}
u.pos--
return &u.states[u.pos]
}
func (u *UndoStack) Redo() *Entry {
if u.pos >= len(u.states)-1 {
return nil
}
u.pos++
return &u.states[u.pos]
}
func (u *UndoStack) Pos() int { return u.pos }
|