# 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 "MenuLabel.h"
# include "Item.h"
class RequiredMaterialsList : public MenuLabel {
std : : weak_ptr < Item > itemRef ;
uint8_t qty = 1 ;
public :
inline RequiredMaterialsList ( geom2d : : rect < float > rect , const std : : weak_ptr < Item > itemRef , ComponentAttr attributes = ComponentAttr : : SHADOW | ComponentAttr : : LEFT_ALIGN )
: MenuLabel ( rect , " " , 1.f , attributes ) , itemRef ( itemRef ) { }
inline void SetItem ( const std : : weak_ptr < Item > itemRef ) {
this - > itemRef = itemRef ;
}
inline const std : : weak_ptr < Item > GetItem ( ) const {
return itemRef ;
}
inline void SetQuantity ( uint8_t quantity ) {
this - > qty = quantity ;
}
protected :
inline virtual void DrawDecal ( ViewPort & window , bool focused ) override final {
MenuComponent : : DrawDecal ( window , focused ) ;
vf2d adjustedScale = { scale , scale } ;
vf2d labelTextSize = vf2d ( game - > GetWrappedTextSizeProp ( GetLabel ( ) , rect . size . x , adjustedScale ) ) ;
if ( fitToLabel ) {
float sizeRatio = ( ( labelTextSize * adjustedScale ) . x ) / ( rect . size . x - 2 ) ;
if ( sizeRatio > 1 ) {
adjustedScale . x / = sizeRatio ;
}
labelTextSize = vf2d ( game - > GetTextSizeProp ( GetLabel ( ) ) * adjustedScale ) ;
}
if ( itemRef . lock ( ) - > EnhancementIsPossible ( ) ) {
size_t enhancementIndex = std : : min ( itemRef . lock ( ) - > EnhancementLevel ( ) + 1 , " Item.Item Max Enhancement Level " _I ) ;
if ( ! itemRef . lock ( ) - > IsEquippable ( ) | | itemRef . lock ( ) - > GetEnhancementInfo ( ) [ enhancementIndex ] . chapterAvailable < = game - > GetCurrentChapter ( ) & & itemRef . lock ( ) - > EnhancementLevel ( ) ! = " Item.Item Max Enhancement Level " _I ) {
if ( itemRef . lock ( ) - > IsEquippable ( ) & & Inventory : : GetItemCount ( itemRef . lock ( ) - > ActualName ( ) ) = = 0 ) enhancementIndex = 0 ; //If we don't have the item, use the initial crafting list instead. But only for equipment!
float drawWidth = rect . size . x / 3 ;
int index = 0 ;
for ( const auto & [ name , amt ] : itemRef . lock ( ) - > GetEnhancementInfo ( ) [ enhancementIndex ] . craftingRequirement . GetItems ( ) ) {
Pixel textCol = WHITE ;
if ( Inventory : : GetItemCount ( name ) < amt * qty ) textCol = RED ;
vf2d drawPos = rect . pos + vf2d { drawWidth * ( index % 3 ) , 12.f * ( index / 3 ) } ;
std : : string labelText = std : : format ( " {} " , std : : string ( const_cast < IT & > ( name ) ) ) ;
float labelWidth = game - > GetTextSizeProp ( labelText ) . x ;
window . DrawShadowStringDecal ( drawPos , std : : format ( " {:>3} " , amt * qty ) , textCol , BLACK ) ;
window . DrawShadowStringPropDecal ( drawPos + vf2d { 26 , 0 } , labelText , textCol , BLACK , { std : : min ( 1.f , ( drawWidth - 26 - 2 ) / labelWidth ) , 1.f } ) ;
index + + ;
}
Pixel textCol = WHITE ;
uint32_t goldAmt = itemRef . lock ( ) - > GetEnhancementInfo ( ) [ enhancementIndex ] . craftingRequirement . GetCost ( ) ;
if ( goldAmt > 0 ) {
if ( game - > GetPlayer ( ) - > GetMoney ( ) < goldAmt * qty ) textCol = RED ;
vf2d drawPos = rect . pos + vf2d { drawWidth * ( index % 3 ) , 12.f * ( index / 3 ) } ;
std : : string goldAmtText = std : : format ( " {:>3} " , goldAmt * qty ) ;
std : : string labelText = std : : format ( " {} " , " Item.Currency Name " _S ) ;
float goldAmtWidth = game - > GetTextSize ( goldAmtText ) . x ;
window . DrawShadowStringDecal ( drawPos , goldAmtText , textCol , BLACK ) ;
window . DrawShadowStringPropDecal ( drawPos + vf2d { goldAmtWidth + 2 , 0 } , labelText , textCol , BLACK ) ;
}
index + + ;
}
}
}
} ;