Skip to main content

ferroui_controls/shapes/
line.rs

1use super::{Shape, ShapeImpl};
2use crate::ControlImpl;
3use ferroui_base::input::InputElementImpl;
4use ferroui_base::interactivity::InteractiveImpl;
5use ferroui_base::layout::LayoutableImpl;
6use ferroui_base::media::{Geometry, LineGeometry};
7use ferroui_base::{
8    ferro_class, ferro_impl_classes, ferro_property, instantiate, FerroObjectImpl, FerroProperty,
9    Point, Ref, StyledElementImpl, StyledProperty, VisualImpl,
10};
11
12/// Represents a straight line between two points.
13#[repr(C)]
14pub struct Line {
15    base: Shape,
16}
17
18ferro_class!(Line: Shape);
19ferroui_base::ferro_class_info!(Line { new: Line::new });
20ferro_impl_classes!(
21    Line: StyledElementImpl,
22    VisualImpl,
23    LayoutableImpl,
24    InteractiveImpl,
25    InputElementImpl,
26    ControlImpl
27);
28
29ferroui_base::ferro_impl_classes!(Line: FerroObjectImpl);
30
31impl ShapeImpl for Line {
32    fn create_defining_geometry(this: &Self) -> Option<Ref<Geometry>> {
33        Some(LineGeometry::with_points(this.start_point(), this.end_point()).upcast())
34    }
35}
36
37ferroui_base::ferro_properties! { impl Line {
38    ferro_property!(
39        /// Defines the `StartPoint` property.
40        pub fn start_point_property() -> StyledProperty<Point> {
41            FerroProperty::register::<Line, _>("StartPoint", Point::default())
42        }
43    );
44
45    ferro_property!(
46        /// Defines the `EndPoint` property.
47        pub fn end_point_property() -> StyledProperty<Point> {
48            FerroProperty::register::<Line, _>("EndPoint", Point::default())
49        }
50    );
51} }
52
53impl Line {
54    fn static_constructor() {
55        Shape::stroke_thickness_property().override_default_value::<Line>(1.0);
56        Shape::affects_geometry::<Line>(&[
57            Self::start_point_property().as_property(),
58            Self::end_point_property().as_property(),
59        ]);
60    }
61
62    /// Creates the class data; see [`ferroui_base::FerroObject::construct`].
63    pub fn construct() -> Self {
64        Self { base: Shape::construct() }
65    }
66
67    pub fn new() -> Ref<Self> {
68        instantiate(Self::construct())
69    }
70
71    /// The point at which the line starts.
72    pub fn start_point(&self) -> Point {
73        self.get_value(Self::start_point_property())
74    }
75
76    pub fn set_start_point(&self, value: Point) {
77        self.set_value(Self::start_point_property(), value)
78    }
79
80    /// The point at which the line ends.
81    pub fn end_point(&self) -> Point {
82        self.get_value(Self::end_point_property())
83    }
84
85    pub fn set_end_point(&self, value: Point) {
86        self.set_value(Self::end_point_property(), value)
87    }
88}