diff options
Diffstat (limited to 'src/tool/icon_tool.rs')
| -rw-r--r-- | src/tool/icon_tool.rs | 69 |
1 files changed, 38 insertions, 31 deletions
diff --git a/src/tool/icon_tool.rs b/src/tool/icon_tool.rs index cf90056..d41d183 100644 --- a/src/tool/icon_tool.rs +++ b/src/tool/icon_tool.rs @@ -24,7 +24,7 @@ struct IconFileInfo { pixels_per_m: f32, } -#[derive(Serialize, Deserialize)] +#[derive(Clone, Serialize, Deserialize)] pub struct IconInfo { /// The id of the icon is the icons position in the currently loaded icon_data vector. pub icon_id: usize, @@ -40,9 +40,21 @@ pub struct IconTool { /// The icon data, containing the image texture and the info for that image texture like the /// scale the image actually has. icon_data: Vec<(Texture2D, IconFileInfo)>, + /// Saves whether the IconTool is the currently active tool or not. + active: bool, /// The information of the icon that should be placed / is currently being placed, if it /// exists. - current_icon: Option<IconInfo>, + current_icon: IconInfo, +} + +impl Default for IconInfo { + fn default() -> Self { + Self { + icon_id: 0, + position: Vec2::new(0., 0.), + rotation: 0., + } + } } impl IconTool { @@ -96,43 +108,38 @@ impl IconTool { Self { keybindings, icon_data, - current_icon: None, + active: false, + current_icon: IconInfo::default(), } } } impl Tool for IconTool { + fn activate(&mut self) { + self.active = true; + } + + fn deactivate(&mut self) { + self.active = false; + } + fn active_update(&mut self, map: &mut MapData, rl: &RaylibHandle, transform: &Transform) { - // Update the position of the icon that should be drawn to the current mouse position, or - // create a new default icon if there was `None`. + // Update the position of the icon that should be drawn to the current mouse position. let snapped_mouse_pos_m = snap_to_grid(transform.point_px_to_m(rl.get_mouse_position().into()), 0.5); - self.current_icon = match self.current_icon.take() { - Some(mut icon) => { - icon.position = snapped_mouse_pos_m; - Some(icon) - } - None => Some(IconInfo { - icon_id: 0, - position: snapped_mouse_pos_m, - rotation: 0., - }), - }; + self.current_icon.position = snapped_mouse_pos_m; - { - // Unwrap the current icon, since it is now definitely set, as we are in the active update. - let active_icon = self.current_icon.as_mut().unwrap(); - if self.keybindings.next.is_pressed(rl) { - active_icon.icon_id = (active_icon.icon_id + 1) % self.icon_data.len(); - } - if self.keybindings.rotate_clockwise.is_pressed(rl) { - active_icon.rotation += 45.; - } + // Unwrap the current icon, since it is now definitely set, as we are in the active update. + if self.keybindings.next.is_pressed(rl) { + self.current_icon.icon_id = (self.current_icon.icon_id + 1) % self.icon_data.len(); + } + if self.keybindings.rotate_clockwise.is_pressed(rl) { + self.current_icon.rotation += 45.; } // Handle placing the icon on the map if self.keybindings.place.is_pressed(rl) { - map.icons_mut().push(self.current_icon.take().unwrap()); + map.icons_mut().push(self.current_icon.clone()); } } @@ -160,17 +167,17 @@ impl Tool for IconTool { } // Draw the icon that would be placed - if let Some(current_icon) = &self.current_icon { - let (texture, info) = &self.icon_data[current_icon.icon_id]; + if self.active { + let (texture, info) = &self.icon_data[self.current_icon.icon_id]; // Round the position to whole pixels to fix rotation problems. - let mut position_px = - transform.point_m_to_px(current_icon.position - (info.anchor / info.pixels_per_m)); + let mut position_px = transform + .point_m_to_px(self.current_icon.position - (info.anchor / info.pixels_per_m)); position_px.x = position_px.x as i32 as f32; position_px.y = position_px.y as i32 as f32; rld.draw_texture_ex( texture, position_px, - current_icon.rotation, + self.current_icon.rotation, transform.pixels_per_m() / info.pixels_per_m, Color { r: 120, |
