From e3fc41fce3be4d04bf205346d5333ccefb7ce505 Mon Sep 17 00:00:00 2001 From: Jaby Blubb Date: Sun, 20 Aug 2023 09:15:24 +0200 Subject: [PATCH] Display top level section information --- src/Tools/psxreadmap/src/lib.rs | 136 ++++++++++++++++++++++---------- 1 file changed, 93 insertions(+), 43 deletions(-) diff --git a/src/Tools/psxreadmap/src/lib.rs b/src/Tools/psxreadmap/src/lib.rs index ada4d42c..402ab37e 100644 --- a/src/Tools/psxreadmap/src/lib.rs +++ b/src/Tools/psxreadmap/src/lib.rs @@ -70,21 +70,42 @@ enum SectionMode { LowerSection, } -impl SectionMode { - pub fn get_id(&self) -> usize { - match self { - SectionMode::UpperSection => 0, - SectionMode::LowerSection => 1, - } +impl ID for SectionMode { + fn get_id(&self) -> usize { + *self as usize } } -struct SectionSelection { - mode: SectionMode, +impl Default for SectionMode { + fn default() -> Self { + SectionMode::UpperSection + } +} + +#[derive(Copy, Clone)] +enum ListSectionMode { + TopSection, + Section, +} + +impl ID for ListSectionMode { + fn get_id(&self) -> usize { + *self as usize + } +} + +impl Default for ListSectionMode { + fn default() -> Self { + ListSectionMode::TopSection + } +} + +struct DualSelection { + mode: T, id: [usize; 2] } -impl SectionSelection { +impl DualSelection { fn increment(&mut self, max: usize) { let cur_id = &mut self.id[self.mode.get_id()]; let new_selection = *cur_id + 1; @@ -101,7 +122,7 @@ impl SectionSelection { } fn decrement(&mut self, max: usize) { - let cur_id = &mut self.id[self.mode.get_id()]; + let cur_id = &mut self.id[self.mode.get_id()%2]; let new_selection = *cur_id - 1; *cur_id = { @@ -115,7 +136,7 @@ impl SectionSelection { } } - fn switch_section_to(&mut self, section_mode: SectionMode) { + fn switch_section_to(&mut self, section_mode: T) { self.mode = section_mode; } @@ -123,38 +144,47 @@ impl SectionSelection { self.mode.get_id() } - fn get_selection_for(&self, section_mode: SectionMode) -> usize { + fn get_selection_for(&self, section_mode: T) -> usize { self.id[section_mode.get_id()] } - fn get_current_selection(&self) -> usize { - self.get_selection_for(self.mode) + fn get_current_selection(&self) -> ListState { + let mut state = ListState::default(); + + state.select(Some(self.get_selection_for(self.mode))); + state } } -impl Default for SectionSelection { +impl Default for DualSelection { fn default() -> Self { - Self{mode: SectionMode::UpperSection, id: [0, 0]} + Self{mode: T::default(), id: [0, 0]} } } +trait ID { + fn get_id(&self) -> usize; +} + pub struct ConsoleUI { - data: ConsoleUIData, - rx: EventReceiver, - terminal: Terminal, - console_size: Rect, - menu_selection: MenuSelection, - section_selection: SectionSelection, + data: ConsoleUIData, + rx: EventReceiver, + terminal: Terminal, + console_size: Rect, + menu_selection: MenuSelection, + section_selection: DualSelection, + list_section_selection: DualSelection, } 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: SectionSelection::default()} + ConsoleUI{data: ConsoleUIData::default(), rx, terminal, console_size: Rect::default(), menu_selection: MenuSelection::Stats, section_selection: DualSelection::default(), list_section_selection: DualSelection::default()} } pub fn update_data(&mut self, memory_map: &MemoryMap) -> Result<(), Error> { - self.data = ConsoleUIData::from(memory_map)?; - self.section_selection = SectionSelection::default(); + self.data = ConsoleUIData::from(memory_map)?; + self.section_selection = DualSelection::default(); + self.list_section_selection = DualSelection::default(); Ok(()) } @@ -249,10 +279,10 @@ impl ConsoleUI { frame.render_widget(menu_bar, main_layout[0]); match self.menu_selection { MenuSelection::Stats => { - Self::render_stats(frame, main_layout[1], &self.section_selection, &mut self.data); + Self::render_stats(frame, main_layout[1], &self.section_selection, &self.data); }, MenuSelection::List => { - Self::render_list(frame, main_layout[1]); + Self::render_list(frame, main_layout[1], &self.list_section_selection, &self.data); }, MenuSelection::Quit => { Self::render_quit(frame, main_layout[1]); @@ -292,8 +322,19 @@ impl ConsoleUI { } } - fn update_list(&mut self, _key: KeyEvent) -> Option { - None + fn update_list(&mut self, key: KeyEvent) -> Option { + let length = self.data.section_info.len() - 1; + match key.code { + KeyCode::Down => { + self.list_section_selection.increment(length); + return Some(UIState::Render); + }, + KeyCode::Up => { + self.list_section_selection.decrement(length); + return Some(UIState::Render); + }, + _ => None + } } fn update_quit(&mut self, key: KeyEvent) -> Option { @@ -303,7 +344,7 @@ impl ConsoleUI { } } - fn render_stats(frame: &mut ConsoleFrame, layout: Rect, section_selection: &SectionSelection, data: &mut ConsoleUIData) { + fn render_stats(frame: &mut ConsoleFrame, layout: Rect, section_selection: &DualSelection, data: &ConsoleUIData) { const HIGHLIGHT_COLOR:[Color;2] = [Color::Cyan, Color::Red]; let layout = Layout::default().direction(Direction::Vertical).constraints([ Constraint::Length(3), @@ -322,16 +363,13 @@ impl ConsoleUI { let first_mem_gauge = Self::create_mem_gauge_from(&data.section_info[section_selection.get_selection_for(SectionMode::UpperSection)], "a", HIGHLIGHT_COLOR[0]); let second_mem_gauge = Self::create_mem_gauge_from(&data.section_info[section_selection.get_selection_for(SectionMode::LowerSection)], "s", HIGHLIGHT_COLOR[1]); - let mut list_state = ListState::default(); - - list_state.select(Some(section_selection.get_current_selection())); - - frame.render_stateful_widget(content_list, layout[0], &mut list_state); + + frame.render_stateful_widget(content_list, layout[0], &mut section_selection.get_current_selection()); frame.render_widget(first_mem_gauge, layout[1]); frame.render_widget(second_mem_gauge, layout[2]); } - fn render_list(frame: &mut ConsoleFrame, layout: Rect) { + fn render_list(frame: &mut ConsoleFrame, layout: Rect, section_selection: &DualSelection, data: &ConsoleUIData) { let (info_layout, list_layout) = { let layout = Layout::default().direction(Direction::Vertical).constraints([ Constraint::Min(5), @@ -343,19 +381,31 @@ impl ConsoleUI { Constraint::Percentage(50) ]).split(layout[0])) }; - - let text = Paragraph::new("Schwimmflügel") + + let section_list = List::new(data.section_info.iter().skip(1).map(|section| { + ListItem::new(section.name.clone()) + }).collect::>()).style(Style::default().fg(Color::White)) + .block(Block::default() + .borders(Borders::ALL) .style(Style::default().fg(Color::White)) - .alignment(Alignment::Center) + .border_type(BorderType::Plain) + .title("Section") + ) + .highlight_style(Style::default().bg(Color::White)); + + let top_section = &data.section_info[section_selection.get_selection_for(ListSectionMode::TopSection) + 1]; + let info_text = Paragraph::new(format!("Name: {}", top_section.name)) + .style(Style::default().fg(Color::White)) + .alignment(Alignment::Left) .block(Block::default() .borders(Borders::ALL) .style(Style::default().fg(Color::White)) - .border_type(BorderType::Plain).title("Wuffi") + .border_type(BorderType::Plain).title("Information") ); - frame.render_widget(text.clone(), info_layout); - frame.render_widget(text.clone(), list_layout[0]); - frame.render_widget(text, list_layout[1]); + frame.render_widget(info_text, info_layout); + frame.render_stateful_widget(section_list, list_layout[0], &mut section_selection.get_current_selection()); + //frame.render_widget(text, list_layout[1]); } fn render_quit(frame: &mut ConsoleFrame, layout: Rect) {