forked from muesli/deckmaster
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwidget.go
181 lines (151 loc) · 3.52 KB
/
widget.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
package main
import (
"fmt"
"image"
"image/color"
"image/draw"
"io/ioutil"
"log"
"os"
"strconv"
"github.com/flopp/go-findfont"
"github.com/golang/freetype"
"github.com/golang/freetype/truetype"
"github.com/muesli/streamdeck"
"github.com/nfnt/resize"
"golang.org/x/image/font"
"golang.org/x/image/math/fixed"
)
var (
ttfFont *truetype.Font
ttfThinFont *truetype.Font
ttfBoldFont *truetype.Font
)
type Widget interface {
Key() uint8
Update(dev *streamdeck.Device)
Action() *ActionConfig
ActionHold() *ActionConfig
TriggerAction()
}
type BaseWidget struct {
key uint8
action *ActionConfig
actionHold *ActionConfig
}
func (w *BaseWidget) Key() uint8 {
return w.key
}
func (w *BaseWidget) Action() *ActionConfig {
return w.action
}
func (w *BaseWidget) ActionHold() *ActionConfig {
return w.actionHold
}
func (w *BaseWidget) TriggerAction() {
}
func NewWidget(index uint8, id string, action *ActionConfig, actionHold *ActionConfig, config map[string]string) Widget {
bw := BaseWidget{index, action, actionHold}
switch id {
case "button":
return &ButtonWidget{
BaseWidget: bw,
icon: config["icon"],
label: config["label"],
}
case "clock":
return &ClockWidget{bw}
case "date":
return &DateWidget{bw}
case "recentWindow":
i, err := strconv.ParseUint(config["window"], 10, 64)
if err != nil {
log.Fatal(err)
}
return &RecentWindowWidget{
BaseWidget: bw,
window: uint8(i),
}
case "top":
return &TopWidget{
BaseWidget: bw,
mode: config["mode"],
fillColor: config["fillColor"],
}
default:
// unknown widget ID
fmt.Println("Unknown widget with ID:", id)
}
return nil
}
func drawImage(img *image.RGBA, path string, size uint, x uint, y uint) error {
f, err := os.Open(path)
if err != nil {
return err
}
defer f.Close()
icon, _, err := image.Decode(f)
if err != nil {
return err
}
icon = resize.Resize(size, size, icon, resize.Bilinear)
draw.Draw(img, image.Rect(int(x), int(y), int(x+size), int(y+size)), icon, image.Point{0, 0}, draw.Src)
return nil
}
func drawString(img *image.RGBA, ttf *truetype.Font, text string, fontsize float64, pt fixed.Point26_6) {
c := freetype.NewContext()
c.SetDPI(124)
c.SetFont(ttf)
c.SetSrc(image.NewUniform(color.RGBA{0, 0, 0, 0}))
c.SetDst(img)
c.SetClip(img.Bounds())
c.SetHinting(font.HintingFull)
c.SetFontSize(fontsize)
// find text entent
c.SetSrc(image.NewUniform(color.RGBA{0, 0, 0, 0}))
extent, _ := c.DrawString(text, freetype.Pt(0, 0))
actwidth := int(float64(extent.X) / 64)
actheight := c.PointToFixed(fontsize/2.0) / 64
xcenter := (float64(img.Bounds().Dx()) / 2.0) - (float64(actwidth) / 2.0)
ycenter := (float64(58) / 2.0) + (float64(actheight) / 2.0)
if pt.X < 0 {
oldy := pt.Y
pt = freetype.Pt(int(xcenter), 0)
pt.Y = oldy
}
if pt.Y < 0 {
oldx := pt.X
pt = freetype.Pt(0, int(ycenter))
pt.X = oldx
}
c.SetSrc(image.NewUniform(color.RGBA{255, 255, 255, 255}))
if _, err := c.DrawString(text, pt); err != nil {
log.Fatal(err)
}
}
func loadFont(name string) (*truetype.Font, error) {
fontPath, err := findfont.Find(name)
if err != nil {
return nil, err
}
ttf, err := ioutil.ReadFile(fontPath)
if err != nil {
return nil, err
}
return freetype.ParseFont(ttf)
}
func init() {
var err error
ttfFont, err = loadFont("Roboto-Regular.ttf")
if err != nil {
log.Fatal(err)
}
ttfThinFont, err = loadFont("Roboto-Thin.ttf")
if err != nil {
log.Fatal(err)
}
ttfBoldFont, err = loadFont("Roboto-Bold.ttf")
if err != nil {
log.Fatal(err)
}
}