aboutsummaryrefslogtreecommitdiff
path: root/src/tool/mod.rs
blob: da95c613b4143474aa63f8e96125a07e5e05dffb (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
pub mod deletion_tool;
pub mod room_tool;
pub mod wall_tool;

pub use deletion_tool::DeletionTool;
pub use room_tool::RoomTool;
pub use wall_tool::WallTool;

use crate::transform::Transform;
use raylib::core::drawing::RaylibDrawHandle;
use raylib::ffi::KeyboardKey;
use raylib::RaylibHandle;

#[derive(Debug)]
#[repr(u8)]
pub enum ToolType {
    RoomTool,
    WallTool,
    DeletionTool,
    NumTools,
}

pub trait Tool {
    fn update(&mut self, rl: &RaylibHandle, transform: &Transform) {}
    fn active_update(&mut self, rl: &RaylibHandle, transform: &Transform);

    fn draw(&self, _rld: &mut RaylibDrawHandle, _transform: &Transform) {}
}

pub struct ToolShed {
    tools: Vec<Box<dyn Tool>>,
    active: usize,
}

impl ToolShed {
    pub fn new() -> Self {
        let mut tools: Vec<Box<dyn Tool>> = Vec::with_capacity(ToolType::NumTools as usize);
        assert_eq!(ToolType::RoomTool as u8, 0);
        tools.push(Box::new(RoomTool::new()));
        assert_eq!(ToolType::WallTool as u8, 1);
        tools.push(Box::new(WallTool::new()));
        assert_eq!(ToolType::DeletionTool as u8, 2);
        tools.push(Box::new(DeletionTool::new()));

        assert_eq!(ToolType::NumTools as usize, tools.len());

        Self { tools, active: 0 }
    }

    pub fn update(&mut self, rl: &RaylibHandle, transform: &Transform) {
        // Handle keybindings for tool change
        self.active = if rl.is_key_pressed(KeyboardKey::KEY_R) {
            ToolType::RoomTool as usize
        } else if rl.is_key_pressed(KeyboardKey::KEY_W) {
            ToolType::WallTool as usize
        } else {
            self.active
        };

        for tool in &mut self.tools {
            tool.update(rl, transform);
        }

        self.tools[self.active].active_update(rl, transform);
    }

    pub fn draw_tools(&self, rld: &mut RaylibDrawHandle, transform: &Transform) {
        for tool in &self.tools {
            tool.draw(rld, transform);
        }
    }
}