Added label, text, and quantity descriptions for all items on the inventory menu.

pull/28/head
sigonasr2 1 year ago
parent 803a0189d8
commit 461265a8a4
  1. 8
      Crawler/InventoryWindow.cpp
  2. 2
      Crawler/Menu.cpp
  3. 2
      Crawler/Menu.h
  4. 8
      Crawler/MenuComponent.cpp
  5. 9
      Crawler/MenuComponent.h
  6. 4
      Crawler/MenuIconButton.h
  7. 32
      Crawler/MenuItemButton.h
  8. 16
      Crawler/MenuLabel.h
  9. 8
      Crawler/TestMenu.cpp
  10. 8
      Crawler/TestSubMenu.cpp
  11. 2
      Crawler/Version.h

@ -5,6 +5,7 @@
#include "safemap.h" #include "safemap.h"
#include "Item.h" #include "Item.h"
#include "MenuItemButton.h" #include "MenuItemButton.h"
#include "MenuLabel.h"
INCLUDE_GFX INCLUDE_GFX
typedef Attribute A; typedef Attribute A;
@ -28,10 +29,15 @@ const Menu Menu::InitializeInventoryWindow(){
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*button=new MenuItemButton{{{float(totalSpacing*x),float(totalSpacing*y)},{float(buttonSize),float(buttonSize)}},Inventory::get("Consumables"),itemIndex,useItemFunc}; MenuItemButton*button=new MenuItemButton{INVENTORY,{{float(totalSpacing*x),float(totalSpacing*y)},{float(buttonSize),float(buttonSize)}},Inventory::get("Consumables"),itemIndex,useItemFunc};
inventoryWindow.AddComponent("item"+std::to_string(itemIndex),button); inventoryWindow.AddComponent("item"+std::to_string(itemIndex),button);
} }
} }
MenuLabel*itemNameLabel=new MenuLabel{INVENTORY,vf2d{0,invHeight*totalSpacing-4},"",false,true};
inventoryWindow.AddComponent("itemName",itemNameLabel);
MenuLabel*itemDescriptionLabel=new MenuLabel{INVENTORY,vf2d{2+inventoryWindow.size.x/2,invHeight*totalSpacing+4+itemSpacing},"",true,true};
inventoryWindow.AddComponent("itemDescription",itemDescriptionLabel);
return inventoryWindow; return inventoryWindow;
} }

@ -111,12 +111,14 @@ void Menu::Update(Crawler*game){
if(!MOUSE_NAVIGATION){ if(!MOUSE_NAVIGATION){
if(game->GetKey(ENTER).bReleased||game->GetKey(SPACE).bReleased){ if(game->GetKey(ENTER).bReleased||game->GetKey(SPACE).bReleased){
if(selectedComponent->DropDraggableItem(draggingComponent)){ if(selectedComponent->DropDraggableItem(draggingComponent)){
delete draggingComponent; //We know we allocated a new instance of this, so we will now free it.
draggingComponent=nullptr; draggingComponent=nullptr;
} }
} }
}else{ }else{
if(game->GetMouse(Mouse::LEFT).bReleased){ if(game->GetMouse(Mouse::LEFT).bReleased){
if(selectedComponent->DropDraggableItem(draggingComponent)){ if(selectedComponent->DropDraggableItem(draggingComponent)){
delete draggingComponent; //We know we allocated a new instance of this, so we will now free it.
draggingComponent=nullptr; draggingComponent=nullptr;
} }
} }

@ -30,7 +30,6 @@ class Menu:IAttributable{
std::map<int/*Y*/,std::vector<MenuComponent*>>buttons; //Buttons are stored in rows followed by their column order. std::map<int/*Y*/,std::vector<MenuComponent*>>buttons; //Buttons are stored in rows followed by their column order.
std::map<int/*Y*/,std::vector<MenuComponent*>>newButtonArrangement; //The new setup that the buttons will be at if a drag operation completes. std::map<int/*Y*/,std::vector<MenuComponent*>>newButtonArrangement; //The new setup that the buttons will be at if a drag operation completes.
std::vector<MenuComponent*>displayComponents; //Components that are only for displaying purposes. std::vector<MenuComponent*>displayComponents; //Components that are only for displaying purposes.
safemap<std::string,MenuComponent*>components; //A friendly way to interrogate any component we are interested in.
vi2d selection={-1,-1}; vi2d selection={-1,-1};
vf2d pos; //Specify the upper-left corner of the window. Using CENTERED will always put this where the upper-left corner would center the window. vf2d pos; //Specify the upper-left corner of the window. Using CENTERED will always put this where the upper-left corner would center the window.
vf2d size; //Size in tiles (24x24), every menu will be tile-based vf2d size; //Size in tiles (24x24), every menu will be tile-based
@ -48,6 +47,7 @@ public:
static std::string themeSelection; static std::string themeSelection;
static safeunorderedmap<std::string,Theme>themes; static safeunorderedmap<std::string,Theme>themes;
static const vf2d CENTERED; static const vf2d CENTERED;
safemap<std::string,MenuComponent*>components; //A friendly way to interrogate any component we are interested in.
private: private:
void HoverMenuSelect(Crawler*game); void HoverMenuSelect(Crawler*game);

@ -1,11 +1,11 @@
#include "Crawler.h" #include "Crawler.h"
#include "MenuComponent.h" #include "MenuComponent.h"
MenuComponent::MenuComponent(geom2d::rect<float>rect,std::string label,MenuFunc onClick,bool selectable) MenuComponent::MenuComponent(MenuType parent,geom2d::rect<float>rect,std::string label,MenuFunc onClick,bool selectable)
:rect(rect),label(label),menuDest(MenuType::ENUM_END),onClick(onClick),hoverEffect(0),selectable(selectable){} :parentMenu(parent),rect(rect),label(label),menuDest(MenuType::ENUM_END),onClick(onClick),hoverEffect(0),selectable(selectable){}
MenuComponent::MenuComponent(geom2d::rect<float>rect,std::string label,MenuType menuDest,MenuFunc onClick,bool selectable) MenuComponent::MenuComponent(MenuType parent,geom2d::rect<float>rect,std::string label,MenuType menuDest,MenuFunc onClick,bool selectable)
:rect(rect),label(label),menuDest(menuDest),onClick(onClick),hoverEffect(0),selectable(selectable){} :parentMenu(parent),rect(rect),label(label),menuDest(menuDest),onClick(onClick),hoverEffect(0),selectable(selectable){}
void MenuComponent::Update(Crawler*game){ void MenuComponent::Update(Crawler*game){
if(hovered){ if(hovered){

@ -3,8 +3,8 @@
class MenuComponent{ class MenuComponent{
friend class Menu; friend class Menu;
friend class MenuItemButton;
MenuType menuDest; MenuType menuDest;
bool hovered=false;
bool selectable=true; bool selectable=true;
private: private:
float hoverEffect=0; float hoverEffect=0;
@ -14,12 +14,15 @@ protected:
bool border=true; bool border=true;
bool draggable=false; bool draggable=false;
MenuFunc onClick; MenuFunc onClick;
MenuType parentMenu=MenuType::ENUM_END;
bool hovered=false;
public: public:
MenuComponent(geom2d::rect<float>rect,std::string label,MenuFunc onClick,bool selectable=true); MenuComponent(MenuType parent,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(MenuType parent,geom2d::rect<float>rect,std::string label,MenuType menuDest,MenuFunc onClick,bool selectable=true);
virtual void Update(Crawler*game); virtual void Update(Crawler*game);
virtual void Draw(Crawler*game,vf2d parentPos,bool focused); virtual void Draw(Crawler*game,vf2d parentPos,bool focused);
//We picked up a draggable component, we should make a copy and return it here. If a nullptr is returned here, the pickup is not allowed. //We picked up a draggable component, we should make a copy and return it here. If a nullptr is returned here, the pickup is not allowed.
//WARNING!!! This allocates a brand new component when successful!!! Be prepared to clear it!
virtual MenuComponent*PickUpDraggableItem(); virtual MenuComponent*PickUpDraggableItem();
//We are attempting to drop draggable onto this item. If it's not allowed, return false. //We are attempting to drop draggable onto this item. If it's not allowed, return false.
virtual bool DropDraggableItem(MenuComponent*draggable); virtual bool DropDraggableItem(MenuComponent*draggable);

@ -9,8 +9,8 @@ class MenuIconButton:public MenuComponent{
protected: protected:
Decal*icon; Decal*icon;
public: public:
inline MenuIconButton(geom2d::rect<float>rect,Decal*icon,MenuFunc onClick) inline MenuIconButton(MenuType parent,geom2d::rect<float>rect,Decal*icon,MenuFunc onClick)
:MenuComponent(rect,"",onClick),icon(icon){} :MenuComponent(parent,rect,"",onClick),icon(icon){}
protected: protected:
virtual inline void Update(Crawler*game)override{ virtual inline void Update(Crawler*game)override{
MenuComponent::Update(game); MenuComponent::Update(game);

@ -14,8 +14,8 @@ private:
int inventoryIndex=0; int inventoryIndex=0;
bool valid=false; bool valid=false;
public: public:
inline MenuItemButton(geom2d::rect<float>rect,std::vector<IT>&invRef,int invIndex,MenuFunc onClick) inline MenuItemButton(MenuType parent,geom2d::rect<float>rect,std::vector<IT>&invRef,int invIndex,MenuFunc onClick)
:MenuIconButton(rect,invRef.size()>invIndex?ITEM_DATA.at(invRef[invIndex]).Decal():nullptr,onClick),invRef(invRef),inventoryIndex(invIndex){ :MenuIconButton(parent,rect,invRef.size()>invIndex?ITEM_DATA.at(invRef[invIndex]).Decal():nullptr,onClick),invRef(invRef),inventoryIndex(invIndex){
draggable=true; draggable=true;
valid=invRef.size()>invIndex; valid=invRef.size()>invIndex;
} }
@ -30,18 +30,44 @@ protected:
virtual inline void Update(Crawler*game)override{ virtual inline void Update(Crawler*game)override{
MenuIconButton::Update(game); MenuIconButton::Update(game);
valid=invRef.size()>inventoryIndex&&ITEM_DATA.count(invRef[inventoryIndex]); valid=invRef.size()>inventoryIndex&&ITEM_DATA.count(invRef[inventoryIndex]);
Menu*menu=Menu::stack.back();
if(valid){ if(valid){
icon=ITEM_DATA.at(invRef[inventoryIndex]).Decal(); icon=ITEM_DATA.at(invRef[inventoryIndex]).Decal();
if(hovered){
switch(parentMenu){
case INVENTORY:{
//There should be an itemName label to modify.
menu->components.at("itemName")->label=ITEM_DATA.at(invRef[inventoryIndex]).Name();
//There should be an itemDescription label to modify.
menu->components.at("itemDescription")->label=ITEM_DATA.at(invRef[inventoryIndex]).Description();
}break;
}
}
}else{ }else{
icon=nullptr; icon=nullptr;
if(hovered){
switch(parentMenu){
case INVENTORY:{
//There should be an itemName label to modify.
menu->components.at("itemName")->label="";
//There should be an itemDescription label to modify.
menu->components.at("itemDescription")->label="";
}break;
}
}
} }
} }
virtual inline void 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);
if(valid){
std::string quantityText="x"+std::to_string(Inventory::GetItemCount(invRef.at(inventoryIndex)));
vf2d textSize=vf2d(game->GetTextSizeProp(quantityText))*0.5;
game->DrawShadowStringDecal(parentPos+rect.pos+rect.size-textSize,quantityText,WHITE,BLACK,{0.5,0.5},0.5);
}
} }
virtual inline MenuComponent*PickUpDraggableItem()override{ virtual inline MenuComponent*PickUpDraggableItem()override{
if(valid){ if(valid){
MenuItemButton*pickUp=new MenuItemButton(rect,invRef,inventoryIndex,onClick); MenuItemButton*pickUp=new MenuItemButton(parentMenu,rect,invRef,inventoryIndex,onClick);
valid=false; valid=false;
return pickUp; return pickUp;
}else{ }else{

@ -6,9 +6,11 @@
INCLUDE_game INCLUDE_game
class MenuLabel:public MenuComponent{ class MenuLabel:public MenuComponent{
bool shadow=false;
bool centered=true;
public: public:
inline MenuLabel(geom2d::rect<float>rect,std::string label) inline MenuLabel(MenuType parent,geom2d::rect<float>rect,std::string label,bool centered=true,bool shadow=false)
:MenuComponent(rect,label,MenuFunc{},false){ :MenuComponent(parent,rect,label,MenuFunc{},false),centered(centered),shadow(shadow){
border=false; border=false;
} }
inline void SetLabel(std::string text){ inline void SetLabel(std::string text){
@ -19,6 +21,14 @@ protected:
MenuComponent::Update(game); MenuComponent::Update(game);
} }
virtual void inline Draw(Crawler*game,vf2d parentPos,bool focused)override{ virtual void inline Draw(Crawler*game,vf2d parentPos,bool focused)override{
MenuComponent::Draw(game,parentPos,focused); vf2d drawPos=rect.pos+parentPos+rect.size/2-game->GetTextSizeProp(label)/2; //Assume centered.
if(!centered){
drawPos=rect.pos+parentPos;
}
if(shadow){
game->DrawShadowStringPropDecal(drawPos,label,focused?WHITE:WHITE*"ThemeGlobal.MenuUnfocusedColorMult"_F);
}else{
game->DrawStringPropDecal(drawPos,label,focused?WHITE:WHITE*"ThemeGlobal.MenuUnfocusedColorMult"_F);
}
} }
}; };

@ -8,19 +8,19 @@ const Menu Menu::InitializeTestMenu(){
data.menu.stack.clear(); data.menu.stack.clear();
}; };
testMenu.AddComponent("Close",new MenuComponent({{24*1,24*1},{24*2,24*1}},"Close",quitWindow)); testMenu.AddComponent("Close",new MenuComponent(TEST,{{24*1,24*1},{24*2,24*1}},"Close",quitWindow));
MenuFunc doNothing=[](MenuFuncData data){}; MenuFunc doNothing=[](MenuFuncData data){};
testMenu.AddComponent("Test",new MenuComponent({{24*4,24*1},{24*3,24*1}},"Test",doNothing)); testMenu.AddComponent("Test",new MenuComponent(TEST,{{24*4,24*1},{24*3,24*1}},"Test",doNothing));
MenuFunc HurtPlayer=[](MenuFuncData data){ MenuFunc HurtPlayer=[](MenuFuncData data){
data.game->GetPlayer()->Hurt(20,data.game->GetPlayer()->OnUpperLevel(),data.game->GetPlayer()->GetZ()); data.game->GetPlayer()->Hurt(20,data.game->GetPlayer()->OnUpperLevel(),data.game->GetPlayer()->GetZ());
}; };
testMenu.AddComponent("Hurt Player",new MenuComponent({{24*4,24*3},{24*3,24*1}},"Hurt Player",HurtPlayer)); testMenu.AddComponent("Hurt Player",new MenuComponent(TEST,{{24*4,24*3},{24*3,24*1}},"Hurt Player",HurtPlayer));
testMenu.AddComponent("Open SubMenu",new MenuComponent({{24*2,24*4.5},{24*4,24*1}},"Open Another\n Menu",TEST_2,doNothing)); testMenu.AddComponent("Open SubMenu",new MenuComponent(TEST,{{24*2,24*4.5},{24*4,24*1}},"Open Another\n Menu",TEST_2,doNothing));
return testMenu; return testMenu;
} }

@ -15,7 +15,7 @@ const Menu Menu::InitializeTestSubMenu(){
data.menu.stack.pop_back(); data.menu.stack.pop_back();
}; };
testSubMenu.AddComponent("BACK",new MenuComponent({{24*1,24*1},{24*2,24*1}},"Go Back",goBack)); testSubMenu.AddComponent("BACK",new MenuComponent(TEST_2,{{24*1,24*1},{24*2,24*1}},"Go Back",goBack));
int index=0; int index=0;
for(auto&theme:Menu::themes){ for(auto&theme:Menu::themes){
@ -43,9 +43,9 @@ const Menu Menu::InitializeTestSubMenu(){
} }
}; };
testSubMenu.AddComponent("PREV_THEME",new MenuComponent({{24*-0.5,24*3},{24*1,24*1}},"<",themePrev)); testSubMenu.AddComponent("PREV_THEME",new MenuComponent(TEST_2,{{24*-0.5,24*3},{24*1,24*1}},"<",themePrev));
testSubMenu.AddComponent("THEME_DISPLAY",new MenuLabel({{24*0.5,24*3},{24*3,24*1}},"Theme\n"+Menu::themes[themeSelection].GetThemeName())); testSubMenu.AddComponent("THEME_DISPLAY",new MenuLabel(TEST_2,{{24*0.5,24*3},{24*3,24*1}},"Theme\n"+Menu::themes[themeSelection].GetThemeName()));
MenuFunc themeNext=[](MenuFuncData data){ MenuFunc themeNext=[](MenuFuncData data){
data.menu.I(A::INDEXED_THEME)=(size_t(data.menu.I(A::INDEXED_THEME))+1)%themes.size(); data.menu.I(A::INDEXED_THEME)=(size_t(data.menu.I(A::INDEXED_THEME))+1)%themes.size();
@ -60,7 +60,7 @@ const Menu Menu::InitializeTestSubMenu(){
} }
}; };
testSubMenu.AddComponent("NEXT_THEME",new MenuComponent({{24*3.5,24*3},{24*1,24*1}},">",themeNext)); testSubMenu.AddComponent("NEXT_THEME",new MenuComponent(TEST_2,{{24*3.5,24*3},{24*1,24*1}},">",themeNext));
return testSubMenu; return testSubMenu;
} }

@ -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 1881 #define VERSION_BUILD 1895
#define stringify(a) stringify_(a) #define stringify(a) stringify_(a)
#define stringify_(a) #a #define stringify_(a) #a

Loading…
Cancel
Save