The open source repository for the action RPG game in development by Sig Productions titled 'Adventures in Lestoria'! https://forums.lestoria.net
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 
AdventuresInLestoria/Adventures in Lestoria/InputHelper.cpp

208 lines
9.9 KiB

#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 "InputHelper.h"
#include "AdventuresInLestoria.h"
#include "Menu.h"
#include "MenuComponent.h"
#include "GameSettings.h"
INCLUDE_game
INCLUDE_GFX
INCLUDE_WINDOW_SIZE
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==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::vector<std::variant<Decal*,std::string>>>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::vector<std::string>buttonDescriptions;
#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<ButtonName>(action.first)){
const ButtonName&name=std::get<ButtonName>(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<std::function<std::string(MenuFuncData)>>(action.first)){
std::weak_ptr<ScrollableWindowComponent>parentComponent;
if(!Menu::stack.back()->GetSelection().expired()){
parentComponent=Menu::stack.back()->GetSelection().lock()->parentComponent;
}
std::string name=std::get<std::function<std::string(MenuFuncData)>>(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_ptr<ScrollableWindowComponent>parentComponent;
if(!Menu::stack.back()->GetSelection().expired()){
parentComponent=Menu::stack.back()->GetSelection().lock()->parentComponent;
}
std::vector<InputGroup>inputGroupsToCheck;
std::string displayName;
if(std::holds_alternative<std::string>(display))displayName=std::get<std::string>(display);
else
if(std::holds_alternative<std::function<std::string(MenuFuncData)>>(display)){
displayName=std::get<std::function<std::string(MenuFuncData)>>(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<std::variant<Decal*,std::string>>&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(primaryKey.value().GetIcon(GameSettings::GetIconType()).Decal());
}else
if(primaryKey.value().HasIcon()){
buttonImgWidth+=primaryKey.value().GetIcon().Sprite()->width+"Interface.InputHelperSpacing"_I;
iconList.push_back(primaryKey.value().GetIcon().Decal());
}else{
buttonImgWidth+=game->GetTextSizeProp(primaryKey.value().GetDisplayName()).x+"Interface.InputHelperSpacing"_I;
iconList.push_back(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(primaryKey.value().GetIcon(GameSettings::GetIconType()).Decal());
}else
if(primaryKey.value().HasIcon()){
buttonImgWidth+=primaryKey.value().GetIcon().Sprite()->width+"Interface.InputHelperSpacing"_I;
iconList.push_back(primaryKey.value().GetIcon().Decal());
}else{
buttonImgWidth+=game->GetTextSizeProp(primaryKey.value().GetDisplayName()).x+"Interface.InputHelperSpacing"_I;
iconList.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-"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(std::variant<Decal*,std::string>button:buttonList){
if(std::holds_alternative<Decal*>(button)){
Decal*img=std::get<Decal*>(button);
game->DrawDecal(vf2d{xOffset,float(WINDOW_SIZE.y-img->sprite->height)-4},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-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)))
}
}
}