Change implementation of scrollable inventory component displays, make tabs on inventory window functional.

pull/28/head
sigonasr2 12 months ago
parent 469bea4c5e
commit e4f63a3550
  1. 20
      Crawler/InventoryConsumableWindow.cpp
  2. 19
      Crawler/InventoryScrollableWindowComponent.h
  3. 21
      Crawler/InventoryWindow.cpp
  4. 32
      Crawler/MenuComponent.cpp
  5. 12
      Crawler/MenuComponent.h
  6. 8
      Crawler/MenuItemItemButton.h
  7. 2
      Crawler/Version.h
  8. 9
      Crawler/assets/config/gfx/themes.txt

@ -46,20 +46,16 @@ All rights reserved.
using A=Attribute; using A=Attribute;
void Menu::InitializeConsumableInventoryWindow(){ void Menu::InitializeConsumableInventoryWindow(){
int invWidth="ThemeGlobal.InventoryWidth"_I; int itemSpacing=24;
int initialInvHeight="ThemeGlobal.InventoryHeight"_I; int totalSpacing=32;
int itemSpacing="ThemeGlobal.InventoryItemSpacing"_I; vf2d windowSize={float(160+16),float(96+64)}; //Need space for the button.
int buttonSize="ThemeGlobal.InventoryButtonSize"_I;
int totalSpacing=buttonSize+itemSpacing;
vf2d windowSize={float(totalSpacing*invWidth-itemSpacing+2+24),float(totalSpacing*(3+1)-itemSpacing+64)}; //Need space for the button.
Menu*inventoryWindow=CreateMenu(INVENTORY_CONSUMABLES,CENTERED,windowSize); Menu*inventoryWindow=CreateMenu(INVENTORY_CONSUMABLES,CENTERED,windowSize);
inventoryWindow->I(A::LOADOUT_SLOT)=0; inventoryWindow->I(A::LOADOUT_SLOT)=0;
inventoryWindow->ADD("inventory",InventoryScrollableWindowComponent)({{1,20},{windowSize.x,float(totalSpacing*3-itemSpacing)}},"Consumables","itemName","itemDescription", inventoryWindow->ADD("inventory",InventoryScrollableWindowComponent)({{1,20},{windowSize.x,96.f}},"Consumables","itemName","itemDescription",
[&](MenuFuncData data){ [&](MenuFuncData data){
MenuItemButton*button=(MenuItemButton*)data.component; MenuItemButton*button=(MenuItemButton*)data.component;
data.game->ClearLoadoutItem(data.menu.I(A::LOADOUT_SLOT)); data.game->ClearLoadoutItem(data.menu.I(A::LOADOUT_SLOT));
@ -84,9 +80,9 @@ void Menu::InitializeConsumableInventoryWindow(){
//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. //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.
inventoryWindow->ADD("Inventory Type Label",MenuLabel)({{0,0},{windowSize.x-1,18}},"Consumables",2,ComponentAttr::SHADOW|ComponentAttr::BACKGROUND|ComponentAttr::OUTLINE)END; inventoryWindow->ADD("Inventory Type Label",MenuLabel)({{0,0},{windowSize.x-1,18}},"Consumables",2,ComponentAttr::SHADOW|ComponentAttr::BACKGROUND|ComponentAttr::OUTLINE)END;
inventoryWindow->ADD("itemName",MenuLabel)(geom2d::rect<float>(vf2d{2,float(initialInvHeight*totalSpacing+itemSpacing-16)},{windowSize.x-4,windowSize.y-108}),"",1,ComponentAttr::SHADOW)END; inventoryWindow->ADD("itemName",MenuLabel)(geom2d::rect<float>(vf2d{2,100.f},{windowSize.x-4,windowSize.y-108}),"",1,ComponentAttr::SHADOW)END;
float itemDescriptionLabelY=float(initialInvHeight*totalSpacing+itemSpacing); inventoryWindow->ADD("itemDescription",MenuLabel)(geom2d::rect<float>(vf2d{2,112.f},{windowSize.x-4,windowSize.y-108}),"",1,ComponentAttr::SHADOW)END;
inventoryWindow->ADD("itemDescription",MenuLabel)(geom2d::rect<float>(vf2d{2,itemDescriptionLabelY},{windowSize.x-4,windowSize.y-108}),"",1,ComponentAttr::SHADOW)END;
inventoryWindow->ADD("OK Button",MenuComponent)({{windowSize.x/2-24,itemDescriptionLabelY+56},{48,12}},"Ok",[](MenuFuncData data){Menu::CloseMenu();return true;})END; auto okButton=inventoryWindow->ADD("OK Button",MenuComponent)({{windowSize.x/2-24,158.f},{48,12}},"Ok",[](MenuFuncData data){Menu::CloseMenu();return true;})END;
okButton->decal=true;
} }

@ -44,18 +44,24 @@ All rights reserved.
using A=Attribute; using A=Attribute;
struct InventoryWindowOptions{
int padding=8;
int size=24;
};
class InventoryScrollableWindowComponent:public ScrollableWindowComponent{ class InventoryScrollableWindowComponent:public ScrollableWindowComponent{
private: private:
std::string itemNameLabelName; std::string itemNameLabelName;
std::string itemDescriptionLabelName; std::string itemDescriptionLabelName;
bool inventoryButtonsActive=true; bool inventoryButtonsActive=true;
std::function<bool(MenuFuncData)>inventoryButtonClickAction; std::function<bool(MenuFuncData)>inventoryButtonClickAction;
InventoryWindowOptions options;
protected: protected:
ITCategory inventoryType; ITCategory inventoryType;
public: public:
inline InventoryScrollableWindowComponent(geom2d::rect<float>rect,ITCategory invType,std::string itemNameLabelName,std::string itemDescriptionLabelName,std::function<bool(MenuFuncData)>inventoryButtonClickAction,bool inventoryButtonsActive=true,ComponentAttr attributes=ComponentAttr::BACKGROUND|ComponentAttr::OUTLINE) inline InventoryScrollableWindowComponent(geom2d::rect<float>rect,ITCategory invType,std::string itemNameLabelName,std::string itemDescriptionLabelName,std::function<bool(MenuFuncData)>inventoryButtonClickAction,InventoryWindowOptions options={.padding=8,.size=24},bool inventoryButtonsActive=true,ComponentAttr attributes=ComponentAttr::BACKGROUND|ComponentAttr::OUTLINE)
:ScrollableWindowComponent(rect,attributes),inventoryType(invType),itemNameLabelName(itemNameLabelName),itemDescriptionLabelName(itemDescriptionLabelName), :ScrollableWindowComponent(rect,attributes),inventoryType(invType),itemNameLabelName(itemNameLabelName),itemDescriptionLabelName(itemDescriptionLabelName),
inventoryButtonClickAction(inventoryButtonClickAction),inventoryButtonsActive(inventoryButtonsActive){ options(options),inventoryButtonClickAction(inventoryButtonClickAction),inventoryButtonsActive(inventoryButtonsActive){
Menu::AddInventoryListener(this,invType); Menu::AddInventoryListener(this,invType);
} }
virtual inline void Update(Crawler*game)override{ virtual inline void Update(Crawler*game)override{
@ -72,9 +78,6 @@ public:
if(itemDescriptionLabelName.length()>0)Component<MenuLabel>(parentMenu,itemDescriptionLabelName)->SetLabel(""); if(itemDescriptionLabelName.length()>0)Component<MenuLabel>(parentMenu,itemDescriptionLabelName)->SetLabel("");
} }
} }
virtual inline void SetInventoryType(ITCategory inventoryType){
this->inventoryType=inventoryType;
}
protected: protected:
inline void RemoveEmptySlots(){ inline void 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. //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.
@ -100,13 +103,13 @@ protected:
size_t invSize=Inventory::get(cat).size(); size_t invSize=Inventory::get(cat).size();
//We only want to refresh the inventory slots if the component count no longer matches what's actually in our inventory. //We only want to refresh the inventory slots if the component count no longer matches what's actually in our inventory.
if(components.size()<invSize){//We need more space to display our items. if(components.size()<invSize){//We need more space to display our items.
int invWidth="ThemeGlobal.InventoryWidth"_I; int invWidth=int(rect.size.x/(float(options.size)+options.padding));
int x=int((invSize-1)%invWidth); int x=int((invSize-1)%invWidth);
int y=int((invSize-1)/invWidth); int y=int((invSize-1)/invWidth);
int itemIndex=y*invWidth+x; int itemIndex=y*invWidth+x;
int buttonSize="ThemeGlobal.InventoryButtonSize"_I; int buttonSize=options.size;
int totalSpacing="ThemeGlobal.InventoryItemSpacing"_I+buttonSize; int totalSpacing=options.padding+buttonSize;
ADD("item_"+cat+"_"+std::to_string(itemIndex),MenuItemButton)({{float(totalSpacing*x),float(totalSpacing*y)},{float(buttonSize),float(buttonSize)}},Inventory::get(cat),itemIndex,inventoryButtonClickAction,parentMenu,itemNameLabelName,itemDescriptionLabelName,inventoryButtonsActive?IconButtonAttr::SELECTABLE:IconButtonAttr::NOT_SELECTABLE)END; ADD("item_"+cat+"_"+std::to_string(itemIndex),MenuItemButton)({{float(totalSpacing*x),float(totalSpacing*y)},{float(buttonSize),float(buttonSize)}},Inventory::get(cat),itemIndex,inventoryButtonClickAction,parentMenu,itemNameLabelName,itemDescriptionLabelName,inventoryButtonsActive?IconButtonAttr::SELECTABLE:IconButtonAttr::NOT_SELECTABLE)END;
}else }else

@ -50,6 +50,8 @@ using ButtonAttr::UNSELECTABLE;
using ButtonAttr::UNSELECTABLE_VIA_KEYBOARD; using ButtonAttr::UNSELECTABLE_VIA_KEYBOARD;
void Menu::InitializeInventoryWindow(){ void Menu::InitializeInventoryWindow(){
static std::string lastInventoryTypeOpened="";
Menu*inventoryWindow=CreateMenu(INVENTORY,CENTERED,game->GetScreenSize()-vi2d{52,52}); Menu*inventoryWindow=CreateMenu(INVENTORY,CENTERED,game->GetScreenSize()-vi2d{52,52});
inventoryWindow->ADD("Inventory Label",MenuLabel)({{0,0},{inventoryWindow->size.x-1,24}},"Inventory",2,SHADOW|OUTLINE|BACKGROUND)END; inventoryWindow->ADD("Inventory Label",MenuLabel)({{0,0},{inventoryWindow->size.x-1,24}},"Inventory",2,SHADOW|OUTLINE|BACKGROUND)END;
@ -62,11 +64,6 @@ void Menu::InitializeInventoryWindow(){
} }
std::sort(categories.begin(),categories.end(),[](std::pair<std::string,int>&cat1,std::pair<std::string,int>&cat2){return cat1.second<cat2.second;}); std::sort(categories.begin(),categories.end(),[](std::pair<std::string,int>&cat1,std::pair<std::string,int>&cat2){return cat1.second<cat2.second;});
#pragma region Inventory Item Display
auto itemList=inventoryWindow->ADD("Inventory Display",InventoryScrollableWindowComponent)({{72,28},{150,inventoryWindow->size.y-44}},categories[0].first,"","",DO_NOTHING)END;
#pragma endregion
#pragma region Inventory Tabs #pragma region Inventory Tabs
bool first=true; bool first=true;
for(float yOffset=0;auto&[category,sortOrder]:categories){ for(float yOffset=0;auto&[category,sortOrder]:categories){
@ -76,16 +73,26 @@ void Menu::InitializeInventoryWindow(){
auto button=inventoryWindow->ADD(category+" Inventory Tab",MenuComponent)({{2,30+yOffset},{68,16}},category,MenuType::ENUM_END, auto button=inventoryWindow->ADD(category+" Inventory Tab",MenuComponent)({{2,30+yOffset},{68,16}},category,MenuType::ENUM_END,
[&](MenuFuncData data){ [&](MenuFuncData data){
Component<InventoryScrollableWindowComponent>(data.menu.GetType(),"Inventory Display")->SetInventoryType(data.component->S(A::CATEGORY_NAME)); //Close the old inventory window and show the proper one.
Component<InventoryScrollableWindowComponent>(data.menu.GetType(),"Inventory Display - "+lastInventoryTypeOpened)->Enable(false);
Component<MenuComponent>(data.menu.GetType(),lastInventoryTypeOpened+" Inventory Tab")->SetSelected(false);
Component<InventoryScrollableWindowComponent>(data.menu.GetType(),"Inventory Display - "+data.component->S(A::CATEGORY_NAME))->Enable(true);
Component<MenuComponent>(data.menu.GetType(),data.component->S(A::CATEGORY_NAME)+" Inventory Tab")->SetSelected(true);
lastInventoryTypeOpened=data.component->S(A::CATEGORY_NAME);
return true; return true;
},{textScaling,1.f})END; },{textScaling,1.f})END;
button->SetSelectionType(HIGHLIGHT);
button->S(A::CATEGORY_NAME)=category; button->S(A::CATEGORY_NAME)=category;
auto inventoryDisplay=inventoryWindow->ADD("Inventory Display - "+category,InventoryScrollableWindowComponent)({{72,28},{150,inventoryWindow->size.y-44}},category,"","",DO_NOTHING)END;
if(first){ if(first){
lastInventoryTypeOpened=category;
button->onClick(MenuFuncData{*inventoryWindow,game,button}); //Simulate a click of this button if it's the top one for an initial inventory display. button->onClick(MenuFuncData{*inventoryWindow,game,button}); //Simulate a click of this button if it's the top one for an initial inventory display.
} }
inventoryDisplay->Enable(first);
yOffset+=20; yOffset+=20;
first=false; first=false;
} }

@ -37,6 +37,8 @@ All rights reserved.
#pragma endregion #pragma endregion
#include "Crawler.h" #include "Crawler.h"
#include "MenuComponent.h" #include "MenuComponent.h"
#include "drawutil.h"
#include "util.h"
using A=Attribute; using A=Attribute;
@ -95,12 +97,23 @@ void MenuComponent::Draw(Crawler*game,vf2d parentPos){
if(background){ if(background){
game->FillRect(rect.pos+parentPos,rect.size,PixelLerp(Menu::themes[Menu::themeSelection].GetButtonCol(),Menu::themes[Menu::themeSelection].GetHighlightCol(),hoverEffect/"ThemeGlobal.HighlightTime"_F)); game->FillRect(rect.pos+parentPos,rect.size,PixelLerp(Menu::themes[Menu::themeSelection].GetButtonCol(),Menu::themes[Menu::themeSelection].GetHighlightCol(),hoverEffect/"ThemeGlobal.HighlightTime"_F));
} }
if(selected&&selectionType==HIGHLIGHT){
game->FillRect(rect.pos+parentPos,rect.size,PixelLerp(Menu::themes[Menu::themeSelection].GetButtonCol(),Menu::themes[Menu::themeSelection].GetHighlightCol(),util::lerp(0.75f,1.0f,hoverEffect/"ThemeGlobal.HighlightTime"_F)));
}
if(border){ if(border){
game->DrawRect(rect.pos+parentPos,rect.size); game->DrawRect(rect.pos+parentPos,rect.size);
} }
if(showDefaultLabel){ if(showDefaultLabel){
game->DrawStringProp(rect.pos+parentPos+rect.size/2-game->GetTextSizeProp(label)/2,label); game->DrawStringProp(rect.pos+parentPos+rect.size/2-game->GetTextSizeProp(label)/2,label);
} }
if(selected){
switch(selectionType){
case CROSSHAIR:drawutil::DrawCrosshair(game,{parentPos+rect.pos,rect.size},0);break;
case INNER_BOX:game->DrawRect(rect.pos+parentPos+vi2d{1,1},rect.size-vi2d{2,2});break;
case HIGHLIGHT:break;//Not used.
default:ERR("Undefined selection type selected: "<<int(selectionType));
}
}
} }
} }
@ -122,6 +135,9 @@ void MenuComponent::DrawDecal(Crawler*game,vf2d parentPos,bool focused){
if(background){ if(background){
game->FillRectDecal(rect.pos+parentPos,rect.size,PixelLerp(Menu::themes[Menu::themeSelection].GetButtonCol(),Menu::themes[Menu::themeSelection].GetHighlightCol(),hoverEffect/"ThemeGlobal.HighlightTime"_F)); game->FillRectDecal(rect.pos+parentPos,rect.size,PixelLerp(Menu::themes[Menu::themeSelection].GetButtonCol(),Menu::themes[Menu::themeSelection].GetHighlightCol(),hoverEffect/"ThemeGlobal.HighlightTime"_F));
} }
if(selected&&selectionType==HIGHLIGHT){
game->FillRectDecal(rect.pos+parentPos,rect.size,PixelLerp(Menu::themes[Menu::themeSelection].GetButtonCol(),Menu::themes[Menu::themeSelection].GetHighlightCol(),util::lerp(0.75f,1.0f,hoverEffect/"ThemeGlobal.HighlightTime"_F)));
}
if(border){ if(border){
game->FillRectDecal(rect.pos+parentPos,{rect.size.x,1}); game->FillRectDecal(rect.pos+parentPos,{rect.size.x,1});
game->FillRectDecal(rect.pos+parentPos,{1,rect.size.y}); game->FillRectDecal(rect.pos+parentPos,{1,rect.size.y});
@ -131,6 +147,14 @@ void MenuComponent::DrawDecal(Crawler*game,vf2d parentPos,bool focused){
if(showDefaultLabel){ if(showDefaultLabel){
game->DrawStringPropDecal(rect.pos+parentPos+rect.size/2-vf2d(game->GetTextSizeProp(label))/2.f*labelScaling,label,WHITE,labelScaling); game->DrawStringPropDecal(rect.pos+parentPos+rect.size/2-vf2d(game->GetTextSizeProp(label))/2.f*labelScaling,label,WHITE,labelScaling);
} }
if(selected){
switch(selectionType){
case CROSSHAIR:drawutil::DrawCrosshairDecal(game,{parentPos+rect.pos,rect.size},0);break;
case INNER_BOX:game->DrawRectDecal(rect.pos+parentPos+vi2d{1,1},rect.size-vi2d{2,2});break;
case HIGHLIGHT:break;//Not used.
default:ERR("Undefined selection type selected: "<<int(selectionType));
}
}
} }
} }
@ -196,3 +220,11 @@ void MenuComponent::Cleanup(){}
std::string MenuComponent::GetName(){ std::string MenuComponent::GetName(){
return name; return name;
} }
void MenuComponent::SetSelected(bool selected){
this->selected=selected;
}
void MenuComponent::SetSelectionType(SelectionType selectionType){
this->selectionType=selectionType;
}

@ -53,6 +53,14 @@ enum class ComponentAttr{
BACKGROUND=0b1000, //Renders the background of the menu theme for this component. BACKGROUND=0b1000, //Renders the background of the menu theme for this component.
}; };
enum class SelectionType{
CROSSHAIR,
HIGHLIGHT,
INNER_BOX,
};
using enum SelectionType;
class MenuComponent:public IAttributable{ class MenuComponent:public IAttributable{
friend class Menu; friend class Menu;
friend class MenuItemButton; friend class MenuItemButton;
@ -69,6 +77,8 @@ private:
void _Draw(Crawler*game,vf2d parentPos); void _Draw(Crawler*game,vf2d parentPos);
void _DrawDecal(Crawler*game,bool focused); void _DrawDecal(Crawler*game,bool focused);
void _DrawDecal(Crawler*game,vf2d parentPos,bool focused); void _DrawDecal(Crawler*game,vf2d parentPos,bool focused);
bool selected=false;
SelectionType selectionType=CROSSHAIR;
protected: protected:
int depth=0; int depth=0;
float hoverEffect=0; float hoverEffect=0;
@ -116,6 +126,8 @@ public:
virtual void OnEquipStatsUpdate(); virtual void OnEquipStatsUpdate();
std::string GetLabel(); std::string GetLabel();
std::string GetName(); std::string GetName();
virtual void SetSelected(bool selected)final;
virtual void SetSelectionType(SelectionType selectionType)final;
virtual void Enable(bool enabled); virtual void Enable(bool enabled);
virtual void Cleanup(); virtual void Cleanup();
}; };

@ -44,7 +44,6 @@ All rights reserved.
#include "Item.h" #include "Item.h"
#include "safemap.h" #include "safemap.h"
#include "olcPGEX_Graphics2D.h" #include "olcPGEX_Graphics2D.h"
#include "drawutil.h"
INCLUDE_game INCLUDE_game
INCLUDE_ITEM_DATA INCLUDE_ITEM_DATA
@ -58,7 +57,6 @@ private:
MenuFunc onHover; MenuFunc onHover;
MenuFunc onMouseOut; MenuFunc onMouseOut;
bool hoverState=false; bool hoverState=false;
bool selected=false;
bool hideQty=false; bool hideQty=false;
CompactText compact=COMPACT; CompactText compact=COMPACT;
public: public:
@ -79,9 +77,6 @@ public:
inline Item&SetItem(Item&newItem){ inline Item&SetItem(Item&newItem){
return itemRef=std::reference_wrapper<Item>(newItem); return itemRef=std::reference_wrapper<Item>(newItem);
} }
inline void SetSelected(bool selected){
this->selected=selected;
}
inline void SetShowQuantity(bool show){ inline void SetShowQuantity(bool show){
this->hideQty=!show; this->hideQty=!show;
} }
@ -146,9 +141,6 @@ protected:
} }
virtual inline void Draw(Crawler*game,vf2d parentPos)override{ virtual inline void Draw(Crawler*game,vf2d parentPos)override{
MenuIconButton::Draw(game,parentPos); MenuIconButton::Draw(game,parentPos);
if(selected){
drawutil::DrawCrosshair(game,{parentPos+rect.pos,rect.size},0);
}
} }
virtual inline void DrawDecal(Crawler*game,vf2d parentPos,bool focused)override{ virtual inline void DrawDecal(Crawler*game,vf2d parentPos,bool focused)override{
MenuIconButton::DrawDecal(game,parentPos,focused); MenuIconButton::DrawDecal(game,parentPos,focused);

@ -39,7 +39,7 @@ All rights reserved.
#define VERSION_MAJOR 0 #define VERSION_MAJOR 0
#define VERSION_MINOR 2 #define VERSION_MINOR 2
#define VERSION_PATCH 1 #define VERSION_PATCH 1
#define VERSION_BUILD 3744 #define VERSION_BUILD 3761
#define stringify(a) stringify_(a) #define stringify(a) stringify_(a)
#define stringify_(a) #a #define stringify_(a) #a

@ -13,15 +13,6 @@ ThemeGlobal
# How far the mouse has to move before mouse mode becomes active from keyboard/controller input mode. # How far the mouse has to move before mouse mode becomes active from keyboard/controller input mode.
MouseActivationDistance = 8 MouseActivationDistance = 8
# How many slots wide the inventory list is.
InventoryWidth = 5
# How tall the view is for the inventory window (before scrolling)
InventoryHeight = 3
# Space between each item slot.
InventoryItemSpacing = 8
# Size of each button.
InventoryButtonSize = 24
} }
Themes Themes

Loading…
Cancel
Save