From 82d11b7d3e15d8175accf7579db1fbe528fc6583 Mon Sep 17 00:00:00 2001 From: Arne Dußin Date: Wed, 16 Dec 2020 13:34:56 +0100 Subject: Add constant for default colours and selection tool --- src/tool/selection_tool.rs | 63 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 src/tool/selection_tool.rs (limited to 'src/tool/selection_tool.rs') diff --git a/src/tool/selection_tool.rs b/src/tool/selection_tool.rs new file mode 100644 index 0000000..49efba9 --- /dev/null +++ b/src/tool/selection_tool.rs @@ -0,0 +1,63 @@ +use super::Tool; +use crate::map::Map; +use crate::math::{Rect, Surface, Vec2}; +use crate::transform::Transform; +use raylib::core::drawing::{RaylibDraw, RaylibDrawHandle}; +use crate::colours::DEFAULT_COLOURS; + +pub struct SelectionTool { + selection_rect: Option<(Vec2, Vec2)>, +} + +impl SelectionTool { + pub fn new() -> Self { + Self { + selection_rect: None, + } + } +} + +impl Tool for SelectionTool { + fn deactivate(&mut self) { + self.selection_rect = None; + } + + fn update(&mut self, _map: &Map, mouse_pos_m: &Vec2) { + if let Some((_, ref mut pos2)) = &mut self.selection_rect { + *pos2 = *mouse_pos_m; + } + } + + fn draw(&self, rld: &mut RaylibDrawHandle, transform: &Transform) { + if let Some((pos1, pos2)) = self.selection_rect { + let rect_px = transform.rect_m_to_px(&Rect::bounding_rect(pos1, pos2)); + rld.draw_rectangle_rec( + rect_px, + DEFAULT_COLOURS.selection_rect + ); + rld.draw_rectangle_lines_ex( + rect_px, + 4, + DEFAULT_COLOURS.selection_rect_outline + ); + } + } + + fn place_single(&mut self, map: &mut Map, mouse_pos_m: &Vec2) { + if let Some((pos1, pos2)) = self.selection_rect { + // Select all items on the map that are inside of the selection rectangle + let bounds = Rect::bounding_rect(pos1, pos2); + for element in map.elements_mut() { + // TODO: Make it possible to do this additively by custom keybinding. + element.set_selected(bounds.contains_rect(&element.bounding_rect())); + } + self.selection_rect = None; + } else { + self.selection_rect = Some((*mouse_pos_m, *mouse_pos_m)); + } + } + + fn abort(&mut self) { + self.selection_rect = None; + } +} -- cgit v1.2.3-70-g09d2