Fix null checks. Inventory display window setup. Ready for draggable interface.

pull/28/head
sigonasr2 1 year ago
parent 99c24b9c37
commit 5903692dbb
  1. 2
      Crawler/Crawler.cpp
  2. 8
      Crawler/InventoryWindow.cpp
  3. 7
      Crawler/Item.cpp
  4. 1
      Crawler/Item.h
  5. 6
      Crawler/Menu.cpp
  6. 2
      Crawler/MenuComponent.h
  7. 6
      Crawler/MenuIconButton.h
  8. 16
      Crawler/MenuItemButton.h
  9. 2
      Crawler/Version.h

@ -356,7 +356,7 @@ void Crawler::HandleUserInput(float fElapsedTime){
}
if(GetKey(I).bPressed){
Menu::OpenMenu(TEST);
Menu::OpenMenu(INVENTORY);
}
}

@ -18,16 +18,18 @@ const Menu Menu::InitializeInventoryWindow(){
constexpr int buttonSize=24;
constexpr int totalSpacing=buttonSize+itemSpacing;
Menu inventoryWindow(CENTERED,{totalSpacing*invWidth,totalSpacing*(invHeight+1)});
Menu inventoryWindow(CENTERED,{totalSpacing*invWidth-itemSpacing,totalSpacing*(invHeight+1)-itemSpacing});
MenuFunc useItemFunc=[](MenuFuncData data){
MenuItemButton*button=(MenuItemButton*)data.component;
button->UseItem();
};
for(int y=0;y<invHeight;y++){
for(int x=0;x<invWidth;x++){
int itemIndex=y*invWidth+x;
MenuItemButton button1{{{float(totalSpacing*x),float(totalSpacing*y)},{float(buttonSize),float(buttonSize)}},Inventory::get("Consumables"),itemIndex,useItemFunc};
MenuItemButton*button=new MenuItemButton{{{float(totalSpacing*x),float(totalSpacing*y)},{float(buttonSize),float(buttonSize)}},Inventory::get("Consumables"),itemIndex,useItemFunc};
inventoryWindow.AddComponent("item"+std::to_string(itemIndex),button);
}
}

@ -24,6 +24,7 @@ void ItemInfo::InitializeItems(){
for(auto&key:DATA["ItemCategory"].GetKeys()){
ITEM_CATEGORIES[key.first];
Inventory::sortedInv[key.first];
}
for(auto&key:DATA["ItemDatabase"].GetKeys()){
@ -137,6 +138,7 @@ uint32_t Inventory::GetItemCount(IT it){
}
void Inventory::UseItem(IT it,uint32_t amt){
if(!_inventory.count(it))return;
//There are two places to manipulate items in (Both the sorted inventory and the actual inventory)
for(int i=0;i<amt;i++){
if(ExecuteAction(it)){
@ -185,6 +187,11 @@ bool Inventory::SwapItems(IT it,IT it2){
std::vector<IT>inv=sortedInv.at(category);
auto index1=std::find(inv.begin(),inv.end(),it);
auto index2=std::find(inv.begin(),inv.end(),it2);
int largestSlot=std::max(index1-inv.begin(),index2-inv.begin());
if(inv.size()<largestSlot){
//The inventory is too small, so expand out blank slots to accomodate.
inv.resize(largestSlot+size_t(1));
}
std::swap(*index1,*index2);
return true;
}

@ -34,6 +34,7 @@ public:
};
class Inventory{
friend class ItemInfo;
public:
static void AddItem(IT it,uint32_t amt=1);
static uint32_t GetItemCount(IT it);

@ -1,5 +1,5 @@
#include "Crawler.h"
#include "Menu.h"
#include "MenuComponent.h"
#include "DEFINES.h"
#include "safemap.h"
@ -58,7 +58,6 @@ void Menu::HoverMenuSelect(Crawler*game){
if(buttonHoldTime<0.3)CheckClickAndPerformMenuSelect(game);
else{
draggingComponent=buttons[selection.y][selection.x];
buttons[selection.y].erase(buttons[selection.y].begin()+selection.x);
}
else CheckClickAndPerformMenuSelect(game);
}
@ -77,8 +76,9 @@ void Menu::MenuSelect(Crawler*game){
}
void Menu::Update(Crawler*game){
if(draggingComponent!=nullptr&&(!MOUSE_NAVIGATION||game->GetMouse(Mouse::LEFT).bHeld)){
buttonHoldTime+=game->GetElapsedTime();
}
HoverMenuSelect(game);

@ -7,13 +7,13 @@ class MenuComponent{
MenuFunc onClick;
bool hovered=false;
bool selectable=true;
bool draggable=false;
private:
float hoverEffect=0;
protected:
geom2d::rect<float>rect;
std::string label;
bool border=true;
bool draggable=false;
public:
MenuComponent(geom2d::rect<float>rect,std::string label,MenuFunc onClick,bool selectable=true);
MenuComponent(geom2d::rect<float>rect,std::string label,MenuType menuDest,MenuFunc onClick,bool selectable=true);

@ -12,11 +12,13 @@ public:
inline MenuIconButton(geom2d::rect<float>rect,Decal*icon,MenuFunc onClick)
:MenuComponent(rect,"",onClick),icon(icon){}
protected:
virtual void inline Update(Crawler*game)override{
virtual inline void Update(Crawler*game)override{
MenuComponent::Update(game);
}
virtual void inline Draw(Crawler*game,vf2d parentPos,bool focused)override{
virtual inline void Draw(Crawler*game,vf2d parentPos,bool focused)override{
MenuComponent::Draw(game,parentPos,focused);
if(icon!=nullptr){
game->DrawRotatedDecal(parentPos+rect.middle(),icon,0,icon->sprite->Size()/2,{1,1},focused?WHITE:WHITE*"ThemeGlobal.MenuUnfocusedColorMult"_F);
}
}
};

@ -14,13 +14,21 @@ private:
int inventoryIndex=0;
public:
inline MenuItemButton(geom2d::rect<float>rect,std::vector<IT>&invRef,int invIndex,MenuFunc onClick)
:MenuIconButton(rect,ITEM_DATA.at(invRef[invIndex]).Decal(),onClick),invRef(invRef),inventoryIndex(invIndex){}
:MenuIconButton(rect,invRef.size()>invIndex?ITEM_DATA.at(invRef[invIndex]).Decal():nullptr,onClick),invRef(invRef),inventoryIndex(invIndex){
draggable=true;
}
inline Item GetItem(){
return Inventory::GetItem(invRef.at(inventoryIndex));
}
inline void UseItem(uint32_t amt=1){
if(invRef.size()<=inventoryIndex)return;
return Inventory::UseItem(invRef.at(inventoryIndex),amt);
}
protected:
virtual void inline Update(Crawler*game)override{
virtual inline void Update(Crawler*game)override{
MenuIconButton::Update(game);
}
virtual void inline Draw(Crawler*game,vf2d parentPos,bool focused)override{
virtual inline void Draw(Crawler*game,vf2d parentPos,bool focused)override{
MenuIconButton::Draw(game,parentPos,focused);
game->DrawRotatedDecal(parentPos+rect.middle(),icon,0,icon->sprite->Size()/2,{1,1},focused?WHITE:WHITE*"ThemeGlobal.MenuUnfocusedColorMult"_F);
}
};

@ -2,7 +2,7 @@
#define VERSION_MAJOR 0
#define VERSION_MINOR 2
#define VERSION_PATCH 0
#define VERSION_BUILD 1839
#define VERSION_BUILD 1848
#define stringify(a) stringify_(a)
#define stringify_(a) #a

Loading…
Cancel
Save