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-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-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-11-21 06:05:52 -06:00
inline MenuItemButton ( MenuType parent , geom2d : : rect < float > rect , std : : vector < Item > & invRef , int invIndex , MenuFunc onClick , MenuType itemDescriptionMenu , std : : string itemNameLabelName , std : : string itemDescriptionLabelName )
: MenuIconButton ( parent , rect , invRef . size ( ) > invIndex ? invRef [ invIndex ] . Decal ( ) : nullptr , onClick ) , 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
}
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-10-07 13:33:27 -05:00
protected :
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-11-14 23:20:13 -06:00
if ( valid ) {
2023-11-21 06:05:52 -06:00
icon = invRef [ inventoryIndex ] . Decal ( ) ;
2023-11-14 23:20:13 -06:00
if ( hovered ) {
2023-11-21 06:05:52 -06:00
Component < MenuLabel > ( itemDescriptionMenu , itemNameLabelName ) - > label = invRef [ inventoryIndex ] . Name ( ) ;
Component < MenuLabel > ( itemDescriptionMenu , itemDescriptionLabelName ) - > label = invRef [ inventoryIndex ] . Description ( ) ;
2023-11-14 23:20:13 -06:00
}
} else {
icon = nullptr ;
if ( hovered ) {
2023-11-16 17:36:36 -06:00
Component < MenuLabel > ( itemDescriptionMenu , itemNameLabelName ) - > label = " " ;
Component < MenuLabel > ( itemDescriptionMenu , itemDescriptionLabelName ) - > label = " " ;
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 {
MenuIconButton : : Draw ( game , parentPos ) ;
if ( selected ! = - 1 ) {
drawutil : : DrawCrosshair ( game , { parentPos + rect . pos , rect . size } , 0 ) ;
}
}
2023-10-11 19:50:12 -05:00
virtual inline void DrawDecal ( Crawler * game , vf2d parentPos , bool focused ) override {
MenuIconButton : : DrawDecal ( game , parentPos , focused ) ;
2023-10-07 19:06:56 -05:00
if ( valid ) {
2023-11-21 06:05:52 -06:00
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-10-07 19:06:56 -05:00
vf2d textSize = vf2d ( game - > GetTextSizeProp ( quantityText ) ) * 0.5 ;
2023-10-12 20:16:22 -05:00
vf2d drawPos = parentPos + rect . pos + rect . size - textSize ;
if ( PointWithinParent ( this , drawPos ) ) {
game - > DrawShadowStringDecal ( drawPos , quantityText , WHITE , BLACK , { 0.5 , 0.5 } , 0.5 ) ;
}
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-11-16 17:36:36 -06:00
MenuItemButton * pickUp = NEW MenuItemButton ( parentMenu , 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
} ;