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

pub struct WallTool {
    walls: Vec<(Vector2, Vector2)>,
    unfinished_wall: Option<(Vector2, Vector2)>,
}

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

impl Tool for WallTool {
    fn active_update(&mut self, rl: &RaylibHandle, transform: &Transform) {
        let mouse_pos_m = transform.point_px_to_m(rl.get_mouse_position());
        if let Some((_, ref mut pos2)) = &mut self.unfinished_wall {
            let snapped_mouse_pos = Vector2::new(
                math::round(mouse_pos_m.x, 0.5),
                math::round(mouse_pos_m.y, 0.5),
            );
            *pos2 = snapped_mouse_pos;
        }

        if rl.is_mouse_button_pressed(MouseButton::MOUSE_LEFT_BUTTON) {
            if let Some((pos1, pos2)) = self.unfinished_wall {
                self.walls.push((pos1, pos2));
                self.unfinished_wall = None;
            } else {
                let snapped_mouse_pos = Vector2::new(
                    math::round(mouse_pos_m.x, 0.5),
                    math::round(mouse_pos_m.y, 0.5),
                );
                self.unfinished_wall = Some((snapped_mouse_pos, snapped_mouse_pos))
            }
        }

        if rl.is_mouse_button_pressed(MouseButton::MOUSE_RIGHT_BUTTON) {
            self.unfinished_wall = None;
        }
    }

    fn draw(&self, rld: &mut RaylibDrawHandle, transform: &Transform) {
        for &(pos1, pos2) in &self.walls {
            rld.draw_line_ex(
                transform.point_m_to_px(pos1),
                transform.point_m_to_px(pos2),
                5.,
                Color {
                    r: 200,
                    g: 120,
                    b: 120,
                    a: 255,
                },
            );
        }

        if let Some((pos1, pos2)) = self.unfinished_wall {
            rld.draw_line_ex(
                transform.point_m_to_px(pos1),
                transform.point_m_to_px(pos2),
                5.,
                Color {
                    r: 150,
                    g: 200,
                    b: 150,
                    a: 255,
                },
            );
        }
    }
}