aboutsummaryrefslogtreecommitdiff
path: root/src/tool/wall_tool.rs
blob: b958799064456d9cfcb24037042347545beaef08 (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
use super::Tool;
use crate::map::Map;
use crate::math::{LineSegment, Vec2};
use crate::transform::Transform;
use raylib::core::drawing::{RaylibDraw, RaylibDrawHandle};
use raylib::ffi::{Color, Vector2};

pub struct WallTool {
    unfinished_wall: Option<LineSegment<f64>>,
}

impl WallTool {
    pub fn new() -> Self {
        Self {
            unfinished_wall: None,
        }
    }
}

impl Tool for WallTool {
    fn deactivate(&mut self) {
        self.unfinished_wall = None;
    }

    fn update(&mut self, _map: &Map, mouse_pos_m: &Vec2<f64>) {
        if let Some(ref mut wall) = &mut self.unfinished_wall {
            wall.end = *mouse_pos_m;
        }
    }

    fn draw(&self, rld: &mut RaylibDrawHandle, transform: &Transform) {
        if let Some(ref wall) = self.unfinished_wall {
            let start: Vector2 = transform.point_m_to_px(&wall.start).into();
            let end: Vector2 = transform.point_m_to_px(&wall.end).into();
            rld.draw_line_ex(
                start,
                end,
                transform.length_m_to_px(0.1) as f32,
                Color {
                    r: 150,
                    g: 200,
                    b: 150,
                    a: 255,
                },
            );
        }
    }

    fn place_single(&mut self, map: &mut Map, mouse_pos_m: &Vec2<f64>) {
        if let Some(wall) = self.unfinished_wall.take() {
            // Continue with the next wall straight away.
            self.unfinished_wall = Some(LineSegment::new(wall.end, wall.end));
            map.push_wall(wall);
        } else {
            self.unfinished_wall = Some(LineSegment::new(*mouse_pos_m, *mouse_pos_m));
        }
    }

    fn abort(&mut self) {
        self.unfinished_wall = None;
    }
}