aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorArne Dußin2020-11-10 11:52:56 +0100
committerArne Dußin2020-11-10 11:52:56 +0100
commitf2caebd11512fce214338b054c1b061b86d84aee (patch)
tree37c717b08f252f6e539d24545aac00b7f0e1ef2c
parent667a3c8335762aa626943a5ca0bcf64c653d0d0f (diff)
downloadgraf_karto-f2caebd11512fce214338b054c1b061b86d84aee.tar.gz
graf_karto-f2caebd11512fce214338b054c1b061b86d84aee.zip
Fix next icon still being drawn when tool is inactive
The icon that would be placed next, but is not on the map was always drawn once the icon-tool was activated once. This is no longer the case. Also, the tool now saves the last icon that was selected and keeps the rotation of it between placements, which I deemed more intuitive.
-rw-r--r--src/editor.rs10
-rw-r--r--src/tool/icon_tool.rs69
-rw-r--r--src/tool/mod.rs3
3 files changed, 51 insertions, 31 deletions
diff --git a/src/editor.rs b/src/editor.rs
index c756990..f8ee4bc 100644
--- a/src/editor.rs
+++ b/src/editor.rs
@@ -37,7 +37,17 @@ impl Editor {
// Handle keybindings for tool change
for (i, tool) in self.tools.iter().enumerate() {
if tool.activation_key().is_pressed(rl) {
+ // Don't do anything if the tool does not change.
+ if i == self.active {
+ break;
+ }
+
+ /* Deactivate the current tool and activate the tool, of which the keybinding has
+ * been pressed.
+ */
+ self.tools[self.active].deactivate();
self.active = i;
+ self.tools[self.active].activate();
break;
}
}
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,
diff --git a/src/tool/mod.rs b/src/tool/mod.rs
index 6b9eba7..3b9b845 100644
--- a/src/tool/mod.rs
+++ b/src/tool/mod.rs
@@ -25,6 +25,9 @@ pub enum ToolType {
}
pub trait Tool {
+ fn activate(&mut self) {}
+ fn deactivate(&mut self) {}
+
fn update(&mut self, _map: &MapData, _rl: &RaylibHandle, _transform: &Transform) {}
fn active_update(&mut self, map: &mut MapData, rl: &RaylibHandle, transform: &Transform);