Kill CONFIG in interactives.py

This commit is contained in:
Grant Sanderson 2022-12-15 18:29:05 -08:00
parent 2a645b27f8
commit 9d65ef3cae

View file

@ -19,7 +19,6 @@ from manimlib.mobject.svg.text_mobject import Text
from manimlib.mobject.types.vectorized_mobject import VGroup from manimlib.mobject.types.vectorized_mobject import VGroup
from manimlib.mobject.value_tracker import ValueTracker from manimlib.mobject.value_tracker import ValueTracker
from manimlib.utils.color import rgb_to_hex from manimlib.utils.color import rgb_to_hex
from manimlib.utils.config_ops import digest_config
from manimlib.utils.space_ops import get_closest_point_on_line from manimlib.utils.space_ops import get_closest_point_on_line
from manimlib.utils.space_ops import get_norm from manimlib.utils.space_ops import get_norm
@ -27,6 +26,7 @@ from typing import TYPE_CHECKING
if TYPE_CHECKING: if TYPE_CHECKING:
from typing import Callable from typing import Callable
from manimlib.constants import ManimColor
# Interactive Mobjects # Interactive Mobjects
@ -95,19 +95,25 @@ class ControlMobject(ValueTracker):
class EnableDisableButton(ControlMobject): class EnableDisableButton(ControlMobject):
CONFIG = { def __init__(
"value_type": np.dtype(bool), self,
"rect_kwargs": { value: bool = True,
value_type: np.dtype = np.dtype(bool),
rect_kwargs: dict = {
"width": 0.5, "width": 0.5,
"height": 0.5, "height": 0.5,
"fill_opacity": 1.0 "fill_opacity": 1.0
}, },
"enable_color": GREEN, enable_color: ManimColor = GREEN,
"disable_color": RED disable_color: ManimColor = RED,
} **kwargs
):
self.value = value
self.value_type = value_type
self.rect_kwargs = rect_kwargs
self.enable_color = enable_color
self.disable_color = disable_color
def __init__(self, value: bool = True, **kwargs):
digest_config(self, kwargs)
self.box = Rectangle(**self.rect_kwargs) self.box = Rectangle(**self.rect_kwargs)
super().__init__(value, self.box, **kwargs) super().__init__(value, self.box, **kwargs)
self.add_mouse_press_listner(self.on_mouse_press) self.add_mouse_press_listner(self.on_mouse_press)
@ -130,27 +136,32 @@ class EnableDisableButton(ControlMobject):
class Checkbox(ControlMobject): class Checkbox(ControlMobject):
CONFIG = { def __init__(
"value_type": np.dtype(bool), self,
"rect_kwargs": { value: bool = True,
value_type: np.dtype = np.dtype(bool),
rect_kwargs: dict = {
"width": 0.5, "width": 0.5,
"height": 0.5, "height": 0.5,
"fill_opacity": 0.0 "fill_opacity": 0.0
}, },
checkmark_kwargs: dict = {
"checkmark_kwargs": {
"stroke_color": GREEN, "stroke_color": GREEN,
"stroke_width": 6, "stroke_width": 6,
}, },
"cross_kwargs": { cross_kwargs: dict = {
"stroke_color": RED, "stroke_color": RED,
"stroke_width": 6, "stroke_width": 6,
}, },
"box_content_buff": SMALL_BUFF box_content_buff: float = SMALL_BUFF,
} **kwargs
):
self.value_type = value_type
self.rect_kwargs = rect_kwargs
self.checkmark_kwargs = checkmark_kwargs
self.cross_kwargs = cross_kwargs
self.box_content_buff = box_content_buff
def __init__(self, value: bool = True, **kwargs):
digest_config(self, kwargs)
self.box = Rectangle(**self.rect_kwargs) self.box = Rectangle(**self.rect_kwargs)
self.box_content = self.get_checkmark() if value else self.get_cross() self.box_content = self.get_checkmark() if value else self.get_cross()
super().__init__(value, self.box, self.box_content, **kwargs) super().__init__(value, self.box, self.box_content, **kwargs)
@ -200,27 +211,33 @@ class Checkbox(ControlMobject):
class LinearNumberSlider(ControlMobject): class LinearNumberSlider(ControlMobject):
CONFIG = { def __init__(
"value_type": np.float64, self,
"min_value": -10.0, value: float = 0,
"max_value": 10.0, value_type: type = np.float64,
"step": 1.0, min_value: float = -10.0,
max_value: float = 10.0,
"rounded_rect_kwargs": { step: float = 1.0,
rounded_rect_kwargs: dict = {
"height": 0.075, "height": 0.075,
"width": 2, "width": 2,
"corner_radius": 0.0375 "corner_radius": 0.0375
}, },
"circle_kwargs": { circle_kwargs: dict = {
"radius": 0.1, "radius": 0.1,
"stroke_color": GREY_A, "stroke_color": GREY_A,
"fill_color": GREY_A, "fill_color": GREY_A,
"fill_opacity": 1.0 "fill_opacity": 1.0
} },
} **kwargs
):
self.value_type = value_type
self.min_value = min_value
self.max_value = max_value
self.step = step
self.rounded_rect_kwargs = rounded_rect_kwargs
self.circle_kwargs = circle_kwargs
def __init__(self, value: float = 0, **kwargs):
digest_config(self, kwargs)
self.bar = RoundedRectangle(**self.rounded_rect_kwargs) self.bar = RoundedRectangle(**self.rounded_rect_kwargs)
self.slider = Circle(**self.circle_kwargs) self.slider = Circle(**self.circle_kwargs)
self.slider_axis = Line( self.slider_axis = Line(
@ -258,24 +275,29 @@ class LinearNumberSlider(ControlMobject):
class ColorSliders(Group): class ColorSliders(Group):
CONFIG = { def __init__(
"sliders_kwargs": {}, self,
"rect_kwargs": { sliders_kwargs: dict = {},
rect_kwargs: dict = {
"width": 2.0, "width": 2.0,
"height": 0.5, "height": 0.5,
"stroke_opacity": 1.0 "stroke_opacity": 1.0
}, },
"background_grid_kwargs": { background_grid_kwargs: dict = {
"colors": [GREY_A, GREY_C], "colors": [GREY_A, GREY_C],
"single_square_len": 0.1 "single_square_len": 0.1
}, },
"sliders_buff": MED_LARGE_BUFF, sliders_buff: float = MED_LARGE_BUFF,
"default_rgb_value": 255, default_rgb_value: int = 255,
"default_a_value": 1, default_a_value: int = 1,
} **kwargs
):
def __init__(self, **kwargs): self.sliders_kwargs = sliders_kwargs
digest_config(self, kwargs) self.rect_kwargs = rect_kwargs
self.background_grid_kwargs = background_grid_kwargs
self.sliders_buff = sliders_buff
self.default_rgb_value = default_rgb_value
self.default_a_value = default_a_value
rgb_kwargs = {"value": self.default_rgb_value, "min_value": 0, "max_value": 255, "step": 1} rgb_kwargs = {"value": self.default_rgb_value, "min_value": 0, "max_value": 255, "step": 1}
a_kwargs = {"value": self.default_a_value, "min_value": 0, "max_value": 1, "step": 0.04} a_kwargs = {"value": self.default_a_value, "min_value": 0, "max_value": 1, "step": 0.04}
@ -358,26 +380,33 @@ class ColorSliders(Group):
class Textbox(ControlMobject): class Textbox(ControlMobject):
CONFIG = { def __init__(
"value_type": np.dtype(object), self,
value: str = "",
"box_kwargs": { value_type: np.dtype = np.dtype(object),
box_kwargs: dict = {
"width": 2.0, "width": 2.0,
"height": 1.0, "height": 1.0,
"fill_color": WHITE, "fill_color": WHITE,
"fill_opacity": 1.0, "fill_opacity": 1.0,
}, },
"text_kwargs": { text_kwargs: dict = {
"color": BLUE "color": BLUE
}, },
"text_buff": MED_SMALL_BUFF, text_buff: float = MED_SMALL_BUFF,
"isInitiallyActive": False, isInitiallyActive: bool = False,
"active_color": BLUE, active_color: ManimColor = BLUE,
"deactive_color": RED, deactive_color: ManimColor = RED,
} **kwargs
):
self.value_type = value_type
self.box_kwargs = box_kwargs
self.text_kwargs = text_kwargs
self.text_buff = text_buff
self.isInitiallyActive = isInitiallyActive
self.active_color = active_color
self.deactive_color = deactive_color
def __init__(self, value: str = "", **kwargs):
digest_config(self, kwargs)
self.isActive = self.isInitiallyActive self.isActive = self.isInitiallyActive
self.box = Rectangle(**self.box_kwargs) self.box = Rectangle(**self.box_kwargs)
self.box.add_mouse_press_listner(self.box_on_mouse_press) self.box.add_mouse_press_listner(self.box_on_mouse_press)
@ -436,28 +465,31 @@ class Textbox(ControlMobject):
class ControlPanel(Group): class ControlPanel(Group):
CONFIG = { def __init__(
"panel_kwargs": { self,
*controls: ControlMobject,
panel_kwargs: dict = {
"width": FRAME_WIDTH / 4, "width": FRAME_WIDTH / 4,
"height": MED_SMALL_BUFF + FRAME_HEIGHT, "height": MED_SMALL_BUFF + FRAME_HEIGHT,
"fill_color": GREY_C, "fill_color": GREY_C,
"fill_opacity": 1.0, "fill_opacity": 1.0,
"stroke_width": 0.0 "stroke_width": 0.0
}, },
"opener_kwargs": { opener_kwargs: dict = {
"width": FRAME_WIDTH / 8, "width": FRAME_WIDTH / 8,
"height": 0.5, "height": 0.5,
"fill_color": GREY_C, "fill_color": GREY_C,
"fill_opacity": 1.0 "fill_opacity": 1.0
}, },
"opener_text_kwargs": { opener_text_kwargs: dict = {
"text": "Control Panel", "text": "Control Panel",
"font_size": 20 "font_size": 20
} },
} **kwargs
):
def __init__(self, *controls: ControlMobject, **kwargs): self.panel_kwargs = panel_kwargs
digest_config(self, kwargs) self.opener_kwargs = opener_kwargs
self.opener_text_kwargs = opener_text_kwargs
self.panel = Rectangle(**self.panel_kwargs) self.panel = Rectangle(**self.panel_kwargs)
self.panel.to_corner(UP + LEFT, buff=0) self.panel.to_corner(UP + LEFT, buff=0)