# 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
# include "Menu.h"
# include "EnhancementStatsLabel.h"
# include "RequiredMaterialsList.h"
# include "SoundEffect.h"
# include "Tutorial.h"
void Menu : : InitializeCraftItemWindow ( ) {
Menu * craftItemWindow = CreateMenu ( CRAFT_ITEM , CENTERED , { 240 , 120 } ) ;
craftItemWindow - > ADD ( " Item Name Header " , MenuLabel ) ( geom2d : : rect < float > { { 2 , - 16 } , { craftItemWindow - > size . x - 4 , 12 } } , " Item Name " , 1.0f , ComponentAttr : : SHADOW | ComponentAttr : : OUTLINE | ComponentAttr : : BACKGROUND ) END ;
craftItemWindow - > ADD ( " Enhancement Level Header " , MenuLabel ) ( geom2d : : rect < float > { { 2 , 0 } , { craftItemWindow - > size . x - 4 , 12 } } , " Level X ->#00AA00 Y " , 1.f , ComponentAttr : : SHADOW | ComponentAttr : : FIXED_WIDTH_FONT ) END ;
craftItemWindow - > ADD ( " Enhancement Stats Label " , EnhancementStatsLabel ) ( geom2d : : rect < float > { { 2 , 16 } , { craftItemWindow - > size . x - 4 , 72 } } , Item : : BLANK , 1.f ) END ;
craftItemWindow - > ADD ( " Materials Requirement Outline " , MenuComponent ) ( geom2d : : rect < float > { { 2 , 86 } , { craftItemWindow - > size . x - 4 , 26 } } , " " , DO_NOTHING , ButtonAttr : : UNSELECTABLE ) END ;
craftItemWindow - > ADD ( " Required Materials Label " , MenuLabel ) ( geom2d : : rect < float > { { 4 , 80 } , { craftItemWindow - > size . x - 8 , 0 } } , " Required Materials " , 1.0f , ComponentAttr : : SHADOW ) END ;
craftItemWindow - > ADD ( " Required Materials List " , RequiredMaterialsList ) ( geom2d : : rect < float > { { 4 , 88 } , { craftItemWindow - > size . x - 8 , 22 } } , Item : : BLANK ) END ;
craftItemWindow - > ADD ( " Back Button " , MenuComponent ) ( geom2d : : rect < float > { { 36 , 116 } , { 48 , 12 } } , " Back " , [ ] ( MenuFuncData data ) { Menu : : CloseMenu ( ) ; return true ; } ) END ;
craftItemWindow - > ADD ( " Craft Button " , MenuComponent ) ( geom2d : : rect < float > { { craftItemWindow - > size . x - 84 , 116 } , { 48 , 12 } } , " Craft " , [ ] ( MenuFuncData data ) {
std : : weak_ptr < Item > item = Component < EnhancementStatsLabel > ( data . menu . GetType ( ) , " Enhancement Stats Label " ) - > GetItem ( ) ;
if ( item . lock ( ) - > CanEnhanceItem ( ) ) {
if ( Inventory : : GetItemCount ( item . lock ( ) - > ActualName ( ) ) = = 0 ) { //We don't own this item, so we need to give the player this item and refresh the blacksmith's inventory.
item = Inventory : : AddItem ( item . lock ( ) - > ActualName ( ) ) ;
Component < EnhancementStatsLabel > ( CRAFT_ITEM , " Enhancement Stats Label " ) - > SetItem ( item ) ;
Component < RequiredMaterialsList > ( CRAFT_ITEM , " Required Materials List " ) - > SetItem ( item ) ; //Update the item refs in the enhancement level screen to now display the correct item.
const CraftingRequirement & consumedResources = item . lock ( ) - > GetEnhancementInfo ( ) [ item . lock ( ) - > EnhancementLevel ( ) ] . craftingRequirement ;
for ( const auto & [ name , amt ] : consumedResources . GetItems ( ) ) {
Inventory : : RemoveItem ( Inventory : : GetItem ( name ) [ 0 ] , amt ) ;
}
game - > GetPlayer ( ) - > SetMoney ( game - > GetPlayer ( ) - > GetMoney ( ) - consumedResources . GetCost ( ) ) ;
Inventory : : UpdateBlacksmithInventoryLists ( ) ;
SoundEffect : : PlaySFX ( " Craft Equip " , SoundEffect : : CENTERED ) ;
} else { //Since we already own this equipment, just enhance it.
item . lock ( ) - > EnhanceItem ( ) ;
}
if ( ! Tutorial : : TaskIsComplete ( TutorialTaskName : : EQUIP_GEAR ) ) {
Tutorial : : SetNextTask ( TutorialTaskName : : EQUIP_GEAR ) ;
}
}
std : : string label = " " ;
if ( item . lock ( ) - > EnhancementIsPossible ( ) & & item . lock ( ) - > GetEnhancementInfo ( ) . size ( ) > item . lock ( ) - > EnhancementLevel ( ) + 1
& & item . lock ( ) - > GetEnhancementInfo ( ) [ item . lock ( ) - > EnhancementLevel ( ) + 1 ] . chapterAvailable < = game - > GetCurrentChapter ( ) ) {
label = std : : format ( " Level {} ->#00AA00 {} " , item . lock ( ) - > EnhancementLevel ( ) , item . lock ( ) - > EnhancementLevel ( ) + 1 ) ;
}
Component < MenuLabel > ( CRAFT_ITEM , " Item Name Header " ) - > SetLabel ( std : : format ( " Enhancing {} " , item . lock ( ) - > DisplayName ( ) ) ) ;
Component < MenuLabel > ( data . menu . GetType ( ) , " Enhancement Level Header " ) - > SetLabel ( label ) ;
data . component . lock ( ) - > SetGrayedOut ( ! item . lock ( ) - > CanEnhanceItem ( ) ) ;
return true ;
} ) END ;
craftItemWindow - > SetupKeyboardNavigation (
[ ] ( MenuType type , Data & returnData ) { //On Open
if ( ! Component < MenuComponent > ( type , " Craft Button " ) - > IsGreyedOut ( ) ) {
returnData = " Craft Button " ;
} else {
returnData = " Back Button " ;
}
} ,
{ //Button Key
{ game - > KEY_BACK , { " Back " , [ ] ( MenuType type ) {
Menu : : CloseMenu ( ) ;
} } } ,
{ game - > KEY_CONFIRM , { " Select " , [ ] ( MenuType type ) { } } } ,
}
, { //Button Navigation Rules
{ " Back Button " , {
. left = " Craft Button " ,
. right = " Craft Button " , } } ,
{ " Craft Button " , {
. left = " Back Button " ,
. right = " Back Button " , } } ,
} ) ;
}