aboutsummaryrefslogtreecommitdiff
path: root/src/tool/deletion_tool.rs
diff options
context:
space:
mode:
authorArne Dußin2020-12-15 00:46:54 +0100
committerArne Dußin2020-12-15 22:51:46 +0100
commit9799d3c6a8f0c242668203a1c70d7b6cfed3e855 (patch)
tree9116acbc886f680f82309a42b4e6147e65c1433b /src/tool/deletion_tool.rs
parent3bc690803fb59493ea8180fd630d65b3e26642d0 (diff)
downloadgraf_karto-9799d3c6a8f0c242668203a1c70d7b6cfed3e855.tar.gz
graf_karto-9799d3c6a8f0c242668203a1c70d7b6cfed3e855.zip
Refactor to make interaction between tools easier
Diffstat (limited to 'src/tool/deletion_tool.rs')
-rw-r--r--src/tool/deletion_tool.rs75
1 files changed, 27 insertions, 48 deletions
diff --git a/src/tool/deletion_tool.rs b/src/tool/deletion_tool.rs
index 74a8f92..ad3af1d 100644
--- a/src/tool/deletion_tool.rs
+++ b/src/tool/deletion_tool.rs
@@ -1,41 +1,25 @@
use super::Tool;
-use crate::button::Button;
-use crate::config::{DeletionToolKeybindings, ToolKeybindings};
-use crate::map_data::MapData;
+use crate::map::Map;
use crate::math::{Rect, Surface, Vec2};
use crate::transform::Transform;
use raylib::core::drawing::{RaylibDraw, RaylibDrawHandle};
use raylib::ffi::Color;
-use raylib::RaylibHandle;
pub struct DeletionTool {
- keybindings: DeletionToolKeybindings,
deletion_rect: Option<(Vec2<f64>, Vec2<f64>)>,
}
impl DeletionTool {
- pub fn new(keybindings: DeletionToolKeybindings) -> Self {
+ pub fn new() -> Self {
Self {
- keybindings,
deletion_rect: None,
}
}
+}
- /// Delete all map-data that is contained inside the provided rectangular space.
- pub fn delete_rect(map_data: &mut MapData, rect: Rect<f64>) {
- map_data
- .rooms_mut()
- .retain(|&room| !rect.contains_rect(&room));
- map_data
- .walls_mut()
- .retain(|&(pos1, pos2)| !rect.contains_point(&pos1) || !rect.contains_point(&pos2));
- map_data
- .icons_mut()
- .retain(|icon| !rect.contains_point(&icon.position));
- map_data
- .polygons_mut()
- .retain(|polygon| !rect.contains_polygon(&polygon));
- }
+fn delete_rect((pos1, pos2): (&Vec2<f64>, &Vec2<f64>), map: &mut Map) {
+ let bounds = Rect::bounding_rect(*pos1, *pos2);
+ map.retain(|e| !bounds.contains_rect(&e.bounding_rect()));
}
impl Tool for DeletionTool {
@@ -43,34 +27,13 @@ impl Tool for DeletionTool {
self.deletion_rect = None;
}
- fn active_update(
- &mut self,
- map_data: &mut MapData,
- rl: &RaylibHandle,
- transform: &Transform,
- mouse_blocked: bool,
- ) {
- let mouse_pos_m = transform.point_px_to_m(&rl.get_mouse_position().into());
+ fn update(&mut self, _map: &Map, mouse_pos_m: &Vec2<f64>) {
if let Some((_, ref mut pos2)) = &mut self.deletion_rect {
- *pos2 = mouse_pos_m;
- }
-
- if self.keybindings.do_delete.is_pressed(rl, mouse_blocked) && self.deletion_rect.is_some()
- {
- let (pos1, pos2) = self.deletion_rect.take().unwrap();
- Self::delete_rect(map_data, Rect::bounding_rect(pos1, pos2));
- } else if self
- .keybindings
- .start_selection
- .is_pressed(rl, mouse_blocked)
- {
- self.deletion_rect = Some((mouse_pos_m, mouse_pos_m))
- } else if self.keybindings.abort_deletion.is_pressed(rl, false) {
- self.deletion_rect = None;
+ *pos2 = *mouse_pos_m;
}
}
- fn draw(&self, _map_data: &MapData, rld: &mut RaylibDrawHandle, transform: &Transform) {
+ fn draw(&self, rld: &mut RaylibDrawHandle, transform: &Transform) {
if let Some((pos1, pos2)) = self.deletion_rect {
let rect_px = transform.rect_m_to_px(&Rect::bounding_rect(pos1, pos2));
rld.draw_rectangle_rec(
@@ -95,7 +58,23 @@ impl Tool for DeletionTool {
}
}
- fn activation_key(&self) -> Button {
- self.keybindings.activation_key()
+ fn place_single(&mut self, map: &mut Map, mouse_pos_m: &Vec2<f64>) {
+ if let Some((pos1, pos2)) = self.deletion_rect {
+ delete_rect((&pos1, &pos2), map);
+ self.deletion_rect = None;
+ } else {
+ self.deletion_rect = Some((*mouse_pos_m, *mouse_pos_m));
+ }
+ }
+
+ fn finish(&mut self, map: &mut Map) {
+ if let Some((pos1, pos2)) = self.deletion_rect {
+ delete_rect((&pos1, &pos2), map);
+ self.deletion_rect = None;
+ }
+ }
+
+ fn abort(&mut self) {
+ self.deletion_rect = None;
}
}