diff --git a/src/editor.rs b/src/editor.rs index c68dc74..37c114d 100644 --- a/src/editor.rs +++ b/src/editor.rs @@ -78,10 +78,16 @@ enum Tool { Wire, Arrow, Mirror, - SelectArea(Option<(Pos, Pos)>, bool), + SelectArea(Selection), Blueprint, } +#[derive(Debug, Clone, Default)] +struct Selection { + area: Option<(Pos, Pos)>, + is_selecting: bool, +} + #[derive(Debug, Clone, PartialEq)] enum SimState { Editing, @@ -279,7 +285,10 @@ impl Editor { pos.x += x; pos.y += y; } - Tool::SelectArea(Some((start, end)), _) => { + Tool::SelectArea(Selection { + area: Some((start, end)), + is_selecting: _, + }) => { start.x += x; start.y += y; end.x += x; @@ -634,7 +643,11 @@ impl Editor { ); let mut hide_tile_tools = false; - if let Tool::SelectArea(Some(selection), _) = self.active_tool { + if let Tool::SelectArea(Selection { + area: Some(selection), + is_selecting: _, + }) = self.active_tool + { hide_tile_tools = true; text_input( d, @@ -649,9 +662,20 @@ impl Editor { } draw_scaled_texture(d, textures.get("save"), 104, footer_top as i32 + 53, 2.); if simple_button(d, 144, footer_top as i32 + 49, 40, 40) { - self.active_tool = Tool::SelectArea(None, false); + self.active_tool = Tool::SelectArea(Selection::default()); } draw_scaled_texture(d, textures.get("cancel"), 148, footer_top as i32 + 53, 2.); + + // if simple_button(d, 144, footer_top as i32 + 49, 40, 40) { + // self.active_tool = Tool::SelectArea(Selection::default()); + // } + // draw_scaled_texture( + // d, + // textures.get("direction_up"), + // 148, + // footer_top as i32 + 53, + // 2., + // ); } let mouse_pos = d.get_mouse_position(); @@ -682,7 +706,7 @@ impl Editor { } }; tool_button((0, -2), "eraser", Tool::Erase); - tool_button((1, -2), "selection", Tool::SelectArea(None, false)); + tool_button((1, -2), "selection", Tool::SelectArea(Selection::default())); tool_button((0, -1), "blueprint", Tool::Blueprint); tool_button((1, -1), "transparent", Tool::None); @@ -841,9 +865,9 @@ impl Editor { let tile_screen_pos = self.pos_to_screen(tile_pos); if self.active_tool != Tool::None { - let tex = match self.active_tool { + let tex = match &self.active_tool { Tool::None => unreachable!(), - Tool::Erase => "selection".into(), + Tool::Erase => "cancel".into(), Tool::SetTile(t) => t.texture(), Tool::Math => format!("{}_off", self.tool_math.texture_name()), Tool::Gate => format!("{}_off", self.tool_gate.texture_name()), @@ -851,8 +875,13 @@ impl Editor { Tool::Arrow => self.tool_arrow.arrow_tile_texture_name().into(), Tool::Mirror => self.tool_mirror.texture_name().into(), Tool::Digits(_) => "selection".into(), - Tool::SelectArea(_, false) => "area_full".into(), - Tool::SelectArea(_, true) => "transparent".into(), + Tool::SelectArea(selection) => { + if selection.is_selecting { + "transparent".into() + } else { + "area_full".into() + } + } Tool::Blueprint => "transparent".into(), }; @@ -910,7 +939,7 @@ impl Editor { } } } - Tool::SelectArea(_, _) => (), + Tool::SelectArea(_) => (), } } if d.is_mouse_button_down(MouseButton::MOUSE_BUTTON_LEFT) @@ -918,20 +947,20 @@ impl Editor { { self.set_tile(tile_pos.into(), Tile::Blank) } - if let Tool::SelectArea(selection, is_selecting) = &mut self.active_tool { + if let Tool::SelectArea(selection) = &mut self.active_tool { if d.is_mouse_button_down(MouseButton::MOUSE_BUTTON_LEFT) { - if *is_selecting { - if let Some((_start, end)) = selection { + if selection.is_selecting { + if let Some((_start, end)) = &mut selection.area { *end = tile_pos.into(); } else { - *selection = Some((tile_pos.into(), tile_pos.into())); + selection.area = Some((tile_pos.into(), tile_pos.into())); } } else { - *selection = Some((tile_pos.into(), tile_pos.into())); - *is_selecting = true; + selection.area = Some((tile_pos.into(), tile_pos.into())); + selection.is_selecting = true; } } else if d.is_mouse_button_released(MouseButton::MOUSE_BUTTON_LEFT) { - *is_selecting = false; + selection.is_selecting = false; } } if let Tool::Blueprint = self.active_tool { @@ -958,7 +987,11 @@ impl Editor { } } // draw selection - if let Tool::SelectArea(Some((start, end)), _) = self.active_tool { + if let Tool::SelectArea(Selection { + area: Some((start, end)), + is_selecting: _, + }) = self.active_tool + { let min = start.min(end); let max = start.max(end); let p_min = self.pos_to_screen(min.to_vec()); @@ -981,7 +1014,7 @@ impl PartialEq for Tool { match (self, other) { (Self::SetTile(l0), Self::SetTile(r0)) => l0 == r0, (Self::Digits(_), Self::Digits(_)) => true, - (Self::SelectArea(_, _), Self::SelectArea(_, _)) => true, + (Self::SelectArea(_), Self::SelectArea(_)) => true, _ => ::core::mem::discriminant(self) == ::core::mem::discriminant(other), } }