Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
2ce0edee76 | ||
|
|
d21cb4e5d0 | ||
|
|
6e36254203 | ||
|
|
5fd1957d74 | ||
|
|
19c595dd7d |
Generated
+1
-1
@@ -2122,7 +2122,7 @@ checksum = "09edd9e8b54e49e587e4f6295a7d29c3ea94d469cb40ab8ca70b288248a81db2"
|
||||
|
||||
[[package]]
|
||||
name = "lemur"
|
||||
version = "0.12.3"
|
||||
version = "0.12.4"
|
||||
dependencies = [
|
||||
"anyhow",
|
||||
"atoi",
|
||||
|
||||
+1
-1
@@ -4,7 +4,7 @@ description = "An emulator for the Virtual Boy."
|
||||
repository = "https://git.virtual-boy.com/PVB/lemur"
|
||||
publish = false
|
||||
license = "MIT"
|
||||
version = "0.12.3"
|
||||
version = "0.12.4"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
|
||||
+1
-1
Submodule shrooms-vb-core updated: 29ade46a0a...31b7403252
+117
-17
@@ -1,4 +1,7 @@
|
||||
use std::sync::{Arc, RwLock};
|
||||
use std::{
|
||||
collections::HashMap,
|
||||
sync::{Arc, RwLock},
|
||||
};
|
||||
|
||||
use gilrs::{Event as GamepadEvent, EventType, GamepadId, ev::Code};
|
||||
use winit::{
|
||||
@@ -8,12 +11,13 @@ use winit::{
|
||||
|
||||
use crate::{
|
||||
emulator::{EmulatorClient, EmulatorCommand, SimId, VBKey},
|
||||
input::{InputMapping, MappingProvider},
|
||||
input::{AxisMapping, InputMapping, MappingProvider},
|
||||
};
|
||||
|
||||
pub struct Controller {
|
||||
pub sim_id: SimId,
|
||||
state: VBKey,
|
||||
axis_values: HashMap<(GamepadId, Code), f32>,
|
||||
mapping: Arc<RwLock<InputMapping>>,
|
||||
}
|
||||
|
||||
@@ -22,6 +26,7 @@ impl Controller {
|
||||
Self {
|
||||
sim_id,
|
||||
state: VBKey::SGN,
|
||||
axis_values: HashMap::new(),
|
||||
mapping: mappings.for_sim(sim_id).clone(),
|
||||
}
|
||||
}
|
||||
@@ -45,20 +50,23 @@ impl Controller {
|
||||
(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);
|
||||
let mapping = self.map_axis(&event.id, &code)?;
|
||||
self.axis_values.insert((event.id, code), value);
|
||||
|
||||
let pair_value = mapping
|
||||
.pair
|
||||
.and_then(|p| self.axis_values.get(&(event.id, p)))
|
||||
.copied()
|
||||
.unwrap_or_default();
|
||||
let neg = mapping.neg;
|
||||
let pos = mapping.pos;
|
||||
|
||||
let (mut pressed, mut released) = axis_presses(value, pair_value, neg, pos);
|
||||
if let Some(other) = mapping.pair.and_then(|p| self.map_axis(&event.id, &p)) {
|
||||
let (other_pressed, other_released) =
|
||||
axis_presses(pair_value, value, other.neg, other.pos);
|
||||
pressed = pressed.union(other_pressed);
|
||||
released = released.union(other_released);
|
||||
}
|
||||
(pressed, released)
|
||||
}
|
||||
@@ -87,7 +95,7 @@ impl Controller {
|
||||
self.mapping.read().unwrap().map_button(id, code)
|
||||
}
|
||||
|
||||
fn map_axis(&self, id: &GamepadId, code: &Code) -> Option<(VBKey, VBKey)> {
|
||||
fn map_axis(&self, id: &GamepadId, code: &Code) -> Option<AxisMapping> {
|
||||
self.mapping.read().unwrap().map_axis(id, code)
|
||||
}
|
||||
}
|
||||
@@ -126,3 +134,95 @@ impl ControllerManager {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn axis_presses(value: f32, pair_value: f32, neg: VBKey, pos: VBKey) -> (VBKey, VBKey) {
|
||||
use std::f32::consts::FRAC_PI_3;
|
||||
|
||||
let mut pressed = VBKey::empty();
|
||||
let mut released = VBKey::empty();
|
||||
|
||||
let magnitude = value.hypot(pair_value);
|
||||
let abs_angle = pair_value.atan2(value).abs();
|
||||
|
||||
if magnitude < 0.65 {
|
||||
released = released.union(neg).union(pos);
|
||||
} else if abs_angle <= FRAC_PI_3 {
|
||||
// stick tilted towards positive
|
||||
released = released.union(neg);
|
||||
|
||||
if magnitude >= 0.75 {
|
||||
pressed = pressed.union(pos);
|
||||
}
|
||||
} else if abs_angle >= 2.0 * FRAC_PI_3 {
|
||||
// stick tilted towards negative
|
||||
released = released.union(pos);
|
||||
|
||||
if magnitude >= 0.75 {
|
||||
pressed = pressed.union(neg);
|
||||
}
|
||||
} else {
|
||||
released = released.union(neg).union(pos);
|
||||
}
|
||||
|
||||
(pressed, released)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::{VBKey, axis_presses};
|
||||
|
||||
const NEG: VBKey = VBKey::LL;
|
||||
const POS: VBKey = VBKey::LR;
|
||||
|
||||
const NONE: VBKey = VBKey::empty();
|
||||
const BOTH: VBKey = NEG.union(POS);
|
||||
|
||||
#[test]
|
||||
fn detects_no_input() {
|
||||
let (pressed, released) = axis_presses(0.0, 0.0, NEG, POS);
|
||||
assert_eq!(pressed, NONE);
|
||||
assert_eq!(released, BOTH);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn detects_pos_input() {
|
||||
let (pressed, released) = axis_presses(1.0, 0.0, NEG, POS);
|
||||
assert_eq!(pressed, POS);
|
||||
assert_eq!(released, NEG);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn detects_neg_input() {
|
||||
let (pressed, released) = axis_presses(-1.0, 0.0, NEG, POS);
|
||||
assert_eq!(pressed, NEG);
|
||||
assert_eq!(released, POS);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn respects_dead_zone() {
|
||||
let (pressed, released) = axis_presses(0.70, 0.0, NEG, POS);
|
||||
assert_eq!(pressed, NONE);
|
||||
assert_eq!(released, NEG);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn handles_diagonals_ok() {
|
||||
let (pressed, released) = axis_presses(0.6, 0.6, NEG, POS);
|
||||
assert_eq!(pressed, POS);
|
||||
assert_eq!(released, NEG);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ignores_diagonal_noise() {
|
||||
let (pressed, released) = axis_presses(-1.0, -0.1484245, NEG, POS);
|
||||
assert_eq!(pressed, NEG);
|
||||
assert_eq!(released, POS);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ignores_small_values() {
|
||||
let (pressed, released) = axis_presses(-0.1484245, -1.0, NEG, POS);
|
||||
assert_eq!(pressed, NONE);
|
||||
assert_eq!(released, BOTH);
|
||||
}
|
||||
}
|
||||
|
||||
+34
-23
@@ -52,12 +52,28 @@ pub trait Mappings {
|
||||
fn use_default_mappings(&mut self);
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Serialize, Deserialize)]
|
||||
pub struct AxisMapping {
|
||||
pub neg: VBKey,
|
||||
pub pos: VBKey,
|
||||
pub pair: Option<Code>,
|
||||
}
|
||||
impl Default for AxisMapping {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
neg: VBKey::empty(),
|
||||
pos: VBKey::empty(),
|
||||
pair: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct GamepadMapping {
|
||||
buttons: HashMap<Code, VBKey>,
|
||||
axes: HashMap<Code, (VBKey, VBKey)>,
|
||||
axes: HashMap<Code, AxisMapping>,
|
||||
default_buttons: HashMap<Code, VBKey>,
|
||||
default_axes: HashMap<Code, (VBKey, VBKey)>,
|
||||
default_axes: HashMap<Code, AxisMapping>,
|
||||
}
|
||||
|
||||
impl GamepadMapping {
|
||||
@@ -78,7 +94,8 @@ impl GamepadMapping {
|
||||
let mut default_axes = HashMap::new();
|
||||
let mut default_axis = |axis: Axis, neg: VBKey, pos: VBKey| {
|
||||
if let Some(code) = gamepad.axis_code(axis) {
|
||||
default_axes.insert(code, (neg, pos));
|
||||
let pair = axis.second_axis().and_then(|a| gamepad.axis_code(a));
|
||||
default_axes.insert(code, AxisMapping { neg, pos, pair });
|
||||
}
|
||||
};
|
||||
default_axis(Axis::LeftStickX, VBKey::LL, VBKey::LR);
|
||||
@@ -102,19 +119,13 @@ impl GamepadMapping {
|
||||
}
|
||||
|
||||
pub fn add_axis_neg_mapping(&mut self, key: VBKey, code: Code) {
|
||||
let entry = self
|
||||
.axes
|
||||
.entry(code)
|
||||
.or_insert((VBKey::empty(), VBKey::empty()));
|
||||
entry.0 = entry.0.union(key);
|
||||
let entry = self.axes.entry(code).or_default();
|
||||
entry.neg = entry.neg.union(key);
|
||||
}
|
||||
|
||||
pub fn add_axis_pos_mapping(&mut self, key: VBKey, code: Code) {
|
||||
let entry = self
|
||||
.axes
|
||||
.entry(code)
|
||||
.or_insert((VBKey::empty(), VBKey::empty()));
|
||||
entry.1 = entry.1.union(key);
|
||||
let entry = self.axes.entry(code).or_default();
|
||||
entry.pos = entry.pos.union(key);
|
||||
}
|
||||
|
||||
fn save_mappings(&self) -> PersistedGamepadMapping {
|
||||
@@ -145,11 +156,11 @@ impl GamepadMapping {
|
||||
impl Mappings for GamepadMapping {
|
||||
fn mapping_names(&self) -> HashMap<VBKey, Vec<String>> {
|
||||
let mut results: HashMap<VBKey, Vec<String>> = HashMap::new();
|
||||
for (axis, (left_keys, right_keys)) in &self.axes {
|
||||
for key in left_keys.iter() {
|
||||
for (axis, mapping) in &self.axes {
|
||||
for key in mapping.neg.iter() {
|
||||
results.entry(key).or_default().push(format!("-{axis}"));
|
||||
}
|
||||
for key in right_keys.iter() {
|
||||
for key in mapping.pos.iter() {
|
||||
results.entry(key).or_default().push(format!("+{axis}"));
|
||||
}
|
||||
}
|
||||
@@ -162,10 +173,10 @@ impl Mappings for GamepadMapping {
|
||||
}
|
||||
|
||||
fn clear_mappings(&mut self, key: VBKey) {
|
||||
self.axes.retain(|_, (left, right)| {
|
||||
*left = left.difference(key);
|
||||
*right = right.difference(key);
|
||||
!(left.is_empty() && right.is_empty())
|
||||
self.axes.retain(|_, mapping| {
|
||||
mapping.neg = mapping.neg.difference(key);
|
||||
mapping.pos = mapping.pos.difference(key);
|
||||
!(mapping.neg.is_empty() && mapping.pos.is_empty())
|
||||
});
|
||||
self.buttons.retain(|_, keys| {
|
||||
*keys = keys.difference(key);
|
||||
@@ -200,7 +211,7 @@ impl InputMapping {
|
||||
mappings.buttons.get(code).copied()
|
||||
}
|
||||
|
||||
pub fn map_axis(&self, id: &GamepadId, code: &Code) -> Option<(VBKey, VBKey)> {
|
||||
pub fn map_axis(&self, id: &GamepadId, code: &Code) -> Option<AxisMapping> {
|
||||
let mappings = self.gamepads.get(id)?.read().unwrap();
|
||||
mappings.axes.get(code).copied()
|
||||
}
|
||||
@@ -459,9 +470,9 @@ struct PersistedKeyboardMapping {
|
||||
#[derive(Serialize, Deserialize)]
|
||||
struct PersistedGamepadMapping {
|
||||
buttons: Vec<(Code, VBKey)>,
|
||||
axes: Vec<(Code, (VBKey, VBKey))>,
|
||||
axes: Vec<(Code, AxisMapping)>,
|
||||
default_buttons: Vec<(Code, VBKey)>,
|
||||
default_axes: Vec<(Code, (VBKey, VBKey))>,
|
||||
default_axes: Vec<(Code, AxisMapping)>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Serialize, Deserialize, PartialEq, Eq, Hash)]
|
||||
|
||||
@@ -1081,8 +1081,8 @@ impl<'a> SourceCoordCalculator<'a> {
|
||||
(sx, sy)
|
||||
}
|
||||
SourceParam::Affine(affine) => {
|
||||
let sx = affine_coord(affine.src_x, x, affine.dx, affine.src_parallax.min(0));
|
||||
let sy = affine_coord(affine.src_y, x, affine.dy, affine.src_parallax.min(0));
|
||||
let sx = affine_coord(affine.src_x, x, affine.dx, -(affine.src_parallax.min(0)));
|
||||
let sy = affine_coord(affine.src_y, x, affine.dy, -(affine.src_parallax.min(0)));
|
||||
(sx, sy)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user