mgui-core 1.1.0

mgui 框架核心:Element/RenderObject/Widget 三树、布局与绘制协议、帧管线与响应式原语
Documentation
//! CustomPaint 的受限绘制门面。
//!
//! `PaintList` 是 RenderObject → Renderer 的完整协议,包含纹理注册、layer 合成、
//! pending texture meta 等框架内部通道。应用侧自定义绘制只需要其中一小部分,
//! 直接暴露 `PaintList` 会让应用绕过缓存/纹理/层语义。这里提供受限门面:
//!
//! - 图元:rect / rounded_rect / linear_gradient / radial_gradient / triangles /
//!   text / text_wrapped / push_clip / push_clip_rrect / pop_clip;
//! - 坐标:闭包使用 widget 局部坐标(原点 0,0),门面统一平移到目标坐标系;
//! - 不暴露:纹理注册、pending textures、extend、clear、push(PaintCommand)、layer 操作。
//!
//! 闭包禁止读墙钟;动画由帧管线与时钟抽象驱动,保证 Agent replay 逐字节可比。

use crate::paint::{PaintCommand, PaintList, Vertex};
use crate::{Color, Point, Rect, Size};

/// 受限绘制门面。生命周期绑定本次 `RenderObject::paint` 的 `PaintList`。
pub struct Painter<'a> {
    list: &'a mut PaintList,
    origin: Point,
    size: Size,
    clip_depth: usize,
}

impl<'a> Painter<'a> {
    /// 由 RenderObject 在 paint 中构造;`origin` 是本 RO 原点在目标坐标系中的位置。
    pub fn new(list: &'a mut PaintList, origin: Point, size: Size) -> Self {
        Self { list, origin, size, clip_depth: 0 }
    }

    /// widget 局部坐标下的可用区域。
    pub fn bounds(&self) -> Rect {
        Rect::new(0.0, 0.0, self.size.width, self.size.height)
    }

    fn local_rect(&self, rect: Rect) -> Rect {
        Rect::new(rect.x + self.origin.x, rect.y + self.origin.y, rect.width, rect.height)
    }

    fn local_point(&self, point: Point) -> Point {
        Point::new(point.x + self.origin.x, point.y + self.origin.y)
    }

    /// 直角矩形(局部坐标)。
    pub fn rect(&mut self, rect: Rect, color: Color) {
        self.list.rect(self.local_rect(rect), color);
    }

    /// 圆角矩形(局部坐标)。
    pub fn rounded_rect(&mut self, rect: Rect, color: Color, radius: f32) {
        self.list.rounded_rect(self.local_rect(rect), color, radius);
    }

    /// 两色线性渐变(局部坐标)。
    pub fn linear_gradient(
        &mut self,
        rect: Rect,
        start: Point,
        end: Point,
        from: Color,
        to: Color,
        corner_radius: f32,
    ) {
        self.list.linear_gradient(
            self.local_rect(rect),
            self.local_point(start),
            self.local_point(end),
            from,
            to,
            corner_radius,
        );
    }

    /// 两色径向渐变(局部坐标)。
    pub fn radial_gradient(
        &mut self,
        rect: Rect,
        center: Point,
        radial_radius: f32,
        from: Color,
        to: Color,
        corner_radius: f32,
    ) {
        self.list.radial_gradient(
            self.local_rect(rect),
            self.local_point(center),
            radial_radius,
            from,
            to,
            corner_radius,
        );
    }

    /// 三角网格。顶点使用局部坐标,门面统一平移到目标坐标系。
    pub fn triangles(&mut self, vertices: Vec<Vertex>, indices: Vec<u32>) {
        let shifted: Vec<Vertex> = vertices
            .into_iter()
            .map(|mut v| {
                v.x += self.origin.x;
                v.y += self.origin.y;
                v
            })
            .collect();
        self.list.commands.push(PaintCommand::Triangles { vertices: shifted, indices });
    }

    /// 高层文字命令(运行时展开为 glyph quads),局部坐标。
    pub fn text(&mut self, text: impl Into<std::rc::Rc<str>>, font_size: f32, color: Color, position: Point) {
        self.list.text(text, font_size, color, self.local_point(position));
    }

    /// 带软换行宽度的高层文字命令,局部坐标。
    pub fn text_wrapped(
        &mut self,
        text: impl Into<std::rc::Rc<str>>,
        font_size: f32,
        color: Color,
        position: Point,
        max_width: Option<f32>,
    ) {
        self.list.text_wrapped(text, font_size, color, self.local_point(position), max_width);
    }

    /// 压入矩形裁剪(局部坐标),必须配对 `pop_clip`。
    pub fn push_clip(&mut self, rect: Rect) {
        self.list.commands.push(PaintCommand::PushClip(self.local_rect(rect)));
        self.clip_depth += 1;
    }

    /// 压入圆角矩形裁剪(局部坐标),必须配对 `pop_clip`。
    pub fn push_clip_rrect(&mut self, rect: Rect, radius: f32) {
        self.list.commands.push(PaintCommand::PushClipRRect { rect: self.local_rect(rect), radius });
        self.clip_depth += 1;
    }

    /// 弹出最近一次裁剪。无未配对裁剪时返回 `false`(不越界弹别人的命令)。
    pub fn pop_clip(&mut self) -> bool {
        if self.clip_depth == 0 {
            return false;
        }
        self.list.commands.push(PaintCommand::PopClip);
        self.clip_depth -= 1;
        true
    }

    /// 关闭本组件遗留的裁剪。RenderObject 在闭包返回后无条件调用,
    /// 避免闭包忘记 pop 时把后续组件的内容裁掉。
    pub fn close_clips(&mut self) {
        while self.clip_depth > 0 {
            self.list.commands.push(PaintCommand::PopClip);
            self.clip_depth -= 1;
        }
    }

    /// 本次绘制已产生的命令数上限检查用:返回当前裁剪深度。
    pub fn clip_depth(&self) -> usize {
        self.clip_depth
    }
}