2023-11-20 22:56:35 -06:00
# pragma region License
2023-11-14 18:11:32 -06:00
/*
License ( OLC - 3 )
~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
2023-11-14 18:12:54 -06:00
Copyright 2018 - 2023 OneLoneCoder . com
2023-11-14 18:11:32 -06:00
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 .
2023-11-29 00:50:00 -06:00
Portions of this software are copyright © 2023 The FreeType
Project ( www . freetype . org ) . Please see LICENSE_FT . txt for more information .
All rights reserved .
2023-11-14 18:11:32 -06:00
*/
2023-11-20 22:56:35 -06:00
# pragma endregion
2023-10-07 13:33:27 -05:00
# pragma once
2023-11-13 21:26:34 -06:00
# include "Menu.h"
# include "MenuLabel.h"
2023-10-07 13:33:27 -05:00
# include "MenuIconButton.h"
# include "DEFINES.h"
# include "Crawler.h"
# include "Item.h"
2023-10-07 15:47:26 -05:00
# include "safemap.h"
2023-10-11 19:50:12 -05:00
# include "olcPGEX_Graphics2D.h"
2023-11-13 21:26:34 -06:00
# include "drawutil.h"
2023-10-07 13:33:27 -05:00
INCLUDE_game
2023-10-07 15:47:26 -05:00
INCLUDE_ITEM_DATA
2023-10-07 13:33:27 -05:00
class MenuItemButton : public MenuIconButton {
private :
2023-11-21 06:05:52 -06:00
std : : vector < Item > & invRef ;
2023-10-07 15:47:26 -05:00
int inventoryIndex = 0 ;
2023-11-16 17:36:36 -06:00
MenuType itemDescriptionMenu ;
std : : string itemNameLabelName ;
std : : string itemDescriptionLabelName ;
2023-12-12 05:20:51 -06:00
CompactText compact = COMPACT ;
bool hoverState = false ;
bool runHoverFunctions = false ;
MenuFunc onHover ;
MenuFunc onMouseOut ;
2023-10-07 13:33:27 -05:00
public :
2023-11-13 21:26:34 -06:00
int selected = - 1 ; //0-2 representing which loadout slot this item consumes. -1 means not selected.
2023-12-10 19:14:37 -06:00
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 )
: MenuIconButton ( rect , invRef . size ( ) > invIndex ? invRef [ invIndex ] . Decal ( ) : nullptr , onClick , attributes ) , invRef ( invRef ) , inventoryIndex ( invIndex ) , itemDescriptionMenu ( itemDescriptionMenu ) , itemNameLabelName ( itemNameLabelName ) , itemDescriptionLabelName ( itemDescriptionLabelName ) {
2023-11-13 21:26:34 -06:00
draggable = false ;
2023-10-07 18:28:19 -05:00
valid = invRef . size ( ) > invIndex ;
2023-10-07 16:26:03 -05:00
}
2023-12-12 05:20:51 -06:00
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 ( ) {
2023-11-21 06:05:52 -06:00
return Inventory : : GetItem ( invRef . at ( inventoryIndex ) . Name ( ) ) ;
2023-10-07 16:26:03 -05:00
}
2023-11-11 17:31:53 -06:00
//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 ;
2023-11-21 06:05:52 -06:00
return Inventory : : UseItem ( invRef . at ( inventoryIndex ) . Name ( ) , amt ) ;
2023-10-07 16:26:03 -05:00
}
2023-12-12 05:20:51 -06:00
inline void SetCompactDescriptions ( bool compact ) {
if ( compact ) this - > compact = COMPACT ;
else this - > compact = NON_COMPACT ;
}
2023-10-07 13:33:27 -05:00
protected :
2023-12-12 05:20:51 -06:00
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 ) } ) ;
}
}
2023-10-07 16:26:03 -05:00
virtual inline void Update ( Crawler * game ) override {
2023-10-07 15:47:26 -05:00
MenuIconButton : : Update ( game ) ;
2023-11-21 06:05:52 -06:00
valid = invRef . size ( ) > inventoryIndex & & ITEM_DATA . count ( invRef [ inventoryIndex ] . Name ( ) ) ;
2023-12-12 05:20:51 -06:00
std : : string labelNameText ;
std : : string labelDescriptionText ;
2023-11-14 23:20:13 -06:00
if ( valid ) {
2023-11-21 06:05:52 -06:00
icon = invRef [ inventoryIndex ] . Decal ( ) ;
2023-12-12 05:20:51 -06:00
labelNameText = invRef [ inventoryIndex ] . Name ( ) ;
labelDescriptionText = invRef [ inventoryIndex ] . Description ( compact ) ;
2023-11-14 23:20:13 -06:00
} else {
icon = nullptr ;
2023-12-12 05:20:51 -06:00
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 ) } ) ;
2023-10-07 19:06:56 -05:00
}
2023-10-07 18:28:19 -05:00
}
2023-10-07 13:33:27 -05:00
}
2023-11-13 21:26:34 -06:00
virtual inline void Draw ( Crawler * game , vf2d parentPos ) override {
2023-12-12 05:20:51 -06:00
if ( ! decal ) {
MenuIconButton : : Draw ( game , parentPos ) ;
if ( selected ! = - 1 ) {
drawutil : : DrawCrosshair ( game , { parentPos + rect . pos , rect . size } , 0 ) ;
}
2023-11-13 21:26:34 -06:00
}
}
2023-10-11 19:50:12 -05:00
virtual inline void DrawDecal ( Crawler * game , vf2d parentPos , bool focused ) override {
2023-12-12 05:20:51 -06:00
if ( decal ) {
MenuIconButton : : DrawDecal ( game , parentPos , focused ) ;
2023-12-12 05:41:44 -06:00
if ( selected ! = - 1 ) {
drawutil : : DrawCrosshairDecal ( game , { parentPos + rect . pos , rect . size } , 0 ) ;
}
}
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 ) ;
2023-12-13 04:03:17 -06:00
vf2d quantityTextScale = rect . size / 48.f ;
vf2d textSize = vf2d ( game - > GetTextSizeProp ( quantityText ) ) * quantityTextScale ;
2023-12-12 05:41:44 -06:00
vf2d drawPos = parentPos + rect . pos + rect . size - textSize ;
2023-12-14 01:43:42 -06:00
geom2d : : rect < float > boundingBox = { drawPos , textSize } ;
if ( PointWithinParent ( this , boundingBox ) ) {
2023-12-13 04:03:17 -06:00
game - > DrawShadowStringDecal ( drawPos , quantityText , WHITE , BLACK , quantityTextScale , quantityTextScale . x ) ;
2023-10-12 20:16:22 -05:00
}
2023-10-07 19:06:56 -05:00
}
2023-10-07 13:33:27 -05:00
}
2023-11-15 07:53:55 -06:00
inline MenuComponent * PickUpDraggableItem ( ) override final {
2023-10-07 18:28:19 -05:00
if ( valid ) {
2023-12-10 19:14:37 -06:00
MenuItemButton * pickUp = NEW MenuItemButton ( rect , invRef , inventoryIndex , onClick , itemDescriptionMenu , itemNameLabelName , itemDescriptionLabelName ) ;
2023-10-07 18:28:19 -05:00
valid = false ;
return pickUp ;
} else {
return nullptr ;
}
}
2023-11-15 07:53:55 -06:00
inline bool DropDraggableItem ( MenuComponent * draggable ) override final {
2023-10-07 18:28:19 -05:00
//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 = ( MenuItemButton * ) draggable ;
2023-11-21 06:05:52 -06:00
ITCategory cat = draggedItem - > invRef . at ( draggedItem - > inventoryIndex ) . Category ( ) ;
2023-10-07 18:28:19 -05:00
return Inventory : : SwapItems ( cat , draggedItem - > inventoryIndex , inventoryIndex ) ;
}
2023-11-15 07:53:55 -06:00
} ;