diff --git a/src/global_state.rs b/src/global_state.rs index da99ca2..f2b88b0 100644 --- a/src/global_state.rs +++ b/src/global_state.rs @@ -1,8 +1,20 @@ use bevy::prelude::*; -#[derive(Resource, Default, Debug, PartialEq, Clone, Copy)] +#[derive(Resource, Debug, PartialEq, Clone, Copy)] pub struct GlobalState { pub cursor_position: Vec2, pub window_extent: Vec2, pub polygon_started: bool, + pub view_2d: bool, +} + +impl Default for GlobalState { + fn default() -> Self { + Self { + cursor_position: Vec2::default(), + window_extent: Vec2::default(), + polygon_started: false, + view_2d: true, + } + } } diff --git a/src/main.rs b/src/main.rs index 7468d2c..609cd26 100644 --- a/src/main.rs +++ b/src/main.rs @@ -31,16 +31,44 @@ fn setup_camera(mut cmd: Commands) { } // Create UI -fn update_ui_side_panel(mut contexts: EguiContexts, mut global_state: ResMut) { - egui::Window::new("Hello").show(contexts.ctx_mut(), |ui| { - if global_state.polygon_started { - // TODO - } else { - if ui.button("New Polygon").clicked() { - global_state.polygon_started = true; +fn update_ui_side_panel( + mut contexts: EguiContexts, + mut global_state: ResMut, + mut object_infos: ResMut, +) { + egui::SidePanel::right("2D View") + .default_width(400.0) + .show(contexts.ctx_mut(), |ui| { + if global_state.view_2d { + if ui.button("Change to 3D View").clicked() { + global_state.view_2d = false; + + object_infos + .iter_mut() + .for_each(|polygon| polygon.swap_to_3d()); + } + } else { + if ui.button("Change to 2D View").clicked() { + global_state.view_2d = true; + + object_infos + .iter_mut() + .for_each(|polygon| polygon.swap_to_2d()); + } } - } - }); + + if global_state.polygon_started { + if let Some(polygon) = object_infos.active_polygon() { + for (index, point) in polygon.points().iter().enumerate() { + ui.label(&format!("{}: ({}, {})", index, point.x, point.y)); + } + } + } else { + if ui.button("New Polygon").clicked() { + global_state.polygon_started = true; + } + } + }); } /// Handles all mouse events diff --git a/src/polygon.rs b/src/polygon.rs index 6c16c87..cc4b0a8 100644 --- a/src/polygon.rs +++ b/src/polygon.rs @@ -1,3 +1,5 @@ +use std::slice::IterMut; + use bevy::{ prelude::*, render::{mesh::Indices, render_resource::PrimitiveTopology}, @@ -33,15 +35,56 @@ impl Default for PolygonState { } } +#[derive(Debug, PartialEq)] +enum PolygonView { + TwoDimensional(PolygonState), + ThreeDimensional(Entity), +} + +impl PolygonView { + fn is_2d(&self) -> bool { + match self { + PolygonView::TwoDimensional(_) => true, + PolygonView::ThreeDimensional(_) => false, + } + } + + fn is_3d(&self) -> bool { + !self.is_2d() + } +} + +impl Default for PolygonView { + fn default() -> Self { + Self::TwoDimensional(PolygonState::default()) + } +} + #[derive(Default, Debug, PartialEq)] pub struct Polygon { points: Vec, - state: PolygonState, + state: PolygonView, } impl Polygon { const PROXIMITY_THRESHOLD: f32 = 10.0; + pub fn swap_to_3d(&mut self) { + debug_assert!(self.state.is_2d()); + + todo!() + } + + pub fn swap_to_2d(&mut self) { + debug_assert!(self.state.is_3d()); + + todo!() + } + + pub fn points(&self) -> &[Vec2] { + &self.points + } + pub fn add_point( &mut self, global_state: &GlobalState, @@ -205,4 +248,8 @@ impl ObjectInfos { self.polygons.push(Polygon::default()); self.polygons.last_mut().unwrap() } + + pub fn iter_mut(&mut self) -> IterMut<'_, Polygon> { + self.polygons.iter_mut() + } }