aboutsummaryrefslogtreecommitdiff
path: root/src/map/wall.rs
blob: d18096bb1f8570b97177359e5232bea8f4116a98 (plain) (blame)
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
use super::Mappable;
use crate::colours::DEFAULT_COLOURS;
use crate::math::{LineSegment, Rect, Vec2};
use crate::transform::Transform;
use crate::transformable::NonRigidTransformable;
use raylib::drawing::{RaylibDraw, RaylibDrawHandle};
use std::ops::{Deref, DerefMut};
use nalgebra::{Matrix3, Point2};

pub type WallData = LineSegment<f64>;

pub struct Wall {
    data: WallData,
    selected: bool,
    round_start: bool,
    round_end: bool,
}

impl Wall {
    pub fn from_data(data: WallData, round_start: bool, round_end: bool) -> Self {
        Self {
            data,
            selected: false,
            round_start,
            round_end,
        }
    }

    pub fn data(&self) -> &WallData {
        &self.data
    }
}

fn draw_round_corner(
    rld: &mut RaylibDrawHandle,
    pos_px: Vec2<f64>,
    transform: &Transform,
    selected: bool,
) {
    rld.draw_circle_v(
        pos_px,
        transform.length_m_to_px(0.05) as f32,
        if selected {
            DEFAULT_COLOURS.wall_selected
        } else {
            DEFAULT_COLOURS.wall_normal
        },
    );
}

impl Mappable for Wall {
    fn draw(&self, rld: &mut RaylibDrawHandle, transform: &Transform) {
        let start_px = transform.point_m_to_px(&self.data.start);
        let end_px = transform.point_m_to_px(&self.data.end);
        rld.draw_line_ex(
            start_px,
            end_px,
            transform.length_m_to_px(0.1) as f32,
            if self.selected() {
                DEFAULT_COLOURS.wall_selected
            } else {
                DEFAULT_COLOURS.wall_normal
            },
        );

        if self.round_start {
            draw_round_corner(rld, start_px, transform, self.selected());
        }
        if self.round_end {
            draw_round_corner(rld, end_px, transform, self.selected());
        }
    }

    fn set_selected(&mut self, selected: bool) {
        self.selected = selected;
    }

    fn selected(&self) -> bool {
        self.selected
    }

    fn bounding_rect(&self) -> Rect<f64> {
        Rect::bounding_rect(self.data.start, self.data.end)
    }

    fn as_non_rigid(&self) -> Option<&dyn NonRigidTransformable> {
        Some(self as &dyn NonRigidTransformable)
    }

    fn as_non_rigid_mut(&mut self) -> Option<&mut dyn NonRigidTransformable> {
        Some(self as &mut dyn NonRigidTransformable)
    }
}

impl NonRigidTransformable for Wall {
    fn apply_matrix(&mut self, matrix: &Matrix3<f64>) {
        self.data.start = matrix
            .transform_point(&self.data.start.into())
            .into();
        self.data.end = matrix
            .transform_point(&self.data.end.into())
            .into();
    }
}

impl Deref for Wall {
    type Target = WallData;

    fn deref(&self) -> &Self::Target {
        &self.data
    }
}

impl DerefMut for Wall {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.data
    }
}