2022-02-15 18:39:45 +08:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2018-12-24 12:37:51 -08:00
|
|
|
from manimlib.animation.animation import Animation
|
2018-03-31 15:11:35 -07:00
|
|
|
|
2022-02-15 18:39:45 +08:00
|
|
|
from typing import TYPE_CHECKING
|
2022-02-16 21:08:25 +08:00
|
|
|
|
2022-02-15 18:39:45 +08:00
|
|
|
if TYPE_CHECKING:
|
2022-04-12 19:19:59 +08:00
|
|
|
from typing import Callable
|
|
|
|
|
2022-02-15 18:39:45 +08:00
|
|
|
from manimlib.mobject.mobject import Mobject
|
|
|
|
|
2018-03-31 15:11:35 -07:00
|
|
|
|
|
|
|
class UpdateFromFunc(Animation):
|
|
|
|
"""
|
|
|
|
update_function of the form func(mobject), presumably
|
|
|
|
to be used when the state of one mobject is dependent
|
|
|
|
on another simultaneously animated mobject
|
|
|
|
"""
|
2022-02-15 18:39:45 +08:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
mobject: Mobject,
|
2022-12-14 16:02:15 -08:00
|
|
|
update_function: Callable[[Mobject], Mobject | None],
|
|
|
|
suspend_mobject_updating: bool = False,
|
2022-02-15 18:39:45 +08:00
|
|
|
**kwargs
|
|
|
|
):
|
2019-02-10 10:43:45 -08:00
|
|
|
self.update_function = update_function
|
2022-12-14 16:02:15 -08:00
|
|
|
super().__init__(
|
|
|
|
mobject,
|
|
|
|
suspend_mobject_updating=suspend_mobject_updating,
|
|
|
|
**kwargs
|
|
|
|
)
|
2018-03-31 15:11:35 -07:00
|
|
|
|
2022-02-15 18:39:45 +08:00
|
|
|
def interpolate_mobject(self, alpha: float) -> None:
|
2018-03-31 15:11:35 -07:00
|
|
|
self.update_function(self.mobject)
|
|
|
|
|
2018-04-06 13:58:59 -07:00
|
|
|
|
2022-12-14 16:02:15 -08:00
|
|
|
class UpdateFromAlphaFunc(Animation):
|
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
mobject: Mobject,
|
|
|
|
update_function: Callable[[Mobject, float], Mobject | None],
|
|
|
|
suspend_mobject_updating: bool = False,
|
|
|
|
**kwargs
|
|
|
|
):
|
|
|
|
self.update_function = update_function
|
|
|
|
super().__init__(mobject, suspend_mobject_updating=suspend_mobject_updating, **kwargs)
|
|
|
|
|
2022-02-15 18:39:45 +08:00
|
|
|
def interpolate_mobject(self, alpha: float) -> None:
|
2018-03-31 15:11:35 -07:00
|
|
|
self.update_function(self.mobject, alpha)
|
|
|
|
|
2018-04-06 13:58:59 -07:00
|
|
|
|
2018-03-31 15:11:35 -07:00
|
|
|
class MaintainPositionRelativeTo(Animation):
|
2022-02-15 18:39:45 +08:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
mobject: Mobject,
|
|
|
|
tracked_mobject: Mobject,
|
|
|
|
**kwargs
|
|
|
|
):
|
2019-02-10 10:43:45 -08:00
|
|
|
self.tracked_mobject = tracked_mobject
|
2022-04-12 19:19:59 +08:00
|
|
|
self.diff = mobject.get_center() - tracked_mobject.get_center()
|
2019-02-10 10:43:45 -08:00
|
|
|
super().__init__(mobject, **kwargs)
|
2018-03-31 15:11:35 -07:00
|
|
|
|
2022-02-15 18:39:45 +08:00
|
|
|
def interpolate_mobject(self, alpha: float) -> None:
|
2019-02-10 10:43:45 -08:00
|
|
|
target = self.tracked_mobject.get_center()
|
|
|
|
location = self.mobject.get_center()
|
2018-12-27 09:41:41 -08:00
|
|
|
self.mobject.shift(target - location + self.diff)
|