from manim import *
class ManimCfgDemo(Scene):
    """manim.cfg：配置项的层级优先级"""
    def construct(self):
        title = Text("manim.cfg 的配置从哪来：五层覆盖", font_size=32).to_edge(UP, buff=0.4)
        self.play(Write(title))
        layers = [
            ("命令行参数 -qh --fps 60", BLUE, "最高"),
            ("项目目录 ./manim.cfg", TEAL, ""),
            ("用户目录 ~/manim.cfg", GREEN, ""),
            ("库默认 default.cfg", GREY_B, ""),
            ("代码内建默认值", GREY_D, "最低"),
        ]
        stack = VGroup()
        for text, color, tag in layers:
            bar = Rectangle(width=7.4, height=0.66, fill_color=color,
                            fill_opacity=0.28, stroke_color=color, stroke_width=2)
            label = Text(text, font_size=21).move_to(bar.get_center())
            item = VGroup(bar, label)
            if tag:
                item.add(Text(tag, font_size=18, color=YELLOW)
                         .next_to(bar, RIGHT, buff=0.22))
            stack.add(item)
        stack.arrange(DOWN, buff=0.15).next_to(title, DOWN, buff=0.55)
        self.play(LaggedStart(*[FadeIn(s, shift=RIGHT * 0.3) for s in stack], lag_ratio=0.16))
        self.wait(0.4)
        # 左侧竖向箭头 + 说明
        arrow = Arrow(ORIGIN, DOWN * stack.height * 0.82, color=YELLOW,
                      stroke_width=4, buff=0)
        arrow.next_to(stack, LEFT, buff=0.5)
        arrow.set_y(stack.get_center()[1])
        hint = Text("优先级\n由高到低", font_size=17, color=YELLOW, line_spacing=0.75)
        hint.next_to(arrow, LEFT, buff=0.12)
        self.play(GrowArrow(arrow), FadeIn(hint))
        self.wait(1.2)
        # 第二幕：真实 manim.cfg 片段
        self.play(FadeOut(stack), FadeOut(arrow), FadeOut(hint), FadeOut(title))
        cfg = VGroup(
            Text("manim.cfg", font_size=26, color=YELLOW),
            Text("[CLI]", font="Consolas", font_size=22, color=YELLOW),
            Text("frame_rate = 60", font="Consolas", font_size=21, color=WHITE),
            Text("pixel_width = 1920", font="Consolas", font_size=21, color=WHITE),
            Text("pixel_height = 1080", font="Consolas", font_size=21, color=WHITE),
            Text("background_color = #0B1E3A", font="Consolas", font_size=21, color=WHITE),
            Text("scene_names = MyScene", font="Consolas", font_size=21, color=WHITE),
        ).arrange(DOWN, buff=0.16, aligned_edge=LEFT)
        box = SurroundingRectangle(cfg, color=YELLOW, buff=0.3, stroke_width=2)
        VGroup(box, cfg).move_to(ORIGIN)
        self.play(Create(box), LaggedStart(*[FadeIn(c) for c in cfg], lag_ratio=0.12))
        self.wait(1.6)