-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathintknob.go
83 lines (74 loc) · 2.25 KB
/
intknob.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
/*
Copyright 2021 Google LLC
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package loupedeck
// IntKnob is an abstraction over the Loupedeck Live's Knobs.
// The IntKnob turns left/right dial actions into incrememnting and
// decrementing an integer within a specified range. In addition, the
// 'click' action of the knob resets the IntKnob's value to 0.
type IntKnob struct {
knob Knob
watchedint *WatchedInt
min int
max int
}
// Get returns the current value of the IntKnob.
func (k *IntKnob) Get() int {
return k.watchedint.Get()
}
// Set sets the current value of the IntKnob, triggering any
// callbacks set on the WatchedInt that underlies the IntKnob.
func (k *IntKnob) Set(v int) {
if v < k.min {
v = k.min
}
if v > k.max {
v = k.max
}
k.watchedint.Set(v)
}
// Inc incremements (or decrements) the current value of the
// IntKnob by a specified amount. This triggers a callback on the
// WatchedInt that underlies the IntKnob.
func (k *IntKnob) Inc(v int) {
x := k.watchedint.Get()
x += v
if x < k.min {
x = k.min
}
if x > k.max {
x = k.max
}
k.watchedint.Set(x)
}
// IntKnob implements a generic dial knob using the specified
// Loupedeck Knob. It binds the dial function of the knob to
// increase/decrease the IntKnob's value and binds the button function
// of the knob to reset the value to 0. Basically, spin the dial and
// it changes, and click and it resets.
func (l *Loupedeck) IntKnob(k Knob, min int, max int, watchedint *WatchedInt) *IntKnob {
i8k := &IntKnob{
knob: k,
watchedint: watchedint,
min: min,
max: max,
}
l.BindKnob(k, func(k Knob, v int) {
i8k.Inc(v)
})
l.BindButton(Button(k), func(b Button, s ButtonStatus) {
if s == ButtonDown {
i8k.Set(0)
}
})
return i8k
}