@@ -94,120 +94,10 @@ def run_animation(self, dt):
9494 :align: center
9595"""
9696
97- import math
98- import sys
99-
10097import kivy .animation
10198
102- float_epsilon = 8.3446500e-7
103-
104- if sys .version_info < (3 , 11 ):
105- cbrt = lambda number : (abs (number ) ** (1 / 3 )) * (- 1 if number < 0 else 1 )
106- else :
107- cbrt = math .cbrt
108-
109-
110- class CubicBezier :
111- """Ported from Android source code"""
112-
113- p0 = 0
114- p1 = 0
115- p2 = 0
116- p3 = 0
117-
118- def __init__ (self , * args ):
119- self .p0 , self .p1 , self .p2 , self .p3 = args
120-
121- def evaluate_cubic (self , p1 , p2 , t ):
122- a = 1.0 / 3.0 + (p1 - p2 )
123- b = p2 - 2.0 * p1
124- c = p1
125- return 3.0 * ((a * t + b ) * t + c ) * t
126-
127- def clamp_range (self , r ):
128- if r < 0.0 :
129- if - float_epsilon <= r < 0.0 :
130- return 0.0
131- else :
132- return math .nan
133- elif r > 1.0 :
134- if 1.0 <= r <= 1.0 + float_epsilon :
135- return 1.0
136- else :
137- return math .nan
138- else :
139- return r
140-
141- def close_to (self , x , y ):
142- return abs (x - y ) < float_epsilon
143-
144- def find_first_cubic_root (self , p0 , p1 , p2 , p3 ):
145- a = 3.0 * (p0 - 2.0 * p1 + p2 )
146- b = 3.0 * (p1 - p0 )
147- c = p0
148- d = - p0 + 3.0 * (p1 - p2 ) + p3
149- if self .close_to (d , 0.0 ):
150- if self .close_to (a , 0.0 ):
151- if self .close_to (b , 0.0 ):
152- return math .nan
153- return self .clamp_range (- c / b )
154- else :
155- q = math .sqrt (b * b - 4.0 * a * c )
156- a2 = 2.0 * a
157- root = self .clamp_range ((q - b ) / a2 )
158- if not math .isnan (root ):
159- return root
160- return self .clamp_range ((- b - q ) / a2 )
161- a /= d
162- b /= d
163- c /= d
164- o3 = (3.0 * b - a * a ) / 9.0
165- q2 = (2.0 * a * a * a - 9.0 * a * b + 27.0 * c ) / 54.0
166- discriminant = q2 * q2 + o3 * o3 * o3
167- a3 = a / 3.0
168-
169- if discriminant < 0.0 :
170- mp33 = - (o3 * o3 * o3 )
171- r = math .sqrt (mp33 )
172- t = - q2 / r
173- cos_phi = max (- 1.0 , min (t , 1.0 ))
174- phi = math .acos (cos_phi )
175- t1 = 2.0 * cbrt (r )
176- root = self .clamp_range (t1 * math .cos (phi / 3.0 ) - a3 )
177- if not math .isnan (root ):
178- return root
179- root = self .clamp_range (
180- t1 * math .cos ((phi + 2.0 * math .pi ) / 3.0 ) - a3
181- )
182- if not math .isnan (root ):
183- return root
184- return self .clamp_range (
185- t1 * math .cos ((phi + 4.0 * math .pi ) / 3.0 ) - a3
186- )
187-
188- elif self .close_to (discriminant , 0.0 ):
189- u1 = - cbrt (q2 )
190- root = self .clamp_range (2.0 * u1 - a3 )
191- if not math .isnan (root ):
192- return root
193- return self .clamp_range (- u1 - a3 )
194-
195- sd = math .sqrt (discriminant )
196- u1 = cbrt (- q2 + sd )
197- v1 = cbrt (q2 + sd )
198- return self .clamp_range (u1 - v1 - a3 )
199-
200- def t (self , value : float ):
201- return self .evaluate_cubic (
202- self .p1 ,
203- self .p3 ,
204- self .find_first_cubic_root (
205- - value ,
206- self .p0 - value ,
207- self .p2 - value ,
208- 1.0 - value ,
209- ),
210- )
99+ from kivymd .utils .cubic_bezier import CubicBezier
100+ import time
211101
212102
213103class MDAnimationTransition (kivy .animation .AnimationTransition ):
@@ -218,10 +108,21 @@ class MDAnimationTransition(kivy.animation.AnimationTransition):
218108 easing_accelerated = CubicBezier (0.4 , 0.0 , 1.0 , 1.0 ).t
219109 easing_linear = CubicBezier (0.0 , 0.0 , 1.0 , 1.0 ).t
220110
111+ # equivalent to
112+ # path(M 0,0 C 0.05, 0, 0.133333, 0.06, 0.166666, 0.4 C 0.208333, 0.82, 0.25, 1, 1, 1)
113+ def easing_emphasized (t : float ):
114+ if t < 0.4 :
115+ # Normalize t: maps [0, 0.4] to [0, 1]
116+ t_norm = t / 0.4
117+ # First segment: (0.05, 0) to (0.1333, 0.06)
118+ return CubicBezier (0.05 , 0 , 0.133333 , 0.06 ).t (t_norm ) * 0.4
119+ else :
120+ # Normalize t: maps [0.4, 1.0] to [0, 1]
121+ t_norm = (t - 0.4 ) / 0.6
122+ # Second segment: (0.2083, 0.82) to (0.25, 1)
123+ # We start at 0.4 (the end of the last segment) and move toward 1.0
124+ return 0.4 + CubicBezier (0.208333 , 0.82 , 0.25 , 1 ).t (t_norm ) * 0.6
221125
222- # TODO: add `easing_emphasized` here
223- # it's defination is
224- # path(M 0,0 C 0.05, 0, 0.133333, 0.06, 0.166666, 0.4 C 0.208333, 0.82, 0.25, 1, 1, 1)
225126
226127# Monkey patch kivy's animation module
227128kivy .animation .AnimationTransition = MDAnimationTransition
0 commit comments