-
Notifications
You must be signed in to change notification settings - Fork 4
/
undo_js.go
79 lines (66 loc) · 1.76 KB
/
undo_js.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package main
import (
"syscall/js"
"github.com/seqsense/pcgol/pc"
)
type historyJS struct {
history []js.Value
historyHeader []pc.PointCloudHeader
maxHistory int
}
func newHistory(n int) history {
return &historyJS{maxHistory: n}
}
func (h *historyJS) MaxHistory() int {
return h.maxHistory
}
func (h *historyJS) SetMaxHistory(m int) {
if m < 0 {
m = 0
}
h.maxHistory = m
}
func (h *historyJS) push(pp *pc.PointCloud) *pc.PointCloud {
header := pp.PointCloudHeader.Clone()
dataJS := js.Global().Get("Uint8Array").New(len(pp.Data))
js.CopyBytesToJS(dataJS, pp.Data)
h.history = append(h.history, dataJS)
h.historyHeader = append(h.historyHeader, header)
if len(h.history) > h.MaxHistory()+1 {
h.history[0] = js.Null()
h.history = h.history[1:]
h.historyHeader = h.historyHeader[1:]
}
return pp
}
func (h *historyJS) pop() *pc.PointCloud {
n := len(h.history)
back := h.history[n-1]
backHeader := h.historyHeader[n-1]
h.history[n-1] = js.Null()
h.history = h.history[:n-1]
h.historyHeader = h.historyHeader[:n-1]
return h.reconstructPointCloud(backHeader, back)
}
func (h *historyJS) undo() (*pc.PointCloud, bool) {
if n := len(h.history); n > 1 {
h.history[n-1] = js.Null()
h.history = h.history[:n-1]
h.historyHeader = h.historyHeader[:n-1]
return h.reconstructPointCloud(h.historyHeader[n-2], h.history[n-2]), true
}
return nil, false
}
func (h *historyJS) reconstructPointCloud(header pc.PointCloudHeader, dataJS js.Value) *pc.PointCloud {
pp := &pc.PointCloud{
PointCloudHeader: header,
Points: header.Width * header.Height,
Data: make([]byte, dataJS.Get("byteLength").Int()),
}
js.CopyBytesToGo(pp.Data, dataJS)
return pp
}
func (h *historyJS) clear() {
h.history = nil
h.historyHeader = nil
}