2022-02-15 18:39:45 +08:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2019-02-10 10:43:45 -08:00
|
|
|
import operator as op
|
2022-02-15 18:39:45 +08:00
|
|
|
from typing import Callable
|
2019-02-10 10:43:45 -08:00
|
|
|
|
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
|
|
|
|
if TYPE_CHECKING:
|
|
|
|
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
|
|
|
|
"""
|
2019-02-10 10:43:45 -08:00
|
|
|
CONFIG = {
|
|
|
|
"suspend_mobject_updating": False,
|
|
|
|
}
|
2018-04-06 13:58:59 -07:00
|
|
|
|
2022-02-15 18:39:45 +08:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
mobject: Mobject,
|
|
|
|
update_function: Callable[[Mobject]],
|
|
|
|
**kwargs
|
|
|
|
):
|
2019-02-10 10:43:45 -08:00
|
|
|
self.update_function = update_function
|
|
|
|
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:
|
2018-03-31 15:11:35 -07:00
|
|
|
self.update_function(self.mobject)
|
|
|
|
|
2018-04-06 13:58:59 -07:00
|
|
|
|
2018-03-31 15:11:35 -07:00
|
|
|
class UpdateFromAlphaFunc(UpdateFromFunc):
|
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
|
|
|
|
self.diff = op.sub(
|
|
|
|
mobject.get_center(),
|
|
|
|
tracked_mobject.get_center(),
|
|
|
|
)
|
|
|
|
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)
|