@@ -12,6 +12,7 @@ import (
12
12
streamdeck "github.com/magicmonkey/go-streamdeck"
13
13
)
14
14
15
+ // TextButton represents a button with text on it
15
16
type TextButton struct {
16
17
label string
17
18
textColour color.Color
@@ -21,53 +22,71 @@ type TextButton struct {
21
22
actionHandler streamdeck.ButtonActionHandler
22
23
}
23
24
25
+ // GetImageForButton is the interface implemention to get the button's image as an image.Image
24
26
func (btn * TextButton ) GetImageForButton () image.Image {
25
27
img := getImageWithText (btn .label , btn .textColour , btn .backgroundColour )
26
28
return img
27
29
}
28
30
31
+ // SetButtonIndex is the interface implemention to set which button on the Streamdeck this is
29
32
func (btn * TextButton ) SetButtonIndex (btnIndex int ) {
30
33
btn .btnIndex = btnIndex
31
34
}
32
35
36
+ // GetButtonIndex is the interface implemention to get which button on the Streamdeck this is
33
37
func (btn * TextButton ) GetButtonIndex () int {
34
38
return btn .btnIndex
35
39
}
36
40
41
+ // SetText allows the text on the button to be changed on the fly
37
42
func (btn * TextButton ) SetText (label string ) {
38
43
btn .label = label
39
44
btn .updateHandler (btn )
40
45
}
41
46
47
+ // SetTextColour allows the colour of the text on the button to be changed on the fly
42
48
func (btn * TextButton ) SetTextColour (textColour color.Color ) {
43
49
btn .textColour = textColour
44
50
btn .updateHandler (btn )
45
51
}
46
52
53
+ // SetBackgroundColor allows the background colour on the button to be changed on the fly
47
54
func (btn * TextButton ) SetBackgroundColor (backgroundColour color.Color ) {
48
55
btn .backgroundColour = backgroundColour
49
56
btn .updateHandler (btn )
50
57
}
51
58
59
+ // RegisterUpdateHandler is the interface implemention to let the engine give this button a callback to
60
+ // use to request that the button image is updated on the Streamdeck.
52
61
func (btn * TextButton ) RegisterUpdateHandler (f func (streamdeck.Button )) {
53
62
btn .updateHandler = f
54
63
}
55
64
65
+ // SetActionHandler allows a ButtonActionHandler implementation to be
66
+ // set on this button, so that something can happen when the button is pressed.
56
67
func (btn * TextButton ) SetActionHandler (a streamdeck.ButtonActionHandler ) {
57
68
btn .actionHandler = a
58
69
}
59
70
71
+ // Pressed is the interface implementation for letting the engine notify that the button has been
72
+ // pressed. This hands-off to the specified ButtonActionHandler if it has been set.
60
73
func (btn * TextButton ) Pressed () {
61
74
if btn .actionHandler != nil {
62
75
btn .actionHandler .Pressed (btn )
63
76
}
64
77
}
65
78
79
+ // NewTextButton creates a new TextButton with the specified text on it, in white on a black
80
+ // background. The text will be set on a single line, and auto-sized to fill the button as best
81
+ // as possible.
66
82
func NewTextButton (label string ) * TextButton {
67
83
btn := NewTextButtonWithColours (label , color .White , color .Black )
68
84
return btn
69
85
}
70
86
87
+ // NewTextButtonWithColours creates a new TextButton with the specified text on it, in the specified
88
+ // text and background colours. The text will be set on a single line, and auto-sized to fill the
89
+ // button as best as possible.
71
90
func NewTextButtonWithColours (label string , textColour color.Color , backgroundColour color.Color ) * TextButton {
72
91
btn := & TextButton {label : label , textColour : textColour , backgroundColour : backgroundColour }
73
92
return btn
@@ -92,17 +111,17 @@ func getImageWithText(text string, textColour color.Color, backgroundColour colo
92
111
}
93
112
}
94
113
95
- src_img := image .NewUniform (textColour )
114
+ srcImg := image .NewUniform (textColour )
96
115
97
- dst_img := image .NewRGBA (image .Rect (0 , 0 , ButtonSize , ButtonSize ))
98
- draw .Draw (dst_img , dst_img .Bounds (), image .NewUniform (backgroundColour ), image.Point {0 , 0 }, draw .Src )
116
+ dstImg := image .NewRGBA (image .Rect (0 , 0 , ButtonSize , ButtonSize ))
117
+ draw .Draw (dstImg , dstImg .Bounds (), image .NewUniform (backgroundColour ), image.Point {0 , 0 }, draw .Src )
99
118
100
119
c := freetype .NewContext ()
101
120
c .SetFont (myfont )
102
- c .SetDst (dst_img )
103
- c .SetSrc (src_img )
121
+ c .SetDst (dstImg )
122
+ c .SetSrc (srcImg )
104
123
c .SetFontSize (size )
105
- c .SetClip (dst_img .Bounds ())
124
+ c .SetClip (dstImg .Bounds ())
106
125
107
126
x := int ((96 - width ) / 2 ) // Horizontally centre text
108
127
y := int (50 + (size / 3 )) // Fudged vertical centre, erm, very "heuristic"
@@ -115,14 +134,14 @@ func getImageWithText(text string, textColour color.Color, backgroundColour colo
115
134
fmt.Println(textWidth)
116
135
117
136
f := &font.Drawer{
118
- Dst: dst_img ,
137
+ Dst: dstImg ,
119
138
Src: src_img,
120
139
Face: basicfont.Face7x13,
121
140
Dot: fixed.Point26_6{fixed.Int26_6(x * 64), fixed.Int26_6(y * 64)},
122
141
}
123
142
f.DrawString(text)
124
143
*/
125
- return dst_img
144
+ return dstImg
126
145
}
127
146
128
147
func getTextWidth (text string , size float64 ) int {
0 commit comments