Prepare view switch

This commit is contained in:
hodasemi 2023-05-14 09:27:16 +02:00
parent 63464c9be4
commit d1a3b9d734
3 changed files with 98 additions and 11 deletions

View file

@ -1,8 +1,20 @@
use bevy::prelude::*; use bevy::prelude::*;
#[derive(Resource, Default, Debug, PartialEq, Clone, Copy)] #[derive(Resource, Debug, PartialEq, Clone, Copy)]
pub struct GlobalState { pub struct GlobalState {
pub cursor_position: Vec2, pub cursor_position: Vec2,
pub window_extent: Vec2, pub window_extent: Vec2,
pub polygon_started: bool, 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,
}
}
} }

View file

@ -31,10 +31,38 @@ fn setup_camera(mut cmd: Commands) {
} }
// Create UI // Create UI
fn update_ui_side_panel(mut contexts: EguiContexts, mut global_state: ResMut<GlobalState>) { fn update_ui_side_panel(
egui::Window::new("Hello").show(contexts.ctx_mut(), |ui| { mut contexts: EguiContexts,
mut global_state: ResMut<GlobalState>,
mut object_infos: ResMut<ObjectInfos>,
) {
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 global_state.polygon_started {
// TODO 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 { } else {
if ui.button("New Polygon").clicked() { if ui.button("New Polygon").clicked() {
global_state.polygon_started = true; global_state.polygon_started = true;

View file

@ -1,3 +1,5 @@
use std::slice::IterMut;
use bevy::{ use bevy::{
prelude::*, prelude::*,
render::{mesh::Indices, render_resource::PrimitiveTopology}, 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)] #[derive(Default, Debug, PartialEq)]
pub struct Polygon { pub struct Polygon {
points: Vec<Vec2>, points: Vec<Vec2>,
state: PolygonState, state: PolygonView,
} }
impl Polygon { impl Polygon {
const PROXIMITY_THRESHOLD: f32 = 10.0; 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( pub fn add_point(
&mut self, &mut self,
global_state: &GlobalState, global_state: &GlobalState,
@ -205,4 +248,8 @@ impl ObjectInfos {
self.polygons.push(Polygon::default()); self.polygons.push(Polygon::default());
self.polygons.last_mut().unwrap() self.polygons.last_mut().unwrap()
} }
pub fn iter_mut(&mut self) -> IterMut<'_, Polygon> {
self.polygons.iter_mut()
}
} }