Added layers and cameras, not working

This commit is contained in:
beffroi 2023-06-21 17:15:37 +02:00
parent cfffd3f98f
commit da9665db9a
1 changed files with 134 additions and 16 deletions

View File

@ -3,13 +3,14 @@
//! It displays the current FPS in the top left corner, as well as text that changes color //! It displays the current FPS in the top left corner, as well as text that changes color
//! in the bottom right. For text within a scene, please see the text2d example. //! in the bottom right. For text within a scene, please see the text2d example.
use std::time::Duration; use std::slice::Windows;
use bevy::{ use bevy::{
diagnostic::{Diagnostics, FrameTimeDiagnosticsPlugin}, diagnostic::FrameTimeDiagnosticsPlugin,
prelude::*, prelude::*,
render::{camera::RenderTarget, view::RenderLayers},
utils::Instant, utils::Instant,
window::WindowMode, window::{WindowMode, WindowRef},
}; };
fn main() { fn main() {
@ -19,17 +20,22 @@ fn main() {
position: WindowPosition::Centered(MonitorSelection::Index(1)), position: WindowPosition::Centered(MonitorSelection::Index(1)),
mode: WindowMode::Fullscreen, mode: WindowMode::Fullscreen,
present_mode: bevy::window::PresentMode::AutoNoVsync, present_mode: bevy::window::PresentMode::AutoNoVsync,
..default() ..default()
}), }),
..default() ..default()
}; };
App::new() App::new()
.add_plugins(DefaultPlugins.set(window_plugin)) .add_plugins(DefaultPlugins.set(window_plugin))
.add_plugin(FrameTimeDiagnosticsPlugin::default()) .add_plugin(FrameTimeDiagnosticsPlugin::default())
.init_resource::<ForgeTimer>() .init_resource::<ForgeTimer>()
.init_resource::<Preferences>()
.add_startup_system(setup) .add_startup_system(setup)
.add_system(bevy::window::close_on_esc) .add_system(bevy::window::close_on_esc)
.add_system(update_timer) .add_system(update_timer)
.add_system(keyboard_events)
.insert_resource(ClearColor(Color::rgb(0., 0., 0.)))
.run(); .run();
} }
@ -44,6 +50,9 @@ struct ColorText;
#[derive(Component)] #[derive(Component)]
struct ShowTimer; struct ShowTimer;
#[derive(Component)]
struct DelayTimer;
#[derive(Debug, Resource)] #[derive(Debug, Resource)]
struct ForgeTimer { struct ForgeTimer {
/// The actual ON/PAUSE timer /// The actual ON/PAUSE timer
@ -61,10 +70,19 @@ struct ForgeTimer {
started: bool, started: bool,
} }
#[derive(Debug, Resource, Default)]
struct Preferences {
/// Main window
main_window: Option<Entity>,
/// Preferences window
pref_window: Option<Entity>,
}
impl FromWorld for ForgeTimer { impl FromWorld for ForgeTimer {
fn from_world(_world: &mut World) -> Self { fn from_world(_world: &mut World) -> Self {
let time = Time::new(Instant::now()); let mut time = Time::new(Instant::now());
time.startup(); // time.startup();
time.pause();
ForgeTimer { ForgeTimer {
time_current: time, time_current: time,
time_target: 60 * 15 + 30, time_target: 60 * 15 + 30,
@ -109,11 +127,56 @@ fn sec_to_str(time: isize) -> String {
format!("{}{:02}:{:02}:{:02}", sign, h, m, s) format!("{}{:02}:{:02}:{:02}", sign, h, m, s)
} }
fn setup(mut commands: Commands, asset_server: Res<AssetServer>) { fn setup(mut commands: Commands, asset_server: Res<AssetServer>, mut pref: ResMut<Preferences>) {
// UI camera let timerlayer = RenderLayers::layer(1);
let preflayer = RenderLayers::layer(2);
// Spawn a first window
let first_window = commands
.spawn(Window {
title: "Fullscreen Test".into(),
position: WindowPosition::Centered(MonitorSelection::Index(1)),
mode: WindowMode::Fullscreen,
present_mode: bevy::window::PresentMode::AutoNoVsync,
..default()
})
.id();
// Spawn a second window
let second_window = commands
.spawn(Window {
position: WindowPosition::Centered(MonitorSelection::Index(0)),
title: "Second window".to_owned(),
..default()
})
.id();
pref.pref_window = Some(second_window);
pref.main_window = Some(second_window);
// commands.spawn((Camera2dBundle::default(), RenderLayers::from_layers(&[2])));
commands.spawn((
Camera2dBundle {
camera: Camera {
target: RenderTarget::Window(WindowRef::Entity(first_window)),
..default()
},
..default()
},
// DelayTimer,
// preflayer,
RenderLayers::from_layers(&[1]),
));
commands.spawn((
Camera2dBundle {
camera: Camera {
target: RenderTarget::Window(WindowRef::Entity(second_window)),
..default()
},
..default()
},
// DelayTimer,
// preflayer,
RenderLayers::from_layers(&[1]),
));
// let mut forge_time = bevy::prelude::Time::default();
commands.spawn(Camera2dBundle::default());
commands.spawn(( commands.spawn((
TextBundle::from_sections([ TextBundle::from_sections([
TextSection::new( TextSection::new(
@ -141,6 +204,37 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
}) })
.with_text_alignment(TextAlignment::Center), .with_text_alignment(TextAlignment::Center),
ShowTimer, ShowTimer,
timerlayer,
));
commands.spawn((
TextBundle::from_sections([
TextSection::new(
"EL TIMO: ",
TextStyle {
font: asset_server.load("fonts/digital-7.ttf"),
font_size: 60.,
color: Color::GREEN,
},
),
TextSection::from_style(TextStyle {
font: asset_server.load("fonts/digital-7.ttf"),
font_size: 60.,
color: Color::GREEN,
}),
])
.with_style(Style {
position_type: PositionType::Absolute,
position: UiRect {
bottom: Val::Px(500.0),
right: Val::Px(150.0),
..default()
},
..default()
})
.with_text_alignment(TextAlignment::Center),
DelayTimer,
preflayer,
)); ));
} }
@ -151,10 +245,34 @@ fn update_timer(mut tmr: ResMut<ForgeTimer>, mut query: Query<&mut Text, With<Sh
} }
} }
// fn sec_to_time(time: Duration) -> String { fn keyboard_events(
// let time = time.as_secs(); keys: Res<Input<KeyCode>>,
// let s = time % 60; mut wind: Query<&mut Window>,
// let m = (time / 60) % 3600; pref: Res<Preferences>,
// let h = time / 3600; mut tmr: ResMut<ForgeTimer>,
// format!("{:02}:{:02}:{:02}", h, m, s) ) {
// } if keys.just_pressed(KeyCode::Space) {
if tmr.started {
tmr.started = false;
tmr.time_current.pause();
// let winwin = wind.iter_mut();
if let Some(w) = pref.pref_window {
if let Ok(w) = wind.get(w) {
println!("{:?}", w);
} else {
// println!("Nowi");
}
} else {
// println!("No prefw");
}
println!("Stopped");
} else {
tmr.started = true;
// tmr.time_current.startup();
tmr.time_current.unpause();
println!("Started");
}
}
if keys.just_pressed(KeyCode::R) {}
}