Implement multiplayer #2
|
@ -432,7 +432,7 @@ dependencies = [
|
||||||
"bitflags 1.3.2",
|
"bitflags 1.3.2",
|
||||||
"block",
|
"block",
|
||||||
"cocoa-foundation",
|
"cocoa-foundation",
|
||||||
"core-foundation",
|
"core-foundation 0.9.4",
|
||||||
"core-graphics",
|
"core-graphics",
|
||||||
"foreign-types",
|
"foreign-types",
|
||||||
"libc",
|
"libc",
|
||||||
|
@ -447,7 +447,7 @@ checksum = "8c6234cbb2e4c785b456c0644748b1ac416dd045799740356f8363dfe00c93f7"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"bitflags 1.3.2",
|
"bitflags 1.3.2",
|
||||||
"block",
|
"block",
|
||||||
"core-foundation",
|
"core-foundation 0.9.4",
|
||||||
"core-graphics-types",
|
"core-graphics-types",
|
||||||
"libc",
|
"libc",
|
||||||
"objc",
|
"objc",
|
||||||
|
@ -498,6 +498,16 @@ dependencies = [
|
||||||
"libc",
|
"libc",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "core-foundation"
|
||||||
|
version = "0.10.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "b55271e5c8c478ad3f38ad24ef34923091e0548492a266d19b3c0b4d82574c63"
|
||||||
|
dependencies = [
|
||||||
|
"core-foundation-sys",
|
||||||
|
"libc",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "core-foundation-sys"
|
name = "core-foundation-sys"
|
||||||
version = "0.8.7"
|
version = "0.8.7"
|
||||||
|
@ -511,7 +521,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "c07782be35f9e1140080c6b96f0d44b739e2278479f64e02fdab4e32dfd8b081"
|
checksum = "c07782be35f9e1140080c6b96f0d44b739e2278479f64e02fdab4e32dfd8b081"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"bitflags 1.3.2",
|
"bitflags 1.3.2",
|
||||||
"core-foundation",
|
"core-foundation 0.9.4",
|
||||||
"core-graphics-types",
|
"core-graphics-types",
|
||||||
"foreign-types",
|
"foreign-types",
|
||||||
"libc",
|
"libc",
|
||||||
|
@ -524,7 +534,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "45390e6114f68f718cc7a830514a96f903cccd70d02a8f6d9f643ac4ba45afaf"
|
checksum = "45390e6114f68f718cc7a830514a96f903cccd70d02a8f6d9f643ac4ba45afaf"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"bitflags 1.3.2",
|
"bitflags 1.3.2",
|
||||||
"core-foundation",
|
"core-foundation 0.9.4",
|
||||||
"libc",
|
"libc",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
@ -668,6 +678,12 @@ dependencies = [
|
||||||
"windows-sys 0.52.0",
|
"windows-sys 0.52.0",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "fnv"
|
||||||
|
version = "1.0.7"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "foreign-types"
|
name = "foreign-types"
|
||||||
version = "0.5.0"
|
version = "0.5.0"
|
||||||
|
@ -716,6 +732,40 @@ dependencies = [
|
||||||
"wasi",
|
"wasi",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "gilrs"
|
||||||
|
version = "0.11.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "bbb2c998745a3c1ac90f64f4f7b3a54219fd3612d7705e7798212935641ed18f"
|
||||||
|
dependencies = [
|
||||||
|
"fnv",
|
||||||
|
"gilrs-core",
|
||||||
|
"log",
|
||||||
|
"uuid",
|
||||||
|
"vec_map",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "gilrs-core"
|
||||||
|
version = "0.6.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "495af945e45efd6386227613cd9fb7bd7c43d3c095040e30c5304c489e6abed5"
|
||||||
|
dependencies = [
|
||||||
|
"core-foundation 0.10.0",
|
||||||
|
"inotify",
|
||||||
|
"io-kit-sys",
|
||||||
|
"js-sys",
|
||||||
|
"libc",
|
||||||
|
"libudev-sys",
|
||||||
|
"log",
|
||||||
|
"nix",
|
||||||
|
"uuid",
|
||||||
|
"vec_map",
|
||||||
|
"wasm-bindgen",
|
||||||
|
"web-sys",
|
||||||
|
"windows 0.58.0",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "gl_generator"
|
name = "gl_generator"
|
||||||
version = "0.14.0"
|
version = "0.14.0"
|
||||||
|
@ -905,6 +955,36 @@ dependencies = [
|
||||||
"hashbrown 0.15.1",
|
"hashbrown 0.15.1",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "inotify"
|
||||||
|
version = "0.11.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "f37dccff2791ab604f9babef0ba14fbe0be30bd368dc541e2b08d07c8aa908f3"
|
||||||
|
dependencies = [
|
||||||
|
"bitflags 2.6.0",
|
||||||
|
"inotify-sys",
|
||||||
|
"libc",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "inotify-sys"
|
||||||
|
version = "0.1.5"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "e05c02b5e89bff3b946cedeca278abc628fe811e604f027c45a8aa3cf793d0eb"
|
||||||
|
dependencies = [
|
||||||
|
"libc",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "io-kit-sys"
|
||||||
|
version = "0.4.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "617ee6cf8e3f66f3b4ea67a4058564628cde41901316e19f559e14c7c72c5e7b"
|
||||||
|
dependencies = [
|
||||||
|
"core-foundation-sys",
|
||||||
|
"mach2",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "is_terminal_polyfill"
|
name = "is_terminal_polyfill"
|
||||||
version = "1.70.1"
|
version = "1.70.1"
|
||||||
|
@ -1013,6 +1093,16 @@ dependencies = [
|
||||||
"redox_syscall 0.5.7",
|
"redox_syscall 0.5.7",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "libudev-sys"
|
||||||
|
version = "0.1.4"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "3c8469b4a23b962c1396b9b451dda50ef5b283e8dd309d69033475fa9b334324"
|
||||||
|
dependencies = [
|
||||||
|
"libc",
|
||||||
|
"pkg-config",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "linux-raw-sys"
|
name = "linux-raw-sys"
|
||||||
version = "0.4.14"
|
version = "0.4.14"
|
||||||
|
@ -1131,7 +1221,7 @@ dependencies = [
|
||||||
"ascii",
|
"ascii",
|
||||||
"block",
|
"block",
|
||||||
"cocoa",
|
"cocoa",
|
||||||
"core-foundation",
|
"core-foundation 0.9.4",
|
||||||
"dirs-next",
|
"dirs-next",
|
||||||
"objc",
|
"objc",
|
||||||
"objc-foundation",
|
"objc-foundation",
|
||||||
|
@ -1198,6 +1288,18 @@ dependencies = [
|
||||||
"jni-sys",
|
"jni-sys",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "nix"
|
||||||
|
version = "0.29.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "71e2746dc3a24dd78b3cfcb7be93368c6de9963d30f43a6a73998a9cf4b17b46"
|
||||||
|
dependencies = [
|
||||||
|
"bitflags 2.6.0",
|
||||||
|
"cfg-if",
|
||||||
|
"cfg_aliases 0.2.1",
|
||||||
|
"libc",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "nom"
|
name = "nom"
|
||||||
version = "7.1.3"
|
version = "7.1.3"
|
||||||
|
@ -1910,6 +2012,7 @@ dependencies = [
|
||||||
"cc",
|
"cc",
|
||||||
"clap",
|
"clap",
|
||||||
"cpal",
|
"cpal",
|
||||||
|
"gilrs",
|
||||||
"imgui",
|
"imgui",
|
||||||
"imgui-wgpu",
|
"imgui-wgpu",
|
||||||
"imgui-winit-support",
|
"imgui-winit-support",
|
||||||
|
@ -2175,6 +2278,18 @@ version = "0.2.2"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821"
|
checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "uuid"
|
||||||
|
version = "1.11.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "f8c5f0a0af699448548ad1a2fbf920fb4bee257eae39953ba95cb84891a0446a"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "vec_map"
|
||||||
|
version = "0.8.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "f1bddf1187be692e79c5ffeab891132dfb0f236ed36a43c7ed39f1165ee20191"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "version_check"
|
name = "version_check"
|
||||||
version = "0.9.5"
|
version = "0.9.5"
|
||||||
|
@ -2875,7 +2990,7 @@ dependencies = [
|
||||||
"calloop",
|
"calloop",
|
||||||
"cfg_aliases 0.2.1",
|
"cfg_aliases 0.2.1",
|
||||||
"concurrent-queue",
|
"concurrent-queue",
|
||||||
"core-foundation",
|
"core-foundation 0.9.4",
|
||||||
"core-graphics",
|
"core-graphics",
|
||||||
"cursor-icon",
|
"cursor-icon",
|
||||||
"dpi",
|
"dpi",
|
||||||
|
|
|
@ -9,6 +9,7 @@ bitflags = "2"
|
||||||
bytemuck = { version = "1", features = ["derive"] }
|
bytemuck = { version = "1", features = ["derive"] }
|
||||||
clap = { version = "4", features = ["derive"] }
|
clap = { version = "4", features = ["derive"] }
|
||||||
cpal = "0.15"
|
cpal = "0.15"
|
||||||
|
gilrs = "0.11"
|
||||||
imgui = { version = "0.12", features = ["tables-api"] }
|
imgui = { version = "0.12", features = ["tables-api"] }
|
||||||
imgui-wgpu = { git = "https://github.com/SupernaviX/imgui-wgpu-rs", rev = "5bb8673" }
|
imgui-wgpu = { git = "https://github.com/SupernaviX/imgui-wgpu-rs", rev = "5bb8673" }
|
||||||
imgui-winit-support = "0.13"
|
imgui-winit-support = "0.13"
|
||||||
|
|
60
src/app.rs
60
src/app.rs
|
@ -1,10 +1,7 @@
|
||||||
use std::{
|
use std::{collections::HashMap, fmt::Debug, thread};
|
||||||
collections::HashMap,
|
|
||||||
fmt::Debug,
|
|
||||||
sync::{Arc, RwLock},
|
|
||||||
};
|
|
||||||
|
|
||||||
use game::GameWindow;
|
use game::GameWindow;
|
||||||
|
use gilrs::{EventType, Gilrs};
|
||||||
use input::InputWindow;
|
use input::InputWindow;
|
||||||
use winit::{
|
use winit::{
|
||||||
application::ApplicationHandler,
|
application::ApplicationHandler,
|
||||||
|
@ -14,9 +11,9 @@ use winit::{
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
controller::ControllerState,
|
controller::ControllerManager,
|
||||||
emulator::{EmulatorClient, EmulatorCommand, SimId},
|
emulator::{EmulatorClient, SimId},
|
||||||
input::InputMapper,
|
input::MappingProvider,
|
||||||
};
|
};
|
||||||
|
|
||||||
mod common;
|
mod common;
|
||||||
|
@ -26,21 +23,26 @@ mod input;
|
||||||
pub struct App {
|
pub struct App {
|
||||||
windows: HashMap<WindowId, Box<dyn AppWindow>>,
|
windows: HashMap<WindowId, Box<dyn AppWindow>>,
|
||||||
client: EmulatorClient,
|
client: EmulatorClient,
|
||||||
input_mapper: Arc<RwLock<InputMapper>>,
|
mappings: MappingProvider,
|
||||||
controller: ControllerState,
|
controllers: ControllerManager,
|
||||||
proxy: EventLoopProxy<UserEvent>,
|
proxy: EventLoopProxy<UserEvent>,
|
||||||
player_2_window: Option<WindowId>,
|
player_2_window: Option<WindowId>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl App {
|
impl App {
|
||||||
pub fn new(client: EmulatorClient, proxy: EventLoopProxy<UserEvent>) -> Self {
|
pub fn new(client: EmulatorClient, proxy: EventLoopProxy<UserEvent>) -> Self {
|
||||||
let input_mapper = Arc::new(RwLock::new(InputMapper::new()));
|
let mappings = MappingProvider::new();
|
||||||
let controller = ControllerState::new(input_mapper.clone());
|
let controllers = ControllerManager::new(client.clone(), &mappings);
|
||||||
|
{
|
||||||
|
let mappings = mappings.clone();
|
||||||
|
let proxy = proxy.clone();
|
||||||
|
thread::spawn(|| process_gamepad_input(mappings, proxy));
|
||||||
|
}
|
||||||
Self {
|
Self {
|
||||||
windows: HashMap::new(),
|
windows: HashMap::new(),
|
||||||
client,
|
client,
|
||||||
input_mapper,
|
mappings,
|
||||||
controller,
|
controllers,
|
||||||
proxy,
|
proxy,
|
||||||
player_2_window: None,
|
player_2_window: None,
|
||||||
}
|
}
|
||||||
|
@ -65,10 +67,7 @@ impl ApplicationHandler<UserEvent> for App {
|
||||||
event: WindowEvent,
|
event: WindowEvent,
|
||||||
) {
|
) {
|
||||||
if let WindowEvent::KeyboardInput { event, .. } = &event {
|
if let WindowEvent::KeyboardInput { event, .. } = &event {
|
||||||
if let Some((sim_id, pressed)) = self.controller.key_event(event) {
|
self.controllers.handle_key_event(event);
|
||||||
self.client
|
|
||||||
.send_command(EmulatorCommand::SetKeys(sim_id, pressed));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
let Some(window) = self.windows.get_mut(&window_id) else {
|
let Some(window) = self.windows.get_mut(&window_id) else {
|
||||||
return;
|
return;
|
||||||
|
@ -80,7 +79,7 @@ impl ApplicationHandler<UserEvent> for App {
|
||||||
match event {
|
match event {
|
||||||
UserEvent::OpenInputWindow => {
|
UserEvent::OpenInputWindow => {
|
||||||
let window =
|
let window =
|
||||||
InputWindow::new(event_loop, self.input_mapper.clone(), self.proxy.clone());
|
InputWindow::new(event_loop, self.mappings.clone(), self.proxy.clone());
|
||||||
self.windows.insert(window.id(), Box::new(window));
|
self.windows.insert(window.id(), Box::new(window));
|
||||||
}
|
}
|
||||||
UserEvent::OpenPlayer2Window => {
|
UserEvent::OpenPlayer2Window => {
|
||||||
|
@ -102,6 +101,9 @@ impl ApplicationHandler<UserEvent> for App {
|
||||||
}
|
}
|
||||||
self.windows.remove(&window_id);
|
self.windows.remove(&window_id);
|
||||||
}
|
}
|
||||||
|
UserEvent::GamepadEvent(event) => {
|
||||||
|
self.controllers.handle_gamepad_event(&event);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -139,4 +141,24 @@ pub enum UserEvent {
|
||||||
OpenInputWindow,
|
OpenInputWindow,
|
||||||
OpenPlayer2Window,
|
OpenPlayer2Window,
|
||||||
Close(WindowId),
|
Close(WindowId),
|
||||||
|
GamepadEvent(gilrs::Event),
|
||||||
|
}
|
||||||
|
|
||||||
|
fn process_gamepad_input(mappings: MappingProvider, proxy: EventLoopProxy<UserEvent>) {
|
||||||
|
let Ok(mut gilrs) = Gilrs::new() else {
|
||||||
|
eprintln!("could not connect gamepad listener");
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
while let Some(event) = gilrs.next_event_blocking(None) {
|
||||||
|
if event.event == EventType::Connected {
|
||||||
|
let Some(gamepad) = gilrs.connected_gamepad(event.id) else {
|
||||||
|
continue;
|
||||||
|
};
|
||||||
|
mappings.map_gamepad(SimId::Player1, &gamepad);
|
||||||
|
}
|
||||||
|
if proxy.send_event(UserEvent::GamepadEvent(event)).is_err() {
|
||||||
|
// main thread has closed! we done
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,4 @@
|
||||||
use std::{
|
use std::time::Instant;
|
||||||
sync::{Arc, RwLock},
|
|
||||||
time::Instant,
|
|
||||||
};
|
|
||||||
|
|
||||||
use winit::{
|
use winit::{
|
||||||
dpi::LogicalSize,
|
dpi::LogicalSize,
|
||||||
|
@ -11,7 +8,7 @@ use winit::{
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
emulator::{SimId, VBKey},
|
emulator::{SimId, VBKey},
|
||||||
input::InputMapper,
|
input::MappingProvider,
|
||||||
};
|
};
|
||||||
|
|
||||||
use super::{
|
use super::{
|
||||||
|
@ -22,7 +19,7 @@ use super::{
|
||||||
pub struct InputWindow {
|
pub struct InputWindow {
|
||||||
window: WindowState,
|
window: WindowState,
|
||||||
imgui: ImguiState,
|
imgui: ImguiState,
|
||||||
input_mapper: Arc<RwLock<InputMapper>>,
|
mappings: MappingProvider,
|
||||||
proxy: EventLoopProxy<UserEvent>,
|
proxy: EventLoopProxy<UserEvent>,
|
||||||
now_binding: Option<(SimId, VBKey)>,
|
now_binding: Option<(SimId, VBKey)>,
|
||||||
}
|
}
|
||||||
|
@ -47,7 +44,7 @@ const KEY_NAMES: [(VBKey, &str); 14] = [
|
||||||
impl InputWindow {
|
impl InputWindow {
|
||||||
pub fn new(
|
pub fn new(
|
||||||
event_loop: &ActiveEventLoop,
|
event_loop: &ActiveEventLoop,
|
||||||
input_mapper: Arc<RwLock<InputMapper>>,
|
mappings: MappingProvider,
|
||||||
proxy: EventLoopProxy<UserEvent>,
|
proxy: EventLoopProxy<UserEvent>,
|
||||||
) -> Self {
|
) -> Self {
|
||||||
let window = WindowStateBuilder::new(event_loop)
|
let window = WindowStateBuilder::new(event_loop)
|
||||||
|
@ -58,7 +55,7 @@ impl InputWindow {
|
||||||
Self {
|
Self {
|
||||||
window,
|
window,
|
||||||
imgui,
|
imgui,
|
||||||
input_mapper,
|
mappings,
|
||||||
now_binding: None,
|
now_binding: None,
|
||||||
proxy,
|
proxy,
|
||||||
}
|
}
|
||||||
|
@ -89,10 +86,11 @@ impl InputWindow {
|
||||||
let ui = context.new_frame();
|
let ui = context.new_frame();
|
||||||
|
|
||||||
let mut render_key_bindings = |sim_id: SimId| {
|
let mut render_key_bindings = |sim_id: SimId| {
|
||||||
|
let mappings = self.mappings.for_sim(sim_id);
|
||||||
if let Some(table) = ui.begin_table("controls", 2) {
|
if let Some(table) = ui.begin_table("controls", 2) {
|
||||||
let binding_names = {
|
let binding_names = {
|
||||||
let mapper = self.input_mapper.read().unwrap();
|
let mapping = mappings.read().unwrap();
|
||||||
mapper.binding_names(&sim_id)
|
mapping.keyboard_mapping_names()
|
||||||
};
|
};
|
||||||
ui.table_next_row();
|
ui.table_next_row();
|
||||||
|
|
||||||
|
@ -115,8 +113,8 @@ impl InputWindow {
|
||||||
});
|
});
|
||||||
ui.same_line();
|
ui.same_line();
|
||||||
if ui.button(format!("Clear##{name}")) {
|
if ui.button(format!("Clear##{name}")) {
|
||||||
let mut mapper = self.input_mapper.write().unwrap();
|
let mut mapping = mappings.write().unwrap();
|
||||||
mapper.clear_binding(sim_id, key);
|
mapping.clear_keyboard_mappings(key);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -185,8 +183,8 @@ impl InputWindow {
|
||||||
let Some((sim_id, vb)) = self.now_binding.take() else {
|
let Some((sim_id, vb)) = self.now_binding.take() else {
|
||||||
return;
|
return;
|
||||||
};
|
};
|
||||||
let mut mapper = self.input_mapper.write().unwrap();
|
let mut mappings = self.mappings.for_sim(sim_id).write().unwrap();
|
||||||
mapper.bind_key(sim_id, vb, event.physical_key);
|
mappings.add_keyboard_mapping(vb, event.physical_key);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,48 +1,128 @@
|
||||||
use std::sync::{Arc, RwLock};
|
use std::sync::{Arc, RwLock};
|
||||||
|
|
||||||
use winit::event::{ElementState, KeyEvent};
|
use gilrs::{ev::Code, Event as GamepadEvent, EventType, GamepadId};
|
||||||
|
use winit::{
|
||||||
use crate::{
|
event::{ElementState, KeyEvent},
|
||||||
emulator::{SimId, VBKey},
|
keyboard::PhysicalKey,
|
||||||
input::InputMapper,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
pub struct ControllerState {
|
use crate::{
|
||||||
input_mapper: Arc<RwLock<InputMapper>>,
|
emulator::{EmulatorClient, EmulatorCommand, SimId, VBKey},
|
||||||
pressed: Vec<VBKey>,
|
input::{InputMapping, MappingProvider},
|
||||||
|
};
|
||||||
|
|
||||||
|
pub struct Controller {
|
||||||
|
pub sim_id: SimId,
|
||||||
|
state: VBKey,
|
||||||
|
mapping: Arc<RwLock<InputMapping>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ControllerState {
|
impl Controller {
|
||||||
pub fn new(input_mapper: Arc<RwLock<InputMapper>>) -> Self {
|
pub fn new(sim_id: SimId, mappings: &MappingProvider) -> Self {
|
||||||
Self {
|
Self {
|
||||||
input_mapper,
|
sim_id,
|
||||||
pressed: vec![VBKey::SGN; 2],
|
state: VBKey::SGN,
|
||||||
|
mapping: mappings.for_sim(sim_id).clone(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn key_event(&mut self, event: &KeyEvent) -> Option<(SimId, VBKey)> {
|
pub fn key_event(&mut self, event: &KeyEvent) -> Option<VBKey> {
|
||||||
let (sim_id, input) = self.key_event_to_input(event)?;
|
let keys = self.map_keys(&event.physical_key)?;
|
||||||
let pressed = &mut self.pressed[sim_id.to_index()];
|
|
||||||
match event.state {
|
match event.state {
|
||||||
ElementState::Pressed => {
|
ElementState::Pressed => self.update_state(keys, VBKey::empty()),
|
||||||
if pressed.contains(input) {
|
ElementState::Released => self.update_state(VBKey::empty(), keys),
|
||||||
return None;
|
|
||||||
}
|
|
||||||
pressed.insert(input);
|
|
||||||
Some((sim_id, *pressed))
|
|
||||||
}
|
|
||||||
ElementState::Released => {
|
|
||||||
if !pressed.contains(input) {
|
|
||||||
return None;
|
|
||||||
}
|
|
||||||
pressed.remove(input);
|
|
||||||
Some((sim_id, *pressed))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn key_event_to_input(&self, event: &KeyEvent) -> Option<(SimId, VBKey)> {
|
pub fn gamepad_event(&mut self, event: &GamepadEvent) -> Option<VBKey> {
|
||||||
let mapper = self.input_mapper.read().unwrap();
|
let (pressed, released) = match event.event {
|
||||||
mapper.key_event(event)
|
EventType::ButtonPressed(_, code) => {
|
||||||
|
let mappings = self.map_button(&event.id, &code)?;
|
||||||
|
(mappings, VBKey::empty())
|
||||||
|
}
|
||||||
|
EventType::ButtonReleased(_, code) => {
|
||||||
|
let mappings = self.map_button(&event.id, &code)?;
|
||||||
|
(VBKey::empty(), mappings)
|
||||||
|
}
|
||||||
|
EventType::AxisChanged(_, value, code) => {
|
||||||
|
let (neg, pos) = self.map_axis(&event.id, &code)?;
|
||||||
|
let mut pressed = VBKey::empty();
|
||||||
|
let mut released = VBKey::empty();
|
||||||
|
if value < -0.75 {
|
||||||
|
pressed = pressed.union(neg);
|
||||||
|
}
|
||||||
|
if value > 0.75 {
|
||||||
|
pressed = pressed.union(pos);
|
||||||
|
}
|
||||||
|
if value > -0.65 {
|
||||||
|
released = released.union(neg);
|
||||||
|
}
|
||||||
|
if value < 0.65 {
|
||||||
|
released = released.union(pos);
|
||||||
|
}
|
||||||
|
(pressed, released)
|
||||||
|
}
|
||||||
|
_ => {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
self.update_state(pressed, released)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn update_state(&mut self, pressed: VBKey, released: VBKey) -> Option<VBKey> {
|
||||||
|
let old_state = self.state;
|
||||||
|
self.state = self.state.union(pressed).difference(released);
|
||||||
|
if self.state != old_state {
|
||||||
|
Some(self.state)
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn map_keys(&self, key: &PhysicalKey) -> Option<VBKey> {
|
||||||
|
self.mapping.read().unwrap().map_keyboard(key)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn map_button(&self, id: &GamepadId, code: &Code) -> Option<VBKey> {
|
||||||
|
self.mapping.read().unwrap().map_button(id, code)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn map_axis(&self, id: &GamepadId, code: &Code) -> Option<(VBKey, VBKey)> {
|
||||||
|
self.mapping.read().unwrap().map_axis(id, code)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct ControllerManager {
|
||||||
|
client: EmulatorClient,
|
||||||
|
controllers: [Controller; 2],
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ControllerManager {
|
||||||
|
pub fn new(client: EmulatorClient, mappings: &MappingProvider) -> Self {
|
||||||
|
Self {
|
||||||
|
client,
|
||||||
|
controllers: [
|
||||||
|
Controller::new(SimId::Player1, mappings),
|
||||||
|
Controller::new(SimId::Player2, mappings),
|
||||||
|
],
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn handle_key_event(&mut self, event: &KeyEvent) {
|
||||||
|
for controller in &mut self.controllers {
|
||||||
|
if let Some(pressed) = controller.key_event(event) {
|
||||||
|
self.client
|
||||||
|
.send_command(EmulatorCommand::SetKeys(controller.sim_id, pressed));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn handle_gamepad_event(&mut self, event: &GamepadEvent) {
|
||||||
|
for controller in &mut self.controllers {
|
||||||
|
if let Some(pressed) = controller.gamepad_event(event) {
|
||||||
|
self.client
|
||||||
|
.send_command(EmulatorCommand::SetKeys(controller.sim_id, pressed));
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
217
src/input.rs
217
src/input.rs
|
@ -1,88 +1,167 @@
|
||||||
use std::collections::HashMap;
|
use std::{
|
||||||
|
collections::{hash_map::Entry, HashMap},
|
||||||
use winit::{
|
sync::{Arc, RwLock},
|
||||||
event::KeyEvent,
|
|
||||||
keyboard::{KeyCode, PhysicalKey},
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
use gilrs::{ev::Code, Axis, Button, Gamepad, GamepadId};
|
||||||
|
use winit::keyboard::{KeyCode, PhysicalKey};
|
||||||
|
|
||||||
use crate::emulator::{SimId, VBKey};
|
use crate::emulator::{SimId, VBKey};
|
||||||
|
|
||||||
pub struct InputMapper {
|
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
|
||||||
vb_bindings: HashMap<SimId, HashMap<VBKey, PhysicalKey>>,
|
struct DeviceId(u16, u16);
|
||||||
key_bindings: HashMap<PhysicalKey, (SimId, VBKey)>,
|
|
||||||
|
pub struct GamepadMapping {
|
||||||
|
buttons: HashMap<Code, VBKey>,
|
||||||
|
axes: HashMap<Code, (VBKey, VBKey)>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl InputMapper {
|
impl GamepadMapping {
|
||||||
pub fn new() -> Self {
|
fn for_gamepad(gamepad: &Gamepad) -> Self {
|
||||||
let mut mapper = Self {
|
let mut buttons = HashMap::new();
|
||||||
vb_bindings: HashMap::new(),
|
let mut default_button = |btn: Button, key: VBKey| {
|
||||||
key_bindings: HashMap::new(),
|
if let Some(code) = gamepad.button_code(btn) {
|
||||||
|
buttons.insert(code, key);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
mapper.bind_key(SimId::Player1, VBKey::SEL, PhysicalKey::Code(KeyCode::KeyA));
|
default_button(Button::South, VBKey::A);
|
||||||
mapper.bind_key(SimId::Player1, VBKey::STA, PhysicalKey::Code(KeyCode::KeyS));
|
default_button(Button::West, VBKey::B);
|
||||||
mapper.bind_key(SimId::Player1, VBKey::B, PhysicalKey::Code(KeyCode::KeyD));
|
default_button(Button::RightTrigger, VBKey::RT);
|
||||||
mapper.bind_key(SimId::Player1, VBKey::A, PhysicalKey::Code(KeyCode::KeyF));
|
default_button(Button::LeftTrigger, VBKey::LT);
|
||||||
mapper.bind_key(SimId::Player1, VBKey::LT, PhysicalKey::Code(KeyCode::KeyE));
|
default_button(Button::Start, VBKey::STA);
|
||||||
mapper.bind_key(SimId::Player1, VBKey::RT, PhysicalKey::Code(KeyCode::KeyR));
|
default_button(Button::Select, VBKey::SEL);
|
||||||
mapper.bind_key(SimId::Player1, VBKey::RU, PhysicalKey::Code(KeyCode::KeyI));
|
|
||||||
mapper.bind_key(SimId::Player1, VBKey::RL, PhysicalKey::Code(KeyCode::KeyJ));
|
let mut axes = HashMap::new();
|
||||||
mapper.bind_key(SimId::Player1, VBKey::RD, PhysicalKey::Code(KeyCode::KeyK));
|
let mut default_axis = |axis: Axis, neg: VBKey, pos: VBKey| {
|
||||||
mapper.bind_key(SimId::Player1, VBKey::RR, PhysicalKey::Code(KeyCode::KeyL));
|
if let Some(code) = gamepad.axis_code(axis) {
|
||||||
mapper.bind_key(
|
axes.insert(code, (neg, pos));
|
||||||
SimId::Player1,
|
}
|
||||||
VBKey::LU,
|
};
|
||||||
PhysicalKey::Code(KeyCode::ArrowUp),
|
default_axis(Axis::LeftStickX, VBKey::LL, VBKey::LR);
|
||||||
);
|
default_axis(Axis::LeftStickY, VBKey::LD, VBKey::LU);
|
||||||
mapper.bind_key(
|
default_axis(Axis::RightStickX, VBKey::RL, VBKey::RR);
|
||||||
SimId::Player1,
|
default_axis(Axis::RightStickY, VBKey::RD, VBKey::RU);
|
||||||
VBKey::LL,
|
default_axis(Axis::DPadX, VBKey::LL, VBKey::LR);
|
||||||
PhysicalKey::Code(KeyCode::ArrowLeft),
|
default_axis(Axis::DPadY, VBKey::LD, VBKey::LU);
|
||||||
);
|
|
||||||
mapper.bind_key(
|
Self { buttons, axes }
|
||||||
SimId::Player1,
|
}
|
||||||
VBKey::LD,
|
}
|
||||||
PhysicalKey::Code(KeyCode::ArrowDown),
|
|
||||||
);
|
#[derive(Default)]
|
||||||
mapper.bind_key(
|
pub struct InputMapping {
|
||||||
SimId::Player1,
|
keys: HashMap<PhysicalKey, VBKey>,
|
||||||
VBKey::LR,
|
gamepads: HashMap<GamepadId, Arc<RwLock<GamepadMapping>>>,
|
||||||
PhysicalKey::Code(KeyCode::ArrowRight),
|
}
|
||||||
);
|
|
||||||
mapper
|
impl InputMapping {
|
||||||
|
pub fn map_keyboard(&self, key: &PhysicalKey) -> Option<VBKey> {
|
||||||
|
self.keys.get(key).copied()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn binding_names(&self, sim_id: &SimId) -> HashMap<VBKey, String> {
|
pub fn map_button(&self, id: &GamepadId, code: &Code) -> Option<VBKey> {
|
||||||
let Some(bindings) = self.vb_bindings.get(sim_id) else {
|
let mappings = self.gamepads.get(id)?.read().unwrap();
|
||||||
return HashMap::new();
|
mappings.buttons.get(code).copied()
|
||||||
};
|
}
|
||||||
bindings
|
|
||||||
.iter()
|
pub fn map_axis(&self, id: &GamepadId, code: &Code) -> Option<(VBKey, VBKey)> {
|
||||||
.map(|(k, v)| {
|
let mappings = self.gamepads.get(id)?.read().unwrap();
|
||||||
let name = match v {
|
mappings.axes.get(code).copied()
|
||||||
PhysicalKey::Code(code) => format!("{code:?}"),
|
}
|
||||||
k => format!("{:?}", k),
|
|
||||||
};
|
pub fn add_keyboard_mapping(&mut self, key: VBKey, keyboard_key: PhysicalKey) {
|
||||||
(*k, name)
|
let entry = self.keys.entry(keyboard_key).or_insert(VBKey::empty());
|
||||||
|
*entry = entry.union(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn clear_keyboard_mappings(&mut self, key: VBKey) {
|
||||||
|
self.keys.retain(|_, keys| {
|
||||||
|
*keys = keys.difference(key);
|
||||||
|
*keys != VBKey::empty()
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn keyboard_mapping_names(&self) -> HashMap<VBKey, String> {
|
||||||
|
let mut results: HashMap<VBKey, Vec<String>> = HashMap::new();
|
||||||
|
for (keyboard_key, keys) in &self.keys {
|
||||||
|
let name = match keyboard_key {
|
||||||
|
PhysicalKey::Code(code) => format!("{code:?}"),
|
||||||
|
k => format!("{:?}", k),
|
||||||
|
};
|
||||||
|
for key in keys.iter() {
|
||||||
|
results.entry(key).or_default().push(name.clone());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
results
|
||||||
|
.into_iter()
|
||||||
|
.map(|(k, mut v)| {
|
||||||
|
v.sort();
|
||||||
|
(k, v.join(", "))
|
||||||
})
|
})
|
||||||
.collect()
|
.collect()
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn bind_key(&mut self, sim_id: SimId, vb: VBKey, key: PhysicalKey) {
|
#[derive(Clone)]
|
||||||
let vb_bindings = self.vb_bindings.entry(sim_id).or_default();
|
pub struct MappingProvider {
|
||||||
if let Some(old) = vb_bindings.insert(vb, key) {
|
device_mappings: Arc<RwLock<HashMap<DeviceId, Arc<RwLock<GamepadMapping>>>>>,
|
||||||
self.key_bindings.remove(&old);
|
sim_mappings: HashMap<SimId, Arc<RwLock<InputMapping>>>,
|
||||||
}
|
}
|
||||||
self.key_bindings.insert(key, (sim_id, vb));
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn clear_binding(&mut self, sim_id: SimId, vb: VBKey) {
|
impl MappingProvider {
|
||||||
let vb_bindings = self.vb_bindings.entry(sim_id).or_default();
|
pub fn new() -> Self {
|
||||||
if let Some(old) = vb_bindings.remove(&vb) {
|
let mut mappings = HashMap::new();
|
||||||
self.key_bindings.remove(&old);
|
|
||||||
|
let mut p1_mappings = InputMapping::default();
|
||||||
|
let p2_mappings = InputMapping::default();
|
||||||
|
let mut default_key = |code, key| {
|
||||||
|
p1_mappings.add_keyboard_mapping(key, PhysicalKey::Code(code));
|
||||||
|
};
|
||||||
|
default_key(KeyCode::KeyA, VBKey::SEL);
|
||||||
|
default_key(KeyCode::KeyS, VBKey::STA);
|
||||||
|
default_key(KeyCode::KeyD, VBKey::B);
|
||||||
|
default_key(KeyCode::KeyF, VBKey::A);
|
||||||
|
default_key(KeyCode::KeyE, VBKey::LT);
|
||||||
|
default_key(KeyCode::KeyR, VBKey::RT);
|
||||||
|
default_key(KeyCode::KeyI, VBKey::RU);
|
||||||
|
default_key(KeyCode::KeyJ, VBKey::RL);
|
||||||
|
default_key(KeyCode::KeyK, VBKey::RD);
|
||||||
|
default_key(KeyCode::KeyL, VBKey::RR);
|
||||||
|
default_key(KeyCode::ArrowUp, VBKey::LU);
|
||||||
|
default_key(KeyCode::ArrowLeft, VBKey::LL);
|
||||||
|
default_key(KeyCode::ArrowDown, VBKey::LD);
|
||||||
|
default_key(KeyCode::ArrowRight, VBKey::LR);
|
||||||
|
|
||||||
|
mappings.insert(SimId::Player1, Arc::new(RwLock::new(p1_mappings)));
|
||||||
|
mappings.insert(SimId::Player2, Arc::new(RwLock::new(p2_mappings)));
|
||||||
|
Self {
|
||||||
|
device_mappings: Arc::new(RwLock::new(HashMap::new())),
|
||||||
|
sim_mappings: mappings,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn key_event(&self, event: &KeyEvent) -> Option<(SimId, VBKey)> {
|
pub fn for_sim(&self, sim_id: SimId) -> &Arc<RwLock<InputMapping>> {
|
||||||
self.key_bindings.get(&event.physical_key).copied()
|
self.sim_mappings.get(&sim_id).unwrap()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn map_gamepad(&self, sim_id: SimId, gamepad: &Gamepad) {
|
||||||
|
let device_id = DeviceId(
|
||||||
|
gamepad.vendor_id().unwrap_or_default(),
|
||||||
|
gamepad.product_id().unwrap_or_default(),
|
||||||
|
);
|
||||||
|
let mut lock = self.device_mappings.write().unwrap();
|
||||||
|
let gamepad_mapping = match lock.entry(device_id) {
|
||||||
|
Entry::Occupied(entry) => entry.get().clone(),
|
||||||
|
Entry::Vacant(entry) => {
|
||||||
|
let mappings = GamepadMapping::for_gamepad(gamepad);
|
||||||
|
entry.insert(Arc::new(RwLock::new(mappings))).clone()
|
||||||
|
}
|
||||||
|
};
|
||||||
|
drop(lock);
|
||||||
|
self.for_sim(sim_id)
|
||||||
|
.write()
|
||||||
|
.unwrap()
|
||||||
|
.gamepads
|
||||||
|
.insert(gamepad.id(), gamepad_mapping);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue