Skip to main content

mgui_core/
geometry.rs

1//! 几何类型 —— 原 mgui-geometry crate,已合入 mgui-core
2//!
3//! Point / Size / Rect / Color / Axis + Transform2D / RRectDef
4//! 所有类型都是 f32 基础,无外部 crate 依赖。
5
6// =================================================================
7// 基础几何类型
8// =================================================================
9
10/// 逻辑坐标点
11#[derive(Clone, Copy, Debug, Default, PartialEq)]
12pub struct Point {
13    pub x: f32,
14    pub y: f32,
15}
16
17impl Point {
18    pub const ZERO: Self = Self { x: 0.0, y: 0.0 };
19    pub fn new(x: f32, y: f32) -> Self {
20        Self { x, y }
21    }
22}
23
24impl std::ops::Add for Point {
25    type Output = Point;
26    fn add(self, rhs: Point) -> Point {
27        Point::new(self.x + rhs.x, self.y + rhs.y)
28    }
29}
30
31/// 尺寸
32#[derive(Clone, Copy, Debug, Default, PartialEq)]
33pub struct Size {
34    pub width: f32,
35    pub height: f32,
36}
37
38impl Size {
39    pub const ZERO: Self = Self { width: 0.0, height: 0.0 };
40    pub fn new(width: f32, height: f32) -> Self {
41        Self { width, height }
42    }
43}
44
45/// 矩形:左上角坐标 + 宽高
46#[derive(Clone, Copy, Debug, Default, PartialEq)]
47pub struct Rect {
48    pub x: f32,
49    pub y: f32,
50    pub width: f32,
51    pub height: f32,
52}
53
54impl Rect {
55    pub const ZERO: Self = Self { x: 0.0, y: 0.0, width: 0.0, height: 0.0 };
56    pub fn new(x: f32, y: f32, width: f32, height: f32) -> Self {
57        Self { x, y, width, height }
58    }
59
60    pub fn from_ltrb(left: f32, top: f32, right: f32, bottom: f32) -> Self {
61        Self { x: left, y: top, width: right - left, height: bottom - top }
62    }
63
64    pub fn right(&self) -> f32 {
65        self.x + self.width
66    }
67    pub fn bottom(&self) -> f32 {
68        self.y + self.height
69    }
70    pub fn size(&self) -> Size {
71        Size::new(self.width, self.height)
72    }
73
74    /// 命中测试:点是否在矩形内(包含边界)
75    pub fn contains(&self, p: Point) -> bool {
76        p.x >= self.x && p.x <= self.right() && p.y >= self.y && p.y <= self.bottom()
77    }
78}
79
80/// 颜色(0.0 - 1.0,sRGB 编码空间)
81#[derive(Clone, Copy, Debug, PartialEq)]
82pub struct Color {
83    pub r: f32,
84    pub g: f32,
85    pub b: f32,
86    pub a: f32,
87}
88
89impl Color {
90    pub const TRANSPARENT: Self = Self { r: 0.0, g: 0.0, b: 0.0, a: 0.0 };
91    pub const WHITE: Self = Self { r: 1.0, g: 1.0, b: 1.0, a: 1.0 };
92    pub const BLACK: Self = Self { r: 0.0, g: 0.0, b: 0.0, a: 1.0 };
93
94    pub const fn rgba(r: f32, g: f32, b: f32, a: f32) -> Self {
95        Self { r, g, b, a }
96    }
97
98    pub const fn rgb(r: f32, g: f32, b: f32) -> Self {
99        Self { r, g, b, a: 1.0 }
100    }
101
102    /// 从 0-255 的 sRGB 值构造
103    pub fn from_rgb8(r: u8, g: u8, b: u8) -> Self {
104        Self::rgb(r as f32 / 255.0, g as f32 / 255.0, b as f32 / 255.0)
105    }
106
107    pub fn from_rgba8(r: u8, g: u8, b: u8, a: u8) -> Self {
108        Self::rgba(r as f32 / 255.0, g as f32 / 255.0, b as f32 / 255.0, a as f32 / 255.0)
109    }
110}
111
112/// 滚动方向 / 主轴方向
113#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
114pub enum Axis {
115    #[default]
116    Vertical,
117    Horizontal,
118}
119
120impl Axis {
121    /// 在 Point 上主轴分量 (Vertical → y, Horizontal → x)
122    pub fn main(&self, p: Point) -> f32 {
123        match self {
124            Axis::Vertical => p.y,
125            Axis::Horizontal => p.x,
126        }
127    }
128    /// 在 Point 上交叉轴分量
129    pub fn cross(&self, p: Point) -> f32 {
130        match self {
131            Axis::Vertical => p.x,
132            Axis::Horizontal => p.y,
133        }
134    }
135    /// 在 Size 上主轴尺寸 (Vertical → height, Horizontal → width)
136    pub fn main_size(&self, s: Size) -> f32 {
137        match self {
138            Axis::Vertical => s.height,
139            Axis::Horizontal => s.width,
140        }
141    }
142    /// 交叉轴尺寸
143    pub fn cross_size(&self, s: Size) -> f32 {
144        match self {
145            Axis::Vertical => s.width,
146            Axis::Horizontal => s.height,
147        }
148    }
149}
150
151// =================================================================
152// Layer 合成相关几何类型
153// =================================================================
154
155/// 2D 仿射变换(平移+旋转+均匀缩放)
156///
157/// 实际应用矩阵(在 shader 中):
158/// - 绕 layer 中心 (cx, cy) 旋转 + 缩放
159/// - 之后平移 `translate` 像素
160///
161/// 数学:`world_p = R(rot) * S(sx, sy) * (local_p - center) + center + translate`
162///
163/// 两轴可独立缩放(`scale_x != scale_y` 即非均匀缩放),FittedBox 的
164/// `BoxFit::Fill` 依赖这一能力;均匀缩放场景下两者相等。
165#[derive(Clone, Copy, Debug, PartialEq)]
166pub struct Transform2D {
167    /// 旋转角度(弧度,顺时针)
168    pub rotation: f32,
169    /// x 轴缩放(1.0 = 原大小)
170    pub scale_x: f32,
171    /// y 轴缩放(1.0 = 原大小)
172    pub scale_y: f32,
173    /// 相对 layer 中心的额外平移(像素)
174    pub translate: Point,
175}
176
177impl Default for Transform2D {
178    fn default() -> Self {
179        Self { rotation: 0.0, scale_x: 1.0, scale_y: 1.0, translate: Point::ZERO }
180    }
181}
182
183impl Transform2D {
184    /// 恒等变换(等价无效果,GPU 走 noop)
185    pub const IDENTITY: Self = Self { rotation: 0.0, scale_x: 1.0, scale_y: 1.0, translate: Point::ZERO };
186
187    /// 是否是恒等变换(可走 shader 无开销分支)
188    pub fn is_identity(&self) -> bool {
189        self.rotation == 0.0 && self.scale_x == 1.0 && self.scale_y == 1.0 && self.translate == Point::ZERO
190    }
191
192    /// 构造平移
193    pub fn translate(x: f32, y: f32) -> Self {
194        Self { translate: Point::new(x, y), ..Self::IDENTITY }
195    }
196
197    /// 构造旋转(弧度)
198    pub fn rotate(angle: f32) -> Self {
199        Self { rotation: angle, ..Self::IDENTITY }
200    }
201
202    /// 构造均匀缩放
203    pub fn scale(s: f32) -> Self {
204        Self { scale_x: s, scale_y: s, ..Self::IDENTITY }
205    }
206
207    /// 构造非均匀缩放(两轴独立)
208    pub fn stretch(sx: f32, sy: f32) -> Self {
209        Self { scale_x: sx, scale_y: sy, ..Self::IDENTITY }
210    }
211}
212
213/// 圆角矩形定义(用于 Clip / 装饰)
214#[derive(Clone, Copy, Debug, PartialEq)]
215pub struct RRectDef {
216    /// 圆角半径(各角同,像素)
217    pub radius: f32,
218}
219
220impl RRectDef {
221    pub const fn new(radius: f32) -> Self {
222        Self { radius }
223    }
224}