#pragma region License /* License (OLC-3) ~~~~~~~~~~~~~~~ Copyright 2024 Joshua Sigona 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 "InputHelper.h" #include "AdventuresInLestoria.h" #include "Menu.h" #include "MenuComponent.h" #include "GameSettings.h" INCLUDE_game INCLUDE_GFX INCLUDE_WINDOW_SIZE using IconScale=float; InputHelper::InputHelper(){} void InputHelper::Initialize(MenuInputGroups&inputGroups){ groupData.clear(); //HACK ALERT: WTF, somehow we must clear this map because there are references to things that are mixed up where they shouldn't be // ONLY for the emscripten build. Clearing this map ensures that we start with fresh copies to things and it appears that emscripten // likes this...? groupData=inputGroups; } 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==CONTROLLER&&SteamInput())mode=STEAM; if(mode==MOUSE)mode=KEY; //We're going to make it so the keyboard controls always show up when navigating using a mouse. auto OriginalGameIcon=[](std::optionalinput)->bool{ if(input.has_value()){ EInputActionOrigin action=Input::steamGameInputToOrigin.at(Steam::SteamInput(input.value().key)).second[0]; EInputActionOrigin analogAction=Input::steamGameInputToAnalogOrigin.at(Steam::SteamInput(input.value().key)).second[0]; return Input::steamIconToGameIcon.count(action)+Input::steamIconToGameIcon.count(analogAction); } return true; }; switch(mode){ case STEAM: case CONTROLLER: case KEY:{ float buttonImgWidth=0.f; float buttonDescriptionWidth=0.f; std::vector>>>buttonImgs; //Store decals for buttons that actually have images, and strings for buttons that have labels. One button can have multiple icons. Store them all together. std::vectorbuttonDescriptions; #pragma region Populate all buttons to display inputGroups.clear(); groupedInputs.clear(); for(auto&[group,action]:groupData){ if(group.GetLabelVisible()){ //If the label is not visible, we don't care to include it in our list. if(std::holds_alternative(action.first)){ const ButtonName&name=std::get(action.first); groupedInputs[name].push_back(group.GetGroup()); if(groupedInputs[name].size()>1)continue; //Skip adding to the list of input groups because this input already has been added. }else if(std::holds_alternative>(action.first)){ std::weak_ptrparentComponent; if(!Menu::stack.back()->GetSelection().expired()){ parentComponent=Menu::stack.back()->GetSelection().lock()->parentComponent; } std::string name=std::get>(action.first)(MenuFuncData{*Menu::stack.back(),game,Menu::stack.back()->GetSelection(),parentComponent}); groupedInputs[name].push_back(group.GetGroup()); if(groupedInputs[name].size()>1)continue; //Skip adding to the list of input groups because this input already has been added. } this->inputGroups[group.GetGroup()]=action.first; } } for(auto&[group,display]:inputGroups){ std::weak_ptrparentComponent; if(!Menu::stack.back()->GetSelection().expired()){ parentComponent=Menu::stack.back()->GetSelection().lock()->parentComponent; } std::vectorinputGroupsToCheck; std::string displayName; if(std::holds_alternative(display))displayName=std::get(display); else if(std::holds_alternative>(display)){ displayName=std::get>(display)(MenuFuncData{*Menu::stack.back(),game,Menu::stack.back()->GetSelection(),parentComponent}); } else ERR("WARNING! display contains a variant alternative that does not exist. THIS SHOULD NOT BE HAPPENING!"); if(groupedInputs.count(displayName)){ inputGroupsToCheck=groupedInputs.at(displayName); }else{ inputGroupsToCheck.push_back(group); } buttonImgs.push_back({}); std::vector>>&iconList=buttonImgs.back(); for(InputGroup&group:inputGroupsToCheck){ if(Menu::UsingMouseNavigation()){ auto&primaryKey=group.GetPrimaryKey(MOUSE); if(displayName.length()>0&&primaryKey.has_value()){ if(primaryKey.value().HasExtendedIcons()){//This means it follows the specialized icon controller schemes, now pick based on these icons. buttonImgWidth+=primaryKey.value().GetIcon(GameSettings::GetIconType()).Sprite()->width+"Interface.InputHelperSpacing"_I; iconList.push_back({1.f,primaryKey.value().GetIcon(GameSettings::GetIconType()).Decal()}); }else if(primaryKey.value().HasIcon()){ buttonImgWidth+=primaryKey.value().GetIcon().Sprite()->width+"Interface.InputHelperSpacing"_I; iconList.push_back({1.f,primaryKey.value().GetIcon().Decal()}); }else{ buttonImgWidth+=game->GetTextSizeProp(primaryKey.value().GetDisplayName()).x+"Interface.InputHelperSpacing"_I; iconList.push_back({1.f,primaryKey.value().GetDisplayName()}); } } } auto&primaryKey=group.GetPrimaryKey(mode); if(displayName.length()>0&&primaryKey.has_value()){ if(primaryKey.value().HasExtendedIcons()){//This means it follows the specialized icon controller schemes, now pick based on these icons. buttonImgWidth+=primaryKey.value().GetIcon(GameSettings::GetIconType()).Sprite()->width+"Interface.InputHelperSpacing"_I; iconList.push_back({1.f,primaryKey.value().GetIcon(GameSettings::GetIconType()).Decal()}); }else if(primaryKey.value().HasIcon()){ float alteredIconScale=1.f; if(mode==STEAM&&!OriginalGameIcon(primaryKey))alteredIconScale/=2.85f; //They are initially 32x32. buttonImgWidth+=primaryKey.value().GetIcon().Sprite()->width*alteredIconScale+"Interface.InputHelperSpacing"_I; iconList.push_back({alteredIconScale,primaryKey.value().GetIcon().Decal()}); }else{ buttonImgWidth+=game->GetTextSizeProp(primaryKey.value().GetDisplayName()).x+"Interface.InputHelperSpacing"_I; iconList.push_back({1.f,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-"Interface.InputHelperSpacing"_I){ buttonDescriptionScaleX=(WINDOW_SIZE.x-buttonImgWidth-"Interface.InputHelperSpacing"_I)/(buttonDescriptionWidth); } #pragma region Underlying box if(buttonDescriptions.size()>0){ game->GradientFillRectDecal(vf2d{0.f,WINDOW_SIZE.y-16.f},vf2d{float(WINDOW_SIZE.x),16.f}, {0,0,0,64},{0,0,0,255},{0,0,0,255},{0,0,0,64}); } #pragma endregion #pragma region Draw all button inputs and descriptions float xOffset="Interface.InputHelperSpacing"_I; for(size_t index=0;auto&buttonList:buttonImgs){ for(auto&[iconScale,button]:buttonList){ if(std::holds_alternative(button)){ Decal*img=std::get(button); game->DrawDecal(vf2d{xOffset,float(WINDOW_SIZE.y-img->sprite->height*iconScale)-4},img,vf2d{1.f,1.f}*iconScale); xOffset+=img->sprite->width*iconScale+"Interface.InputHelperSpacing"_I; }else if(std::holds_alternative(button)){ std::string label=std::get(button); vf2d textSize=game->GetTextSizeProp(label); game->FillRectDecal(vf2d{xOffset-2,float(WINDOW_SIZE.y-textSize.y-6)},vf2d{textSize.x+4,textSize.y},"Interface.InputButtonBackCol"_Pixel); game->FillRectDecal(vf2d{xOffset-1,float(WINDOW_SIZE.y-textSize.y-6)-1.f},vf2d{textSize.x+2,textSize.y},"Interface.InputButtonBackCol"_Pixel); game->FillRectDecal(vf2d{xOffset-1,float(WINDOW_SIZE.y-textSize.y-6)},vf2d{textSize.x+2,textSize.y+1.f},"Interface.InputButtonBackCol"_Pixel); game->DrawStringPropDecal(vf2d{xOffset,float(WINDOW_SIZE.y-textSize.y-6)},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-6)},description,WHITE,BLACK,vf2d{buttonDescriptionScaleX,1.f}); xOffset+=descriptionTextSize.x+"Interface.InputHelperSpacing"_I*buttonDescriptionScaleX; index++; } #pragma endregion }break; [[unlikely]]default:{ ERR(std::format("Invalid Input Mode detected: {}! THIS SHOULD NOT BE HAPPENING!",int(mode))) } } }