winit 0.29
This commit is contained in:
parent
6ae2697a39
commit
f62065930c
4 changed files with 505 additions and 213 deletions
592
Cargo.lock
generated
592
Cargo.lock
generated
File diff suppressed because it is too large
Load diff
|
@ -33,8 +33,8 @@ nokhwa = { version = "0.10", features = [
|
|||
"input-avfoundation",
|
||||
], optional = true }
|
||||
send_wrapper = { version = "0.6.0", optional = true }
|
||||
winit = "0.28"
|
||||
winit_input_helper = "0.14"
|
||||
winit = { version = "0.29", features = ["rwh_05"] }
|
||||
winit_input_helper = "0.15"
|
||||
raw-window-handle = { version = "0.5", optional = true }
|
||||
serde = { version = "1.0", features = ["derive"] }
|
||||
image = { version = "0.24", default-features = false, features = ["png"] }
|
||||
|
|
|
@ -187,7 +187,7 @@ pub fn prepare(
|
|||
}
|
||||
}
|
||||
|
||||
pub fn run(prepared: PreparedEmulator<NoCamera>) -> ! {
|
||||
pub fn run(prepared: PreparedEmulator<NoCamera>) {
|
||||
let configs = access_config();
|
||||
let mut window_manager = WindowManager::new(prepared.sender, prepared.stream, prepared.record);
|
||||
|
||||
|
@ -255,7 +255,7 @@ pub fn run(prepared: PreparedEmulator<NoCamera>) -> ! {
|
|||
core.run(100);
|
||||
});
|
||||
|
||||
window_manager.run_events_blocking();
|
||||
window_manager.run_events_blocking().unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -18,9 +18,10 @@ use image::ImageBuffer;
|
|||
use raw_window_handle::HasRawDisplayHandle;
|
||||
use winit::{
|
||||
dpi::PhysicalSize,
|
||||
event::{Event, VirtualKeyCode, WindowEvent},
|
||||
event::{Event, WindowEvent},
|
||||
event_loop::{ControlFlow, EventLoop, EventLoopWindowTarget},
|
||||
platform::run_return::EventLoopExtRunReturn,
|
||||
keyboard::KeyCode,
|
||||
platform::pump_events::EventLoopExtPumpEvents,
|
||||
window::{Window, WindowBuilder, WindowId},
|
||||
};
|
||||
use winit_input_helper::WinitInputHelper;
|
||||
|
@ -48,7 +49,7 @@ struct WindowManagerData {
|
|||
|
||||
impl WindowManager {
|
||||
pub fn new(sender: Sender<EmulatorMessage>, _stream: Stream, record_main: bool) -> Self {
|
||||
let event_loop = EventLoop::new();
|
||||
let event_loop = EventLoop::new().unwrap();
|
||||
#[cfg(feature = "vulkan")]
|
||||
let window_data_manager =
|
||||
Arc::new(RendererBackendManager::new(event_loop.raw_display_handle()));
|
||||
|
@ -103,15 +104,13 @@ impl WindowManager {
|
|||
}
|
||||
|
||||
pub fn update_events(&mut self) {
|
||||
self.event_loop.run_return(|event, target, control_flow| {
|
||||
self.data.handler(true, event, target, control_flow)
|
||||
});
|
||||
self.event_loop
|
||||
.pump_events(None, |event, target| self.data.handler(true, event, target));
|
||||
}
|
||||
|
||||
pub fn run_events_blocking(mut self) -> ! {
|
||||
self.event_loop.run(move |event, target, control_flow| {
|
||||
self.data.handler(false, event, target, control_flow)
|
||||
})
|
||||
pub fn run_events_blocking(mut self) -> Result<(), winit::error::EventLoopError> {
|
||||
self.event_loop
|
||||
.run(move |event, target| self.data.handler(false, event, target))
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -141,16 +140,10 @@ impl WindowManagerData {
|
|||
sender
|
||||
}
|
||||
|
||||
fn handler(
|
||||
&mut self,
|
||||
run_return: bool,
|
||||
event: Event<'_, ()>,
|
||||
_target: &EventLoopWindowTarget<()>,
|
||||
control_flow: &mut ControlFlow,
|
||||
) {
|
||||
control_flow.set_poll();
|
||||
fn handler(&mut self, run_return: bool, event: Event<()>, target: &EventLoopWindowTarget<()>) {
|
||||
target.set_control_flow(ControlFlow::Poll);
|
||||
|
||||
if self.input.update(&event) && self.input.key_pressed(VirtualKeyCode::Space) {
|
||||
if self.input.update(&event) && self.input.key_pressed(KeyCode::Space) {
|
||||
if let Some(window) = self
|
||||
.main_window
|
||||
.as_ref()
|
||||
|
@ -192,37 +185,40 @@ impl WindowManagerData {
|
|||
Event::Resumed => {
|
||||
self.sender.send(EmulatorMessage::Start).unwrap();
|
||||
}
|
||||
Event::WindowEvent {
|
||||
event: WindowEvent::CloseRequested,
|
||||
window_id,
|
||||
} => {
|
||||
if self.main_window.is_some_and(|v| v == window_id) {
|
||||
self.sender.send(EmulatorMessage::Exit).unwrap();
|
||||
} else if let Some(window) = self
|
||||
.main_window
|
||||
.as_ref()
|
||||
.and_then(|id| self.windows.get(id))
|
||||
{
|
||||
window.window.focus_window();
|
||||
}
|
||||
}
|
||||
Event::MainEventsCleared => {
|
||||
Event::AboutToWait => {
|
||||
for window in self.windows.values_mut() {
|
||||
window.process();
|
||||
// if window.is_prepared {
|
||||
window.render(&self.window_data_manager);
|
||||
// }
|
||||
}
|
||||
|
||||
if run_return {
|
||||
control_flow.set_exit();
|
||||
target.exit();
|
||||
}
|
||||
}
|
||||
Event::RedrawRequested(window_id) => {
|
||||
if let Some(w) = self.windows.get_mut(&window_id) {
|
||||
w.render(&self.window_data_manager);
|
||||
Event::WindowEvent { window_id, event } => match event {
|
||||
WindowEvent::Resized(size) => {
|
||||
if let Some(w) = self.windows.get_mut(&window_id) {
|
||||
w.on_resize(size);
|
||||
}
|
||||
}
|
||||
}
|
||||
WindowEvent::RedrawRequested => {
|
||||
if let Some(w) = self.windows.get_mut(&window_id) {
|
||||
w.render(&self.window_data_manager);
|
||||
}
|
||||
}
|
||||
WindowEvent::CloseRequested => {
|
||||
if self.main_window.is_some_and(|v| v == window_id) {
|
||||
self.sender.send(EmulatorMessage::Exit).unwrap();
|
||||
} else if let Some(window) = self
|
||||
.main_window
|
||||
.as_ref()
|
||||
.and_then(|id| self.windows.get(id))
|
||||
{
|
||||
window.window.focus_window();
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
},
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
@ -258,17 +254,17 @@ impl WindowManagerData {
|
|||
}
|
||||
|
||||
self.joypad_state.down |=
|
||||
self.input.key_held(VirtualKeyCode::Down) || self.input.key_held(VirtualKeyCode::S);
|
||||
self.input.key_held(KeyCode::ArrowDown) || self.input.key_held(KeyCode::KeyS);
|
||||
self.joypad_state.up |=
|
||||
self.input.key_held(VirtualKeyCode::Up) || self.input.key_held(VirtualKeyCode::W);
|
||||
self.input.key_held(KeyCode::ArrowUp) || self.input.key_held(KeyCode::KeyW);
|
||||
self.joypad_state.left |=
|
||||
self.input.key_held(VirtualKeyCode::Left) || self.input.key_held(VirtualKeyCode::A);
|
||||
self.input.key_held(KeyCode::ArrowLeft) || self.input.key_held(KeyCode::KeyA);
|
||||
self.joypad_state.right |=
|
||||
self.input.key_held(VirtualKeyCode::Right) || self.input.key_held(VirtualKeyCode::D);
|
||||
self.joypad_state.start |= self.input.key_held(VirtualKeyCode::Equals);
|
||||
self.joypad_state.select |= self.input.key_held(VirtualKeyCode::Minus);
|
||||
self.joypad_state.a |= self.input.key_held(VirtualKeyCode::Apostrophe);
|
||||
self.joypad_state.b |= self.input.key_held(VirtualKeyCode::Semicolon);
|
||||
self.input.key_held(KeyCode::ArrowRight) || self.input.key_held(KeyCode::KeyD);
|
||||
self.joypad_state.start |= self.input.key_held(KeyCode::Equal);
|
||||
self.joypad_state.select |= self.input.key_held(KeyCode::Minus);
|
||||
self.joypad_state.a |= self.input.key_held(KeyCode::Quote);
|
||||
self.joypad_state.b |= self.input.key_held(KeyCode::Semicolon);
|
||||
|
||||
self.sender
|
||||
.send(EmulatorMessage::JoypadUpdate(self.joypad_state))
|
||||
|
@ -386,8 +382,8 @@ impl WindowRenderer {
|
|||
fn process(&mut self) {
|
||||
while let Ok(message) = self.receiver.try_recv() {
|
||||
match message {
|
||||
RendererMessage::Prepare { width, height } => self.prepare(width, height),
|
||||
RendererMessage::Resize { width, height } => self.prepare(width, height),
|
||||
RendererMessage::Prepare { width, height } => self.attempt_resize(width, height),
|
||||
RendererMessage::Resize { width, height } => self.attempt_resize(width, height),
|
||||
RendererMessage::Display { buffer } => self.display(buffer),
|
||||
RendererMessage::SetTitle { title } => self.window.set_title(&title),
|
||||
RendererMessage::Rumble { rumble: _ } => todo!(),
|
||||
|
@ -395,7 +391,7 @@ impl WindowRenderer {
|
|||
}
|
||||
}
|
||||
|
||||
fn prepare(&mut self, width: usize, height: usize) {
|
||||
fn attempt_resize(&mut self, width: usize, height: usize) {
|
||||
self.width = width;
|
||||
self.height = height;
|
||||
|
||||
|
@ -404,14 +400,20 @@ impl WindowRenderer {
|
|||
let real_width = (width as u32) * real_factor;
|
||||
let real_height = (height as u32) * real_factor;
|
||||
|
||||
self.window
|
||||
.set_inner_size(PhysicalSize::new(real_width, real_height));
|
||||
if let Some(size) = self
|
||||
.window
|
||||
.request_inner_size(PhysicalSize::new(real_width, real_height))
|
||||
{
|
||||
self.on_resize(size);
|
||||
}
|
||||
}
|
||||
|
||||
fn on_resize(&mut self, size: PhysicalSize<u32>) {
|
||||
let resolutions = ResolutionData {
|
||||
real_width,
|
||||
real_height,
|
||||
scaled_width: width as u32,
|
||||
scaled_height: height as u32,
|
||||
real_width: size.width,
|
||||
real_height: size.height,
|
||||
scaled_width: self.width as u32,
|
||||
scaled_height: self.height as u32,
|
||||
};
|
||||
|
||||
self.renderer.resize(resolutions, &self.window);
|
||||
|
|
Loading…
Add table
Reference in a new issue