1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
//! 矩形,由 (left, top, right, bottom) 定义。
//! Rectangle defined by (left, top, right, bottom) coordinates.
use crate::bridge::ffi;
/// 矩形,由 (left, top, right, bottom) 定义。
/// Rectangle defined by (left, top, right, bottom) coordinates.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Rect {
/// 左边界 / Left edge
pub left: f32,
/// 上边界 / Top edge
pub top: f32,
/// 右边界 / Right edge
pub right: f32,
/// 下边界 / Bottom edge
pub bottom: f32,
}
impl Rect {
/// 创建矩形。Creates a rectangle from bounds.
pub const fn new(left: f32, top: f32, right: f32, bottom: f32) -> Self {
Self {
left,
top,
right,
bottom,
}
}
/// 宽度。Returns the width.
pub fn width(&self) -> f32 {
self.right - self.left
}
/// 高度。Returns the height.
pub fn height(&self) -> f32 {
self.bottom - self.top
}
/// 是否为空矩形。Returns true if the rect has zero or negative width/height.
pub fn is_empty(&self) -> bool {
self.left >= self.right || self.top >= self.bottom
}
}
impl From<Rect> for ffi::Rect {
fn from(r: Rect) -> Self {
ffi::Rect {
fLeft: r.left,
fTop: r.top,
fRight: r.right,
fBottom: r.bottom,
}
}
}
impl From<ffi::Rect> for Rect {
fn from(r: ffi::Rect) -> Self {
Self {
left: r.fLeft,
top: r.fTop,
right: r.fRight,
bottom: r.fBottom,
}
}
}