diff --git a/src/geometry/entity.rs b/src/geometry/entity.rs index d91b0bf..39eebcd 100644 --- a/src/geometry/entity.rs +++ b/src/geometry/entity.rs @@ -16,6 +16,10 @@ impl Default for EntityId { impl nohash_hasher::IsEnabled for EntityId {} impl EntityId { + pub fn id(&self) -> u32 { + self.0 + } + fn take_next(&mut self) -> Self { let id = *self; self.0 += 1; diff --git a/src/main.rs b/src/main.rs index a4185df..8b0632a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,7 +3,7 @@ use eframe::{ emath::{Pos2, Rect, RectTransform, Vec2}, epaint::{color::Hsva, Color32, Stroke}, }; -use geometry::{LineEntity, PointId, PointPos, SketchEntities}; +use geometry::{LineEntity, LineId, PointId, PointPos, SketchEntities}; mod geometry; mod optimization; @@ -37,6 +37,8 @@ struct ViewState { tool: Tool, hover_point: Option, select_point: Option, + hover_line: Option, + select_line: Option, drag_delta: Vec2, } @@ -102,9 +104,8 @@ impl MyApp { let transform_pos = |pos: &PointPos| to_screen * Pos2::new(pos.x as f32, pos.y as f32); - let mut hover_line = None; - state.hover_point = None; + state.hover_line = None; if let Some(hover_pos) = response.hover_pos() { for (id, point) in self @@ -133,7 +134,7 @@ impl MyApp { let hovered = ((0.)..=(1.)).contains(&p) && perp.length() < 5.0; if hovered { - hover_line = Some(id); + state.hover_line = Some(id); break; } } @@ -168,7 +169,13 @@ impl MyApp { match state.tool { Tool::Select => { if response.clicked() { - state.select_point = state.hover_point; + state.select_point = None; + state.select_line = None; + if state.hover_point.is_some() { + state.select_point = state.hover_point; + } else if state.hover_line.is_some() { + state.select_line = state.hover_line; + } } } Tool::Move => { @@ -240,7 +247,7 @@ impl MyApp { color.v -= 0.6; if state.hover_point.is_none() && (state.select_point.is_none() || state.tool != Tool::Move) - && Some(id) == hover_line + && (Some(id) == state.hover_line || Some(id) == state.select_line) { color.s -= 0.8; } @@ -266,6 +273,23 @@ impl MyApp { painter.circle(center, POINT_RADIUS, color, stroke); } } + + fn show_side_panel(&mut self, ui: &mut Ui) { + ui.vertical(|ui| match self.state.tool { + Tool::Select => { + if let Some(point_id) = self.state.select_point { + ui.label(format!("Selected point {}", point_id.id())); + } else if let Some(line_id) = self.state.select_line { + ui.label(format!("Selected line {}", line_id.id())); + } else { + ui.label("No selection"); + } + } + _ => { + ui.label(":)"); + } + }); + } } impl eframe::App for MyApp { @@ -285,14 +309,7 @@ impl eframe::App for MyApp { .resizable(true) .default_width(150.0) .width_range(80.0..=200.0) - .show(ctx, |ui| { - ui.vertical_centered(|ui| { - ui.heading("Left Panel"); - }); - // egui::ScrollArea::vertical().show(ui, |ui| { - // lorem_ipsum(ui); - // }); - }); + .show(ctx, |ui| self.show_side_panel(ui)); egui::TopBottomPanel::bottom("bottom_panel").show(ctx, |ui| { ui.horizontal(|ui| {