feather_ui/render/domain/
line.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: 2025 Fundament Research Institute <https://fundament.institute>
3
4use crate::color::sRGB;
5use crate::render::compositor::{self, DataFlags};
6use crate::{CrossReferenceDomain, SourceID};
7
8use std::sync::Arc;
9
10pub struct Instance {
11    pub domain: Arc<CrossReferenceDomain>,
12    pub start: Arc<SourceID>,
13    pub end: Arc<SourceID>,
14    pub color: sRGB,
15}
16
17impl super::Renderable for Instance {
18    fn render(
19        &self,
20        _: crate::PxRect,
21        _: &crate::graphics::Driver,
22        compositor: &mut compositor::CompositorView<'_>,
23    ) -> Result<(), crate::Error> {
24        let domain = self.domain.clone();
25        let start_id = self.start.clone();
26        let end_id = self.end.clone();
27        let color = self.color.as_32bit();
28
29        compositor.defer(move |_, data| {
30            let start = domain.get_area(&start_id).unwrap_or_default();
31            let end = domain.get_area(&end_id).unwrap_or_default();
32
33            let p1 = (start.topleft() + start.bottomright().to_vector()) * 0.5;
34            let p2 = (end.topleft() + end.bottomright().to_vector()) * 0.5;
35            let p = p2 - p1;
36
37            *data = compositor::Data {
38                pos: (((p1 + p2.to_vector()) * 0.5)
39                    - (crate::PxVector::new(p.length() * 0.5, 0.0)))
40                .to_array()
41                .into(),
42                dim: [p.length(), 1.0].into(),
43                uv: [0.0, 0.0].into(),
44                uvdim: [0.0, 0.0].into(),
45                color: color.rgba,
46                rotation: p.y.atan2(p.x) % std::f32::consts::TAU,
47                flags: DataFlags::new().with_tex(u8::MAX).into(),
48                ..Default::default()
49            };
50        });
51
52        Ok(())
53    }
54}