-
Notifications
You must be signed in to change notification settings - Fork 0
/
knob.py
289 lines (245 loc) · 9.71 KB
/
knob.py
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
"""
Knob
====
The :class:`Knob` widget creates a component that looks like a
control Knob or Dial (from Wikipedia : "A control knob is a rotary
control used to provide input to a device when grasped by an
operator and turned, so that the degree of rotation corresponds to
the desired input." http://en.wikipedia.org/wiki/Control_knob).
To configure a knob a max/min, slope and step values should be provided.
Additionally, knobimg_source could be set to load
a texture that visually represents the knob.
"""
__all__ = ("Knob",)
__version__ = "0.2"
import math
from kivy.lang import Builder
from kivy.uix.widget import Widget
from kivy.properties import (
NumericProperty,
StringProperty,
BooleanProperty,
ReferenceListProperty,
BoundedNumericProperty,
ListProperty,
)
Builder.load_string(
"""
#
# Knob
# ====
# To create a basic knob (in a kv file):
#
# Knob:
# size: 100, 100
# min: 0
# max: 100
# step: 1
# slope: 1
# value: 0 # Default position of knob.
# knobimg_source: "img/knob_metal.png" # Knob texture
# show_marker: False # Do not show surrounding marker
#
# To create a knob with a surrounding marker:
#
# Knob:
# size: 100, 100
# min: 0
# max: 100
# step: 1
# slope: 1
# value: 0 # Default position of knob.
# knobimg_source: "img/knob_metal.png" # Knob texture
# show_marker: True # Show surrounding marker
# marker_img: "img/bline.png" # Marker texture image
# knob_size: 0.9 # Scales knob size to leave space for marker
# markeroff_color: 0, 0, 0, 0
<Knob>
size_hint: None, None
canvas.before:
Color:
rgba: self.markeroff_color
Ellipse:
pos: self.pos
size: self.size[0], self.size[1]
angle_start: 0
angle_end: 360
source: self.markeroff_img
Color:
rgba: self.marker_color
Ellipse:
pos: self.pos
size: self.size[0], self.size[1]
angle_start: self.marker_startangle
angle_end: self._angle + self.marker_ahead if self._angle > self.marker_startangle else self.marker_startangle
source: self.marker_img
Color:
rgba: self.knobimg_bgcolor
Ellipse:
pos: self.pos[0] + (self.size[0] * (1 - self.knobimg_size))/2, self.pos[1] + (self.size[1] * (1 - self.knobimg_size)) / 2
size: self.size[0] * (self.knobimg_size), self.size[1] * (self.knobimg_size)
Color:
rgba: self.knobimg_color
PushMatrix
Rotate:
angle: 360 - self._angle
origin: self.center
Rectangle:
pos: self.pos[0] + (self.size[0] * (1 - self.knobimg_size)) /2, self.pos[1] + (self.size[1] * (1 - self.knobimg_size)) / 2
size: self.size[0] * (self.knobimg_size), self.size[1] * (self.knobimg_size)
source: self.knobimg_source
canvas:
PopMatrix
"""
)
class Knob(Widget):
"""Class for creating a Knob widget."""
min = NumericProperty(0)
"""Minimum value for value :attr:`value`.
:attr:`min` is a :class:`~kivy.properties.NumericProperty` and defaults
to 0.
"""
max = NumericProperty(100)
"""Maximum value for value :attr:`value`.
:attr:`max` is a :class:`~kivy.properties.NumericProperty` and defaults
to 100.
"""
range = ReferenceListProperty(min, max)
""" Range of the values for Knob.
:attr:`range` is a :class:`~kivy.properties.ReferenceListProperty` of
(:attr:`min`, :attr:`max`).
"""
value = NumericProperty(0)
"""Current value of the knob. Set :attr:`value` when creating a knob to
set its initial position. An internal :attr:`_angle` is calculated to set
the position of the knob.
:attr:`value` is a :class:`~kivy.properties.NumericProperty` and defaults
to 0.
"""
step = BoundedNumericProperty(1, min=0)
"""Step interval of knob to go from min to max. An internal
:attr:`_angle_step` is calculated to set knob step in degrees.
:attr:`step` is a :class:`~kivy.properties.BoundedNumericProperty`
and defaults to 1 (min=0).
"""
curve = BoundedNumericProperty(1, min=1)
"""This parameter determines the shape of the map function. It represent the
reciprocal of a power function's exponent used to map the input value.
So, for higher values of curve the contol is more reactive, and conversely.
"""
knobimg_source = StringProperty("")
"""Path of texture image that visually represents the knob. Use PNG for
transparency support. The texture is rendered on a centered rectangle of
size = :attr:`size` * :attr:`knobimg_size`.
:attr:`knobimg_source` is a :class:`~kivy.properties.StringProperty`
and defaults to empty string.
"""
knobimg_color = ListProperty([1, 1, 1, 1])
"""Color to apply to :attr:`knobimg_source` texture when loaded.
:attr:`knobimg_color` is a :class:`~kivy.properties.ListProperty`
and defaults to [1,1,1,1].
"""
knobimg_size = BoundedNumericProperty(0.9, max=1.0, min=0.1)
""" Internal proportional size of rectangle that holds
:attr:`knobimg_source` texture.
:attr:`knobimg_size` is a :class:`~kivy.properties.BoundedNumericProperty`
and defaults to 0.9.
"""
show_marker = BooleanProperty(True)
""" Shows/hides marker surrounding knob. use :attr:`knob_size` < 1.0 to
leave space to marker.
:attr:`show_marker` is a :class:`~kivy.properties.BooleanProperty`
and defaults to True.
"""
marker_img = StringProperty("")
"""Path of texture image that visually represents the knob marker. The
marker is rendered in a centered Ellipse (Circle) with the same size of
the widget and goes from angle_start=:attr:`marker_startangle` to
angle_end=:attr:`_angle`.
:attr:`marker_img` is a :class:`~kivy.properties.StringProperty` and
defaults to "".
"""
marker_color = ListProperty([1, 1, 1, 1])
"""Color to apply to :attr:`marker_img` texture when loaded.
:attr:`marker_color` is a :class:`~kivy.properties.ListProperty`
and defaults to [1,1,1,1].
"""
knobimg_bgcolor = ListProperty([0, 0, 0, 1])
""" Background color behind :attr:`knobimg_source` texture.
:attr:`value` is a :class:`~kivy.properties.ListProperty` and defaults
to [0,0,0,1].
"""
markeroff_img = StringProperty("")
"""Path of texture image that visually represents the knob marker where
it's off, that is, parts of the marker that haven't been reached yet by
the knob (:attr:`value`).
:attr:`markeroff_img` is a :class:`~kivy.properties.StringProperty`
and defaults to "".
"""
markeroff_color = ListProperty([0, 0, 0, 0])
"""Color applied to :attr:`markeroff_img` int the Canvas.
:attr:`markeroff_color` is a :class:`~kivy.properties.ListProperty`
and defaults to [0,0,0,0].
"""
marker_startangle = NumericProperty(0)
"""Starting angle of Ellipse where :attr:`marker_img` is rendered.
:attr:`value` is a :class:`~kivy.properties.NumericProperty` and defaults
to 0.
"""
marker_ahead = NumericProperty(0)
""" Adds degrees to angle_end of marker (except when :attr:`value` == 0).
:attr:`marker_ahead` is a :class:`~kivy.properties.NumericProperty`
and defaults to 0.
"""
_angle = NumericProperty(0) # Internal angle calculated from value.
_angle_step = NumericProperty(0) # Internal angle_step calculated from step.
def __init__(self, *args, **kwargs):
super(Knob, self).__init__(*args, **kwargs)
self.bind(show_marker=self._show_marker)
self.bind(value=self._value)
def _value(self, instance, value):
self._angle = (
pow((value - self.min) / (self.max - self.min), 1.0 / self.curve) * 360.0
)
self.on_knob(value)
def _show_marker(self, instance, flag):
# "show/hide" marker.
if flag:
self.knobimg_bgcolor[3] = 1
self.marker_color[3] = 1
self.markeroff_color[3] = 1
else:
self.knobimg_bgcolor[3] = 0
self.marker_color[3] = 0
self.markeroff_color[3] = 0
def on_touch_down(self, touch):
if self.collide_point(*touch.pos):
self.update_angle(touch)
def on_touch_move(self, touch):
if self.collide_point(*touch.pos):
self.update_angle(touch)
def update_angle(self, touch):
posx, posy = touch.pos
cx, cy = self.center
rx, ry = posx - cx, posy - cy
if ry >= 0: # Quadrants are clockwise.
quadrant = 1 if rx >= 0 else 4
else:
quadrant = 3 if rx <= 0 else 2
try:
angle = math.atan(rx / ry) * (180.0 / math.pi)
if quadrant == 2 or quadrant == 3:
angle = 180 + angle
elif quadrant == 4:
angle = 360 + angle
except: # atan not def for angle 90 and 270
angle = 90 if quadrant <= 2 else 270
self._angle_step = (self.step * 360) / (self.max - self.min)
self._angle = self._angle_step
while self._angle < angle:
self._angle = self._angle + self._angle_step
relativeValue = pow((angle / 360.0), 1.0 / self.curve)
self.value = (relativeValue * (self.max - self.min)) + self.min
# TO OVERRIDE
def on_knob(self, value):
pass # Knob values listenerr