This commit is contained in:
parent
df99ef3a3f
commit
4f6216635f
17 changed files with 110 additions and 100 deletions
17
src/core/window/config.rs
Normal file
17
src/core/window/config.rs
Normal file
|
@ -0,0 +1,17 @@
|
|||
use bevy_ecs::system::Resource;
|
||||
use winit::{dpi::PhysicalSize, window::WindowAttributes};
|
||||
|
||||
#[derive(Resource, Clone)]
|
||||
pub struct WindowConfig {
|
||||
pub title: String,
|
||||
pub width: u32,
|
||||
pub height: u32,
|
||||
}
|
||||
|
||||
impl Into<WindowAttributes> for &WindowConfig {
|
||||
fn into(self) -> WindowAttributes {
|
||||
WindowAttributes::default()
|
||||
.with_title(self.title.clone())
|
||||
.with_inner_size(PhysicalSize::new(self.width as f64, self.height as f64))
|
||||
}
|
||||
}
|
|
@ -1,24 +1,27 @@
|
|||
pub mod window_handler;
|
||||
|
||||
use super::app::{App, plugin::Plugin};
|
||||
use window_handler::WindowHandler;
|
||||
use config::WindowConfig;
|
||||
use state::WindowState;
|
||||
use winit::event_loop::EventLoop;
|
||||
use winit::window::WindowAttributes;
|
||||
|
||||
pub struct WindowPlugin {
|
||||
window_attributes: WindowAttributes,
|
||||
event_loop: EventLoop<()>,
|
||||
use super::app::{App, AppExit};
|
||||
|
||||
pub mod config;
|
||||
pub mod state;
|
||||
|
||||
pub fn init(app: &mut App, window_config: WindowConfig) {
|
||||
let world = app.world_mut();
|
||||
world.insert_resource(window_config);
|
||||
|
||||
let mut event_loop_builder = EventLoop::with_user_event();
|
||||
let event_loop = event_loop_builder.build().unwrap();
|
||||
|
||||
app.set_runner(Box::new(move |app| runner(app, event_loop)));
|
||||
}
|
||||
|
||||
impl Plugin for WindowPlugin {
|
||||
fn build(&self, app: &mut App) {
|
||||
let world = app.world_mut();
|
||||
world.insert_resource(WindowHandler::new(self.window_attributes.clone()));
|
||||
fn runner(app: App, event_loop: EventLoop<()>) -> AppExit {
|
||||
let mut window_state = WindowState::new(app);
|
||||
|
||||
let window_handler = world.get_resource_mut::<WindowHandler>().unwrap();
|
||||
|
||||
// app.set_runner(Box::new(move || {
|
||||
// self.event_loop.run_app(&mut window_handler);
|
||||
// }));
|
||||
match event_loop.run_app(&mut window_state) {
|
||||
Ok(_) => AppExit::Success,
|
||||
Err(e) => AppExit::Error(Box::new(e)),
|
||||
}
|
||||
}
|
||||
|
|
45
src/core/window/state.rs
Normal file
45
src/core/window/state.rs
Normal file
|
@ -0,0 +1,45 @@
|
|||
use bevy_ecs::world::World;
|
||||
use winit::{
|
||||
application::ApplicationHandler,
|
||||
event::WindowEvent,
|
||||
event_loop::ActiveEventLoop,
|
||||
window::{Window, WindowId},
|
||||
};
|
||||
|
||||
use crate::core::app::App;
|
||||
|
||||
use super::config::WindowConfig;
|
||||
|
||||
pub struct WindowState {
|
||||
app: App,
|
||||
window: Option<Window>,
|
||||
}
|
||||
|
||||
impl WindowState {
|
||||
pub fn new(app: App) -> Self {
|
||||
Self { app, window: None }
|
||||
}
|
||||
|
||||
fn world(&self) -> &World {
|
||||
self.app.world()
|
||||
}
|
||||
}
|
||||
|
||||
impl ApplicationHandler for WindowState {
|
||||
fn resumed(&mut self, event_loop: &ActiveEventLoop) {
|
||||
let window_config = self.world().get_resource::<WindowConfig>().unwrap();
|
||||
|
||||
let window = event_loop.create_window(window_config.into()).unwrap();
|
||||
self.window = Some(window);
|
||||
}
|
||||
|
||||
fn window_event(&mut self, event_loop: &ActiveEventLoop, _id: WindowId, event: WindowEvent) {
|
||||
match event {
|
||||
WindowEvent::CloseRequested => {
|
||||
log::debug!("The close button was pressed; stopping");
|
||||
event_loop.exit();
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,40 +0,0 @@
|
|||
use bevy_ecs::system::Resource;
|
||||
use winit::{
|
||||
application::ApplicationHandler,
|
||||
event::WindowEvent,
|
||||
event_loop::ActiveEventLoop,
|
||||
window::{Window, WindowAttributes, WindowId},
|
||||
};
|
||||
|
||||
#[derive(Resource)]
|
||||
pub struct WindowHandler {
|
||||
window_attributes: WindowAttributes,
|
||||
window: Option<Box<Window>>,
|
||||
}
|
||||
|
||||
impl WindowHandler {
|
||||
pub fn new(window_attributes: WindowAttributes) -> Self {
|
||||
Self {
|
||||
window_attributes,
|
||||
window: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
impl ApplicationHandler for WindowHandler {
|
||||
fn resumed(&mut self, event_loop: &ActiveEventLoop) {
|
||||
let window = event_loop
|
||||
.create_window(self.window_attributes.clone())
|
||||
.unwrap();
|
||||
self.window = Some(Box::new(window));
|
||||
}
|
||||
|
||||
fn window_event(&mut self, event_loop: &ActiveEventLoop, _id: WindowId, event: WindowEvent) {
|
||||
match event {
|
||||
WindowEvent::CloseRequested => {
|
||||
log::debug!("The close button was pressed; stopping");
|
||||
event_loop.exit();
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue