Clean up special_tex contents, make each class use MTexText instead of TexText

This commit is contained in:
Grant Sanderson 2022-12-20 15:08:54 -08:00
parent 3293f72adc
commit 0d525baf29

View file

@ -1,61 +1,48 @@
from __future__ import annotations from __future__ import annotations
from manimlib.constants import WHITE, GREY_C from manimlib.constants import MED_SMALL_BUFF, WHITE, GREY_C
from manimlib.constants import DOWN, LEFT, RIGHT, UP from manimlib.constants import DOWN, LEFT, RIGHT, UP
from manimlib.constants import FRAME_WIDTH from manimlib.constants import FRAME_WIDTH
from manimlib.constants import MED_LARGE_BUFF, SMALL_BUFF from manimlib.constants import MED_LARGE_BUFF, SMALL_BUFF
from manimlib.mobject.geometry import Line from manimlib.mobject.geometry import Line
from manimlib.mobject.svg.tex_mobject import Tex, TexText from manimlib.mobject.types.vectorized_mobject import VGroup
from manimlib.mobject.svg.mtex_mobject import MTex, MTexText from manimlib.mobject.svg.mtex_mobject import MTexText
from typing import TYPE_CHECKING from typing import TYPE_CHECKING
if TYPE_CHECKING: if TYPE_CHECKING:
from manimlib.typing import ManimColor from manimlib.typing import ManimColor, Vect3
class BulletedList(TexText): class BulletedList(VGroup):
def __init__( def __init__(
self, self,
*items: str, *items: str,
buff: float = MED_LARGE_BUFF, buff: float = MED_LARGE_BUFF,
dot_scale_factor: float = 2.0, aligned_edge: Vect3 = LEFT,
alignment: str = "",
**kwargs **kwargs
): ):
super().__init__( labelled_content = [R"\item " + item for item in items]
*(s + R"\\" for s in items), tex_string = "\n".join([
alignment=alignment, R"\begin{itemize}",
**kwargs *labelled_content,
) R"\end{itemize}"
for part in self: ])
dot = Tex(R"\cdot").scale(dot_scale_factor) tex_text = MTexText(tex_string, isolate=labelled_content, **kwargs)
dot.next_to(part[0], LEFT, SMALL_BUFF) lines = (tex_text.select_part(part) for part in labelled_content)
part.add_to_back(dot)
self.arrange(
DOWN,
aligned_edge=LEFT,
buff=buff
)
def fade_all_but(self, index_or_string: int | str, opacity: float = 0.5) -> None: super().__init__(*lines)
arg = index_or_string
if isinstance(arg, str): self.arrange(DOWN, buff=buff, aligned_edge=aligned_edge)
part = self.get_part_by_tex(arg)
elif isinstance(arg, int): def fade_all_but(self, index: int, opacity: float = 0.25) -> None:
part = self.submobjects[arg] for i, part in enumerate(self.submobjects):
else: part.set_fill(opacity=(1.0 if i == index else opacity))
raise Exception("Expected int or string, got {0}".format(arg))
for other_part in self.submobjects:
if other_part is part:
other_part.set_fill(opacity=1)
else:
other_part.set_fill(opacity=opacity)
class TexTextFromPresetString(TexText): class TexTextFromPresetString(MTexText):
tex: str = "" tex: str = ""
default_color: ManimColor = WHITE default_color: ManimColor = WHITE
@ -67,11 +54,11 @@ class TexTextFromPresetString(TexText):
) )
class Title(TexText): class Title(MTexText):
def __init__( def __init__(
self, self,
*text_parts: str, *text_parts: str,
scale_factor: float = 1.0, font_size: int = 72,
include_underline: bool = True, include_underline: bool = True,
underline_width: float = FRAME_WIDTH - 2, underline_width: float = FRAME_WIDTH - 2,
# This will override underline_width # This will override underline_width
@ -80,9 +67,8 @@ class Title(TexText):
underline_style: dict = dict(stroke_width=2, stroke_color=GREY_C), underline_style: dict = dict(stroke_width=2, stroke_color=GREY_C),
**kwargs **kwargs
): ):
super().__init__(*text_parts, **kwargs) super().__init__(*text_parts, font_size=font_size, **kwargs)
self.scale(scale_factor) self.to_edge(UP, buff=MED_SMALL_BUFF)
self.to_edge(UP)
if include_underline: if include_underline:
underline = Line(LEFT, RIGHT, **underline_style) underline = Line(LEFT, RIGHT, **underline_style)
underline.next_to(self, DOWN, buff=underline_buff) underline.next_to(self, DOWN, buff=underline_buff)