aboutsummaryrefslogtreecommitdiff
path: root/src/tool/room_tool.rs
blob: 09126c9d0bb48069e13f7f030f153a7eb26bd73b (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
use super::Tool;
use crate::grid::snap_to_grid;
use crate::map_data::MapData;
use crate::math::{Rect, Vec2};
use crate::transform::Transform;
use raylib::core::drawing::{RaylibDraw, RaylibDrawHandle};
use raylib::ffi::{Color, MouseButton};
use raylib::RaylibHandle;

pub struct RoomTool {
    /// The rectangle that is currently being drawn by the user. Once it is finished, it will be
    /// pushed into the room_rects.
    unfinished_rect: Option<(Vec2<f32>, Vec2<f32>)>,
}

impl RoomTool {
    /// Create a new room tool where no rooms have been drawn yet.
    pub fn new() -> Self {
        Self {
            unfinished_rect: None,
        }
    }
}

impl Tool for RoomTool {
    fn active_update(&mut self, map_data: &mut MapData, rl: &RaylibHandle, transform: &Transform) {
        let mouse_pos_m = transform.point_px_to_m(rl.get_mouse_position().into());
        // Update the currently drawn rectangle, if it exists
        if let Some((_, ref mut pos2)) = &mut self.unfinished_rect {
            *pos2 = snap_to_grid(mouse_pos_m, 0.5);
        }

        // Start or finish drawing the currently unfinished rectangle
        if rl.is_mouse_button_pressed(MouseButton::MOUSE_LEFT_BUTTON) {
            if let Some((pos1, pos2)) = self.unfinished_rect {
                map_data.rooms_mut().push(Rect::bounding_rect(pos1, pos2));
                self.unfinished_rect = None;
            } else {
                let snapped_mouse_pos = snap_to_grid(mouse_pos_m, 0.5);
                self.unfinished_rect = Some((snapped_mouse_pos, snapped_mouse_pos))
            }
        }

        // Abort drawing the room (if any) in case the right mouse button was pressed.
        if rl.is_mouse_button_pressed(MouseButton::MOUSE_RIGHT_BUTTON) {
            self.unfinished_rect = None;
        }
    }

    fn draw(&self, map_data: &MapData, rld: &mut RaylibDrawHandle, transform: &Transform) {
        // Draw all finished rectangles.
        for &rect in map_data.rooms() {
            rld.draw_rectangle_rec(
                transform.rect_m_to_px(rect),
                Color {
                    r: 180,
                    g: 180,
                    b: 180,
                    a: 255,
                },
            );
        }

        // Do the same for the unfinished rectangle
        if let Some((pos1, pos2)) = self.unfinished_rect {
            rld.draw_rectangle_rec(
                transform.rect_m_to_px(Rect::bounding_rect(pos1, pos2)),
                Color {
                    r: 150,
                    g: 200,
                    b: 150,
                    a: 255,
                },
            );
        }
    }
}