68 lines
2.7 KiB
Rust

use anyhow::anyhow;
use prometeu_core::model::ButtonId;
/// Mapping of physical button names to their virtual Button ID.
///
/// These constants match the `ButtonId` enum in `prometeu-core`.
pub const BTN_UP: u32 = ButtonId::Up as u32;
pub const BTN_DOWN: u32 = ButtonId::Down as u32;
pub const BTN_LEFT: u32 = ButtonId::Left as u32;
pub const BTN_RIGHT: u32 = ButtonId::Right as u32;
pub const BTN_A: u32 = ButtonId::A as u32;
pub const BTN_B: u32 = ButtonId::B as u32;
pub const BTN_X: u32 = ButtonId::X as u32;
pub const BTN_Y: u32 = ButtonId::Y as u32;
pub const BTN_L: u32 = ButtonId::L as u32;
pub const BTN_R: u32 = ButtonId::R as u32;
pub const BTN_START: u32 = ButtonId::Start as u32;
pub const BTN_SELECT: u32 = ButtonId::Select as u32;
/// Translates a string identifier (e.g., "up") into a numeric Button ID.
pub fn map_btn_name(btn_name: &str) -> anyhow::Result<u32> {
match btn_name.to_lowercase().as_str() {
"up" => Ok(BTN_UP),
"down" => Ok(BTN_DOWN),
"left" => Ok(BTN_LEFT),
"right" => Ok(BTN_RIGHT),
"a" => Ok(BTN_A),
"b" => Ok(BTN_B),
"x" => Ok(BTN_X),
"y" => Ok(BTN_Y),
"l" => Ok(BTN_L),
"r" => Ok(BTN_R),
"start" => Ok(BTN_START),
"select" => Ok(BTN_SELECT),
_ => Err(anyhow!("Unsupported button: {}. Expected one of: up, down, left, right, a, b, x, y, l, r, start, select.", btn_name)),
}
}
/// Translates a pad state name (e.g., "pressed") into the corresponding syscall name.
pub fn map_pad_state(state_name: &str) -> anyhow::Result<&'static str> {
match state_name {
"down" => Ok("input.getPad"),
"pressed" => Ok("input.getPadPressed"),
"released" => Ok("input.getPadReleased"),
"holdFrames" => Ok("input.getPadHold"),
_ => Err(anyhow!("Unsupported button state: {}. Expected one of: down, pressed, released, holdFrames.", state_name)),
}
}
/// Translates a touch property name (e.g., "x") into the corresponding syscall name.
pub fn map_touch_prop(prop_name: &str) -> anyhow::Result<&'static str> {
match prop_name {
"x" => Ok("touch.getX"),
"y" => Ok("touch.getY"),
_ => Err(anyhow!("Unsupported touch property: {}. Expected one of: x, y.", prop_name)),
}
}
/// Translates a touch button state name (e.g., "down") into the corresponding syscall name.
pub fn map_touch_button_state(state_name: &str) -> anyhow::Result<&'static str> {
match state_name {
"down" => Ok("touch.isDown"),
"pressed" => Ok("touch.isPressed"),
"released" => Ok("touch.isReleased"),
"holdFrames" => Ok("touch.getHold"),
_ => Err(anyhow!("Unsupported touch button state: {}. Expected one of: down, pressed, released, holdFrames.", state_name)),
}
}