3b1b-manim/manimlib/mobject/shape_matchers.py

131 lines
3.7 KiB
Python
Raw Normal View History

from __future__ import annotations
2022-04-12 19:19:59 +08:00
from colour import Color
Cleaner local caching of Tex/Text data, and partially cleaned up configuration (#2259) * Remove print("Reloading...") * Change where exception mode is set, to be quieter * Add default fallback monitor for when no monitors are detected * Have StringMobject work with svg strings rather than necessarily writing to file Change SVGMobject to allow taking in a string of svg code as an input * Add caching functionality, and have Tex and Text both use it for saved svg strings * Clean up tex_file_writing * Get rid of get_tex_dir and get_text_dir * Allow for a configurable cache location * Make caching on disk a decorator, and update implementations for Tex and Text mobjects * Remove stray prints * Clean up how configuration is handled In principle, all we need here is that manim looks to the default_config.yaml file, and updates it based on any local configuration files, whether in the current working directory or as specified by a CLI argument. * Make the default size for hash_string an option * Remove utils/customization.py * Remove stray prints * Consolidate camera configuration This is still not optimal, but at least makes clearer the way that importing from constants.py kicks off some of the configuration code. * Factor out configuration to be passed into a scene vs. that used to run a scene * Use newer extract_scene.main interface * Add clarifying message to note what exactly is being reloaded * Minor clean up * Minor clean up * If it's worth caching to disk, then might as well do so in memory too during development * No longer any need for custom hash_seeds in Tex and Text * Remove display_during_execution * Get rid of (no longer used) mobject_data directory reference * Remove get_downloads_dir reference from register_font * Update where downloads go * Easier use of subdirectories in configuration * Add new pip requirements
2024-12-05 16:51:14 -06:00
from manimlib.config import get_global_config
2022-12-15 20:48:53 -08:00
from manimlib.constants import BLACK, RED, YELLOW, WHITE
2022-04-12 19:19:59 +08:00
from manimlib.constants import DL, DOWN, DR, LEFT, RIGHT, UL, UR
from manimlib.constants import SMALL_BUFF
from manimlib.mobject.geometry import Line
from manimlib.mobject.geometry import Rectangle
from manimlib.mobject.types.vectorized_mobject import VGroup
from manimlib.mobject.types.vectorized_mobject import VMobject
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from typing import Sequence
from manimlib.mobject.mobject import Mobject
from manimlib.typing import ManimColor, Self
class SurroundingRectangle(Rectangle):
2022-12-15 20:48:53 -08:00
def __init__(
self,
mobject: Mobject,
buff: float = SMALL_BUFF,
color: ManimColor = YELLOW,
**kwargs
):
super().__init__(color=color, **kwargs)
self.buff = buff
self.surround(mobject)
if mobject.is_fixed_in_frame():
self.fix_in_frame()
def surround(self, mobject, buff=None) -> Self:
self.mobject = mobject
self.buff = buff if buff is not None else self.buff
super().surround(mobject, self.buff)
return self
def set_buff(self, buff) -> Self:
self.buff = buff
self.surround(self.mobject)
return self
class BackgroundRectangle(SurroundingRectangle):
2022-12-15 20:48:53 -08:00
def __init__(
self,
mobject: Mobject,
color: ManimColor = None,
stroke_width: float = 0,
stroke_opacity: float = 0,
fill_opacity: float = 0.75,
buff: float = 0,
**kwargs
):
if color is None:
Cleaner local caching of Tex/Text data, and partially cleaned up configuration (#2259) * Remove print("Reloading...") * Change where exception mode is set, to be quieter * Add default fallback monitor for when no monitors are detected * Have StringMobject work with svg strings rather than necessarily writing to file Change SVGMobject to allow taking in a string of svg code as an input * Add caching functionality, and have Tex and Text both use it for saved svg strings * Clean up tex_file_writing * Get rid of get_tex_dir and get_text_dir * Allow for a configurable cache location * Make caching on disk a decorator, and update implementations for Tex and Text mobjects * Remove stray prints * Clean up how configuration is handled In principle, all we need here is that manim looks to the default_config.yaml file, and updates it based on any local configuration files, whether in the current working directory or as specified by a CLI argument. * Make the default size for hash_string an option * Remove utils/customization.py * Remove stray prints * Consolidate camera configuration This is still not optimal, but at least makes clearer the way that importing from constants.py kicks off some of the configuration code. * Factor out configuration to be passed into a scene vs. that used to run a scene * Use newer extract_scene.main interface * Add clarifying message to note what exactly is being reloaded * Minor clean up * Minor clean up * If it's worth caching to disk, then might as well do so in memory too during development * No longer any need for custom hash_seeds in Tex and Text * Remove display_during_execution * Get rid of (no longer used) mobject_data directory reference * Remove get_downloads_dir reference from register_font * Update where downloads go * Easier use of subdirectories in configuration * Add new pip requirements
2024-12-05 16:51:14 -06:00
color = get_global_config()['style']['background_color']
2022-12-15 20:48:53 -08:00
super().__init__(
mobject,
color=color,
stroke_width=stroke_width,
stroke_opacity=stroke_opacity,
fill_opacity=fill_opacity,
buff=buff,
**kwargs
)
self.original_fill_opacity = fill_opacity
2023-01-31 14:18:02 -08:00
def pointwise_become_partial(self, mobject: Mobject, a: float, b: float) -> Self:
self.set_fill(opacity=b * self.original_fill_opacity)
return self
2023-01-31 14:18:02 -08:00
def set_style(
self,
stroke_color: ManimColor | None = None,
stroke_width: float | None = None,
fill_color: ManimColor | None = None,
fill_opacity: float | None = None,
family: bool = True
2023-01-31 14:18:02 -08:00
) -> Self:
2021-08-07 22:25:26 +07:00
# Unchangeable style, except for fill_opacity
2023-01-31 14:18:02 -08:00
VMobject.set_style(
self,
stroke_color=BLACK,
stroke_width=0,
fill_color=BLACK,
fill_opacity=fill_opacity
)
return self
def get_fill_color(self) -> Color:
return Color(self.color)
class Cross(VGroup):
2022-12-15 20:48:53 -08:00
def __init__(
self,
mobject: Mobject,
stroke_color: ManimColor = RED,
stroke_width: float | Sequence[float] = [0, 6, 0],
**kwargs
):
super().__init__(
Line(UL, DR),
Line(UR, DL),
)
2022-05-04 09:30:54 -07:00
self.insert_n_curves(20)
self.replace(mobject, stretch=True)
2022-12-15 20:48:53 -08:00
self.set_stroke(stroke_color, width=stroke_width)
2019-12-10 13:38:30 -08:00
class Underline(Line):
2022-12-15 20:48:53 -08:00
def __init__(
self,
mobject: Mobject,
buff: float = SMALL_BUFF,
stroke_color=WHITE,
2024-03-25 19:10:30 -03:00
stroke_width: float | Sequence[float] = [0, 3, 3, 0],
stretch_factor=1.2,
2022-12-15 20:48:53 -08:00
**kwargs
):
2024-03-21 09:59:29 -03:00
super().__init__(LEFT, RIGHT, **kwargs)
if not isinstance(stroke_width, (float, int)):
self.insert_n_curves(len(stroke_width) - 2)
self.set_stroke(stroke_color, stroke_width)
self.set_width(mobject.get_width() * stretch_factor)
2022-12-15 20:48:53 -08:00
self.next_to(mobject, DOWN, buff=buff)