Skip to main content

ferroui_base/media/
line_geometry.rs

1use crate::media::{Geometry, GeometryImpl};
2use crate::platform::{self, IGeometryImpl};
3use crate::{
4    ferro_class, ferro_property, instantiate, FerroObjectImpl, FerroProperty, Point, Ref,
5    StyledProperty,
6};
7use std::sync::Arc;
8
9/// Represents the geometry of a line.
10#[repr(C)]
11pub struct LineGeometry {
12    base: Geometry,
13}
14
15ferro_class!(LineGeometry: Geometry);
16crate::ferro_class_info!(LineGeometry { new: LineGeometry::new });
17
18crate::ferro_impl_classes!(LineGeometry: FerroObjectImpl);
19
20impl GeometryImpl for LineGeometry {
21    fn clone_geometry(this: &Self) -> Ref<Geometry> {
22        LineGeometry::with_points(this.start_point(), this.end_point()).upcast()
23    }
24
25    fn create_defining_geometry(this: &Self) -> Option<Arc<dyn IGeometryImpl>> {
26        let factory = platform::render_interface();
27
28        Some(factory.create_line_geometry(this.start_point(), this.end_point()))
29    }
30}
31
32crate::ferro_properties! { impl LineGeometry {
33    ferro_property!(pub fn start_point_property() -> StyledProperty<Point> {
34        FerroProperty::register::<LineGeometry, _>("StartPoint", Point::default())
35    });
36
37    ferro_property!(pub fn end_point_property() -> StyledProperty<Point> {
38        FerroProperty::register::<LineGeometry, _>("EndPoint", Point::default())
39    });
40} }
41
42impl LineGeometry {
43    fn static_constructor() {
44        Geometry::affects_geometry(&[
45            Self::start_point_property().as_property(),
46            Self::end_point_property().as_property(),
47        ]);
48    }
49
50    /// Creates the class data.
51    pub fn construct() -> Self {
52        Self { base: Geometry::construct() }
53    }
54
55    pub fn new() -> Ref<Self> {
56        instantiate(Self::construct())
57    }
58
59    /// Creates a line geometry between the given points.
60    pub fn with_points(start_point: Point, end_point: Point) -> Ref<Self> {
61        let result = Self::new();
62        result.set_start_point(start_point);
63        result.set_end_point(end_point);
64        result
65    }
66
67    /// The start point of the line.
68    pub fn start_point(&self) -> Point {
69        self.get_value(Self::start_point_property())
70    }
71
72    pub fn set_start_point(&self, value: Point) {
73        self.set_value(Self::start_point_property(), value)
74    }
75
76    /// The end point of the line.
77    pub fn end_point(&self) -> Point {
78        self.get_value(Self::end_point_property())
79    }
80
81    pub fn set_end_point(&self, value: Point) {
82        self.set_value(Self::end_point_property(), value)
83    }
84}