-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathui.go
301 lines (252 loc) · 6.59 KB
/
ui.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
package main
import (
"fmt"
"image/draw"
"image/color"
"image"
"sort"
"time"
"github.com/skelterjohn/go.wde"
)
// note: return value is currently ignored. should perhaps be expanded to include cursor info?
type DragFn func(pos image.Point, finished, moved bool) bool
type Widget interface {
Drawable
}
type Drawable interface {
Rect() image.Rectangle
Draw(wde.Image, image.Rectangle)
}
type Hoverable interface {
MouseMoved(image.Point) wde.Cursor
}
type LeftDraggable interface {
LeftButtonDown(image.Point) DragFn
}
type RightDraggable interface {
RightButtonDown(image.Point) DragFn
}
type LeftClickable interface {
LeftClick(image.Point)
}
type RightClickable interface {
RightClick(image.Point)
}
type BpmTracker struct {
t0 time.Time
Hits []time.Duration
}
func (bt *BpmTracker) Clear() {
bt.Hits = bt.Hits[0:0]
}
func (bt *BpmTracker) AppendTime(t time.Time) {
if len(bt.Hits) == 0 {
bt.t0 = t
}
bt.Hits = append(bt.Hits, t.Sub(bt.t0))
}
func (bt *BpmTracker) Append(d time.Duration) {
bt.Hits = append(bt.Hits, d)
}
func (bt *BpmTracker) LastTime() time.Time {
if len(bt.Hits) == 0 {
return time.Time{}
}
return bt.t0.Add(bt.Hits[len(bt.Hits) - 1])
}
func (bt *BpmTracker) Bpm() float64 {
if len(bt.Hits) <= 1 {
return 0.0
}
n := len(bt.Hits) - 1
return 60.0 * float64(time.Second) / (float64(bt.Hits[n] - bt.Hits[0]) / float64(n))
}
type BpmWidget struct {
BpmTracker
bpm float64
area image.Rectangle
}
func (bw *BpmWidget) Rect() image.Rectangle {
return bw.area
}
func (bw *BpmWidget) Draw(dst wde.Image, r image.Rectangle) {
bw.area = r
bg := color.RGBA{0xcc, 0xcc, 0xcc, 0xff}
draw.Draw(dst, r, &image.Uniform{bg}, image.ZP, draw.Src)
G.font.luxi.Draw(dst, color.Black, r, fmt.Sprintf("%f", bw.bpm))
}
func (bw *BpmWidget) Hit() float64 {
cutoff := time.Second*3
now := time.Now()
if now.After(bw.LastTime().Add(cutoff)) {
bw.Clear()
}
bw.AppendTime(now)
bpm := bw.Bpm()
if bpm != 0.0 {
bw.bpm = bpm
}
return bpm
}
func (bw *BpmWidget) SetBpm(bpm float64) {
bw.bpm = bpm
}
type WidgetCore struct {
refresh chan Widget
}
type ImageWidget struct {
WidgetCore
img *image.RGBA
}
func (w *ImageWidget) Rect() image.Rectangle {
if w.img == nil {
return image.ZR
}
return w.img.Bounds()
}
func (w *ImageWidget) Img(r image.Rectangle) (dst *image.RGBA, resized bool) {
resized = !r.Eq(w.Rect())
if resized {
w.img = image.NewRGBA(r)
}
return w.img, resized
}
type Overlay struct {
data Drawable
cache *image.RGBA
flushCache bool
}
type OverlayId uint
type OverlayHandle struct {
id OverlayId
c chan interface{}
}
var NoOverlay OverlayHandle = OverlayHandle{}
type OverlayWidget struct {
WidgetCore
active map[OverlayId]*Overlay
c chan interface{}
}
type OverlayAdd struct {
reply chan OverlayId
}
type OverlayUpdate struct {
id OverlayId
data Drawable // nil means "we're done"
flushCache bool
}
type OverlayPaint struct {
wait chan bool
}
func NewOverlayWidget(refresh chan Widget) *OverlayWidget {
w := &OverlayWidget{WidgetCore{refresh}, make(map[OverlayId]*Overlay), make(chan interface{})}
go w.loop()
return w
}
func (w *OverlayWidget) loop() {
id := OverlayId(1) // start at 1 so 0 (aka NoOverlay) is never a valid ID
for {
cmd := <-w.c
switch c := cmd.(type) {
case OverlayAdd:
w.active[id] = &Overlay{}
c.reply <- id
close(c.reply)
id++
case OverlayUpdate:
w.active[c.id].data = c.data
w.active[c.id].flushCache = c.flushCache
w.refresh <- w
case OverlayPaint:
<- c.wait // block other events so painter has exclusive access to map
}
}
}
func (w *OverlayWidget) Rect() image.Rectangle {
return image.ZR
}
func (w *OverlayWidget) Draw(dst wde.Image, bounds image.Rectangle) {
blocker := make(chan bool)
defer close(blocker)
w.c <- OverlayPaint{blocker}
for id, overlay := range w.active {
if overlay.cache != nil && overlay.flushCache {
dst.CopyRGBA(overlay.cache, overlay.cache.Bounds())
}
if overlay.data == nil {
if overlay.cache == nil {
continue // fresh new overlay, yet to be updated
}
delete(w.active, id)
} else {
r := overlay.data.Rect()
if overlay.cache == nil || overlay.cache.Bounds() != r {
overlay.cache = image.NewRGBA(r)
}
draw.Draw(overlay.cache, r, dst, r.Min, draw.Src)
overlay.data.Draw(dst, r)
}
}
}
// Make creates a new overlay, returning a handle
func (w *OverlayWidget) Make() OverlayHandle {
reply := make(chan OverlayId)
w.c <- OverlayAdd{reply}
return OverlayHandle{<-reply, w.c}
}
// Updates the image data associated with an overlay handle
func (h *OverlayHandle) Update(data Drawable) {
h.c <- OverlayUpdate{h.id, data, true}
}
// Finalises and cleans up an overlay handle
func (h *OverlayHandle) Close(flushCache bool) {
h.c <- OverlayUpdate{h.id, nil, flushCache}
}
func boxDrawable(centre image.Point, radiusw, radiush int, border, fill color.Color) DrawBox {
return DrawBox{image.Rectangle{centre.Sub(image.Pt(radiusw, radiush)), centre.Add(image.Pt(radiusw, radiush))}, border, fill}
}
type DrawBox struct {
r image.Rectangle
border, fill color.Color
}
func (b DrawBox) Rect() image.Rectangle {
return b.r
}
func (b DrawBox) Draw(dst wde.Image, r image.Rectangle) {
drawBorders(dst, r, b.border, b.fill)
}
func drawHorzSlider(dst draw.Image, r image.Rectangle, fg color.Color, posn float64) {
mid := r.Min.Y + r.Dy() / 2
draw.Draw(dst, image.Rect(r.Min.X, mid, r.Max.X, mid + 1), &image.Uniform{fg}, image.ZP, draw.Over)
x := int(float64(r.Min.X) + posn * float64(r.Dx()) + 0.5)
draw.Draw(dst, image.Rect(x - 1, r.Min.Y + 1, x + 2, r.Max.Y - 2), &image.Uniform{fg}, image.ZP, draw.Over)
}
func drawVertSlider(dst draw.Image, r image.Rectangle, fg color.Color, posn float64) {
mid := r.Min.X + r.Dx() / 2
draw.Draw(dst, image.Rect(mid, r.Min.Y, mid + 1, r.Max.Y), &image.Uniform{fg}, image.ZP, draw.Over)
y := int(float64(r.Max.Y) - posn * float64(r.Dy()) + 0.5)
x := r.Dx() / 2 - 1
draw.Draw(dst, image.Rect(mid - x, y - 1, mid + x + 1, y + 2), &image.Uniform{fg}, image.ZP, draw.Over)
}
type ColourBar struct {
pts []ColourPoint
}
type ColourPoint struct {
x float64
col color.Color
}
func (cb ColourBar) At(x float64) color.Color {
i := sort.Search(len(cb.pts), func(i int)bool { return x <= cb.pts[i].x })
if i >= len(cb.pts) {
return cb.pts[len(cb.pts)-1].col
} else if i == 0 {
return cb.pts[i].col
}
α := (x - cb.pts[i-1].x) / (cb.pts[i].x - cb.pts[i-1].x)
r0, g0, b0, a0 := cb.pts[i-1].col.RGBA()
r1, g1, b1, a1 := cb.pts[i].col.RGBA()
f0, f1 := a0/255, a1/255
θ := func(a, b uint32)uint8 { return uint8((1-α)*float64(a/f0) + α*float64(b/f1)) }
c := color.RGBA{θ(r0, r1), θ(g0, g1), θ(b0, b1), θ(a0, a1)}
return c
}