From 2db23f3213ff1bb2fe0cf005e922536da7ff5cd7 Mon Sep 17 00:00:00 2001 From: Arne Dußin Date: Sun, 1 Nov 2020 17:44:51 +0100 Subject: 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. --- src/tool/room_tool.rs | 25 +++++++++++-------------- 1 file changed, 11 insertions(+), 14 deletions(-) (limited to 'src/tool/room_tool.rs') diff --git a/src/tool/room_tool.rs b/src/tool/room_tool.rs index 10b9edf..0321ff3 100644 --- a/src/tool/room_tool.rs +++ b/src/tool/room_tool.rs @@ -1,35 +1,32 @@ use super::Tool; -use crate::math; +use crate::map_data::MapData; +use crate::math::{self, Rect, Vec2}; use crate::transform::Transform; use raylib::core::drawing::{RaylibDraw, RaylibDrawHandle}; use raylib::ffi::{Color, MouseButton}; -use raylib::math::{Rectangle, Vector2}; use raylib::RaylibHandle; pub struct RoomTool { - /// Vector of all Rectangles representing rooms that have already been drawn. - room_rects: Vec, /// 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<(Vector2, Vector2)>, + unfinished_rect: Option<(Vec2, Vec2)>, } impl RoomTool { /// Create a new room tool where no rooms have been drawn yet. pub fn new() -> Self { Self { - room_rects: Vec::new(), unfinished_rect: None, } } } impl Tool for RoomTool { - fn active_update(&mut self, rl: &RaylibHandle, transform: &Transform) { - let mouse_pos_m = transform.point_px_to_m(rl.get_mouse_position()); + 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 { - let snapped_mouse_pos = Vector2::new( + let snapped_mouse_pos = Vec2::new( math::round(mouse_pos_m.x, 0.5), math::round(mouse_pos_m.y, 0.5), ); @@ -39,10 +36,10 @@ impl Tool for RoomTool { // 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 { - self.room_rects.push(math::bounding_rect(pos1, pos2)); + map_data.rooms_mut().push(Rect::bounding_rect(pos1, pos2)); self.unfinished_rect = None; } else { - let snapped_mouse_pos = Vector2::new( + let snapped_mouse_pos = Vec2::new( math::round(mouse_pos_m.x, 0.5), math::round(mouse_pos_m.y, 0.5), ); @@ -56,9 +53,9 @@ impl Tool for RoomTool { } } - fn draw(&self, rld: &mut RaylibDrawHandle, transform: &Transform) { + fn draw(&self, map_data: &MapData, rld: &mut RaylibDrawHandle, transform: &Transform) { // Draw all finished rectangles. - for &rect in &self.room_rects { + for &rect in map_data.rooms() { rld.draw_rectangle_rec( transform.rect_m_to_px(rect), Color { @@ -73,7 +70,7 @@ impl Tool for RoomTool { // Do the same for the unfinished rectangle if let Some((pos1, pos2)) = self.unfinished_rect { rld.draw_rectangle_rec( - transform.rect_m_to_px(math::bounding_rect(pos1, pos2)), + transform.rect_m_to_px(Rect::bounding_rect(pos1, pos2)), Color { r: 150, g: 200, -- cgit v1.2.3-70-g09d2