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