Prepare view switch
This commit is contained in:
parent
63464c9be4
commit
d1a3b9d734
3 changed files with 98 additions and 11 deletions
|
@ -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,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
46
src/main.rs
46
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<GlobalState>) {
|
||||
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<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 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
|
||||
|
|
|
@ -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<Vec2>,
|
||||
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()
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue