Refactor inventory window so the scrollable consumable window now subscribes to a listener.

Listener list system removes the arbitrary requirement of components needing to be called "inventory" (very error-prone)
This commit is contained in:
2023-10-18 16:06:08 +00:00
parent 04d6f42526
commit 46462a24e7
7 changed files with 116 additions and 46 deletions
+26 -2
View File
@@ -191,7 +191,7 @@ public:
virtual inline size_t ComponentCount(){
return components.size();
}
virtual void RemoveButton(MenuComponent*button){
virtual inline void RemoveButton(MenuComponent*button){
std::vector<MenuComponent*>&buttonList=Menu::menus[button->parentMenu]->buttons.at(button->GetPos().y);
std::vector<MenuComponent*>&keyboardButtonList=Menu::menus[button->parentMenu]->keyboardButtons.at(button->GetPos().y);
size_t removedCount=0;
@@ -214,7 +214,7 @@ public:
}
}
}
virtual void RemoveEmptySlots(){
void inline RemoveEmptySlots(){
//Algorithm will iterate through all slots, finding blank slots. Each time a blank slot is found, all items will shift over by one, and then the last item will be removed. Repeat until all slots iterated through.
for(int i=0;i<components.size();i++){
MenuComponent*button=components[i];
@@ -237,4 +237,28 @@ public:
}
bounds=CalculateBounds(); //Recalculate the bounds as it's possible the width/height of the component has changed.
}
virtual void OnInventorySlotsUpdate(ITCategory cat)override{
std::vector<std::string>&inv=Inventory::get(cat);
//We only want to refresh the inventory slots if the component count no longer matches what's actually in our inventory.
if(ComponentCount()<inv.size()){//We need more space to display our items.
int invWidth="ThemeGlobal.InventoryWidth"_I;
int x=(inv.size()-1)%invWidth;
int y=(inv.size()-1)/invWidth;
int itemIndex=y*invWidth+x;
int buttonSize="ThemeGlobal.InventoryButtonSize"_I;
int totalSpacing="ThemeGlobal.InventoryItemSpacing"_I+buttonSize;
MenuFunc useItemFunc=[](MenuFuncData data){
MenuItemButton*button=(MenuItemButton*)data.component;
button->UseItem();
};
MenuItemButton*button=new MenuItemButton{parentMenu,{{float(totalSpacing*x),float(totalSpacing*y)},{float(buttonSize),float(buttonSize)}},Inventory::get("Consumables"),itemIndex,useItemFunc};
AddComponent(Menu::menus[parentMenu],"item"+std::to_string(itemIndex),button);
}else
if(ComponentCount()>inv.size()){ //There are empty spots, so let's clean up.
RemoveEmptySlots();
}
}
};