mirror of
https://github.com/Yonokid/PyTaiko.git
synced 2026-02-04 03:30:13 +01:00
slowly updating the animation library
This commit is contained in:
@@ -1,3 +1,8 @@
|
||||
from typing import Optional
|
||||
|
||||
from libs.utils import get_current_ms
|
||||
|
||||
|
||||
class Animation:
|
||||
def __init__(self, current_ms: float, duration: float, type: str):
|
||||
self.type = type
|
||||
@@ -8,23 +13,7 @@ class Animation:
|
||||
self.is_finished = False
|
||||
|
||||
def update(self, current_ms: float):
|
||||
if self.type == 'fade':
|
||||
self.fade(current_ms,
|
||||
self.duration,
|
||||
initial_opacity=self.params.get('initial_opacity', 1.0),
|
||||
final_opacity=self.params.get('final_opacity', 0.0),
|
||||
delay=self.params.get('delay', 0.0),
|
||||
ease_in=self.params.get('ease_in', None),
|
||||
ease_out=self.params.get('ease_out', None))
|
||||
if self.params.get('reverse', None) is not None and current_ms - self.start_ms >= self.duration + self.params.get('delay', 0.0):
|
||||
self.fade(current_ms,
|
||||
self.duration,
|
||||
final_opacity=self.params.get('initial_opacity', 1.0),
|
||||
initial_opacity=self.params.get('final_opacity', 0.0),
|
||||
delay=self.params.get('delay', 0.0) + self.duration + self.params.get('reverse'),
|
||||
ease_in=self.params.get('ease_in', None),
|
||||
ease_out=self.params.get('ease_out', None))
|
||||
elif self.type == 'move':
|
||||
if self.type == 'move':
|
||||
self.move(current_ms,
|
||||
self.duration,
|
||||
self.params['total_distance'],
|
||||
@@ -71,25 +60,6 @@ class Animation:
|
||||
else:
|
||||
return progress
|
||||
|
||||
def fade(self, current_ms: float, duration: float, initial_opacity: float, final_opacity: float, delay: float, ease_in: str | None, ease_out: str | None) -> None:
|
||||
elapsed_time = current_ms - self.start_ms
|
||||
if elapsed_time < delay:
|
||||
self.attribute = initial_opacity
|
||||
|
||||
elapsed_time -= delay
|
||||
if elapsed_time >= duration:
|
||||
self.attribute = final_opacity
|
||||
self.is_finished = True
|
||||
|
||||
if ease_in is not None:
|
||||
progress = self._ease_in_progress(elapsed_time / duration, ease_in)
|
||||
elif ease_out is not None:
|
||||
progress = self._ease_out_progress(elapsed_time / duration, ease_out)
|
||||
else:
|
||||
progress = elapsed_time / duration
|
||||
|
||||
current_opacity = initial_opacity + (final_opacity - initial_opacity) * progress
|
||||
self.attribute = current_opacity
|
||||
def move(self, current_ms: float, duration: float, total_distance: float, start_position: float, delay: float, ease_in: str | None, ease_out: str | None) -> None:
|
||||
elapsed_time = current_ms - self.start_ms
|
||||
if elapsed_time < delay:
|
||||
@@ -139,3 +109,137 @@ class Animation:
|
||||
else:
|
||||
self.attribute = final_size
|
||||
self.is_finished = True
|
||||
|
||||
class BaseAnimation():
|
||||
def __init__(self, duration: float, delay: float = 0.0):
|
||||
"""
|
||||
Initialize a base animation.
|
||||
|
||||
Args:
|
||||
duration: Length of the animation in milliseconds
|
||||
delay: Time to wait before starting the animation
|
||||
reverse_delay: If provided, animation will play in reverse after this delay
|
||||
"""
|
||||
self.duration = duration
|
||||
self.delay = delay
|
||||
self.start_ms = get_current_ms()
|
||||
self.is_finished = False
|
||||
self.attribute = 0
|
||||
|
||||
def update(self, current_time_ms: float) -> None:
|
||||
"""Update the animation based on the current time."""
|
||||
pass
|
||||
|
||||
def _ease_in(self, progress: float, ease_type: str) -> float:
|
||||
if ease_type == "quadratic":
|
||||
return progress * progress
|
||||
elif ease_type == "cubic":
|
||||
return progress * progress * progress
|
||||
elif ease_type == "exponential":
|
||||
return 0 if progress == 0 else pow(2, 10 * (progress - 1))
|
||||
return progress
|
||||
|
||||
def _ease_out(self, progress: float, ease_type: str) -> float:
|
||||
if ease_type == "quadratic":
|
||||
return progress * (2 - progress)
|
||||
elif ease_type == "cubic":
|
||||
return 1 - pow(1 - progress, 3)
|
||||
elif ease_type == "exponential":
|
||||
return 1 if progress == 1 else 1 - pow(2, -10 * progress)
|
||||
return progress
|
||||
|
||||
def _apply_easing(self, progress: float, ease_in: Optional[str] = None,
|
||||
ease_out: Optional[str] = None) -> float:
|
||||
if ease_in:
|
||||
return self._ease_in(progress, ease_in)
|
||||
elif ease_out:
|
||||
return self._ease_out(progress, ease_out)
|
||||
return progress
|
||||
|
||||
class FadeAnimation(BaseAnimation):
|
||||
def __init__(self, duration: float, initial_opacity: float = 1.0,
|
||||
final_opacity: float = 0.0, delay: float = 0.0,
|
||||
ease_in: Optional[str] = None, ease_out: Optional[str] = None,
|
||||
reverse_delay: Optional[float] = None):
|
||||
super().__init__(duration, delay)
|
||||
self.initial_opacity = initial_opacity
|
||||
self.final_opacity = final_opacity
|
||||
self.ease_in = ease_in
|
||||
self.ease_out = ease_out
|
||||
self.reverse_delay = reverse_delay
|
||||
|
||||
def update(self, current_time_ms: float):
|
||||
elapsed_time = current_time_ms - self.start_ms
|
||||
|
||||
if elapsed_time <= self.delay:
|
||||
self.attribute = self.initial_opacity
|
||||
elif elapsed_time >= self.delay + self.duration:
|
||||
self.attribute = self.final_opacity
|
||||
|
||||
if self.reverse_delay is not None:
|
||||
self.start_ms = current_time_ms
|
||||
self.delay = self.reverse_delay
|
||||
self.initial_opacity, self.final_opacity = self.final_opacity, self.initial_opacity
|
||||
self.reverse_delay = None
|
||||
else:
|
||||
self.is_finished = True
|
||||
else:
|
||||
animation_time = elapsed_time - self.delay
|
||||
progress = animation_time / self.duration
|
||||
progress = max(0.0, min(1.0, progress))
|
||||
progress = self._apply_easing(progress, self.ease_in, self.ease_out)
|
||||
self.attribute = self.initial_opacity + progress * (self.final_opacity - self.initial_opacity)
|
||||
|
||||
class MoveAnimation(BaseAnimation):
|
||||
def __init__(self, duration: float, total_distance: int = 0,
|
||||
start_position: int = 0, delay: float = 0.0,
|
||||
ease_in: Optional[str] = None, ease_out: Optional[str] = None):
|
||||
super().__init__(duration, delay)
|
||||
self.total_distance = total_distance
|
||||
self.start_position = start_position
|
||||
self.ease_in = ease_in
|
||||
self.ease_out = ease_out
|
||||
|
||||
def update(self, current_time_ms: float):
|
||||
elapsed_time = current_time_ms - self.start_ms
|
||||
if elapsed_time < self.delay:
|
||||
self.attribute = self.start_position
|
||||
|
||||
elif elapsed_time >= self.delay + self.duration:
|
||||
self.attribute = self.start_position + self.total_distance
|
||||
self.is_finished = True
|
||||
else:
|
||||
progress = (elapsed_time - self.delay) / self.duration
|
||||
progress = self._apply_easing(progress, self.ease_in, self.ease_out)
|
||||
self.attribute = self.start_position + (self.total_distance * progress)
|
||||
|
||||
|
||||
class Animation2:
|
||||
"""Factory for creating different types of animations."""
|
||||
|
||||
@staticmethod
|
||||
def create_fade(duration: float, **kwargs) -> FadeAnimation:
|
||||
"""Create a fade animation."""
|
||||
return FadeAnimation(duration, **kwargs)
|
||||
|
||||
@staticmethod
|
||||
def create_move(duration: float, **kwargs) -> MoveAnimation:
|
||||
"""Create a movement animation."""
|
||||
return MoveAnimation(duration, **kwargs)
|
||||
'''
|
||||
|
||||
@staticmethod
|
||||
def create_texture_change(duration: float, **kwargs) -> TextureChangeAnimation:
|
||||
"""Create a texture change animation."""
|
||||
return TextureChangeAnimation(duration, **kwargs)
|
||||
|
||||
@staticmethod
|
||||
def create_text_stretch(duration: float) -> TextStretchAnimation:
|
||||
"""Create a text stretch animation."""
|
||||
return TextStretchAnimation(duration)
|
||||
|
||||
@staticmethod
|
||||
def create_texture_resize(duration: float, **kwargs) -> TextureResizeAnimation:
|
||||
"""Create a texture resize animation."""
|
||||
return TextureResizeAnimation(duration, **kwargs)
|
||||
'''
|
||||
|
||||
Reference in New Issue
Block a user