Support running two sims at once

This commit is contained in:
2024-11-11 00:52:02 -05:00
parent 9fcf6b3dc5
commit 544990c58f
9 changed files with 302 additions and 184 deletions
+34 -23
View File
@@ -5,6 +5,7 @@ use std::{
};
use game::GameWindow;
use input::InputWindow;
use winit::{
application::ApplicationHandler,
event::{Event, WindowEvent},
@@ -14,7 +15,7 @@ use winit::{
use crate::{
controller::ControllerState,
emulator::{EmulatorClient, EmulatorCommand},
emulator::{EmulatorClient, EmulatorCommand, SimId},
input::InputMapper,
};
@@ -28,6 +29,7 @@ pub struct App {
input_mapper: Arc<RwLock<InputMapper>>,
controller: ControllerState,
proxy: EventLoopProxy<UserEvent>,
player_2_window: Option<WindowId>,
}
impl App {
@@ -40,19 +42,19 @@ impl App {
input_mapper,
controller,
proxy,
player_2_window: None,
}
}
}
impl ApplicationHandler<UserEvent> for App {
fn resumed(&mut self, event_loop: &ActiveEventLoop) {
let mut window = GameWindow::new(
let window = GameWindow::new(
event_loop,
SimId::Player1,
self.client.clone(),
self.input_mapper.clone(),
self.proxy.clone(),
);
window.init();
self.windows.insert(window.id(), Box::new(window));
}
@@ -63,9 +65,9 @@ impl ApplicationHandler<UserEvent> for App {
event: WindowEvent,
) {
if let WindowEvent::KeyboardInput { event, .. } = &event {
if self.controller.key_event(event) {
if let Some((sim_id, pressed)) = self.controller.key_event(event) {
self.client
.send_command(EmulatorCommand::SetKeys(self.controller.pressed()));
.send_command(EmulatorCommand::SetKeys(sim_id, pressed));
}
}
let Some(window) = self.windows.get_mut(&window_id) else {
@@ -74,13 +76,30 @@ impl ApplicationHandler<UserEvent> for App {
window.handle_event(event_loop, &Event::WindowEvent { window_id, event });
}
fn user_event(&mut self, _event_loop: &ActiveEventLoop, event: UserEvent) {
fn user_event(&mut self, event_loop: &ActiveEventLoop, event: UserEvent) {
match event {
UserEvent::OpenWindow(mut window) => {
window.init();
self.windows.insert(window.id(), window);
UserEvent::OpenInputWindow => {
let window =
InputWindow::new(event_loop, self.input_mapper.clone(), self.proxy.clone());
self.windows.insert(window.id(), Box::new(window));
}
UserEvent::CloseWindow(window_id) => {
UserEvent::OpenPlayer2Window => {
if self.player_2_window.is_some() {
return;
}
let window = GameWindow::new(
event_loop,
SimId::Player2,
self.client.clone(),
self.proxy.clone(),
);
self.player_2_window = Some(window.id());
self.windows.insert(window.id(), Box::new(window));
}
UserEvent::Close(window_id) => {
if self.player_2_window == Some(window_id) {
self.player_2_window.take();
}
self.windows.remove(&window_id);
}
}
@@ -112,20 +131,12 @@ impl ApplicationHandler<UserEvent> for App {
pub trait AppWindow {
fn id(&self) -> WindowId;
fn init(&mut self);
fn handle_event(&mut self, event_loop: &ActiveEventLoop, event: &Event<UserEvent>);
}
#[derive(Debug)]
pub enum UserEvent {
OpenWindow(Box<dyn AppWindow>),
CloseWindow(WindowId),
}
impl Debug for UserEvent {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::OpenWindow(window) => f.debug_tuple("OpenWindow").field(&window.id()).finish(),
Self::CloseWindow(window_id) => f.debug_tuple("CloseWindow").field(window_id).finish(),
}
}
OpenInputWindow,
OpenPlayer2Window,
Close(WindowId),
}