aboutsummaryrefslogtreecommitdiff
path: root/src/tool/polygon_room_tool.rs
blob: b37774b5ddc3c21394cb8fb2c0ddda6b984ad67c (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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
use super::Tool;
use crate::button::Button;
use crate::config::{PolygonRoomToolKeybindings, ToolKeybindings};
use crate::dimension_indicator::DimensionIndicator;
use crate::grid::{snap_to_grid, SNAP_SIZE};
use crate::map_data::MapData;
use crate::math::{self, Polygon, Vec2};
use crate::transform::Transform;
use raylib::core::drawing::{RaylibDraw, RaylibDrawHandle};
use raylib::ffi::Color;
use raylib::RaylibHandle;

pub struct PolygonRoomTool {
    keybindings: PolygonRoomToolKeybindings,
    unfinished_polygon: Option<Vec<Vec2<f32>>>,
    dimension_indicator: DimensionIndicator,
}

impl PolygonRoomTool {
    pub fn new(keybindings: PolygonRoomToolKeybindings) -> Self {
        Self {
            keybindings,
            unfinished_polygon: None,
            dimension_indicator: DimensionIndicator::new(),
        }
    }
}

impl Tool for PolygonRoomTool {
    fn activate(&mut self) {}

    fn deactivate(&mut self) {
        self.unfinished_polygon = None;
    }

    fn active_update(
        &mut self,
        map: &mut MapData,
        rl: &RaylibHandle,
        transform: &Transform,
        mouse_blocked: bool,
    ) {
        let mouse_pos_m = transform.point_px_to_m(rl.get_mouse_position().into());
        let snapped_mouse_pos_m = snap_to_grid(mouse_pos_m, SNAP_SIZE);
        // Update the position of the node that would be placed into the polygon next.
        if let Some(ref mut corners) = &mut self.unfinished_polygon {
            let last_element = corners.len() - 1;
            corners[last_element] = snapped_mouse_pos_m;
            self.dimension_indicator.update_dimensions(&corners);
        }

        if self.keybindings.finish.is_pressed(rl, mouse_blocked)
            && self.unfinished_polygon.is_some()
        {
            // Make sure the polygon is at least a triangle, so it can be drawn.
            if self.unfinished_polygon.as_ref().unwrap().len() >= 3 {
                let polygon = Polygon::new(self.unfinished_polygon.take().unwrap());
                self.dimension_indicator.clear_dimensions();
                map.polygons_mut().push(polygon);
            }
        }

        if self.keybindings.place_node.is_pressed(rl, mouse_blocked) {
            if let Some(ref mut corners) = self.unfinished_polygon.as_mut() {
                if snapped_mouse_pos_m == corners[0] {
                    // Make sure the polygon is at least a triangle, so it can be drawn.
                    if corners.len() >= 3 {
                        // The last corner is redundant.
                        corners.pop();
                        let polygon = Polygon::new(self.unfinished_polygon.take().unwrap());
                        self.dimension_indicator.clear_dimensions();
                        map.polygons_mut().push(polygon);
                    }
                } else {
                    corners.push(snapped_mouse_pos_m);
                }
            } else {
                self.unfinished_polygon = Some(vec![snapped_mouse_pos_m, snapped_mouse_pos_m]);
            }
        }

        if self.keybindings.abort.is_pressed(rl, false) {
            self.unfinished_polygon = None;
        }
    }

    fn draw(&self, map: &MapData, rld: &mut RaylibDrawHandle, transform: &Transform) {
        // TODO: Buffer triangles so the polygons don't always have to be retriangulated.
        for polygon in map.polygons() {
            let triangles = math::triangulate(polygon.clone());
            for triangle in triangles {
                let triangle: [Vec2<f32>; 3] = triangle.into();
                rld.draw_triangle(
                    transform.point_m_to_px(triangle[0]),
                    transform.point_m_to_px(triangle[1]),
                    transform.point_m_to_px(triangle[2]),
                    Color {
                        r: 180,
                        g: 180,
                        b: 180,
                        a: 255,
                    },
                )
            }
        }

        // Draw the current polygon
        if let Some(corners) = &self.unfinished_polygon {
            let mut corners = corners.clone();
            corners.dedup();
            match corners.len() {
                0 | 1 => {}
                2 => rld.draw_line_ex(
                    transform.point_m_to_px(corners[0]),
                    transform.point_m_to_px(corners[1]),
                    transform.length_m_to_px(0.1),
                    Color {
                        r: 150,
                        g: 200,
                        b: 150,
                        a: 255,
                    },
                ),
                _ => {
                    let polygon = Polygon::new(corners);
                    let triangles = math::triangulate(polygon);
                    for triangle in triangles {
                        let triangle: [Vec2<f32>; 3] = triangle.into();
                        rld.draw_triangle(
                            transform.point_m_to_px(triangle[0]),
                            transform.point_m_to_px(triangle[1]),
                            transform.point_m_to_px(triangle[2]),
                            Color {
                                r: 150,
                                g: 200,
                                b: 150,
                                a: 255,
                            },
                        )
                    }
                }
            }
            self.dimension_indicator.draw(rld, transform);
        }
    }

    fn activation_key(&self) -> Button {
        self.keybindings.activation_key()
    }
}