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
+25 -26
View File
@@ -7,10 +7,12 @@ use winit::{
dpi::LogicalSize,
event::{Event, KeyEvent, WindowEvent},
event_loop::{ActiveEventLoop, EventLoopProxy},
platform::modifier_supplement::KeyEventExtModifierSupplement,
};
use crate::{input::InputMapper, shrooms_vb_core::VBKey};
use crate::{
emulator::{SimId, VBKey},
input::InputMapper,
};
use super::{
common::{ImguiState, UiExt, WindowState, WindowStateBuilder},
@@ -19,10 +21,10 @@ use super::{
pub struct InputWindow {
window: WindowState,
imgui: Option<ImguiState>,
imgui: ImguiState,
input_mapper: Arc<RwLock<InputMapper>>,
proxy: EventLoopProxy<UserEvent>,
now_binding: Option<VBKey>,
now_binding: Option<(SimId, VBKey)>,
}
const KEY_NAMES: [(VBKey, &str); 14] = [
@@ -52,9 +54,10 @@ impl InputWindow {
.with_title("Bind Inputs")
.with_inner_size(LogicalSize::new(600, 400))
.build();
let imgui = ImguiState::new(&window);
Self {
window,
imgui: None,
imgui,
input_mapper,
now_binding: None,
proxy,
@@ -63,7 +66,7 @@ impl InputWindow {
fn draw(&mut self) {
let window = &mut self.window;
let imgui = self.imgui.as_mut().unwrap();
let imgui = &mut self.imgui;
let mut context = imgui.context.lock().unwrap();
let now = Instant::now();
@@ -85,11 +88,11 @@ impl InputWindow {
.expect("Failed to prepare frame");
let ui = context.new_frame();
let mut render_key_bindings = || {
let mut render_key_bindings = |sim_id: SimId| {
if let Some(table) = ui.begin_table("controls", 2) {
let binding_names = {
let mapper = self.input_mapper.read().unwrap();
mapper.binding_names()
mapper.binding_names(&sim_id)
};
ui.table_next_row();
@@ -100,20 +103,20 @@ impl InputWindow {
ui.group(|| {
ui.right_align_text(name, space * 0.20);
ui.same_line();
let label_text = if self.now_binding == Some(key) {
let label_text = if self.now_binding == Some((sim_id, key)) {
"Press any input"
} else {
binding.unwrap_or("")
};
let label = format!("{}##{}", label_text, name);
if ui.button_with_size(label, [space * 0.60, 0.0]) {
self.now_binding = Some(key);
self.now_binding = Some((sim_id, key));
}
});
ui.same_line();
if ui.button(format!("Clear##{name}")) {
let mut mapper = self.input_mapper.write().unwrap();
mapper.clear_binding(key);
mapper.clear_binding(sim_id, key);
}
}
@@ -124,7 +127,11 @@ impl InputWindow {
if let Some(window) = ui.fullscreen_window() {
if let Some(tabs) = ui.tab_bar("tabs") {
if let Some(tab) = ui.tab_item("Player 1") {
render_key_bindings();
render_key_bindings(SimId::Player1);
tab.end();
}
if let Some(tab) = ui.tab_item("Player 2") {
render_key_bindings(SimId::Player2);
tab.end();
}
tabs.end();
@@ -175,11 +182,11 @@ impl InputWindow {
if !event.state.is_pressed() {
return;
}
let Some(vb) = self.now_binding.take() else {
let Some((sim_id, vb)) = self.now_binding.take() else {
return;
};
let mut mapper = self.input_mapper.write().unwrap();
mapper.bind_key(vb, event.key_without_modifiers());
mapper.bind_key(sim_id, vb, event.physical_key);
}
}
@@ -188,19 +195,13 @@ impl AppWindow for InputWindow {
self.window.window.id()
}
fn init(&mut self) {
self.imgui = Some(ImguiState::new(&self.window));
self.window.window.request_redraw();
}
fn handle_event(&mut self, _: &ActiveEventLoop, event: &Event<UserEvent>) {
match event {
Event::WindowEvent { event, .. } => match event {
WindowEvent::Resized(size) => self.window.handle_resize(size),
WindowEvent::CloseRequested => self
.proxy
.send_event(UserEvent::CloseWindow(self.id()))
.unwrap(),
WindowEvent::CloseRequested => {
self.proxy.send_event(UserEvent::Close(self.id())).unwrap()
}
WindowEvent::KeyboardInput { event, .. } => self.try_bind_key(event),
WindowEvent::RedrawRequested => self.draw(),
_ => (),
@@ -212,9 +213,7 @@ impl AppWindow for InputWindow {
}
let window = &self.window;
let Some(imgui) = self.imgui.as_mut() else {
return;
};
let imgui = &mut self.imgui;
let mut context = imgui.context.lock().unwrap();
imgui
.platform