mgui-core 0.2.0

mgui 框架核心:Element/RenderObject/Widget 三树、布局与绘制协议、帧管线与响应式原语
Documentation
//! 几何类型 —— 原 mgui-geometry crate,已合入 mgui-core
//!
//! Point / Size / Rect / Color / Axis + Transform2D / RRectDef
//! 所有类型都是 f32 基础,无外部 crate 依赖。

// =================================================================
// 基础几何类型
// =================================================================

/// 逻辑坐标点
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct Point {
    pub x: f32,
    pub y: f32,
}

impl Point {
    pub const ZERO: Self = Self { x: 0.0, y: 0.0 };
    pub fn new(x: f32, y: f32) -> Self {
        Self { x, y }
    }
}

impl std::ops::Add for Point {
    type Output = Point;
    fn add(self, rhs: Point) -> Point {
        Point::new(self.x + rhs.x, self.y + rhs.y)
    }
}

/// 尺寸
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct Size {
    pub width: f32,
    pub height: f32,
}

impl Size {
    pub const ZERO: Self = Self { width: 0.0, height: 0.0 };
    pub fn new(width: f32, height: f32) -> Self {
        Self { width, height }
    }
}

/// 矩形:左上角坐标 + 宽高
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct Rect {
    pub x: f32,
    pub y: f32,
    pub width: f32,
    pub height: f32,
}

impl Rect {
    pub const ZERO: Self = Self { x: 0.0, y: 0.0, width: 0.0, height: 0.0 };
    pub fn new(x: f32, y: f32, width: f32, height: f32) -> Self {
        Self { x, y, width, height }
    }

    pub fn from_ltrb(left: f32, top: f32, right: f32, bottom: f32) -> Self {
        Self { x: left, y: top, width: right - left, height: bottom - top }
    }

    pub fn right(&self) -> f32 {
        self.x + self.width
    }
    pub fn bottom(&self) -> f32 {
        self.y + self.height
    }
    pub fn size(&self) -> Size {
        Size::new(self.width, self.height)
    }

    /// 命中测试:点是否在矩形内(包含边界)
    pub fn contains(&self, p: Point) -> bool {
        p.x >= self.x && p.x <= self.right() && p.y >= self.y && p.y <= self.bottom()
    }
}

/// 颜色(0.0 - 1.0,sRGB 编码空间)
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Color {
    pub r: f32,
    pub g: f32,
    pub b: f32,
    pub a: f32,
}

impl Color {
    pub const TRANSPARENT: Self = Self { r: 0.0, g: 0.0, b: 0.0, a: 0.0 };
    pub const WHITE: Self = Self { r: 1.0, g: 1.0, b: 1.0, a: 1.0 };
    pub const BLACK: Self = Self { r: 0.0, g: 0.0, b: 0.0, a: 1.0 };

    pub const fn rgba(r: f32, g: f32, b: f32, a: f32) -> Self {
        Self { r, g, b, a }
    }

    pub const fn rgb(r: f32, g: f32, b: f32) -> Self {
        Self { r, g, b, a: 1.0 }
    }

    /// 从 0-255 的 sRGB 值构造
    pub fn from_rgb8(r: u8, g: u8, b: u8) -> Self {
        Self::rgb(r as f32 / 255.0, g as f32 / 255.0, b as f32 / 255.0)
    }

    pub fn from_rgba8(r: u8, g: u8, b: u8, a: u8) -> Self {
        Self::rgba(r as f32 / 255.0, g as f32 / 255.0, b as f32 / 255.0, a as f32 / 255.0)
    }
}

/// 滚动方向 / 主轴方向
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum Axis {
    #[default]
    Vertical,
    Horizontal,
}

impl Axis {
    /// 在 Point 上主轴分量 (Vertical → y, Horizontal → x)
    pub fn main(&self, p: Point) -> f32 {
        match self {
            Axis::Vertical => p.y,
            Axis::Horizontal => p.x,
        }
    }
    /// 在 Point 上交叉轴分量
    pub fn cross(&self, p: Point) -> f32 {
        match self {
            Axis::Vertical => p.x,
            Axis::Horizontal => p.y,
        }
    }
    /// 在 Size 上主轴尺寸 (Vertical → height, Horizontal → width)
    pub fn main_size(&self, s: Size) -> f32 {
        match self {
            Axis::Vertical => s.height,
            Axis::Horizontal => s.width,
        }
    }
    /// 交叉轴尺寸
    pub fn cross_size(&self, s: Size) -> f32 {
        match self {
            Axis::Vertical => s.width,
            Axis::Horizontal => s.height,
        }
    }
}

// =================================================================
// Layer 合成相关几何类型
// =================================================================

/// 2D 仿射变换(平移+旋转+均匀缩放)
///
/// 实际应用矩阵(在 shader 中):
/// - 绕 layer 中心 (cx, cy) 旋转 + 缩放
/// - 之后平移 `translate` 像素
///
/// 数学:`world_p = R(rot) * S(sx, sy) * (local_p - center) + center + translate`
///
/// 两轴可独立缩放(`scale_x != scale_y` 即非均匀缩放),FittedBox 的
/// `BoxFit::Fill` 依赖这一能力;均匀缩放场景下两者相等。
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Transform2D {
    /// 旋转角度(弧度,顺时针)
    pub rotation: f32,
    /// x 轴缩放(1.0 = 原大小)
    pub scale_x: f32,
    /// y 轴缩放(1.0 = 原大小)
    pub scale_y: f32,
    /// 相对 layer 中心的额外平移(像素)
    pub translate: Point,
}

impl Default for Transform2D {
    fn default() -> Self {
        Self { rotation: 0.0, scale_x: 1.0, scale_y: 1.0, translate: Point::ZERO }
    }
}

impl Transform2D {
    /// 恒等变换(等价无效果,GPU 走 noop)
    pub const IDENTITY: Self = Self { rotation: 0.0, scale_x: 1.0, scale_y: 1.0, translate: Point::ZERO };

    /// 是否是恒等变换(可走 shader 无开销分支)
    pub fn is_identity(&self) -> bool {
        self.rotation == 0.0 && self.scale_x == 1.0 && self.scale_y == 1.0 && self.translate == Point::ZERO
    }

    /// 构造平移
    pub fn translate(x: f32, y: f32) -> Self {
        Self { translate: Point::new(x, y), ..Self::IDENTITY }
    }

    /// 构造旋转(弧度)
    pub fn rotate(angle: f32) -> Self {
        Self { rotation: angle, ..Self::IDENTITY }
    }

    /// 构造均匀缩放
    pub fn scale(s: f32) -> Self {
        Self { scale_x: s, scale_y: s, ..Self::IDENTITY }
    }

    /// 构造非均匀缩放(两轴独立)
    pub fn stretch(sx: f32, sy: f32) -> Self {
        Self { scale_x: sx, scale_y: sy, ..Self::IDENTITY }
    }
}

/// 圆角矩形定义(用于 Clip / 装饰)
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct RRectDef {
    /// 圆角半径(各角同,像素)
    pub radius: f32,
}

impl RRectDef {
    pub const fn new(radius: f32) -> Self {
        Self { radius }
    }
}