window: customizable default descriptor

This commit is contained in:
Carter Anderson 2020-07-20 02:05:56 -07:00
parent b1162f0c29
commit 009141d453
5 changed files with 35 additions and 11 deletions

View file

@ -203,3 +203,7 @@ path = "examples/window/clear_color.rs"
[[example]]
name = "multiple_windows"
path = "examples/window/multiple_windows.rs"
[[example]]
name = "window_settings"
path = "examples/window/window_settings.rs"

View file

@ -8,18 +8,22 @@ pub use system::*;
pub use window::*;
pub use windows::*;
pub mod prelude {
pub use crate::{CursorMoved, Window, WindowDescriptor, Windows};
}
use bevy_app::prelude::*;
use bevy_ecs::IntoQuerySystem;
pub struct WindowPlugin {
pub primary_window: Option<WindowDescriptor>,
pub add_primary_window: bool,
pub exit_on_close: bool,
}
impl Default for WindowPlugin {
fn default() -> Self {
WindowPlugin {
primary_window: Some(WindowDescriptor::default()),
add_primary_window: true,
exit_on_close: true,
}
}
@ -35,14 +39,16 @@ impl AppPlugin for WindowPlugin {
.add_event::<CursorMoved>()
.init_resource::<Windows>();
if let Some(ref primary_window_descriptor) = self.primary_window {
let mut create_window_event = app
.resources_mut()
.get_mut::<Events<CreateWindow>>()
.unwrap();
if self.add_primary_window {
let resources = app.resources();
let window_descriptor = resources
.get::<WindowDescriptor>()
.map(|descriptor| (*descriptor).clone())
.unwrap_or_else(|| WindowDescriptor::default());
let mut create_window_event = resources.get_mut::<Events<CreateWindow>>().unwrap();
create_window_event.send(CreateWindow {
id: WindowId::new(),
descriptor: primary_window_descriptor.clone(),
descriptor: window_descriptor.clone(),
});
}

View file

@ -44,13 +44,13 @@ fn setup(
})
// light
.spawn(LightComponents {
translation: Translation::new(4.0, 5.0, -4.0),
translation: Translation::new(4.0, 8.0, 4.0),
..Default::default()
})
// camera
.spawn(Camera3dComponents {
transform: Transform::new_sync_disabled(Mat4::face_toward(
Vec3::new(3.0, 5.0, 8.0),
Vec3::new(-3.0, 5.0, 8.0),
Vec3::new(0.0, 0.0, 0.0),
Vec3::new(0.0, 1.0, 0.0),
)),

View file

@ -0,0 +1,13 @@
use bevy::prelude::*;
fn main() {
App::build()
.add_resource(WindowDescriptor {
title: "I am a window!".to_string(),
width: 300,
height: 300,
vsync: true,
})
.add_default_plugins()
.run();
}

View file

@ -14,6 +14,7 @@ pub use crate::{
text::prelude::*,
transform::prelude::*,
type_registry::RegisterType,
window::prelude::*,
ui::prelude::*,
AddDefaultPlugins,
};