diff options
| author | Arne Dußin | 2021-01-27 14:01:50 +0100 |
|---|---|---|
| committer | Arne Dußin | 2021-02-02 22:16:15 +0100 |
| commit | f92e9f6f07b1e3834c2ca58ce3510734819d08e4 (patch) | |
| tree | 20e3d3afce342a56ae98f6c20491482ccd2b5c6b /src/client/tool/selection_tool.rs | |
| parent | c60a6d07efb120724b308e29e8e70f27c87c952d (diff) | |
| download | graf_karto-f92e9f6f07b1e3834c2ca58ce3510734819d08e4.tar.gz graf_karto-f92e9f6f07b1e3834c2ca58ce3510734819d08e4.zip | |
Rework graf karto to fit the client/server structure
Diffstat (limited to 'src/client/tool/selection_tool.rs')
| -rw-r--r-- | src/client/tool/selection_tool.rs | 74 |
1 files changed, 74 insertions, 0 deletions
diff --git a/src/client/tool/selection_tool.rs b/src/client/tool/selection_tool.rs new file mode 100644 index 0000000..52c2155 --- /dev/null +++ b/src/client/tool/selection_tool.rs @@ -0,0 +1,74 @@ +//! Selection of items on the map. +//! +//! When selecting items on the map, the editor goes into a different mode than when editing a +//! specific kind of item. Actions that are available for specific types of items become +//! unavailable, while other actions that make use of the properties to a wide range of items +//! become available instead. +//! For this reason, the selection tool can be thought of as a kind of meta tool over tools. + +use super::Tool; +use crate::client::colours::DEFAULT_COLOURS; +use crate::client::map::Map; +use crate::client::transform::Transform; +use crate::math::{ExactSurface, Rect, Vec2}; +use crate::net::{Cargo, Connection}; +use raylib::core::drawing::{RaylibDraw, RaylibDrawHandle}; + +/// The selection tool makes it possible to select any item on the map when activated. +pub struct SelectionTool { + selection_rect: Option<(Vec2<f64>, Vec2<f64>)>, +} + +impl SelectionTool { + /// Create a new selection tool. There should be only one such tool per program instance and it + /// should be created in the editor. + #[allow(clippy::new_without_default)] + 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<f64>) { + 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, + _server: &Connection<Cargo>, + mouse_pos_m: &Vec2<f64>, + ) { + 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 (_id, element) in map.elements_mut() { + // TODO: Make it possible to do this additively by custom keybinding. + element.set_selected(bounds.contains_rect(&element.as_component().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; + } +} |
