diff --git a/src/Tools/psxreadmap/src/lib.rs b/src/Tools/psxreadmap/src/lib.rs index 2f1e1964..5eca68e4 100644 --- a/src/Tools/psxreadmap/src/lib.rs +++ b/src/Tools/psxreadmap/src/lib.rs @@ -26,7 +26,7 @@ pub enum UIState { Terminated } -pub enum MenuSelection { +enum MenuSelection { Stats, Quit, } @@ -51,15 +51,32 @@ impl MenuSelection { } } -pub struct ListSelection { - pub id: usize +#[derive(Copy, Clone)] +enum SectionMode { + UpperSection, + LowerSection, } -impl ListSelection { - pub fn increment(&mut self, max: usize) { - let new_selection = self.id + 1; +impl SectionMode { + pub fn as_id(&self) -> usize { + match self { + SectionMode::UpperSection => 0, + SectionMode::LowerSection => 1, + } + } +} - self.id = { +struct SectionSelection { + mode: SectionMode, + id: [usize; 2] +} + +impl SectionSelection { + fn increment(&mut self, max: usize) { + let cur_id = &mut self.id[self.mode.as_id()]; + let new_selection = *cur_id + 1; + + *cur_id = { if new_selection >= max { 0 } @@ -70,10 +87,11 @@ impl ListSelection { } } - pub fn decrement(&mut self, max: usize) { - let new_selection = self.id - 1; + fn decrement(&mut self, max: usize) { + let cur_id = &mut self.id[self.mode.as_id()]; + let new_selection = *cur_id - 1; - self.id = { + *cur_id = { if new_selection >= max { max - 1 } @@ -83,11 +101,27 @@ impl ListSelection { } } } + + fn switch_section_to(&mut self, section_mode: SectionMode) { + self.mode = section_mode; + } + + fn get_section_mode_id(&self) -> usize { + self.mode.as_id() + } + + fn get_selection_for(&self, section_mode: SectionMode) -> usize { + self.id[section_mode.as_id()] + } + + fn get_current_selection(&self) -> usize { + self.get_selection_for(self.mode) + } } -impl Default for ListSelection { +impl Default for SectionSelection { fn default() -> Self { - Self{id: 0} + Self{mode: SectionMode::UpperSection, id: [0, 0]} } } @@ -97,19 +131,17 @@ pub struct ConsoleUI { terminal: Terminal, console_size: Rect, menu_selection: MenuSelection, - section_selection: ListSelection, - first_section_id: usize, - second_section_id: usize, + section_selection: SectionSelection, } impl ConsoleUI { pub fn new(rx: EventReceiver, terminal: Terminal) -> ConsoleUI { - ConsoleUI{data: ConsoleUIData::default(), rx, terminal, console_size: Rect::default(), menu_selection: MenuSelection::Stats, section_selection: ListSelection::default(), first_section_id: 0, second_section_id: 0} + ConsoleUI{data: ConsoleUIData::default(), rx, terminal, console_size: Rect::default(), menu_selection: MenuSelection::Stats, section_selection: SectionSelection::default()} } pub fn update_data(&mut self, memory_map: &MemoryMap) -> Result<(), Error> { - self.data = ConsoleUIData::from(memory_map)?; - self.section_selection = ListSelection::default(); + self.data = ConsoleUIData::from(memory_map)?; + self.section_selection = SectionSelection::default(); Ok(()) } @@ -118,6 +150,28 @@ impl ConsoleUI { let input_result = { match self.rx.recv()? { Event::Input(event) => { + if matches!(self.menu_selection, MenuSelection::Stats) { + match event.code { + KeyCode::Down => { + self.section_selection.increment(self.data.section_info.len()); + return Ok(UIState::Render); + }, + KeyCode::Up => { + self.section_selection.decrement(self.data.section_info.len()); + return Ok(UIState::Render); + }, + KeyCode::Char('a') => { + self.section_selection.switch_section_to(SectionMode::UpperSection); + return Ok(UIState::Render); + }, + KeyCode::Char('s') => { + self.section_selection.switch_section_to(SectionMode::LowerSection); + return Ok(UIState::Render); + }, + _ => () + } + } + match event.code { KeyCode::Char('q') => Ok(UIState::Terminated), KeyCode::Char('s') => { @@ -132,26 +186,6 @@ impl ConsoleUI { self.menu_selection = self.menu_selection.next(); Ok(UIState::Render) }, - KeyCode::Down => { - if matches!(self.menu_selection, MenuSelection::Stats) { - self.section_selection.increment(self.data.section_info.len()); - Ok(UIState::Render) - } - - else { - Ok(UIState::Alive) - } - }, - KeyCode::Up => { - if matches!(self.menu_selection, MenuSelection::Stats) { - self.section_selection.decrement(self.data.section_info.len()); - Ok(UIState::Render) - } - - else { - Ok(UIState::Alive) - } - }, KeyCode::Enter => { if matches!(self.menu_selection, MenuSelection::Quit) { Ok(UIState::Terminated) @@ -178,7 +212,6 @@ impl ConsoleUI { } }; - self.first_section_id = self.section_selection.id; input_result } @@ -233,7 +266,8 @@ impl ConsoleUI { Ok(()) } - fn render_stats(frame: &mut ConsoleFrame, layout: Rect, content_selection: &ListSelection, data: &mut ConsoleUIData) { + fn render_stats(frame: &mut ConsoleFrame, layout: Rect, section_selection: &SectionSelection, data: &mut ConsoleUIData) { + const HIGHLIGHT_COLOR:[Color;2] = [Color::Cyan, Color::Red]; let layout = Layout::default().direction(Direction::Vertical).constraints([ Constraint::Length(3), Constraint::Max(3), @@ -252,13 +286,13 @@ impl ConsoleUI { .style(Style::default().fg(Color::White)) .border_type(BorderType::Plain) ) - .highlight_style(Style::default().bg(Color::Cyan)); + .highlight_style(Style::default().bg(HIGHLIGHT_COLOR[section_selection.get_section_mode_id()])); - let first_mem_gauge = Self::create_mem_gauge_from(&data.section_info[content_selection.id], Color::Cyan); - let second_mem_gauge = Self::create_mem_gauge_from(&data.section_info[0], Color::Red); + let first_mem_gauge = Self::create_mem_gauge_from(&data.section_info[section_selection.get_selection_for(SectionMode::UpperSection)], HIGHLIGHT_COLOR[0]); + let second_mem_gauge = Self::create_mem_gauge_from(&data.section_info[section_selection.get_selection_for(SectionMode::LowerSection)], HIGHLIGHT_COLOR[1]); let mut list_state = ListState::default(); - list_state.select(Some(content_selection.id)); + list_state.select(Some(section_selection.get_current_selection())); frame.render_stateful_widget(content_list, layout[0], &mut list_state); frame.render_widget(first_mem_gauge, layout[1]);