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. 8
      Crawler/Menu.cpp
  6. 2
      Crawler/MenuComponent.h
  7. 8
      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){ if(GetKey(I).bPressed){
Menu::OpenMenu(TEST); Menu::OpenMenu(INVENTORY);
} }
} }

@ -18,16 +18,18 @@ const Menu Menu::InitializeInventoryWindow(){
constexpr int buttonSize=24; constexpr int buttonSize=24;
constexpr int totalSpacing=buttonSize+itemSpacing; 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){ MenuFunc useItemFunc=[](MenuFuncData data){
MenuItemButton*button=(MenuItemButton*)data.component;
button->UseItem();
}; };
for(int y=0;y<invHeight;y++){ for(int y=0;y<invHeight;y++){
for(int x=0;x<invWidth;x++){ for(int x=0;x<invWidth;x++){
int itemIndex=y*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()){ for(auto&key:DATA["ItemCategory"].GetKeys()){
ITEM_CATEGORIES[key.first]; ITEM_CATEGORIES[key.first];
Inventory::sortedInv[key.first];
} }
for(auto&key:DATA["ItemDatabase"].GetKeys()){ for(auto&key:DATA["ItemDatabase"].GetKeys()){
@ -137,6 +138,7 @@ uint32_t Inventory::GetItemCount(IT it){
} }
void Inventory::UseItem(IT it,uint32_t amt){ 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) //There are two places to manipulate items in (Both the sorted inventory and the actual inventory)
for(int i=0;i<amt;i++){ for(int i=0;i<amt;i++){
if(ExecuteAction(it)){ if(ExecuteAction(it)){
@ -185,6 +187,11 @@ bool Inventory::SwapItems(IT it,IT it2){
std::vector<IT>inv=sortedInv.at(category); std::vector<IT>inv=sortedInv.at(category);
auto index1=std::find(inv.begin(),inv.end(),it); auto index1=std::find(inv.begin(),inv.end(),it);
auto index2=std::find(inv.begin(),inv.end(),it2); 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); std::swap(*index1,*index2);
return true; return true;
} }

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

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

@ -7,13 +7,13 @@ class MenuComponent{
MenuFunc onClick; MenuFunc onClick;
bool hovered=false; bool hovered=false;
bool selectable=true; bool selectable=true;
bool draggable=false;
private: private:
float hoverEffect=0; float hoverEffect=0;
protected: protected:
geom2d::rect<float>rect; geom2d::rect<float>rect;
std::string label; std::string label;
bool border=true; bool border=true;
bool draggable=false;
public: public:
MenuComponent(geom2d::rect<float>rect,std::string label,MenuFunc onClick,bool selectable=true); 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); 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) inline MenuIconButton(geom2d::rect<float>rect,Decal*icon,MenuFunc onClick)
:MenuComponent(rect,"",onClick),icon(icon){} :MenuComponent(rect,"",onClick),icon(icon){}
protected: protected:
virtual void inline Update(Crawler*game)override{ virtual inline void Update(Crawler*game)override{
MenuComponent::Update(game); 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); MenuComponent::Draw(game,parentPos,focused);
game->DrawRotatedDecal(parentPos+rect.middle(),icon,0,icon->sprite->Size()/2,{1,1},focused?WHITE:WHITE*"ThemeGlobal.MenuUnfocusedColorMult"_F); 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; int inventoryIndex=0;
public: public:
inline MenuItemButton(geom2d::rect<float>rect,std::vector<IT>&invRef,int invIndex,MenuFunc onClick) 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: protected:
virtual void inline Update(Crawler*game)override{ virtual inline void Update(Crawler*game)override{
MenuIconButton::Update(game); 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); 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_MAJOR 0
#define VERSION_MINOR 2 #define VERSION_MINOR 2
#define VERSION_PATCH 0 #define VERSION_PATCH 0
#define VERSION_BUILD 1839 #define VERSION_BUILD 1848
#define stringify(a) stringify_(a) #define stringify(a) stringify_(a)
#define stringify_(a) #a #define stringify_(a) #a

Loading…
Cancel
Save