forked from magicmonkey/go-streamdeck
/
imagefile.go
103 lines (88 loc) · 2.95 KB
/
imagefile.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
package buttons
import (
"image"
"image/draw"
"os"
"github.com/disintegration/gift"
streamdeck "github.com/SKAARHOJ/go-streamdeck"
)
// ImageFileButton represents a button with an image on it, where the image is loaded
// from a file.
type ImageFileButton struct {
filePath string
img image.Image
updateHandler func(streamdeck.Button)
btnIndex int
actionHandler streamdeck.ButtonActionHandler
}
// GetImageForButton is the interface implemention to get the button's image as an image.Image
func (btn *ImageFileButton) GetImageForButton(btnSize int) image.Image {
// Resize the image to what the button wants
g := gift.New(gift.Resize(btnSize, btnSize, gift.LanczosResampling))
newimg := image.NewRGBA(image.Rect(0, 0, btnSize, btnSize))
g.Draw(newimg, btn.img)
return newimg
}
// SetButtonIndex is the interface implemention to set which button on the Streamdeck this is
func (btn *ImageFileButton) SetButtonIndex(btnIndex int) {
btn.btnIndex = btnIndex
}
// GetButtonIndex is the interface implemention to get which button on the Streamdeck this is
func (btn *ImageFileButton) GetButtonIndex() int {
return btn.btnIndex
}
// SetFilePath allows the image file to be changed on the fly
func (btn *ImageFileButton) SetFilePath(filePath string) error {
btn.filePath = filePath
err := btn.loadImage()
if err != nil {
return err
}
btn.updateHandler(btn)
return nil
}
func (btn *ImageFileButton) loadImage() error {
f, err := os.Open(btn.filePath)
if err != nil {
return err
}
img, _, err := image.Decode(f)
// We want the image as an RGBA, so convert it if it isn't
var newimg *image.RGBA
newimg, ok := img.(*image.RGBA)
if !ok {
newimg = image.NewRGBA(image.Rect(0, 0, img.Bounds().Max.X, img.Bounds().Max.Y))
draw.Draw(newimg, newimg.Bounds(), img, image.Point{0, 0}, draw.Src)
}
if err != nil {
return err
}
btn.img = newimg
return nil
}
// RegisterUpdateHandler is the interface implemention to let the engine give this button a callback to
// use to request that the button image is updated on the Streamdeck.
func (btn *ImageFileButton) RegisterUpdateHandler(f func(streamdeck.Button)) {
btn.updateHandler = f
}
// SetActionHandler allows a ButtonActionHandler implementation to be
// set on this button, so that something can happen when the button is pressed.
func (btn *ImageFileButton) SetActionHandler(a streamdeck.ButtonActionHandler) {
btn.actionHandler = a
}
// Pressed is the interface implementation for letting the engine notify that the button has been
// pressed. This hands-off to the specified ButtonActionHandler if it has been set.
func (btn *ImageFileButton) Pressed() {
if btn.actionHandler != nil {
btn.actionHandler.Pressed(btn)
}
}
// NewImageFileButton creates a new ImageFileButton with the specified image on it
func NewImageFileButton(filePath string) (*ImageFileButton, error) {
btn := &ImageFileButton{filePath: filePath}
err := btn.loadImage()
if err != nil {
return nil, err
}
return btn, nil
}