Split crates
Some checks failed
Build legacy Nix package on Ubuntu / build (push) Failing after 7m49s
Some checks failed
Build legacy Nix package on Ubuntu / build (push) Failing after 7m49s
This commit is contained in:
parent
99be029ff8
commit
b977f446d3
16 changed files with 84 additions and 110 deletions
12
crates/engine_window/Cargo.toml
Normal file
12
crates/engine_window/Cargo.toml
Normal file
|
@ -0,0 +1,12 @@
|
|||
[package]
|
||||
name = "engine_window"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
thiserror = { workspace = true }
|
||||
log = { workspace = true }
|
||||
env_logger = { workspace = true }
|
||||
bevy_app = { workspace = true }
|
||||
bevy_ecs = { workspace = true }
|
||||
winit = { workspace = true }
|
17
crates/engine_window/src/config.rs
Normal file
17
crates/engine_window/src/config.rs
Normal file
|
@ -0,0 +1,17 @@
|
|||
use bevy_ecs::resource::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))
|
||||
}
|
||||
}
|
56
crates/engine_window/src/lib.rs
Normal file
56
crates/engine_window/src/lib.rs
Normal file
|
@ -0,0 +1,56 @@
|
|||
use bevy_app::{App, AppExit, Plugin, PluginsState};
|
||||
use config::WindowConfig;
|
||||
use raw_handle::{DisplayHandleWrapper, EventLoopProxyWrapper};
|
||||
use state::WindowState;
|
||||
use winit::event_loop::EventLoop;
|
||||
|
||||
pub mod config;
|
||||
pub mod raw_handle;
|
||||
pub mod state;
|
||||
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
pub enum WindowError {
|
||||
#[error("Failed to create event loop")]
|
||||
FailedToCreateEventLoop,
|
||||
}
|
||||
|
||||
pub struct WindowPlugin {
|
||||
pub window_config: WindowConfig,
|
||||
}
|
||||
|
||||
impl Plugin for WindowPlugin {
|
||||
fn build(&self, app: &mut App) {
|
||||
let world = app.world_mut();
|
||||
world.insert_resource(self.window_config.clone());
|
||||
|
||||
let mut event_loop_builder = EventLoop::with_user_event();
|
||||
let event_loop = event_loop_builder
|
||||
.build()
|
||||
.map_err(|_| WindowError::FailedToCreateEventLoop)
|
||||
.expect("Failed to create event loop");
|
||||
|
||||
world.insert_resource(DisplayHandleWrapper(event_loop.owned_display_handle()));
|
||||
|
||||
app.set_runner(Box::new(move |app| runner(app, event_loop)));
|
||||
}
|
||||
}
|
||||
|
||||
fn runner(mut app: App, event_loop: EventLoop<()>) -> AppExit {
|
||||
if app.plugins_state() == PluginsState::Ready {
|
||||
app.finish();
|
||||
app.cleanup();
|
||||
}
|
||||
|
||||
app.world_mut()
|
||||
.insert_resource(EventLoopProxyWrapper::new(event_loop.create_proxy()));
|
||||
|
||||
let mut window_state = WindowState::new(app);
|
||||
|
||||
match event_loop.run_app(&mut window_state) {
|
||||
Ok(_) => AppExit::Success,
|
||||
Err(e) => {
|
||||
log::error!("Error running window state: {e}");
|
||||
AppExit::error()
|
||||
}
|
||||
}
|
||||
}
|
23
crates/engine_window/src/raw_handle.rs
Normal file
23
crates/engine_window/src/raw_handle.rs
Normal file
|
@ -0,0 +1,23 @@
|
|||
use std::sync::Arc;
|
||||
|
||||
use bevy_ecs::resource::Resource;
|
||||
use winit::{event_loop::EventLoopProxy, window::Window};
|
||||
|
||||
#[derive(Resource)]
|
||||
pub struct EventLoopProxyWrapper<T: 'static>(EventLoopProxy<T>);
|
||||
|
||||
impl<T: 'static> EventLoopProxyWrapper<T> {
|
||||
pub fn new(event_loop: EventLoopProxy<T>) -> Self {
|
||||
Self(event_loop)
|
||||
}
|
||||
|
||||
pub fn proxy(&self) -> &EventLoopProxy<T> {
|
||||
&self.0
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Resource)]
|
||||
pub struct DisplayHandleWrapper(pub winit::event_loop::OwnedDisplayHandle);
|
||||
|
||||
#[derive(Resource)]
|
||||
pub struct WindowWrapper(pub Arc<Window>);
|
63
crates/engine_window/src/state.rs
Normal file
63
crates/engine_window/src/state.rs
Normal file
|
@ -0,0 +1,63 @@
|
|||
use std::sync::Arc;
|
||||
|
||||
use bevy_app::{App, PluginsState};
|
||||
use bevy_ecs::world::World;
|
||||
use winit::{
|
||||
application::ApplicationHandler, event::WindowEvent, event_loop::ActiveEventLoop,
|
||||
window::WindowId,
|
||||
};
|
||||
|
||||
use super::{config::WindowConfig, raw_handle::WindowWrapper};
|
||||
|
||||
pub struct WindowState {
|
||||
app: App,
|
||||
}
|
||||
|
||||
impl WindowState {
|
||||
pub fn new(app: App) -> Self {
|
||||
Self { app }
|
||||
}
|
||||
|
||||
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.app
|
||||
.world_mut()
|
||||
.insert_resource(WindowWrapper(Arc::new(window)));
|
||||
}
|
||||
|
||||
fn new_events(&mut self, event_loop: &ActiveEventLoop, cause: winit::event::StartCause) {
|
||||
if self.app.plugins_state() == PluginsState::Ready {
|
||||
self.app.finish();
|
||||
self.app.cleanup();
|
||||
}
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
WindowEvent::RedrawRequested => {
|
||||
if self.app.plugins_state() == PluginsState::Cleaned {
|
||||
self.app.update();
|
||||
}
|
||||
|
||||
let window_wrapper = self.app.world().get_resource::<WindowWrapper>().unwrap();
|
||||
|
||||
window_wrapper.0.request_redraw();
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
fn about_to_wait(&mut self, _event_loop: &ActiveEventLoop) {}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue