# 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 <EFBFBD> 2023 The FreeType
Project ( www . freetype . org ) . Please see LICENSE_FT . txt for more information .
All rights reserved .
*/
# pragma endregion
# include "InputHelper.h"
# include "AdventuresInLestoria.h"
# include "Menu.h"
INCLUDE_game
INCLUDE_GFX
INCLUDE_WINDOW_SIZE
InputHelper : : InputHelper ( ) { }
void InputHelper : : Initialize ( MenuInputGroups & inputGroups ) {
for ( auto & data : inputGroups ) {
this - > inputGroups [ data . first . GetGroup ( ) ] = data . second . first ;
}
}
const InputType InputHelper : : InputMode ( ) const {
if ( Input : : UsingGamepad ( ) ) return CONTROLLER ;
if ( ! Menu : : stack . back ( ) - > UsingMouseNavigation ( ) ) return KEY ;
return MOUSE ;
}
void InputHelper : : Draw ( ) {
InputType mode = InputMode ( ) ;
if ( mode = = MOUSE ) mode = KEY ; //We're going to make it so the keyboard controls always show up when navigating using a mouse.
switch ( mode ) {
case CONTROLLER :
case KEY : {
float buttonImgWidth = 0.f ;
float buttonDescriptionWidth = 0.f ;
std : : vector < std : : variant < Decal * , std : : string > > buttonImgs ; //Store decals for buttons that actually have images, and strings for buttons that have labels.
std : : vector < std : : string > buttonDescriptions ;
# pragma region Populate all buttons to display
for ( auto & [ group , displayName ] : inputGroups ) {
auto & primaryKey = group . GetPrimaryKey ( mode ) ;
if ( displayName . length ( ) > 0 & & primaryKey . has_value ( ) ) {
if ( primaryKey . value ( ) . HasIcon ( ) ) {
buttonImgWidth + = primaryKey . value ( ) . GetIcon ( ) . Sprite ( ) - > width + " Interface.InputHelperSpacing " _I ;
buttonImgs . push_back ( primaryKey . value ( ) . GetIcon ( ) . Decal ( ) ) ;
} else {
buttonImgs . push_back ( primaryKey . value ( ) . GetDisplayName ( ) ) ;
}
vf2d descriptionSize = game - > GetTextSizeProp ( displayName ) ;
buttonDescriptionWidth + = descriptionSize . x + " Interface.InputHelperSpacing " _I ;
buttonDescriptions . push_back ( displayName ) ;
}
}
# pragma endregion
float buttonDescriptionScaleX = 1.0f ;
if ( buttonImgWidth + buttonDescriptionWidth > WINDOW_SIZE . x ) {
buttonDescriptionScaleX = ( WINDOW_SIZE . x - buttonImgWidth ) / ( buttonDescriptionWidth + buttonImgWidth ) ;
}
# pragma region Draw all button inputs and descriptions
float xOffset = " Interface.InputHelperSpacing " _I ;
for ( size_t index = 0 ; auto & button : buttonImgs ) {
if ( std : : holds_alternative < Decal * > ( button ) ) {
Decal * img = std : : get < Decal * > ( button ) ;
game - > DrawDecal ( vf2d { xOffset , float ( WINDOW_SIZE . y - img - > sprite - > height ) - 8 } , img ) ;
xOffset + = img - > sprite - > width + " Interface.InputHelperSpacing " _I ;
} else
if ( std : : holds_alternative < std : : string > ( button ) ) {
std : : string label = std : : get < std : : string > ( button ) ;
vf2d textSize = game - > GetTextSizeProp ( label ) ;
game - > FillRectDecal ( vf2d { xOffset - 2 , float ( WINDOW_SIZE . y - textSize . y - 10 ) } , vf2d { textSize . x + 4 , textSize . y } , " Interface.InputButtonBackCol " _Pixel ) ;
game - > FillRectDecal ( vf2d { xOffset - 1 , float ( WINDOW_SIZE . y - textSize . y - 10 ) - 1.f } , vf2d { textSize . x + 2 , textSize . y } , " Interface.InputButtonBackCol " _Pixel ) ;
game - > FillRectDecal ( vf2d { xOffset - 1 , float ( WINDOW_SIZE . y - textSize . y - 10 ) } , vf2d { textSize . x + 2 , textSize . y + 1.f } , " Interface.InputButtonBackCol " _Pixel ) ;
game - > DrawStringPropDecal ( vf2d { xOffset , float ( WINDOW_SIZE . y - textSize . y - 10 ) } , label , " Interface.InputButtonTextCol " _Pixel ) ;
xOffset + = textSize . x + " Interface.InputHelperSpacing " _I ;
} else [[unlikely]] ERR ( " WARNING! Hit a state where no data is inside of the button! THIS SHOULD NOT BE HAPPENING! " ) ;
std : : string_view description = buttonDescriptions [ index ] ;
vf2d descriptionTextSize = game - > GetTextSizeProp ( description ) * vf2d { buttonDescriptionScaleX , 1.f } ;
game - > DrawShadowStringPropDecal ( vf2d { xOffset , float ( WINDOW_SIZE . y - descriptionTextSize . y - 10 ) } , description , WHITE , BLACK , vf2d { buttonDescriptionScaleX , 1.f } ) ;
xOffset + = descriptionTextSize . x + " Interface.InputHelperSpacing " _I ;
index + + ;
}
# pragma endregion
} break ;
[[unlikely]] default : {
ERR ( std : : format ( " Invalid Input Mode detected: {}! THIS SHOULD NOT BE HAPPENING! " , int ( mode ) ) )
}
}
}