Skip to content

Commit

Permalink
inventory updated
Browse files Browse the repository at this point in the history
  • Loading branch information
jossse69 committed Sep 3, 2023
1 parent cee2b4a commit 3562a99
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 26 deletions.
19 changes: 12 additions & 7 deletions src/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
58 changes: 46 additions & 12 deletions src/player.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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),
_ => {}
}
Expand All @@ -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);
}
}
}
Expand All @@ -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();
}
}
}
}
}
}
}
36 changes: 29 additions & 7 deletions src/ui.rs
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -7,6 +8,7 @@ pub struct UI {
pub message_log: Vec<String>,
pub popup_windows: Vec<PopupWindow>,
pub active_popup: Option<usize>, // Track the index of the active popup, if any
pub previous_popup: Option<usize>,
}

impl UI {
Expand All @@ -15,6 +17,7 @@ impl UI {
message_log: Vec::new(),
popup_windows: Vec::new(),
active_popup: None,
previous_popup: None,
}
}

Expand All @@ -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
Expand All @@ -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
}
}

Expand Down Expand Up @@ -129,12 +141,12 @@ impl UI {
}

pub struct PopupWindow {
x: i32,
y: i32,
width: i32,
height: i32,
title: String,
content: Vec<String>,
pub x: i32,
pub y: i32,
pub width: i32,
pub height: i32,
pub title: String,
pub content: Vec<String>,
}

impl PopupWindow {
Expand Down Expand Up @@ -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();
}
}
}

0 comments on commit 3562a99

Please sign in to comment.