aboutsummaryrefslogtreecommitdiff
path: root/src_testbed/objects/polyline.rs
blob: 98a8f24353dc1515df8e149164f99453c4f14c61 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
use kiss3d::window::Window;
use na::Point3;
use rapier::geometry::{ColliderHandle, ColliderSet};
use rapier::math::{Isometry, Point};

pub struct Polyline {
    color: Point3<f32>,
    base_color: Point3<f32>,
    vertices: Vec<Point<f32>>,
    indices: Vec<[u32; 2]>,
    collider: ColliderHandle,
    pos: Isometry<f32>,
}

impl Polyline {
    pub fn new(
        collider: ColliderHandle,
        vertices: Vec<Point<f32>>,
        indices: Vec<[u32; 2]>,
        color: Point3<f32>,
    ) -> Polyline {
        Polyline {
            color,
            pos: Isometry::identity(),
            base_color: color,
            vertices,
            indices,
            collider,
        }
    }

    pub fn select(&mut self) {
        self.color = Point3::new(1.0, 0.0, 0.0);
    }

    pub fn unselect(&mut self) {
        self.color = self.base_color;
    }

    pub fn set_color(&mut self, color: Point3<f32>) {
        self.color = color;
        self.base_color = color;
    }

    pub fn update(&mut self, colliders: &ColliderSet) {
        self.pos = colliders
            .get(self.collider)
            .map(|c| *c.position())
            .unwrap_or(Isometry::identity());
    }

    pub fn object(&self) -> ColliderHandle {
        self.collider
    }

    pub fn draw(&mut self, window: &mut Window) {
        for idx in &self.indices {
            let p1 = self.pos * self.vertices[idx[0] as usize];
            let p2 = self.pos * self.vertices[idx[1] as usize];

            #[cfg(feature = "dim2")]
            window.draw_planar_line(&p1, &p2, &self.color);
            #[cfg(feature = "dim3")]
            window.draw_line(&p1, &p2, &self.color);
        }
    }
}