Add multi controller option (#110)

This commit is contained in:
Logan McNaughton 2025-01-07 12:34:36 +01:00 committed by GitHub
parent 774bb672fd
commit 89673df459
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 27 additions and 4 deletions

View File

@ -229,8 +229,12 @@ pub fn init(device: &mut device::Device) {
write: device::mempak::write,
};
device.pif.channels[0].pak_handler = Some(mempak_handler);
device.pif.channels[0].process = Some(device::controller::process);
for i in 0..4 {
if device.ui.config.input.controller_enabled[i] {
device.pif.channels[i].pak_handler = Some(mempak_handler);
device.pif.channels[i].process = Some(device::controller::process);
}
}
device.pif.channels[4].process = Some(device::cart::process)
}

View File

@ -15,6 +15,7 @@ pub struct Input {
pub input_profiles: std::collections::HashMap<String, InputProfile>,
pub input_profile_binding: [String; 4],
pub controller_assignment: [Option<String>; 4],
pub controller_enabled: [bool; 4],
}
#[derive(serde::Serialize, serde::Deserialize)]
pub struct Config {
@ -35,6 +36,7 @@ impl Config {
],
controller_assignment: [None, None, None, None],
input_profiles,
controller_enabled: [true, false, false, false],
},
}
}

View File

@ -9,6 +9,7 @@ pub struct GopherEguiApp {
selected_controller: [i32; 4],
selected_profile: [String; 4],
input_profiles: Vec<String>,
controller_enabled: [bool; 4],
}
fn get_input_profiles(game_ui: &ui::Ui) -> Vec<String> {
@ -63,11 +64,17 @@ impl GopherEguiApp {
selected_controller,
controllers: get_controllers(&game_ui),
input_profiles: get_input_profiles(&game_ui),
controller_enabled: game_ui.config.input.controller_enabled,
}
}
}
fn save_config(game_ui: &mut ui::Ui, selected_controller: [i32; 4], selected_profile: [String; 4]) {
fn save_config(
game_ui: &mut ui::Ui,
selected_controller: [i32; 4],
selected_profile: [String; 4],
controller_enabled: [bool; 4],
) {
let joystick_subsystem = game_ui.joystick_subsystem.as_ref().unwrap();
for (pos, item) in selected_controller.iter().enumerate() {
if *item != -1 {
@ -83,6 +90,7 @@ fn save_config(game_ui: &mut ui::Ui, selected_controller: [i32; 4], selected_pro
}
game_ui.config.input.input_profile_binding = selected_profile;
game_ui.config.input.controller_enabled = controller_enabled;
}
impl Drop for GopherEguiApp {
@ -92,6 +100,7 @@ impl Drop for GopherEguiApp {
&mut game_ui,
self.selected_controller,
self.selected_profile.clone(),
self.controller_enabled,
);
}
}
@ -138,6 +147,7 @@ impl eframe::App for GopherEguiApp {
let task = rfd::AsyncFileDialog::new().pick_file();
let selected_controller = self.selected_controller;
let selected_profile = self.selected_profile.clone();
let controller_enabled = self.controller_enabled;
execute(async move {
let file = task.await;
@ -151,7 +161,12 @@ impl eframe::App for GopherEguiApp {
}
let _ = std::fs::File::create(running_file.clone());
let mut device = device::Device::new();
save_config(&mut device.ui, selected_controller, selected_profile);
save_config(
&mut device.ui,
selected_controller,
selected_profile,
controller_enabled,
);
device::run_game(std::path::Path::new(file.path()), &mut device, false);
let _ = std::fs::remove_file(running_file.clone());
}
@ -173,11 +188,13 @@ impl eframe::App for GopherEguiApp {
ui.label("Controller Config:");
egui::Grid::new("some_unique_id").show(ui, |ui| {
ui.label("Port");
ui.label("Enabled");
ui.label("Profile");
ui.label("Controller");
ui.end_row();
for i in 0..4 {
ui.label(format!("{}", i + 1));
ui.checkbox(&mut self.controller_enabled[i], "");
egui::ComboBox::from_id_salt(format!("profile-combo-{}", i))
.selected_text(self.selected_profile[i].clone())