# pragma region License
/*
License ( OLC - 3 )
~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
Copyright 2024 Joshua Sigona < sigonasr2 @ gmail . com >
Redistribution and use in source and binary forms , with or without modification ,
are permitted provided that the following conditions are met :
1. Redistributions or derivations of source code must retain the above copyright
notice , this list of conditions and the following disclaimer .
2. Redistributions or derivative works in binary form must reproduce the above
copyright notice . This list of conditions and the following disclaimer must be
reproduced in the documentation and / or other materials provided with the distribution .
3. Neither the name of the copyright holder nor the names of its contributors may
be used to endorse or promote products derived from this software without specific
prior written permission .
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS " AS IS " AND ANY
EXPRESS OR IMPLIED WARRANTIES , INCLUDING , BUT NOT LIMITED TO , THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED . IN NO EVENT
SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT , INDIRECT ,
INCIDENTAL , SPECIAL , EXEMPLARY , OR CONSEQUENTIAL DAMAGES ( INCLUDING , BUT NOT LIMITED
TO , PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES ; LOSS OF USE , DATA , OR PROFITS ; OR
BUSINESS INTERRUPTION ) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY , WHETHER IN
CONTRACT , STRICT LIABILITY , OR TORT ( INCLUDING NEGLIGENCE OR OTHERWISE ) ARISING IN
ANY WAY OUT OF THE USE OF THIS SOFTWARE , EVEN IF ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE .
Portions of this software are copyright © 2024 The FreeType
Project ( www . freetype . org ) . Please see LICENSE_FT . txt for more information .
All rights reserved .
*/
# pragma endregion
# pragma once
# include "Menu.h"
# include "MenuLabel.h"
# include "MenuIconButton.h"
# include "DEFINES.h"
# include "AdventuresInLestoria.h"
# include "Item.h"
# include "safemap.h"
# include "drawutil.h"
INCLUDE_game
INCLUDE_ITEM_DATA
class MenuItemButton : public MenuIconButton {
friend class InventoryScrollableWindowComponent ;
private :
const std : : vector < std : : shared_ptr < Item > > & invRef ;
MenuType itemDescriptionMenu ;
std : : string itemNameLabelName ;
std : : string itemDescriptionLabelName ;
CompactText compact = COMPACT ;
public :
int selected = - 1 ; //0-2 representing which loadout slot this item consumes. -1 means not selected.
inline MenuItemButton ( geom2d : : rect < float > rect , const std : : vector < std : : shared_ptr < Item > > & invRef , int invIndex , MenuFunc onClick , MenuType itemDescriptionMenu , std : : string itemNameLabelName , std : : string itemDescriptionLabelName , IconButtonAttr attributes = IconButtonAttr : : SELECTABLE )
: MenuIconButton ( rect , invRef . size ( ) > invIndex ? const_cast < Decal * > ( invRef [ invIndex ] - > Decal ( ) ) : nullptr , onClick , attributes ) , invRef ( invRef ) , itemDescriptionMenu ( itemDescriptionMenu ) , itemNameLabelName ( itemNameLabelName ) , itemDescriptionLabelName ( itemDescriptionLabelName ) {
draggable = false ;
inventoryIndex = invIndex ;
valid = invRef . size ( ) > invIndex ;
}
inline MenuItemButton ( geom2d : : rect < float > rect , const std : : vector < std : : shared_ptr < 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 ? const_cast < Decal * > ( invRef [ invIndex ] - > Decal ( ) ) : nullptr , onClick , attributes ) , invRef ( invRef ) , itemDescriptionMenu ( itemDescriptionMenu ) , itemNameLabelName ( itemNameLabelName ) , itemDescriptionLabelName ( itemDescriptionLabelName ) {
draggable = false ;
inventoryIndex = invIndex ;
runHoverFunctions = true ;
valid = invRef . size ( ) > invIndex ;
SetHoverFunc ( onHover ) ;
SetMouseOutFunc ( onMouseOut ) ;
}
inline std : : weak_ptr < Item > GetItem ( ) {
return invRef . at ( inventoryIndex ) ;
}
//Returns true if the item has been consumed completely and there are 0 remaining of that type in our inventory.
inline bool UseItem ( uint32_t amt = 1 ) {
if ( invRef . size ( ) < = inventoryIndex ) return false ;
return Inventory : : UseItem ( invRef . at ( inventoryIndex ) - > ActualName ( ) , amt ) ;
}
inline void SetCompactDescriptions ( CompactText compact ) {
this - > compact = compact ;
}
inline virtual void Update ( AiL * game ) override {
MenuIconButton : : Update ( game ) ;
valid = invRef . size ( ) > inventoryIndex & & ITEM_DATA . count ( invRef [ inventoryIndex ] - > ActualName ( ) ) & & invRef [ inventoryIndex ] - > Amt ( ) > 0 ;
if ( ! valid ) {
icon = nullptr ;
}
if ( hovered ) {
UpdateLabel ( ) ;
}
}
protected :
virtual inline void OnMouseOut ( ) override {
if ( itemNameLabelName ! = " " ) {
Component < MenuLabel > ( parentMenu , itemNameLabelName ) - > Disable ( ) ;
}
if ( itemDescriptionLabelName ! = " " ) {
Component < MenuLabel > ( parentMenu , itemDescriptionLabelName ) - > Disable ( ) ;
}
}
void UpdateLabel ( ) {
if ( ISBLANK ( invRef [ inventoryIndex ] ) ) return ;
std : : string labelNameText ;
std : : string labelDescriptionText ;
if ( valid ) {
icon = const_cast < Decal * > ( invRef [ inventoryIndex ] - > Decal ( ) ) ;
labelNameText = invRef [ inventoryIndex ] - > DisplayName ( ) ;
labelDescriptionText = invRef [ inventoryIndex ] - > Description ( compact ) ;
} else {
icon = nullptr ;
labelNameText = " " ;
labelDescriptionText = " " ;
}
if ( itemNameLabelName ! = " " ) {
Component < MenuLabel > ( parentMenu , itemNameLabelName ) - > SetLabel ( labelNameText ) ;
Component < MenuLabel > ( parentMenu , itemNameLabelName ) - > Enable ( ) ;
}
if ( itemDescriptionLabelName ! = " " ) {
Component < MenuLabel > ( parentMenu , itemDescriptionLabelName ) - > SetLabel ( labelDescriptionText ) ;
Component < MenuLabel > ( parentMenu , itemDescriptionLabelName ) - > Enable ( ) ;
}
}
virtual inline void OnHover ( ) override {
UpdateLabel ( ) ;
}
virtual inline void DrawDecal ( ViewPort & window , bool focused ) override {
MenuIconButton : : DrawDecal ( window , focused ) ;
if ( selected ! = - 1 ) {
drawutil : : DrawCrosshairDecalViewPort ( window , { rect . pos , rect . size } , 0 ) ;
}
if ( valid ) {
int itemQuantity = Inventory : : GetItemCount ( invRef . at ( inventoryIndex ) - > ActualName ( ) ) ; //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 quantityTextScale = rect . size / 48.f ;
vf2d textSize = vf2d ( game - > GetTextSizeProp ( quantityText ) ) * quantityTextScale ;
vf2d drawPos = rect . pos + rect . size - textSize ;
if ( itemQuantity ! = INFINITE ) {
window . DrawShadowStringDecal ( drawPos , quantityText , WHITE , BLACK , quantityTextScale ) ;
}
}
}
inline std : : unique_ptr < MenuComponent > PickUpDraggableItem ( ) override final {
if ( valid ) {
std : : unique_ptr < MenuItemButton > pickUp = std : : make_unique < MenuItemButton > ( rect , invRef , inventoryIndex , onClick , itemDescriptionMenu , itemNameLabelName , itemDescriptionLabelName ) ;
valid = false ;
return pickUp ;
} else {
return nullptr ;
}
}
inline bool DropDraggableItem ( std : : unique_ptr < MenuComponent > draggable ) override final {
//HACK Warning! We're making a bold assumption that every component that is draggable is of the same type! This may change in the future....
MenuItemButton * draggedItem = DYNAMIC_CAST < MenuItemButton * > ( draggable . get ( ) ) ;
ITCategory cat = draggedItem - > invRef . at ( draggedItem - > inventoryIndex ) - > Category ( ) ;
return Inventory : : SwapItems ( cat , draggedItem - > inventoryIndex , inventoryIndex ) ;
}
} ;