Skip to content

Commit

Permalink
Fixes for Quick Menu Multi-selections (#362)
Browse files Browse the repository at this point in the history
  • Loading branch information
jugeeya authored Jun 1, 2022
1 parent 9cc4c6b commit 283279b
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 19 deletions.
8 changes: 4 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ pub fn main() {
std::thread::sleep(std::time::Duration::from_secs(10));
let menu;
unsafe {
menu = crate::common::consts::get_menu();
menu = consts::get_menu();
}

let mut app = training_mod_tui::App::new(menu);
Expand All @@ -157,7 +157,7 @@ pub fn main() {
let mut has_slept_millis = 0;
let render_frames = 5;
let mut url = String::new();
let button_presses = &mut common::menu::BUTTON_PRESSES;
let button_presses = &mut menu::BUTTON_PRESSES;
let mut received_input = true;
loop {
button_presses.a.read_press().then(|| {
Expand All @@ -174,7 +174,7 @@ pub fn main() {
{
// Leave menu.
menu::QUICK_MENU_ACTIVE = false;
crate::menu::set_menu_from_url(url.as_str());
menu::set_menu_from_url(url.as_str());
println!("URL: {}", url.as_str());
}
});
Expand Down Expand Up @@ -210,7 +210,7 @@ pub fn main() {
}
has_slept_millis = 16;
if !menu::QUICK_MENU_ACTIVE {
app = training_mod_tui::App::new(crate::common::consts::get_menu());
app = training_mod_tui::App::new(consts::get_menu());
set_should_display_text_to_screen(false);
continue;
}
Expand Down
4 changes: 2 additions & 2 deletions src/training/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,8 @@ fn once_per_frame_per_fighter(
}

unsafe {
if crate::common::menu::menu_condition(module_accessor) {
crate::common::menu::spawn_menu();
if menu::menu_condition(module_accessor) {
menu::spawn_menu();
}

input_record::get_command_flag_cat(module_accessor);
Expand Down
12 changes: 4 additions & 8 deletions training_mod_consts/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1571,27 +1571,23 @@ pub unsafe fn get_menu() -> UiMenu<'static> {
let non_ui_menu = MENU;
let url_params = non_ui_menu.to_url_params(false);
let toggle_values_all = url_params.split("&");
let mut sub_menu_id_to_vals : HashMap<&str, Vec<u32>> = HashMap::new();
let mut sub_menu_id_to_vals : HashMap<&str, u32> = HashMap::new();
for toggle_values in toggle_values_all {
let toggle_value_split = toggle_values.split('=').collect::<Vec<&str>>();
let mut sub_menu_id = toggle_value_split[0];
if sub_menu_id.is_empty() { continue }
sub_menu_id = sub_menu_id.strip_prefix("__").unwrap_or(sub_menu_id);

let bits: u32 = toggle_value_split[1].parse().unwrap_or(0);
if sub_menu_id_to_vals.contains_key(sub_menu_id) {
sub_menu_id_to_vals.get_mut(sub_menu_id).unwrap().push(bits);
} else {
sub_menu_id_to_vals.insert(sub_menu_id, vec![bits]);
}
let full_bits: u32 = toggle_value_split[1].parse().unwrap_or(0);
sub_menu_id_to_vals.insert(sub_menu_id, full_bits);
}
overall_menu.tabs.iter_mut()
.for_each(|tab| {
tab.tab_submenus.iter_mut().for_each(|sub_menu| {
let sub_menu_id = sub_menu.submenu_id;
sub_menu.toggles.iter_mut().for_each(|toggle| {
if sub_menu_id_to_vals.contains_key(sub_menu_id) &&
sub_menu_id_to_vals[sub_menu_id].contains(&(toggle.toggle_value as u32)) {
(sub_menu_id_to_vals[sub_menu_id] & (toggle.toggle_value as u32) != 0) {
toggle.checked = true
}
})
Expand Down
39 changes: 34 additions & 5 deletions training_mod_tui/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,47 @@ use tui::Terminal;

use training_mod_consts::*;

fn test_backend_setup(ui_menu: UiMenu) -> Result<
(Terminal<training_mod_tui::TestBackend>, training_mod_tui::App),
Box<dyn Error>> {
let app = training_mod_tui::App::new(ui_menu);
let backend = tui::backend::TestBackend::new(75, 15);
let terminal = Terminal::new(backend)?;
let mut state = tui::widgets::ListState::default();
state.select(Some(1));

Ok((terminal, app))
}

#[test]
fn ensure_menu_retains_multi_selections() -> Result<(), Box<dyn Error>> {
let menu;
unsafe {
menu = get_menu();
println!("MENU.miss_tech_state: {}", MENU.miss_tech_state);
}

let (mut terminal, mut app) = test_backend_setup(menu)?;
let mut url = String::new();
let _frame_res = terminal.draw(|f| url = training_mod_tui::ui(f, &mut app))?;

unsafe {
// At this point, we didn't change the menu at all; we should still see all missed tech flags.
assert_eq!(get_menu_from_url(MENU, url.as_str(), false).miss_tech_state,
MissTechFlags::all());
}

Ok(())
}

fn main() -> Result<(), Box<dyn Error>> {
let menu;
unsafe {
menu = get_menu();
}

#[cfg(not(feature = "has_terminal"))] {
let mut app = training_mod_tui::App::new(menu);
let backend = tui::backend::TestBackend::new(75, 15);
let mut terminal = Terminal::new(backend)?;
let mut state = tui::widgets::ListState::default();
state.select(Some(1));
let (mut terminal, mut app) = test_backend_setup(menu)?;
let mut url = String::new();
let frame_res = terminal.draw(|f| url = training_mod_tui::ui(f, &mut app))?;

Expand Down

0 comments on commit 283279b

Please sign in to comment.