aboutsummaryrefslogtreecommitdiff
path: root/src/editor.rs
diff options
context:
space:
mode:
authorArne Dußin2020-11-01 17:44:51 +0100
committerArne Dußin2020-11-01 17:44:51 +0100
commit2db23f3213ff1bb2fe0cf005e922536da7ff5cd7 (patch)
tree912b6d829ea8c8c28a2ed257e70e0fd6814bef50 /src/editor.rs
parenta6be14ada463cb5a3fc0a810b8b341093abbb68d (diff)
downloadgraf_karto-2db23f3213ff1bb2fe0cf005e922536da7ff5cd7.tar.gz
graf_karto-2db23f3213ff1bb2fe0cf005e922536da7ff5cd7.zip
Refactor a major part of the project
In order to be able to save and load the map, a major rework of the code seemed necessary, since Vector2 and Rectangle of raylib do not implement serialize, and it seems cleanest to use the serialize/deserialize traits of serde, to save for instance to RON. ToolShed was renamed to Editor, since it should better show, that it does quite a bit more than harbour tools. The map data is now centrally saved in the editor, instead of decentralised in the tool structs.
Diffstat (limited to 'src/editor.rs')
-rw-r--r--src/editor.rs59
1 files changed, 59 insertions, 0 deletions
diff --git a/src/editor.rs b/src/editor.rs
new file mode 100644
index 0000000..9dcbefc
--- /dev/null
+++ b/src/editor.rs
@@ -0,0 +1,59 @@
+use crate::map_data::MapData;
+use crate::tool::*;
+use crate::transform::Transform;
+use raylib::core::drawing::RaylibDrawHandle;
+use raylib::ffi::KeyboardKey;
+use raylib::RaylibHandle;
+
+pub struct Editor {
+ map_data: MapData,
+ tools: Vec<Box<dyn Tool>>,
+ active: usize,
+}
+
+impl Editor {
+ 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 {
+ map_data: MapData::new(),
+ 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(&self.map_data, rl, transform);
+ }
+
+ self.tools[self.active].active_update(&mut self.map_data, rl, transform);
+ }
+
+ 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
+ }
+}