Handled dynamic adding and removing of inventory items and syncing with menu systems.

This commit is contained in:
2023-10-17 05:35:19 -05:00
parent 6656e3d036
commit 04d6f42526
17 changed files with 254 additions and 50 deletions
+10 -23
View File
@@ -1,6 +1,5 @@
#pragma once
#include "Crawler.h"
#include "DEFINES.h"
#include "olcPixelGameEngine.h"
#include "safemap.h"
#include "Item.h"
@@ -8,39 +7,27 @@
#include "MenuLabel.h"
#include "ScrollableWindowComponent.h"
INCLUDE_GFX
typedef Attribute A;
void Menu::InitializeInventoryWindow(){
constexpr int invWidth=5;
constexpr int initialInvHeight=3;
int invWidth="ThemeGlobal.InventoryWidth"_I;
int initialInvHeight="ThemeGlobal.InventoryHeight"_I;
constexpr int itemSpacing=8;
constexpr int buttonSize=24;
constexpr int totalSpacing=buttonSize+itemSpacing;
int itemSpacing="ThemeGlobal.InventoryItemSpacing"_I;
int buttonSize="ThemeGlobal.InventoryButtonSize"_I;
int totalSpacing=buttonSize+itemSpacing;
vf2d windowSize={totalSpacing*invWidth-itemSpacing+2+24,totalSpacing*(3+1)-itemSpacing+24}; //Need space for the button.
vf2d windowSize={float(totalSpacing*invWidth-itemSpacing+2+24),float(totalSpacing*(3+1)-itemSpacing+24)}; //Need space for the button.
Menu*inventoryWindow=CreateMenu(INVENTORY,CENTERED,windowSize);
ScrollableWindowComponent*inventory=new ScrollableWindowComponent(INVENTORY,{{1,0},{windowSize.x,totalSpacing*3-itemSpacing}},nullptr,[](MenuFuncData data){});
ScrollableWindowComponent*inventory=new ScrollableWindowComponent(INVENTORY,{{1,0},{windowSize.x,float(totalSpacing*3-itemSpacing)}},nullptr,[](MenuFuncData data){});
inventoryWindow->AddComponent("inventory",inventory);
MenuFunc useItemFunc=[](MenuFuncData data){
MenuItemButton*button=(MenuItemButton*)data.component;
button->UseItem();
};
//We don't have to actually populate the inventory list because now when an item gets added, it will automatically add the correct component in for us.
for(int i=0;i<Inventory::get("Consumables").size();i++){
int x=i%invWidth;
int y=i/invWidth;
int itemIndex=y*invWidth+x;
MenuItemButton*button=new MenuItemButton{INVENTORY,{{float(totalSpacing*x),float(totalSpacing*y)},{float(buttonSize),float(buttonSize)}},Inventory::get("Consumables"),itemIndex,useItemFunc};
inventory->AddComponent(inventoryWindow,"item"+std::to_string(itemIndex),button);
}
MenuLabel*itemNameLabel=new MenuLabel{INVENTORY,geom2d::rect<float>(vf2d{2,initialInvHeight*totalSpacing-4},windowSize),"",false,true};
MenuLabel*itemNameLabel=new MenuLabel{INVENTORY,geom2d::rect<float>(vf2d{2,float(initialInvHeight*totalSpacing-4)},windowSize),"",false,true};
inventoryWindow->AddComponent("itemName",itemNameLabel);
MenuLabel*itemDescriptionLabel=new MenuLabel{INVENTORY,geom2d::rect<float>(vf2d{2,initialInvHeight*totalSpacing+itemSpacing},{windowSize.x-4,windowSize.y-108}),"",true,true};
MenuLabel*itemDescriptionLabel=new MenuLabel{INVENTORY,geom2d::rect<float>(vf2d{2,float(initialInvHeight*totalSpacing+itemSpacing)},{windowSize.x-4,windowSize.y-108}),"",true,true};
inventoryWindow->AddComponent("itemDescription",itemDescriptionLabel);
}