Start parallel view

This commit is contained in:
hodasemi 2023-05-15 06:00:46 +02:00
parent 1da3a67046
commit 64d474694f
2 changed files with 26 additions and 94 deletions

View file

@ -1,39 +1,5 @@
use bevy::prelude::*;
#[derive(Debug, PartialEq, Clone)]
pub enum Camera {
TwoD(Entity),
ThreeD(Entity),
}
impl Camera {
pub fn is_3d(&self) -> bool {
match self {
Camera::TwoD(_) => false,
Camera::ThreeD(_) => true,
}
}
pub fn is_2d(&self) -> bool {
!self.is_3d()
}
fn create_2d(cmd: &mut Commands) -> Self {
Self::TwoD(cmd.spawn(Camera2dBundle::default()).id())
}
fn create_3d(cmd: &mut Commands, window_extent: Vec2) -> Self {
Self::ThreeD(
cmd.spawn(Camera3dBundle {
transform: Transform::from_xyz(200.0, -600.0, 600.0)
.looking_at(window_extent.extend(0.0), Vec3::Z),
..default()
})
.id(),
)
}
}
#[derive(Resource, Debug, PartialEq, Clone)]
pub struct GlobalState {
pub cursor_position: Vec2,
@ -42,36 +8,34 @@ pub struct GlobalState {
pub view_2d: bool,
pub extrusion_height: f32,
pub camera: Option<Camera>,
pub offscreen_image
camera_3d: Option<Entity>,
camera_2d: Option<Entity>,
}
impl GlobalState {
pub fn setup_2d_cam(&mut self, cmd: &mut Commands) {
match &mut self.camera {
Some(camera) => match camera {
Camera::TwoD(_) => (),
Camera::ThreeD(cam) => {
cmd.entity(*cam).despawn();
*camera = Camera::create_2d(cmd);
}
},
None => self.camera = Some(Camera::create_2d(cmd)),
}
self.camera_2d = Some(cmd.spawn(Camera2dBundle::default()).id());
}
pub fn setup_3d_cam(&mut self, cmd: &mut Commands) {
match &mut self.camera {
Some(camera) => match camera {
Camera::TwoD(cam) => {
cmd.entity(*cam).despawn();
self.camera_3d = Some(
cmd.spawn(Camera3dBundle {
transform: Transform::from_xyz(200.0, -600.0, 600.0)
.looking_at((self.window_extent * 0.5).extend(0.0), Vec3::Z),
..default()
})
.id(),
);
}
*camera = Camera::create_3d(cmd, self.window_extent);
}
Camera::ThreeD(_) => (),
},
None => self.camera = Some(Camera::create_3d(cmd, self.window_extent)),
pub fn camera_2d(&self) -> Entity {
self.camera_2d.unwrap()
}
pub fn camera_3d(&self) -> Entity {
self.camera_3d.unwrap()
}
}
@ -84,43 +48,10 @@ impl Default for GlobalState {
view_2d: true,
extrusion_height: 20.0,
camera: None,
camera_3d: None,
camera_2d: None,
}
}
}
#[cfg(test)]
mod test {
use bevy::{ecs::system::CommandQueue, prelude::*};
use super::GlobalState;
#[test]
fn camera_conversion() {
let world = World::new();
let mut command_queue = CommandQueue::default();
let mut cmd = Commands::new(&mut command_queue, &world);
// default global state
let mut global_state = GlobalState::default();
// create 2d cam
global_state.setup_2d_cam(&mut cmd);
assert!(global_state.camera.is_some());
assert!(global_state.camera.as_ref().unwrap().is_2d());
// switch to 3d cam
global_state.setup_3d_cam(&mut cmd);
assert!(global_state.camera.is_some());
assert!(global_state.camera.as_ref().unwrap().is_3d());
// switch to 2d cam
global_state.setup_2d_cam(&mut cmd);
assert!(global_state.camera.is_some());
assert!(global_state.camera.as_ref().unwrap().is_2d());
}
}
#[derive()]

View file

@ -22,14 +22,15 @@ fn main() {
.add_plugin(WorldInspectorPlugin::new())
.insert_resource(GlobalState::default())
.insert_resource(ObjectInfos::default())
.add_startup_system(setup_camera)
.add_startup_system(setup_cameras)
.add_systems((mouse_event_system, update_ui_side_panel, on_resize_system))
.run();
}
// Spawns the camera
fn setup_camera(mut cmd: Commands, mut global_state: ResMut<GlobalState>) {
fn setup_cameras(mut cmd: Commands, mut global_state: ResMut<GlobalState>) {
global_state.setup_2d_cam(&mut cmd);
global_state.setup_3d_cam(&mut cmd);
}
// Create UI