diff --git a/src/item.rs b/src/item.rs index 8a90fd1..6ba3bf8 100644 --- a/src/item.rs +++ b/src/item.rs @@ -60,14 +60,19 @@ impl Item { pub fn inspect(&self, ctx: &mut BTerm, ui: &mut UI) { // Create a new popup window - let mut popup = PopupWindow::new(20, 10, 40, 20, "Item Description"); + ui.create_popup(10, 10, 40, 20, "Item Inspection"); - // Add the item's description to the popup window content - popup.add_content(&self.data.description); - - // Add the popup window to the UI - ui.create_popup(20, 10, 40, 20, "Item Description"); - ui.popup_windows.push(popup); + // Add item inspection content to the popup + if let Some(active_popup_index) = ui.active_popup { + if let Some(popup) = ui.popup_windows.get_mut(active_popup_index) { + popup.add_content(&self.data.name); + + //separetor + popup.add_content("────────────"); + + popup.add_content(&self.data.description); + } + } } // Implement item-specific methods here diff --git a/src/player.rs b/src/player.rs index dcd6182..53d28f3 100644 --- a/src/player.rs +++ b/src/player.rs @@ -4,7 +4,7 @@ use bracket_lib::prelude::*; use crate::entity::Entity; use crate::item::Item; use crate::map::{Map, self, TileType}; -use crate::ui::{UI, PopupWindow}; +use crate::ui::{UI, PopupWindow, self}; pub struct Player { pub entity: Entity, @@ -56,8 +56,8 @@ impl Player { VirtualKeyCode::Numpad3 => self.move_by(1, 1, map), VirtualKeyCode::E => self.open_inventory(ui), // Add more keybindings for item selection and inspection - VirtualKeyCode::Up => self.select_previous_item(), - VirtualKeyCode::Down => self.select_next_item(), + VirtualKeyCode::Up => self.select_previous_item(ui), + VirtualKeyCode::Down => self.select_next_item(ui), VirtualKeyCode::I => self.inspect_selected_item(ui, ctx), _ => {} } @@ -76,31 +76,44 @@ impl Player { } fn open_inventory(&mut self, ui: &mut UI) { - let mut inventory_popup = PopupWindow::new(10, 10, 40, 20, "Inventory"); - + // Create an inventory popup + ui.create_popup(10, 10, 40, 20, "Inventory"); + // Add inventory items to the popup content - for item in &self.inventory { - inventory_popup.add_content(&item.data.name); + for (index, item) in self.inventory.iter().enumerate() { + if let Some(active_popup_index) = ui.active_popup { + if let Some(popup) = ui.popup_windows.get_mut(active_popup_index) { + // Add arrow symbol for the selected item + let content = if Some(index) == self.selected_item { + format!("> {}", &item.data.name) + } else { + item.data.name.clone() + }; + popup.add_content(&content); + } + } } - + // Update the selected item to the first item in the inventory self.selected_item = Some(0); - - ui.popup_windows.push(inventory_popup); } - fn select_previous_item(&mut self) { + fn select_previous_item(&mut self, ui: &mut UI) { if let Some(selected_item) = self.selected_item { if selected_item > 0 { self.selected_item = Some(selected_item - 1); + // update iventory popup + self.update_inventory(ui); } } } - fn select_next_item(&mut self) { + fn select_next_item(&mut self, ui: &mut UI) { if let Some(selected_item) = self.selected_item { if selected_item < self.inventory.len() - 1 { self.selected_item = Some(selected_item + 1); + // update iventory popup + self.update_inventory(ui); } } } @@ -112,4 +125,25 @@ impl Player { } } } + + fn update_inventory(&mut self, ui: &mut UI) { + // TODO: add arrow to inventory item to show its selected in the popup + + // Update the inventory popup content with arrow symbol for the selected item + if let Some(selected_item) = self.selected_item { + if let Some(active_popup_index) = ui.active_popup { + if let Some(popup) = ui.popup_windows.get_mut(active_popup_index) { + for (index, content) in popup.content.iter_mut().enumerate() { + if index == selected_item { + // Add arrow symbol to the selected item + *content = format!("> {}", content); + } else { + // Remove arrow symbol from other items + *content = content.trim_start_matches("> ").to_string(); + } + } + } + } + } + } } diff --git a/src/ui.rs b/src/ui.rs index bd1cd45..ac23b91 100644 --- a/src/ui.rs +++ b/src/ui.rs @@ -1,4 +1,5 @@ use bracket_lib::prelude::*; +use rand::seq::index; use crate::player::Player; const MESSAGE_LOG_MAX_LINES: usize = 5; // Maximum lines in the message log @@ -7,6 +8,7 @@ pub struct UI { pub message_log: Vec, pub popup_windows: Vec, pub active_popup: Option, // Track the index of the active popup, if any + pub previous_popup: Option, } impl UI { @@ -15,6 +17,7 @@ impl UI { message_log: Vec::new(), popup_windows: Vec::new(), active_popup: None, + previous_popup: None, } } @@ -30,6 +33,14 @@ impl UI { pub fn create_popup(&mut self, x: i32, y: i32, width: i32, height: i32, title: &str) { let popup = PopupWindow::new(x, y, width, height, title); + + + //add the previous popup to the previous popup list + if let Some(active_popup_index) = self.active_popup { + self.previous_popup = Some(active_popup_index); + } + + // Add the new popup to the list self.popup_windows.push(popup); // Set the active popup to the newly created one @@ -39,7 +50,8 @@ impl UI { pub fn remove_active_popup(&mut self) { if let Some(active_popup_index) = self.active_popup { self.popup_windows.remove(active_popup_index); - self.active_popup = None; + //set the active popup to previous popup + self.active_popup = self.previous_popup } } @@ -129,12 +141,12 @@ impl UI { } pub struct PopupWindow { - x: i32, - y: i32, - width: i32, - height: i32, - title: String, - content: Vec, + pub x: i32, + pub y: i32, + pub width: i32, + pub height: i32, + pub title: String, + pub content: Vec, } impl PopupWindow { @@ -171,5 +183,15 @@ impl PopupWindow { for (i, line) in self.content.iter().enumerate() { ctx.print(self.x + 3, self.y + 4 + i as i32, line); } + + + } + + pub fn update_content(&mut self, index: usize, content: &str) { + // Ensure the index is within bounds before updating + if index < self.content.len() { + self.content[index] = content.to_string(); + } + } }