-
Notifications
You must be signed in to change notification settings - Fork 0
/
state.go
49 lines (42 loc) · 977 Bytes
/
state.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
43
44
45
46
47
48
49
package main
import "image"
type History struct {
undoStck [](image.Image)
redoStck [](image.Image)
undoLength int
redoLength int
}
func NewHistory() *History {
return &History{undoStck: [](image.Image){}, redoStck: [](image.Image){}, undoLength: 0}
}
func (h *History) Push(cxt image.Image) {
h.undoStck = append(h.undoStck, cxt)
h.undoLength += 1
}
func (h *History) Undo() image.Image {
if h.undoLength != 0 {
tmp := h.undoStck[h.undoLength-1]
arr := h.undoStck[:h.undoLength-1]
h.redoStck = append(h.redoStck, tmp)
h.undoStck = arr
h.undoLength -= 1
h.redoLength += 1
return tmp
}
return nil
}
func (h *History) Redo() image.Image {
if h.redoLength != 0 {
tmp := h.redoStck[h.redoLength-1]
arr := h.redoStck[:h.redoLength-1]
h.undoStck = append(h.redoStck, tmp)
h.redoStck = arr
h.redoLength -= 1
h.undoLength += 1
return tmp
}
return nil
}
func (h *History) UndoPeek() image.Image {
return h.undoStck[h.undoLength-1]
}