2
0
mirror of https://github.com/AxioDL/metaforce.git synced 2025-12-13 08:06:10 +00:00

Input: Add onControllerAdded/Removed callbacks and also display the controller name in the input viewer

This commit is contained in:
2022-02-09 00:54:53 -08:00
parent 52fd54bc3e
commit a6b2d66e1e
12 changed files with 175 additions and 59 deletions

View File

@@ -36,8 +36,10 @@ struct AppDelegate {
virtual void onSpecialKeyUp(SpecialKey key) noexcept = 0;
// Controller
virtual void onControllerButton(uint32_t idx, ControllerButton button, bool pressed) noexcept = 0;
virtual void onControllerAxis(uint32_t idx, ControllerAxis axis, int16_t value) noexcept = 0;
virtual void onControllerAdded(uint32_t which) noexcept = 0;
virtual void onControllerRemoved(uint32_t which) noexcept = 0;
virtual void onControllerButton(uint32_t which, ControllerButton button, bool pressed) noexcept = 0;
virtual void onControllerAxis(uint32_t which, ControllerAxis axis, int16_t value) noexcept = 0;
// virtual void resized([[maybe_unused]] const WindowSize& rect, [[maybe_unused]] bool sync) noexcept {}
// virtual void mouseDown([[maybe_unused]] const SWindowCoord& coord, [[maybe_unused]] EMouseButton button,

View File

@@ -15,6 +15,8 @@ void App_onCharKeyUp(AppDelegate& cb, uint8_t code) noexcept;
void App_onSpecialKeyDown(AppDelegate& cb, SpecialKey key, bool isRepeat) noexcept;
void App_onSpecialKeyUp(AppDelegate& cb, SpecialKey key) noexcept;
// Controller
void App_onControllerButton(AppDelegate& cb, uint32_t idx, ControllerButton button, bool pressed) noexcept;
void App_onControllerAxis(AppDelegate& cb, uint32_t idx, ControllerAxis axis, int16_t value) noexcept;
void App_onControllerAdded(AppDelegate& cb, uint32_t which) noexcept;
void App_onControllerRemoved(AppDelegate& cb, uint32_t which) noexcept;
void App_onControllerButton(AppDelegate& cb, uint32_t which, ControllerButton button, bool pressed) noexcept;
void App_onControllerAxis(AppDelegate& cb, uint32_t which, ControllerAxis axis, int16_t value) noexcept;
} // namespace aurora

View File

@@ -28,10 +28,16 @@ void App_onSpecialKeyUp(AppDelegate& cb, SpecialKey key) noexcept {
}
// Controller
void App_onControllerButton(AppDelegate& cb, uint32_t idx, ControllerButton button, bool pressed) noexcept {
cb.onControllerButton(idx, button, pressed);
void App_onControllerAdded(AppDelegate& cb, uint32_t which) noexcept {
cb.onControllerAdded(which);
}
void App_onControllerAxis(AppDelegate& cb, uint32_t idx, ControllerAxis axis, int16_t value) noexcept {
cb.onControllerAxis(idx, axis, value);
void App_onControllerRemoved(AppDelegate& cb, uint32_t which) noexcept {
cb.onControllerRemoved(which);
}
void App_onControllerButton(AppDelegate& cb, uint32_t which, ControllerButton button, bool pressed) noexcept {
cb.onControllerButton(which, button, pressed);
}
void App_onControllerAxis(AppDelegate& cb, uint32_t which, ControllerAxis axis, int16_t value) noexcept {
cb.onControllerAxis(which, axis, value);
}
} // namespace aurora

View File

@@ -3,7 +3,12 @@ use sdl2::controller::{Axis, Button};
use crate::{
app_run, get_args, get_backend, get_backend_string, get_dxt_compression_supported,
get_window_size,
sdl::{get_controller_player_index, set_controller_player_index},
sdl::{
get_controller_player_index,
set_controller_player_index,
is_controller_gamecube,
get_controller_name
},
set_fullscreen, set_window_title, App, WindowContext,
};
@@ -33,6 +38,8 @@ pub(crate) mod ffi {
);
pub(crate) fn App_onSpecialKeyUp(cb: Pin<&mut AppDelegate>, key: SpecialKey);
// Controller
pub(crate) fn App_onControllerAdded(cb: Pin<&mut AppDelegate>, which: u32);
pub(crate) fn App_onControllerRemoved(cb: Pin<&mut AppDelegate>, which: u32);
pub(crate) fn App_onControllerButton(
cb: Pin<&mut AppDelegate>,
idx: u32,
@@ -162,6 +169,8 @@ pub(crate) mod ffi {
fn set_fullscreen(v: bool);
fn get_controller_player_index(which: u32) -> i32;
fn set_controller_player_index(which: u32, index: i32);
fn is_controller_gamecube(which: u32) -> bool;
fn get_controller_name(which: u32) -> String;
}
}
impl From<Button> for ffi::ControllerButton {

View File

@@ -38,17 +38,18 @@ pub(crate) fn poll_sdl_events(
.add_mapping(new_mapping.as_str())
.expect("Failed to overwrite mapping");
}
state.open_controllers.insert(controller.instance_id(), controller);
let instance_id: u32 = controller.instance_id();
state.open_controllers.insert(instance_id, controller);
unsafe { ffi::App_onControllerAdded(delegate.as_mut().unwrap_unchecked(), instance_id); }
}
Err(err) => {
log::warn!("Failed to open SDL controller {} ({:?})", which, err);
}
}
// TODO app connected event
}
Event::ControllerDeviceRemoved { which, .. } => {
unsafe { ffi::App_onControllerRemoved(delegate.as_mut().unwrap_unchecked(), which); }
state.open_controllers.remove(&which);
// TODO app disconnected event
}
Event::ControllerButtonDown { which, button, .. } => unsafe {
ffi::App_onControllerButton(
@@ -135,3 +136,15 @@ pub(crate) fn set_controller_player_index(which: u32, index: i32) {
c.set_player_index(index);
}
}
pub(crate) fn is_controller_gamecube(which: u32) -> bool {
get_app().sdl.open_controllers.get(&which)
.map_or(false, |c| c.name()
.to_lowercase()
.eq("nintendo gamecube controller"))
}
pub(crate) fn get_controller_name(which: u32) -> String {
get_app().sdl.open_controllers.get(&which).map_or(String::from(""), |c| c.name())
}