forked from unix-streamdeck/streamdeckd
/
counter.go
58 lines (49 loc) · 1.19 KB
/
counter.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
package modules
import (
"image"
"image/draw"
"log"
"strconv"
"github.com/unix-streamdeck/api"
)
type CounterIconHandler struct {
Count int
Running bool
Callback func(image image.Image)
}
func (c *CounterIconHandler) Start(k api.Key, info api.StreamDeckInfo, callback func(image image.Image)) {
if c.Callback == nil {
c.Callback = callback
}
if c.Running {
img := image.NewRGBA(image.Rect(0, 0, info.IconSize, info.IconSize))
draw.Draw(img, img.Bounds(), image.Black, image.ZP, draw.Src)
Count := strconv.Itoa(c.Count)
imgParsed, err := api.DrawText(img, Count, k.TextSize, k.TextAlignment)
if err != nil {
log.Println(err)
} else {
callback(imgParsed)
}
}
}
func (c *CounterIconHandler) IsRunning() bool {
return c.Running
}
func (c *CounterIconHandler) SetRunning(running bool) {
c.Running = running
}
func (c CounterIconHandler) Stop() {
c.Running = false
}
type CounterKeyHandler struct{}
func (CounterKeyHandler) Key(key api.Key, info api.StreamDeckInfo) {
if key.IconHandler != "Counter" {
return
}
handler := key.IconHandlerStruct.(*CounterIconHandler)
handler.Count += 1
if handler.Callback != nil {
handler.Start(key, info, handler.Callback)
}
}