Added layers and cameras, not working
This commit is contained in:
parent
cfffd3f98f
commit
da9665db9a
150
src/main.rs
150
src/main.rs
|
|
@ -3,13 +3,14 @@
|
|||
//! 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.
|
||||
|
||||
use std::time::Duration;
|
||||
use std::slice::Windows;
|
||||
|
||||
use bevy::{
|
||||
diagnostic::{Diagnostics, FrameTimeDiagnosticsPlugin},
|
||||
diagnostic::FrameTimeDiagnosticsPlugin,
|
||||
prelude::*,
|
||||
render::{camera::RenderTarget, view::RenderLayers},
|
||||
utils::Instant,
|
||||
window::WindowMode,
|
||||
window::{WindowMode, WindowRef},
|
||||
};
|
||||
|
||||
fn main() {
|
||||
|
|
@ -19,17 +20,22 @@ fn main() {
|
|||
position: WindowPosition::Centered(MonitorSelection::Index(1)),
|
||||
mode: WindowMode::Fullscreen,
|
||||
present_mode: bevy::window::PresentMode::AutoNoVsync,
|
||||
|
||||
..default()
|
||||
}),
|
||||
|
||||
..default()
|
||||
};
|
||||
App::new()
|
||||
.add_plugins(DefaultPlugins.set(window_plugin))
|
||||
.add_plugin(FrameTimeDiagnosticsPlugin::default())
|
||||
.init_resource::<ForgeTimer>()
|
||||
.init_resource::<Preferences>()
|
||||
.add_startup_system(setup)
|
||||
.add_system(bevy::window::close_on_esc)
|
||||
.add_system(update_timer)
|
||||
.add_system(keyboard_events)
|
||||
.insert_resource(ClearColor(Color::rgb(0., 0., 0.)))
|
||||
.run();
|
||||
}
|
||||
|
||||
|
|
@ -44,6 +50,9 @@ struct ColorText;
|
|||
#[derive(Component)]
|
||||
struct ShowTimer;
|
||||
|
||||
#[derive(Component)]
|
||||
struct DelayTimer;
|
||||
|
||||
#[derive(Debug, Resource)]
|
||||
struct ForgeTimer {
|
||||
/// The actual ON/PAUSE timer
|
||||
|
|
@ -61,10 +70,19 @@ struct ForgeTimer {
|
|||
started: bool,
|
||||
}
|
||||
|
||||
#[derive(Debug, Resource, Default)]
|
||||
struct Preferences {
|
||||
/// Main window
|
||||
main_window: Option<Entity>,
|
||||
/// Preferences window
|
||||
pref_window: Option<Entity>,
|
||||
}
|
||||
|
||||
impl FromWorld for ForgeTimer {
|
||||
fn from_world(_world: &mut World) -> Self {
|
||||
let time = Time::new(Instant::now());
|
||||
time.startup();
|
||||
let mut time = Time::new(Instant::now());
|
||||
// time.startup();
|
||||
time.pause();
|
||||
ForgeTimer {
|
||||
time_current: time,
|
||||
time_target: 60 * 15 + 30,
|
||||
|
|
@ -109,11 +127,56 @@ fn sec_to_str(time: isize) -> String {
|
|||
format!("{}{:02}:{:02}:{:02}", sign, h, m, s)
|
||||
}
|
||||
|
||||
fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
|
||||
// UI camera
|
||||
fn setup(mut commands: Commands, asset_server: Res<AssetServer>, mut pref: ResMut<Preferences>) {
|
||||
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((
|
||||
TextBundle::from_sections([
|
||||
TextSection::new(
|
||||
|
|
@ -141,6 +204,37 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
|
|||
})
|
||||
.with_text_alignment(TextAlignment::Center),
|
||||
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 {
|
||||
// let time = time.as_secs();
|
||||
// let s = time % 60;
|
||||
// let m = (time / 60) % 3600;
|
||||
// let h = time / 3600;
|
||||
// format!("{:02}:{:02}:{:02}", h, m, s)
|
||||
// }
|
||||
fn keyboard_events(
|
||||
keys: Res<Input<KeyCode>>,
|
||||
mut wind: Query<&mut Window>,
|
||||
pref: Res<Preferences>,
|
||||
mut tmr: ResMut<ForgeTimer>,
|
||||
) {
|
||||
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) {}
|
||||
}
|
||||
|
|
|
|||
Reference in New Issue