use std::sync::mpsc::Sender;
use winit::event_loop::EventLoop;
use winit::window::Window;
use crate::high_level_fighter::HighLevelSubaction;
use crate::renderer::app::winit_app::WinitApp;
pub mod state;
mod winit_app;
use state::{AppEventIncoming, AppEventOutgoingHandler};
pub struct App {
winit_app: WinitApp,
event_loop: Option<EventLoop<()>>,
}
impl App {
pub async fn new(subaction: HighLevelSubaction) -> Self {
let event_loop = EventLoop::new().unwrap();
#[allow(
deprecated,
reason = r#"We should fix this, but its going to take some refactoring I dont have capacity for at the moment.
We need to move the window + wgpu resource creation logic inside the winit app.
This will require making async calls inside the winit app, via futures::executor::block_on and the equivalent wasm function.
We should look at https://github.com/gfx-rs/wgpu/pull/5709/files or the repo itself if upstreamed."#
)]
let window = event_loop
.create_window(Window::default_attributes())
.unwrap();
App::new_common(window, event_loop, subaction).await
}
#[cfg(target_arch = "wasm32")]
pub async fn new_insert_into_element(
element: web_sys::Element,
subaction: HighLevelSubaction,
) -> Self {
use winit::platform::web::WindowExtWebSys;
let event_loop = EventLoop::new().unwrap();
#[allow(
deprecated,
reason = r#"We should fix this, but its going to take some refactoring I dont have capacity for at the moment.
We need to move the window + wgpu resource creation logic inside the winit app.
This will require making async calls inside the winit app, via futures::executor::block_on and the equivalent wasm function.
We should look at https://github.com/gfx-rs/wgpu/pull/5709/files or the repo itself if upstreamed."#
)]
let window = event_loop
.create_window(Window::default_attributes())
.unwrap();
let canvas = window.canvas().unwrap();
canvas
.style()
.set_css_text("display: block; width: 100%; height: 100%");
element
.append_child(&web_sys::Element::from(canvas))
.unwrap();
App::new_common(window, event_loop, subaction).await
}
async fn new_common(
window: Window,
event_loop: EventLoop<()>,
subaction: HighLevelSubaction,
) -> App {
App {
winit_app: WinitApp::new(window, subaction).await,
event_loop: Some(event_loop),
}
}
pub fn run(mut self) {
self.event_loop
.take()
.unwrap()
.run_app(&mut self.winit_app)
.unwrap();
}
pub fn set_event_handler(&mut self, event_handler: AppEventOutgoingHandler) {
self.winit_app.set_event_handler(event_handler);
}
pub fn get_event_tx(&self) -> Sender<AppEventIncoming> {
self.winit_app.get_event_tx()
}
}