1
0
Fork 0
mirror of https://github.com/jugeeya/UltimateTrainingModpack.git synced 2024-11-20 00:46:34 +00:00

Refactor Menu Input (#584)

This commit is contained in:
jugeeya 2023-08-10 09:34:28 -07:00 committed by GitHub
parent 77f527c99a
commit 00268b3d72
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 136 additions and 81 deletions

View file

@ -1,3 +1,4 @@
use std::collections::HashMap;
use std::fs; use std::fs;
use lazy_static::lazy_static; use lazy_static::lazy_static;
@ -89,6 +90,22 @@ lazy_static! {
pub static ref P1_CONTROLLER_STATE: Mutex<Controller> = Mutex::new(Controller::default()); pub static ref P1_CONTROLLER_STATE: Mutex<Controller> = Mutex::new(Controller::default());
} }
#[derive(Eq, PartialEq, Hash, Copy, Clone)]
enum DirectionButton {
DpadLeft,
LLeft,
RLeft,
DpadDown,
LDown,
RDown,
DpadRight,
LRight,
RRight,
DpadUp,
LUp,
RUp,
}
pub fn handle_final_input_mapping( pub fn handle_final_input_mapping(
player_idx: i32, player_idx: i32,
controller_struct: &SomeControllerStruct, controller_struct: &SomeControllerStruct,
@ -100,102 +117,143 @@ pub fn handle_final_input_mapping(
if QUICK_MENU_ACTIVE { if QUICK_MENU_ACTIVE {
// If we're here, remove all other presses // If we're here, remove all other presses
*out = MappedInputs::empty(); *out = MappedInputs::empty();
}
}
}
}
pub unsafe fn quick_menu_loop() { let mut received_input = false;
loop {
std::thread::sleep(std::time::Duration::from_secs(10));
let mut received_input = true;
loop {
std::thread::sleep(std::time::Duration::from_millis(16));
if !QUICK_MENU_ACTIVE { const DIRECTION_HOLD_REPEAT_FRAMES: u32 = 20;
continue; use DirectionButton::*;
} let directions = [
DpadLeft, LLeft, RLeft, DpadDown, LDown, RDown, DpadRight, LRight, RRight,
DpadUp, LUp, RUp,
];
let mut direction_hold_frames: HashMap<DirectionButton, u32> =
directions.iter().map(|config| (*config, 0)).collect();
// Check for all controllers unplugged // Check for all controllers unplugged
let mut potential_controller_ids = (0..8).collect::<Vec<u32>>(); let mut potential_controller_ids = (0..8).collect::<Vec<u32>>();
potential_controller_ids.push(0x20); potential_controller_ids.push(0x20);
if potential_controller_ids if potential_controller_ids
.iter() .iter()
.all(|i| GetNpadStyleSet(i as *const _).flags == 0) .all(|i| GetNpadStyleSet(i as *const _).flags == 0)
{ {
QUICK_MENU_ACTIVE = false;
continue;
}
let p1_controller_state = *P1_CONTROLLER_STATE.data_ptr();
let style = p1_controller_state.style;
let button_presses = p1_controller_state.just_down;
let app = &mut *QUICK_MENU_APP.data_ptr();
button_mapping(ButtonConfig::A, style, button_presses).then(|| {
app.on_a();
received_input = true;
});
button_mapping(ButtonConfig::B, style, button_presses).then(|| {
received_input = true;
if app.page != AppPage::SUBMENU {
app.on_b()
} else if FRAME_COUNTER > MENU_CLOSE_WAIT_FRAMES {
// Leave menu.
QUICK_MENU_ACTIVE = false; QUICK_MENU_ACTIVE = false;
FRAME_COUNTER = 0; return;
let menu_json = app.get_menu_selections();
set_menu_from_json(&menu_json);
EVENT_QUEUE.push(Event::menu_open(menu_json));
} }
});
button_mapping(ButtonConfig::X, style, button_presses).then(|| {
app.save_defaults();
received_input = true;
});
button_mapping(ButtonConfig::Y, style, button_presses).then(|| {
app.reset_all_submenus();
received_input = true;
});
button_mapping(ButtonConfig::ZL, style, button_presses).then(|| { let p1_controller_state = *P1_CONTROLLER_STATE.data_ptr();
app.previous_tab(); let style = p1_controller_state.style;
received_input = true; let button_presses = p1_controller_state.just_down;
});
button_mapping(ButtonConfig::ZR, style, button_presses).then(|| {
app.next_tab();
received_input = true;
});
button_mapping(ButtonConfig::R, style, button_presses).then(|| {
app.reset_current_submenu();
received_input = true;
});
(button_presses.dpad_left() || button_presses.l_left() || button_presses.r_left()) let button_current_held = p1_controller_state.current_buttons;
let button_released = p1_controller_state.just_release;
direction_hold_frames
.iter_mut()
.for_each(|(direction, frames)| {
let (still_held, released) = match direction {
DpadLeft => {
(button_current_held.dpad_left(), button_released.dpad_left())
}
LLeft => (button_current_held.l_left(), button_released.l_left()),
RLeft => (button_current_held.r_left(), button_released.r_left()),
DpadDown => {
(button_current_held.dpad_down(), button_released.dpad_down())
}
LDown => (button_current_held.l_down(), button_released.l_down()),
RDown => (button_current_held.r_down(), button_released.r_down()),
DpadRight => (
button_current_held.dpad_right(),
button_released.dpad_right(),
),
LRight => (button_current_held.l_right(), button_released.l_right()),
RRight => (button_current_held.r_right(), button_released.r_right()),
DpadUp => (button_current_held.dpad_up(), button_released.dpad_up()),
LUp => (button_current_held.l_up(), button_released.l_up()),
RUp => (button_current_held.r_up(), button_released.r_up()),
};
if still_held {
*frames += 1;
}
if released {
*frames = 0;
}
});
let app = &mut *QUICK_MENU_APP.data_ptr();
button_mapping(ButtonConfig::A, style, button_presses).then(|| {
app.on_a();
received_input = true;
});
button_mapping(ButtonConfig::B, style, button_presses).then(|| {
received_input = true;
if app.page != AppPage::SUBMENU {
app.on_b()
} else if FRAME_COUNTER > MENU_CLOSE_WAIT_FRAMES {
// Leave menu.
QUICK_MENU_ACTIVE = false;
FRAME_COUNTER = 0;
let menu_json = app.get_menu_selections();
set_menu_from_json(&menu_json);
EVENT_QUEUE.push(Event::menu_open(menu_json));
}
});
button_mapping(ButtonConfig::X, style, button_presses).then(|| {
app.save_defaults();
received_input = true;
});
button_mapping(ButtonConfig::Y, style, button_presses).then(|| {
app.reset_all_submenus();
received_input = true;
});
button_mapping(ButtonConfig::ZL, style, button_presses).then(|| {
app.previous_tab();
received_input = true;
});
button_mapping(ButtonConfig::ZR, style, button_presses).then(|| {
app.next_tab();
received_input = true;
});
button_mapping(ButtonConfig::R, style, button_presses).then(|| {
app.reset_current_submenu();
received_input = true;
});
let hold_condition = |direction_button| {
direction_hold_frames[direction_button] > DIRECTION_HOLD_REPEAT_FRAMES
};
(button_presses.dpad_left()
|| button_presses.l_left()
|| button_presses.r_left()
|| [DpadLeft, LLeft, RLeft].iter().any(hold_condition))
.then(|| { .then(|| {
app.on_left(); app.on_left();
received_input = true;
}); });
(button_presses.dpad_right() || button_presses.l_right() || button_presses.r_right()) (button_presses.dpad_right()
|| button_presses.l_right()
|| button_presses.r_right()
|| [DpadRight, LRight, RRight].iter().any(hold_condition))
.then(|| { .then(|| {
app.on_right(); app.on_right();
received_input = true;
}); });
(button_presses.dpad_up() || button_presses.l_up() || button_presses.r_up()).then( (button_presses.dpad_up()
|| { || button_presses.l_up()
|| button_presses.r_up()
|| [DpadUp, LUp, RUp].iter().any(hold_condition))
.then(|| {
app.on_up(); app.on_up();
received_input = true; });
}, (button_presses.dpad_down()
); || button_presses.l_down()
(button_presses.dpad_down() || button_presses.l_down() || button_presses.r_down()) || button_presses.r_down()
|| [DpadDown, LDown, RDown].iter().any(hold_condition))
.then(|| { .then(|| {
app.on_down(); app.on_down();
received_input = true;
}); });
if received_input { if received_input {
received_input = false; set_menu_from_json(&app.get_menu_selections());
set_menu_from_json(&app.get_menu_selections()); }
} }
} }
} }

View file

@ -25,7 +25,6 @@ use crate::common::*;
use crate::consts::TRAINING_MODPACK_ROOT; use crate::consts::TRAINING_MODPACK_ROOT;
use crate::events::{Event, EVENT_QUEUE}; use crate::events::{Event, EVENT_QUEUE};
use crate::logging::*; use crate::logging::*;
use crate::menu::quick_menu_loop;
use crate::training::ui::notifications::notification; use crate::training::ui::notifications::notification;
pub mod common; pub mod common;
@ -160,6 +159,4 @@ pub fn main() {
} }
std::thread::spawn(events_loop); std::thread::spawn(events_loop);
std::thread::spawn(|| unsafe { quick_menu_loop() });
} }