Skip to content

Commit 24b1cf5

Browse files
committed
Sample joystick as dpad
1 parent df2a390 commit 24b1cf5

File tree

4 files changed

+33
-10
lines changed

4 files changed

+33
-10
lines changed

src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -493,7 +493,7 @@ pub fn actual_main() {
493493
let mut screen_layout = emu_unsafe.get_mut().settings.screen_layout();
494494
let arm7_emu = emu_unsafe.get_mut().settings.arm7_emu();
495495
loop {
496-
let pause = match presenter.poll_event() {
496+
let pause = match presenter.poll_event(&emu_unsafe.get_mut().settings) {
497497
PresentEvent::Inputs { mut keymap, touch } => {
498498
if let Some((x, y)) = touch {
499499
let (x_norm, y_norm) = screen_layout.normalize_touch_points(x, y);

src/presenter/linux.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ impl Presenter {
173173
show_pause_menu(self, gpu_renderer, settings)
174174
}
175175

176-
pub fn poll_event(&mut self) -> PresentEvent {
176+
pub fn poll_event(&mut self, _: &Settings) -> PresentEvent {
177177
for event in self.event_pump.poll_iter() {
178178
match event {
179179
Event::KeyDown {

src/presenter/vita.rs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,9 @@ impl Presenter {
129129
}
130130
}
131131

132-
pub fn poll_event(&mut self) -> PresentEvent {
132+
pub fn poll_event(&mut self, settings: &Settings) -> PresentEvent {
133+
let mut stick_keymap = 0xFFFFFFFF;
134+
133135
unsafe {
134136
let pressed = MaybeUninit::<SceCtrlData>::uninit();
135137
let mut pressed = pressed.assume_init();
@@ -177,6 +179,21 @@ impl Presenter {
177179
}
178180
}
179181

182+
if settings.joystick_as_dpad() {
183+
let stick_x = (pressed.lx as f32 - 127.0) / 127.0;
184+
let stick_y = (pressed.ly as f32 - 127.0) / 127.0;
185+
let length_threshold = 0.8;
186+
if stick_x * stick_x + stick_y * stick_y > length_threshold * length_threshold {
187+
const STICK_MAPPING: [((f32, f32), Keycode); 4] = [((-1.0, 0.0), Keycode::Left), ((0.0, -1.0), Keycode::Up), ((1.0, 0.0), Keycode::Right), ((0.0, 1.0), Keycode::Down)];
188+
for ((x, y), guest_key) in STICK_MAPPING {
189+
let dot = stick_x * x + stick_y * y;
190+
if dot > 0.5 {
191+
stick_keymap &= !(1 << guest_key as u8);
192+
}
193+
}
194+
}
195+
}
196+
180197
let touch_report = MaybeUninit::<SceTouchData>::uninit();
181198
let mut touch_report = touch_report.assume_init();
182199
sceTouchPeek(SCE_TOUCH_PORT_FRONT, &mut touch_report, 1);
@@ -191,7 +208,7 @@ impl Presenter {
191208
}
192209
}
193210
PresentEvent::Inputs {
194-
keymap: self.keymap,
211+
keymap: self.keymap & stick_keymap,
195212
touch: self.touch_points,
196213
}
197214
}

src/settings.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ lazy_static! {
161161
Setting::new("Screen Layout", "Press PS + L Trigger or PS + R Trigger to cycle through layouts in game.", ScreenLayout::settings_value(), true),
162162
Setting::new("Swap screens", "Press PS + X to swap screens in game.", SettingValue::Bool(false), true),
163163
Setting::new("Language", "Some ROMs only come with one language. Make sure yours is multilingual.", Language::iter().into(), false),
164+
Setting::new("Joystick as D-Pad", "", SettingValue::Bool(true), true),
164165
Setting::new("Framelimit", "Limits gamespeed to 60fps", SettingValue::Bool(true), true),
165166
Setting::new("Audio", "Disabling audio can give a performance boost", SettingValue::Bool(true), true),
166167
Setting::new(
@@ -180,15 +181,16 @@ lazy_static! {
180181
}
181182

182183
#[derive(Clone)]
183-
pub struct Settings([Setting; 7]);
184+
pub struct Settings([Setting; 8]);
184185

185186
const SCREEN_LAYOUT_SETTING: usize = 0;
186187
const SWAP_SCREEN_SETTING: usize = 1;
187188
const LANGUAGE_SETTING: usize = 2;
188-
const FRAMELIMIT_SETTING: usize = 3;
189-
const AUDIO_SETTING: usize = 4;
190-
const ARM7_EMU_SETTING: usize = 5;
191-
const ARM7_BLOCK_VALIDATION_SETTING: usize = 6;
189+
const JOYSTICK_AS_DPAD_SETTING: usize = 3;
190+
const FRAMELIMIT_SETTING: usize = 4;
191+
const AUDIO_SETTING: usize = 5;
192+
const ARM7_EMU_SETTING: usize = 6;
193+
const ARM7_BLOCK_VALIDATION_SETTING: usize = 7;
192194

193195
impl Settings {
194196
pub fn screen_layout(&self) -> ScreenLayout {
@@ -200,6 +202,10 @@ impl Settings {
200202
}
201203
}
202204

205+
pub fn joystick_as_dpad(&self) -> bool {
206+
unsafe { self.0[JOYSTICK_AS_DPAD_SETTING].value.as_bool().unwrap_unchecked() }
207+
}
208+
203209
pub fn framelimit(&self) -> bool {
204210
unsafe { self.0[FRAMELIMIT_SETTING].value.as_bool().unwrap_unchecked() }
205211
}
@@ -241,7 +247,7 @@ impl Settings {
241247
*self.0[ARM7_BLOCK_VALIDATION_SETTING].value.as_bool_mut().unwrap() = value;
242248
}
243249

244-
pub fn get_all_mut(&mut self) -> &mut [Setting; 7] {
250+
pub fn get_all_mut(&mut self) -> &mut [Setting; 8] {
245251
&mut self.0
246252
}
247253
}

0 commit comments

Comments
 (0)