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
|
use crate::config::Config;
use crate::map_data::MapData;
use crate::tool::*;
use crate::transform::Transform;
use raylib::core::drawing::RaylibDrawHandle;
use raylib::ffi::KeyboardKey;
use raylib::{RaylibHandle, RaylibThread};
use std::mem;
pub struct Editor {
map_data: MapData,
tools: Vec<Box<dyn Tool>>,
active: usize,
}
impl Editor {
pub fn new(rl: &mut RaylibHandle, rlt: &RaylibThread, config: Config) -> 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(config.room_keybindings)));
assert_eq!(ToolType::PolygonRoomTool as u8, 1);
tools.push(Box::new(PolygonRoomTool::new(config.polygon_keybindings)));
assert_eq!(ToolType::WallTool as u8, 2);
tools.push(Box::new(WallTool::new(config.wall_keybindings)));
assert_eq!(ToolType::IconTool as u8, 3);
tools.push(Box::new(IconTool::new(rl, rlt, config.icon_keybindings)));
assert_eq!(ToolType::DeletionTool as u8, 4);
tools.push(Box::new(DeletionTool::new(config.deletion_keybindings)));
assert_eq!(ToolType::NumTools as usize, tools.len());
Self {
map_data: MapData::new(),
tools,
active: 0,
}
}
/// Get the currently active tool.
pub fn active(&self) -> ToolType {
unsafe { mem::transmute(self.active as u8) }
}
/// Set the currently active tool. Any process currently going on in a different tool will be
/// aborted.
pub fn set_active(&mut self, tool: ToolType) {
if tool as usize != self.active {
self.tools[self.active].deactivate();
self.active = tool as usize;
self.tools[self.active].activate();
}
}
pub fn update(&mut self, rl: &RaylibHandle, transform: &Transform, mouse_blocked: bool) {
// Handle keybindings for tool change
for (i, tool) in self.tools.iter().enumerate() {
if tool.activation_key().is_pressed(rl, false) {
// Don't do anything if the tool does not change.
if i == self.active {
break;
}
// Activate the tool of which the key binding has been pressed.
self.set_active(unsafe { mem::transmute(i as u8) });
break;
}
}
// Handle saving and loading the editor contents to the swap file
if rl.is_key_pressed(KeyboardKey::KEY_S) {
self.map_data
.write_file("swap.ron")
.expect("Unable to write buffer file");
} else if rl.is_key_pressed(KeyboardKey::KEY_L) {
self.map_data
.load_file("swap.ron")
.expect("Unable to read buffer file");
}
for tool in &mut self.tools {
tool.update(&self.map_data, rl, transform);
}
self.tools[self.active].active_update(&mut self.map_data, rl, transform, mouse_blocked);
}
pub fn draw_tools(&self, rld: &mut RaylibDrawHandle, transform: &Transform) {
for tool in &self.tools {
tool.draw(&self.map_data, rld, transform);
}
}
pub fn map_data(&self) -> &MapData {
&self.map_data
}
}
|