Skip to content

Add step argument to UISliders #2655

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions arcade/gui/widgets/slider.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ class UIBaseSlider(UIInteractiveWidget, metaclass=ABCMeta):
size_hint_min: Minimum size hint of the slider.
size_hint_max: Maximum size hint of the slider.
style: Used to style the slider for different states.
step: Smallest change the slider value can move by.
**kwargs: Passed to UIInteractiveWidget.

"""
Expand All @@ -67,6 +68,7 @@ def __init__(
size_hint_min=None,
size_hint_max=None,
style: Union[Mapping[str, UISliderStyle], None] = None,
step: Union[float, None] = None,
**kwargs,
):
super().__init__(
Expand All @@ -81,7 +83,8 @@ def __init__(
**kwargs,
)

self.value = value
self.step = step
self.value = self._apply_step(value)
self.min_value = min_value
self.max_value = max_value

Expand All @@ -95,6 +98,13 @@ def __init__(

self.register_event_type("on_change")

def _apply_step(self, value: float):
if self.step:
inverse = 1 / self.step
return round(value * inverse) / inverse

return value

def _x_for_value(self, value: float):
"""Provides the x coordinate for the given value."""

Expand All @@ -110,7 +120,9 @@ def norm_value(self):
@norm_value.setter
def norm_value(self, value):
"""Normalized value between 0.0 and 1.0"""
self.value = min(value * (self.max_value - self.min_value) + self.min_value, self.max_value)
self.value = self._apply_step(
min(value * (self.max_value - self.min_value) + self.min_value, self.max_value)
)

@property
def _thumb_x(self):
Expand Down Expand Up @@ -254,7 +266,7 @@ class UISlider(UIStyledWidget[UISliderStyle], UIBaseSlider):
width: Width of the slider.
height: Height of the slider.
style: Used to style the slider for different states.

step: Smallest change the slider value can move by.
"""

UIStyle = UISliderStyle
Expand Down Expand Up @@ -294,6 +306,7 @@ def __init__(
size_hint_min=None,
size_hint_max=None,
style: Union[dict[str, UISliderStyle], None] = None,
step: Union[float, None] = None,
**kwargs,
):
super().__init__(
Expand All @@ -308,6 +321,7 @@ def __init__(
size_hint_min=size_hint_min,
size_hint_max=size_hint_max,
style=style or UISlider.DEFAULT_STYLE,
step=step,
**kwargs,
)

Expand Down
Loading