@ -56,6 +56,11 @@ private:
MenuType itemDescriptionMenu ;
std : : string itemNameLabelName ;
std : : string itemDescriptionLabelName ;
CompactText compact = COMPACT ;
bool hoverState = false ;
bool runHoverFunctions = false ;
MenuFunc onHover ;
MenuFunc onMouseOut ;
public :
int selected = - 1 ; //0-2 representing which loadout slot this item consumes. -1 means not selected.
inline MenuItemButton ( geom2d : : rect < float > rect , std : : vector < Item > & invRef , int invIndex , MenuFunc onClick , MenuType itemDescriptionMenu , std : : string itemNameLabelName , std : : string itemDescriptionLabelName , IconButtonAttr attributes = IconButtonAttr : : SELECTABLE )
@ -63,7 +68,13 @@ public:
draggable = false ;
valid = invRef . size ( ) > invIndex ;
}
inline Item GetItem ( ) {
inline MenuItemButton ( geom2d : : rect < float > rect , std : : vector < Item > & invRef , int invIndex , MenuFunc onClick , MenuFunc onHover , MenuFunc onMouseOut , MenuType itemDescriptionMenu , std : : string itemNameLabelName , std : : string itemDescriptionLabelName , IconButtonAttr attributes = IconButtonAttr : : SELECTABLE )
: MenuIconButton ( rect , invRef . size ( ) > invIndex ? invRef [ invIndex ] . Decal ( ) : nullptr , onClick , attributes ) , onHover ( onHover ) , onMouseOut ( onMouseOut ) , invRef ( invRef ) , inventoryIndex ( invIndex ) , itemDescriptionMenu ( itemDescriptionMenu ) , itemNameLabelName ( itemNameLabelName ) , itemDescriptionLabelName ( itemDescriptionLabelName ) {
draggable = false ;
runHoverFunctions = true ;
valid = invRef . size ( ) > invIndex ;
}
inline Item & GetItem ( ) {
return Inventory : : GetItem ( invRef . at ( inventoryIndex ) . Name ( ) ) ;
}
//Returns true if the item has been consumed completely and there are 0 remaining of that type in our inventory.
@ -71,42 +82,79 @@ public:
if ( invRef . size ( ) < = inventoryIndex ) return false ;
return Inventory : : UseItem ( invRef . at ( inventoryIndex ) . Name ( ) , amt ) ;
}
inline void SetCompactDescriptions ( bool compact ) {
if ( compact ) this - > compact = COMPACT ;
else this - > compact = NON_COMPACT ;
}
protected :
virtual inline void BeforeUpdate ( Crawler * game ) override {
if ( ! hovered & & runHoverFunctions & & hoverState ) {
hoverState = false ;
if ( itemNameLabelName ! = " " ) {
Component < MenuLabel > ( parentMenu , itemNameLabelName ) - > Enable ( false ) ;
}
if ( itemDescriptionLabelName ! = " " ) {
Component < MenuLabel > ( parentMenu , itemDescriptionLabelName ) - > Enable ( false ) ;
}
onMouseOut ( MenuFuncData { * Menu : : menus [ parentMenu ] , game , this , ( ScrollableWindowComponent * ) ( parentComponent ) } ) ;
}
}
virtual inline void Update ( Crawler * game ) override {
MenuIconButton : : Update ( game ) ;
valid = invRef . size ( ) > inventoryIndex & & ITEM_DATA . count ( invRef [ inventoryIndex ] . Name ( ) ) ;
std : : string labelNameText ;
std : : string labelDescriptionText ;
if ( valid ) {
icon = invRef [ inventoryIndex ] . Decal ( ) ;
if ( hovered ) {
if ( itemNameLabelName . length ( ) > 0 ) Component < MenuLabel > ( itemDescriptionMenu , itemNameLabelName ) - > label = invRef [ inventoryIndex ] . Name ( ) ;
if ( itemDescriptionLabelName . length ( ) > 0 ) Component < MenuLabel > ( itemDescriptionMenu , itemDescriptionLabelName ) - > label = invRef [ inventoryIndex ] . Description ( ) ;
}
labelNameText = invRef [ inventoryIndex ] . Name ( ) ;
labelDescriptionText = invRef [ inventoryIndex ] . Description ( compact ) ;
} else {
icon = nullptr ;
if ( hovered ) {
if ( itemNameLabelName . length ( ) > 0 ) Component < MenuLabel > ( itemDescriptionMenu , itemNameLabelName ) - > label = " " ;
if ( itemDescriptionLabelName . length ( ) > 0 ) Component < MenuLabel > ( itemDescriptionMenu , itemDescriptionLabelName ) - > label = " " ;
labelNameText = " " ;
labelDescriptionText = " " ;
}
if ( hovered ) {
if ( itemNameLabelName ! = " " ) {
Component < MenuLabel > ( parentMenu , itemNameLabelName ) - > label = labelNameText ;
}
if ( itemDescriptionLabelName ! = " " ) {
Component < MenuLabel > ( parentMenu , itemDescriptionLabelName ) - > label = labelDescriptionText ;
}
if ( runHoverFunctions & & ! hoverState ) {
hoverState = true ;
if ( itemNameLabelName ! = " " & & labelNameText . length ( ) > 0 ) {
Component < MenuLabel > ( parentMenu , itemNameLabelName ) - > Enable ( true ) ;
}
if ( itemDescriptionLabelName ! = " " & & labelDescriptionText . length ( ) > 0 ) {
Component < MenuLabel > ( parentMenu , itemDescriptionLabelName ) - > Enable ( true ) ;
}
onHover ( MenuFuncData { * Menu : : menus [ parentMenu ] , game , this , ( ScrollableWindowComponent * ) ( parentComponent ) } ) ;
}
}
}
virtual inline void Draw ( Crawler * game , vf2d parentPos ) override {
MenuIconButton : : Draw ( game , parentPos ) ;
if ( selected ! = - 1 ) {
drawutil : : DrawCrosshair ( game , { parentPos + rect . pos , rect . size } , 0 ) ;
if ( ! decal ) {
MenuIconButton : : Draw ( game , parentPos ) ;
if ( selected ! = - 1 ) {
drawutil : : DrawCrosshair ( game , { parentPos + rect . pos , rect . size } , 0 ) ;
}
}
}
virtual inline void DrawDecal ( Crawler * game , vf2d parentPos , bool focused ) override {
MenuIconButton : : DrawDecal ( game , parentPos , focused ) ;
if ( valid ) {
int itemQuantity = Inventory : : GetItemCount ( invRef . at ( inventoryIndex ) . Name ( ) ) ; //Normally we'd retrieve how many of this item we have from our inventory...However Monster Loot and Stage Loot inventories are special and hold their own inventory counts...
if ( & invRef = = & Inventory : : get ( " Monster Loot " ) | | & invRef = = & Inventory : : get ( " Stage Loot " ) ) {
itemQuantity = invRef . at ( inventoryIndex ) . Amt ( ) ; //So the item quantity comes from the stack itself and not our main inventory.
}
std : : string quantityText = " x " + std : : to_string ( itemQuantity ) ;
vf2d textSize = vf2d ( game - > GetTextSizeProp ( quantityText ) ) * 0.5 ;
vf2d drawPos = parentPos + rect . pos + rect . size - textSize ;
if ( PointWithinParent ( this , drawPos ) ) {
game - > DrawShadowStringDecal ( drawPos , quantityText , WHITE , BLACK , { 0.5 , 0.5 } , 0.5 ) ;
if ( decal ) {
MenuIconButton : : DrawDecal ( game , parentPos , focused ) ;
if ( valid ) {
int itemQuantity = Inventory : : GetItemCount ( invRef . at ( inventoryIndex ) . Name ( ) ) ; //Normally we'd retrieve how many of this item we have from our inventory...However Monster Loot and Stage Loot inventories are special and hold their own inventory counts...
if ( & invRef = = & Inventory : : get ( " Monster Loot " ) | | & invRef = = & Inventory : : get ( " Stage Loot " ) ) {
itemQuantity = invRef . at ( inventoryIndex ) . Amt ( ) ; //So the item quantity comes from the stack itself and not our main inventory.
}
std : : string quantityText = " x " + std : : to_string ( itemQuantity ) ;
vf2d textSize = vf2d ( game - > GetTextSizeProp ( quantityText ) ) * 0.5 ;
vf2d drawPos = parentPos + rect . pos + rect . size - textSize ;
if ( PointWithinParent ( this , drawPos ) ) {
game - > DrawShadowStringDecal ( drawPos , quantityText , WHITE , BLACK , { 0.5 , 0.5 } , 0.5 ) ;
}
}
}
}