This commit is contained in:
parent
b361965033
commit
f4694157ab
8 changed files with 143 additions and 3 deletions
24
src/core/window/mod.rs
Normal file
24
src/core/window/mod.rs
Normal file
|
@ -0,0 +1,24 @@
|
|||
pub mod window_handler;
|
||||
|
||||
use super::app::{App, plugin::Plugin};
|
||||
use window_handler::WindowHandler;
|
||||
use winit::event_loop::EventLoop;
|
||||
use winit::window::WindowAttributes;
|
||||
|
||||
pub struct WindowPlugin {
|
||||
window_attributes: WindowAttributes,
|
||||
event_loop: EventLoop<()>,
|
||||
}
|
||||
|
||||
impl Plugin for WindowPlugin {
|
||||
fn build(&self, app: &mut App) {
|
||||
let world = app.world_mut();
|
||||
world.insert_resource(WindowHandler::new(self.window_attributes.clone()));
|
||||
|
||||
let window_handler = world.get_resource_mut::<WindowHandler>().unwrap();
|
||||
|
||||
// app.set_runner(Box::new(move || {
|
||||
// self.event_loop.run_app(&mut window_handler);
|
||||
// }));
|
||||
}
|
||||
}
|
40
src/core/window/window_handler.rs
Normal file
40
src/core/window/window_handler.rs
Normal file
|
@ -0,0 +1,40 @@
|
|||
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