1 use std::sync::Arc;
2
3 use crate::{
4 base::{panel::Panel, rect::Rect, vector2::Vector2},
5 widgets::{PivotType, Widget},
6 };
7 /// # 函数功能
8 /// 根据pivot和offset来进行矩形位置的对齐
9 ///
10 /// ## 参数
11 /// - origin_rect: 待对齐的矩形
12 /// - relative_rect: 作为对齐参考的矩形
13 /// - pivot: 对齐方式
14 /// - pivot_offset: 偏移量
15 ///
16 /// ## 返回值
17 /// 对齐后的矩形
align_rect( origin_rect: Rect, relative_rect: Rect, pivot: PivotType, pivot_offset: Vector2, ) -> Rect18 pub fn align_rect(
19 origin_rect: Rect,
20 relative_rect: Rect,
21 pivot: PivotType,
22 pivot_offset: Vector2,
23 ) -> Rect {
24 let relative_pos = match pivot {
25 PivotType::None => Vector2::new(0, 0),
26 PivotType::Bottom => relative_rect.bottom_pos(),
27 PivotType::BottomLeft => relative_rect.bottom_left_pos(),
28 PivotType::BottomRight => relative_rect.bottom_right_pos(),
29 PivotType::Center => relative_rect.center_pos(),
30 PivotType::Top => relative_rect.top_pos(),
31 PivotType::TopLeft => relative_rect.top_left_pos(),
32 PivotType::TopRight => relative_rect.top_right_pos(),
33 PivotType::Left => relative_rect.left_pos(),
34 PivotType::Right => relative_rect.right_pos(),
35 };
36
37 let mut target_pos = relative_pos + pivot_offset;
38
39 let negative_width = -(origin_rect.width as i32);
40 let negative_height = -(origin_rect.height as i32);
41 let offset_vec = match pivot {
42 PivotType::None => Vector2::new(0, 0),
43 PivotType::Bottom => Vector2::new(negative_width / 2, negative_height),
44 PivotType::BottomLeft => Vector2::new(0, negative_height),
45 PivotType::BottomRight => Vector2::new(negative_width, negative_height),
46 PivotType::Center => Vector2::new(negative_width / 2, negative_height / 2),
47 PivotType::Top => Vector2::new(negative_width / 2, 0),
48 PivotType::TopLeft => Vector2::new(0, 0),
49 PivotType::TopRight => Vector2::new(negative_width, 0),
50 PivotType::Left => Vector2::new(0, negative_height / 2),
51 PivotType::Right => Vector2::new(negative_width, negative_height / 2),
52 };
53
54 target_pos = target_pos + offset_vec;
55
56 Rect::new(
57 target_pos.x,
58 target_pos.y,
59 origin_rect.width,
60 origin_rect.height,
61 )
62 }
63
64 /// # 函数功能
65 /// 获得局部坐标系下的矩形区域
66 ///
67 /// ## 参数
68 /// source_rect: 原来的矩形区域
69 /// target_rect: 作为参考的矩形
70 ///
71 /// ## 返回值
72 /// 根据参考计算局部位置后的矩形区域
get_local_rect(source_rect: Rect, target_rect: Rect) -> Rect73 pub fn get_local_rect(source_rect: Rect, target_rect: Rect) -> Rect {
74 Rect::new(
75 source_rect.x - target_rect.x,
76 source_rect.y - target_rect.y,
77 source_rect.width,
78 source_rect.height,
79 )
80 }
81
82 // TODO 注释补充
widget_set_panel(widget: &Arc<dyn Widget>, panel: &Arc<Panel>)83 pub fn widget_set_panel(widget: &Arc<dyn Widget>, panel: &Arc<Panel>) {
84 (*widget.panel().borrow_mut()) = Some(panel.clone());
85
86 for child in widget.children().borrow().iter() {
87 widget_set_panel(child, panel);
88 }
89 }
90