Skip to content

Commit

Permalink
Added way to mark the knob that has the focus.
Browse files Browse the repository at this point in the history
  • Loading branch information
phuel committed Aug 23, 2022
1 parent a61c460 commit 459e4b4
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 7 deletions.
3 changes: 3 additions & 0 deletions example.kv
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
start_angle: 225
angle_range: 270
knobimg_source: "img/knob_black3.png"
focus_knobimg_source: "img/focus_knob_black3.png"
knobimg_size: 0.7
markeroff_color: 0, 0, 0, 1
show_marker: False
Expand All @@ -32,6 +33,7 @@
start_angle: 225
angle_range: 270
knobimg_source: "img/knob_black3.png"
focus_knobimg_source: "img/focus_knob_black3.png"
knobimg_size: 0.7
markeroff_color: 0, 0, 0, 1
show_marker: False
Expand All @@ -44,6 +46,7 @@
start_angle: 315
angle_range: 90
knobimg_source: "img/knob_black3.png"
focus_knobimg_source: "img/focus_knob_black3.png"
knobimg_size: 0.7
markeroff_color: 0, 0, 0, 1
show_marker: False
Expand Down
Binary file added img/focus_knob_black3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/knob_black3.xcf
Binary file not shown.
30 changes: 23 additions & 7 deletions knob.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
from kivy.uix.widget import Widget
from kivy.properties import NumericProperty, StringProperty,\
BooleanProperty, BoundedNumericProperty,\
ListProperty
ListProperty, AliasProperty
import kivy.graphics as kg

Builder.load_string('''
Expand Down Expand Up @@ -96,7 +96,7 @@
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
source: self._knobimg_source
PopMatrix
''')

Expand Down Expand Up @@ -144,6 +144,14 @@ class Knob(FocusBehavior, Widget):
and defaults to empty string.
'''

focus_knobimg_source = StringProperty("")
'''Path of texture image that visually represents the knob when it has keyboard focus.
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`
Expand Down Expand Up @@ -225,6 +233,7 @@ class Knob(FocusBehavior, Widget):

_angle = NumericProperty(0) # Internal angle calculated from value.


def __init__(self, *args, **kwargs):
self.is_focusable = kwargs.get('is_focusable', True)
super(Knob, self).__init__(*args, **kwargs)
Expand All @@ -250,6 +259,12 @@ def _show_marker(self, instance, flag):
self.marker_color[3] = 0
self.markeroff_color[3] = 0

def get_knobimg_source(self):
if self.selected and self.focus_knobimg_source != "":
return self.focus_knobimg_source
return self.knobimg_source

_knobimg_source = AliasProperty(get_knobimg_source, bind=['knobimg_source', 'focus_knobimg_source', 'focus'])

def get_angle(self, value):
return pow( (value - self.min)/(self.max - self.min), 1./self.curve) * self.angle_range
Expand Down Expand Up @@ -287,9 +302,9 @@ def update_angle(self, touch):
posx, posy = touch.pos
cx, cy = self.center
relx, rely = posx - cx, posy - cy
# Don't change the angle after clicks in the middle third of the
# Don't change the angle after clicks in the middle fifth of the
# knob so that the knob can be focused with a center click.
if abs(relx) < self.width / 6 and abs(rely) < self.height / 6:
if abs(relx) < (self.width * self.knobimg_size) / 10 and abs(rely) < (self.height * self.knobimg_size) / 10:
return
coss = math.cos(self.start_angle / 180.0 * math.pi)
sins = math.sin(self.start_angle / 180.0 * math.pi)
Expand Down Expand Up @@ -330,15 +345,16 @@ def set_angle(self, angle):

#TO OVERRIDE
def on_knob(self, value):
pass #Knob values listenerr
pass #Knob values listener


class KnobWithTicks(Knob):
"""Knob widget that shows markers around the knob."""

def __init__(self, ticks, *args, **kwargs):
"""
"""
'''Constructor
:param ticks: An array of :class:`Tick` instances to be shown around the knob.
'''
self.ticks = ticks
self.bind(size=self.position_ticks)
self.bind(pos=self.position_ticks)
Expand Down

0 comments on commit 459e4b4

Please sign in to comment.