5
.. versionadded:: 1.7.0
7
This damped scroll effect will use the
8
:attr:`~kivy.effects.scroll.ScrollEffect.overscroll` to calculate the scroll
9
value, and slows going back to the upper or lower limit.
13
__all__ = ('DampedScrollEffect',)
16
from kivy.effects.scroll import ScrollEffect
17
from kivy.properties import NumericProperty, BooleanProperty
18
from kivy.metrics import sp
21
class DampedScrollEffect(ScrollEffect):
22
'''DampedScrollEffect class. See the module documentation for more
26
edge_damping = NumericProperty(0.25)
29
:attr:`edge_damping` is a :class:`~kivy.properties.NumericProperty` and
33
spring_constant = NumericProperty(2.0)
36
:attr:`spring_constant` is a :class:`~kivy.properties.NumericProperty` and
40
min_overscroll = NumericProperty(.5)
41
'''An overscroll less than this amount will be normalized to 0.
43
.. versionadded:: 1.8.0
45
:attr:`min_overscroll` is a :class:`~kivy.properties.NumericProperty` and
49
round_value = BooleanProperty(True)
50
'''If True, when the motion stops, :attr:`value` is rounded to the nearest
53
.. versionadded:: 1.8.0
55
:attr:`round_value` is a :class:`~kivy.properties.BooleanProperty` and
58
def update_velocity(self, dt):
59
if abs(self.velocity) <= self.min_velocity and self.overscroll == 0:
61
# why does this need to be rounded? For now refactored it.
63
self.value = round(self.value)
66
total_force = self.velocity * self.friction
67
if abs(self.overscroll) > self.min_overscroll:
68
total_force += self.velocity * self.edge_damping
69
total_force += self.overscroll * self.spring_constant
74
if not self.is_manual:
75
if self.overscroll > 0 and self.velocity < 0:
76
stop_overscroll = 'max'
77
elif self.overscroll < 0 and self.velocity > 0:
78
stop_overscroll = 'min'
80
self.velocity = self.velocity - total_force
81
if not self.is_manual:
82
self.apply_distance(self.velocity * dt)
83
if stop_overscroll == 'min' and self.value > self.min:
87
if stop_overscroll == 'max' and self.value < self.max:
91
self.trigger_velocity_update()
93
def on_value(self, *args):
96
if scroll_min > scroll_max:
97
scroll_min, scroll_max = scroll_max, scroll_min
98
if self.value < scroll_min:
99
self.overscroll = self.value - scroll_min
100
elif self.value > scroll_max:
101
self.overscroll = self.value - scroll_max
104
self.scroll = self.value
106
def on_overscroll(self, *args):
107
self.trigger_velocity_update()
109
def apply_distance(self, distance):
110
os = abs(self.overscroll)
112
distance /= 1. + os / sp(200.)
113
super(DampedScrollEffect, self).apply_distance(distance)