aboutsummaryrefslogtreecommitdiff
path: root/src/tool/wall_tool.rs
blob: e6503ba4c05a1196b67f9d6e0def6e52c481ca30 (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
use super::Tool;
use crate::button::Button;
use crate::config::{ToolKeybindings, WallToolKeybindings};
use crate::grid::{snap_to_grid, SNAP_SIZE};
use crate::map_data::MapData;
use crate::math::Vec2;
use crate::transform::Transform;
use raylib::core::drawing::{RaylibDraw, RaylibDrawHandle};
use raylib::ffi::{Color, Vector2};
use raylib::RaylibHandle;

pub struct WallTool {
    keybindings: WallToolKeybindings,
    unfinished_wall: Option<(Vec2<f64>, Vec2<f64>)>,
}

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

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

    fn active_update(
        &mut self,
        map_data: &mut MapData,
        rl: &RaylibHandle,
        transform: &Transform,
        mouse_blocked: bool,
    ) {
        let mouse_pos_m = transform.point_px_to_m(&rl.get_mouse_position().into());
        if let Some((_, ref mut pos2)) = &mut self.unfinished_wall {
            *pos2 = snap_to_grid(mouse_pos_m, SNAP_SIZE);
        }

        if self
            .keybindings
            .finish_segment
            .is_pressed(rl, mouse_blocked)
            && self.unfinished_wall.is_some()
        {
            let (pos1, pos2) = self.unfinished_wall.unwrap();
            map_data.walls_mut().push((pos1, pos2));
            self.unfinished_wall = Some((pos2, pos2));
        } else if self.keybindings.start_wall.is_pressed(rl, mouse_blocked) {
            let snapped_mouse_pos = snap_to_grid(mouse_pos_m, SNAP_SIZE);
            self.unfinished_wall = Some((snapped_mouse_pos, snapped_mouse_pos))
        }

        if self.keybindings.abort_segment.is_pressed(rl, false) {
            self.unfinished_wall = None;
        }
    }

    fn draw(&self, map_data: &MapData, rld: &mut RaylibDrawHandle, transform: &Transform) {
        for &(pos1, pos2) in map_data.walls() {
            let pos1: Vector2 = transform.point_m_to_px(&pos1).into();
            let pos2: Vector2 = transform.point_m_to_px(&pos2).into();
            rld.draw_line_ex(
                pos1,
                pos2,
                transform.length_m_to_px(0.1) as f32,
                Color {
                    r: 200,
                    g: 120,
                    b: 120,
                    a: 255,
                },
            );
        }

        if let Some((pos1, pos2)) = self.unfinished_wall {
            let pos1: Vector2 = transform.point_m_to_px(&pos1).into();
            let pos2: Vector2 = transform.point_m_to_px(&pos2).into();
            rld.draw_line_ex(
                pos1,
                pos2,
                transform.length_m_to_px(0.1) as f32,
                Color {
                    r: 150,
                    g: 200,
                    b: 150,
                    a: 255,
                },
            );
        }
    }

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