aboutsummaryrefslogtreecommitdiff
path: root/src/tool/wall_tool.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/tool/wall_tool.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/tool/wall_tool.rs')
-rw-r--r--src/tool/wall_tool.rs36
1 files changed, 19 insertions, 17 deletions
diff --git a/src/tool/wall_tool.rs b/src/tool/wall_tool.rs
index 21eb895..a1d3c15 100644
--- a/src/tool/wall_tool.rs
+++ b/src/tool/wall_tool.rs
@@ -1,30 +1,28 @@
use super::Tool;
-use crate::math;
+use crate::map_data::MapData;
+use crate::math::{self, Vec2};
use crate::transform::Transform;
use raylib::core::drawing::{RaylibDraw, RaylibDrawHandle};
-use raylib::ffi::{Color, MouseButton};
-use raylib::math::Vector2;
+use raylib::ffi::{Color, MouseButton, Vector2};
use raylib::RaylibHandle;
pub struct WallTool {
- walls: Vec<(Vector2, Vector2)>,
- unfinished_wall: Option<(Vector2, Vector2)>,
+ unfinished_wall: Option<(Vec2<f32>, Vec2<f32>)>,
}
impl WallTool {
pub fn new() -> Self {
Self {
- walls: Vec::new(),
unfinished_wall: None,
}
}
}
impl Tool for WallTool {
- 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());
if let Some((_, ref mut pos2)) = &mut self.unfinished_wall {
- 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),
);
@@ -33,10 +31,10 @@ impl Tool for WallTool {
if rl.is_mouse_button_pressed(MouseButton::MOUSE_LEFT_BUTTON) {
if let Some((pos1, pos2)) = self.unfinished_wall {
- self.walls.push((pos1, pos2));
+ map_data.walls_mut().push((pos1, pos2));
self.unfinished_wall = 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),
);
@@ -49,11 +47,13 @@ impl Tool for WallTool {
}
}
- fn draw(&self, rld: &mut RaylibDrawHandle, transform: &Transform) {
- for &(pos1, pos2) in &self.walls {
+ fn draw(&self, map_data: &MapData, rld: &mut RaylibDrawHandle, transform: &Transform) {
+ for &(pos1, pos2) in map_data.walls() {
+ let pos1: Vector2 = transform.point_m_to_px(pos1).into();
+ let pos2: Vector2 = transform.point_m_to_px(pos2).into();
rld.draw_line_ex(
- transform.point_m_to_px(pos1),
- transform.point_m_to_px(pos2),
+ pos1,
+ pos2,
5.,
Color {
r: 200,
@@ -65,9 +65,11 @@ impl Tool for WallTool {
}
if let Some((pos1, pos2)) = self.unfinished_wall {
+ let pos1: Vector2 = transform.point_m_to_px(pos1).into();
+ let pos2: Vector2 = transform.point_m_to_px(pos2).into();
rld.draw_line_ex(
- transform.point_m_to_px(pos1),
- transform.point_m_to_px(pos2),
+ pos1,
+ pos2,
5.,
Color {
r: 150,