Handled dynamic adding and removing of inventory items and syncing with menu systems.
This commit is contained in:
+88
-7
@@ -2,6 +2,9 @@
|
||||
#include "MenuComponent.h"
|
||||
#include "DEFINES.h"
|
||||
#include "safemap.h"
|
||||
#include "Item.h"
|
||||
#include "MenuItemButton.h"
|
||||
#include "ScrollableWindowComponent.h"
|
||||
|
||||
bool Menu::MOUSE_NAVIGATION=true;
|
||||
std::vector<Menu*>Menu::stack;
|
||||
@@ -32,11 +35,13 @@ void Menu::InitializeMenus(){
|
||||
std::cout<<"WARNING! Menu Type "<<type<<" does not exist!"<<std::endl;
|
||||
throw;
|
||||
}
|
||||
//Lock up everything once it's done.
|
||||
menus[type]->buttons.SetInitialized();
|
||||
menus[type]->keyboardButtons.SetInitialized();
|
||||
for(auto&key:menus[type]->components){
|
||||
MenuComponent*component=key.second;
|
||||
component->AfterCreate();
|
||||
}
|
||||
menus[type]->components.SetInitialized(); //Lock all known components to prevent invalid access.
|
||||
}
|
||||
}
|
||||
|
||||
@@ -47,9 +52,33 @@ Menu*Menu::CreateMenu(MenuType type,vf2d pos,vf2d size){
|
||||
|
||||
void Menu::AddComponent(std::string key,MenuComponent*button){
|
||||
if(button->selectable){
|
||||
buttons[button->rect.pos.y].push_back(button);
|
||||
buttons.Unlock();
|
||||
if(buttons.count(button->rect.pos.y)){
|
||||
buttons.at(button->rect.pos.y).push_back(button);
|
||||
}else{
|
||||
buttons[button->rect.pos.y].push_back(button);
|
||||
}
|
||||
if(button->selectableViaKeyboard){
|
||||
keyboardButtons[button->rect.pos.y].push_back(button);
|
||||
keyboardButtons.Unlock();
|
||||
if(keyboardButtons.count(button->rect.pos.y)){
|
||||
keyboardButtons.at(button->rect.pos.y).push_back(button);
|
||||
}else{
|
||||
keyboardButtons[button->rect.pos.y].push_back(button);
|
||||
}
|
||||
}
|
||||
|
||||
//We must lock the values before calling sort. Sort seems to try and create new accesses.
|
||||
buttons.SetInitialized();
|
||||
keyboardButtons.SetInitialized();
|
||||
|
||||
//We make an assumption that menu components are supposed to be in left-to-right order. Sometimes we may add things out-of-order, so this fixes the problem by sorting the items afterwards.
|
||||
std::sort(buttons[button->rect.pos.y].begin(),buttons[button->rect.pos.y].end(),[](MenuComponent*c1,MenuComponent*c2){
|
||||
return c1->GetPos().x<c2->GetPos().x;
|
||||
});
|
||||
if(keyboardButtons.count(button->rect.pos.y)){ //Keyboard buttons may not necessarily contain this key...Let's be sure.
|
||||
std::sort(keyboardButtons[button->rect.pos.y].begin(),keyboardButtons[button->rect.pos.y].end(),[](MenuComponent*c1,MenuComponent*c2){
|
||||
return c1->GetPos().x<c2->GetPos().x;
|
||||
});
|
||||
}
|
||||
}else{
|
||||
displayComponents.push_back(button);
|
||||
@@ -59,7 +88,9 @@ void Menu::AddComponent(std::string key,MenuComponent*button){
|
||||
throw;
|
||||
}
|
||||
button->name=key;
|
||||
components.Unlock(); //It's possible we can add a component later on, so we will make sure we remove the lock first.
|
||||
components[key]=button;
|
||||
components.SetInitialized();
|
||||
}
|
||||
|
||||
void Menu::CheckClickAndPerformMenuSelect(Crawler*game){
|
||||
@@ -84,7 +115,6 @@ void Menu::HoverMenuSelect(Crawler*game){
|
||||
|
||||
void Menu::MenuSelect(Crawler*game){
|
||||
if(selection==vi2d{-1,-1}||buttons[selection.y][selection.x]->disabled)return;
|
||||
buttons[selection.y][selection.x]->onClick(MenuFuncData{*this,game,buttons[selection.y][selection.x]});
|
||||
if(buttons[selection.y][selection.x]->menuDest!=MenuType::ENUM_END){
|
||||
if(stack.size()<32){
|
||||
stack.push_back(menus[buttons[selection.y][selection.x]->menuDest]);//Navigate to the next menu.
|
||||
@@ -93,6 +123,7 @@ void Menu::MenuSelect(Crawler*game){
|
||||
throw;
|
||||
}
|
||||
}
|
||||
buttons[selection.y][selection.x]->onClick(MenuFuncData{*this,game,buttons[selection.y][selection.x]});
|
||||
}
|
||||
|
||||
void Menu::Update(Crawler*game){
|
||||
@@ -315,9 +346,9 @@ void Menu::KeyboardButtonNavigation(Crawler*game,vf2d menuPos){
|
||||
selectedItem=true;
|
||||
break;
|
||||
}
|
||||
if(key.first==selection.y&&
|
||||
if(key.first==selection.y
|
||||
//It's entirely possible this button was selected from the button selection list and may be out-of-bounds here.
|
||||
selection.x>=0&&selection.x<keyboardButtons[selection.y].size()){
|
||||
&&selection.x>=0&&selection.x<keyboardButtons[selection.y].size()){
|
||||
found=true;
|
||||
}
|
||||
}
|
||||
@@ -528,4 +559,54 @@ void Menu::SetMouseNavigation(bool mouseNavigation){
|
||||
lastActiveMousePos=game->GetMousePos();
|
||||
}
|
||||
MOUSE_NAVIGATION=mouseNavigation;
|
||||
};
|
||||
};
|
||||
|
||||
void Menu::InventorySlotsUpdated(ITCategory cat){
|
||||
//Update the inventory with a new inventory slot, since there's one additional item to interact with now.
|
||||
std::vector<std::string>&inv=Inventory::get(cat);
|
||||
MenuType inventoryWindow;
|
||||
if(cat=="Consumables"){
|
||||
inventoryWindow=INVENTORY;
|
||||
}else
|
||||
if(cat=="Equipment"){
|
||||
//TODO: Update different inventories depending on what the item's category is.
|
||||
std::cout<<"WARNING! Unimplemented inventory slot addition for category "<<cat<<"!"<<std::endl;
|
||||
return; //Currenly a no-op
|
||||
}else
|
||||
if(cat=="Accesories"){
|
||||
//TODO: Update different inventories depending on what the item's category is.
|
||||
std::cout<<"WARNING! Unimplemented inventory slot addition for category "<<cat<<"!"<<std::endl;
|
||||
return; //Currenly a no-op
|
||||
}else
|
||||
if(cat=="Materials"){
|
||||
//TODO: Update different inventories depending on what the item's category is.
|
||||
std::cout<<"WARNING! Unimplemented inventory slot addition for category "<<cat<<"!"<<std::endl;
|
||||
return; //Currenly a no-op
|
||||
}else{
|
||||
throw;
|
||||
std::cout<<"WARNING! Attempting to add an inventory to a category that does not exist. THIS SHOULD NOT BE HAPPENING!"<<std::endl;
|
||||
}
|
||||
//HACK ALERT!! We make a very bold assumption here that every inventory window will contain a ScrollableWindowComponent called "inventory"! Because it's a safemap, if it doesn't exist it will let us know as such, we won't include any extra checks here.
|
||||
ScrollableWindowComponent*inventory=((ScrollableWindowComponent*)menus[inventoryWindow]->components["inventory"]);
|
||||
//We only want to refresh the inventory slots if the component count no longer matches what's actually in our inventory.
|
||||
if(inventory->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{inventoryWindow,{{float(totalSpacing*x),float(totalSpacing*y)},{float(buttonSize),float(buttonSize)}},Inventory::get("Consumables"),itemIndex,useItemFunc};
|
||||
inventory->AddComponent(menus[inventoryWindow],"item"+std::to_string(itemIndex),button);
|
||||
}else
|
||||
if(inventory->ComponentCount()>inv.size()){ //There are empty spots, so let's clean up.
|
||||
inventory->RemoveEmptySlots();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user