From 4eec0ce337dada15f369b7d5e10169052bf059bd Mon Sep 17 00:00:00 2001 From: sigonasr2 Date: Sat, 20 Jan 2024 14:42:35 -0600 Subject: [PATCH] Implemented overworld gamepad control functions. Added ability to have certain inputer helper labels to be hidden away. When click functions are called on MenuComponents, it should also apply the menu navigation rules of the button. --- .../AdventuresInLestoria.cpp | 6 +- Adventures in Lestoria/AdventuresInLestoria.h | 1 + Adventures in Lestoria/InputHelper.cpp | 4 +- Adventures in Lestoria/LoadGameWindow.cpp | 124 +++++++++--------- Adventures in Lestoria/Menu.cpp | 2 +- Adventures in Lestoria/MenuComponent.cpp | 12 +- .../OverworldMapLevelWindow.cpp | 25 ++++ .../ScrollableWindowComponent.h | 4 + Adventures in Lestoria/State_OverworldMap.cpp | 11 -- Adventures in Lestoria/Version.h | 2 +- 10 files changed, 112 insertions(+), 79 deletions(-) diff --git a/Adventures in Lestoria/AdventuresInLestoria.cpp b/Adventures in Lestoria/AdventuresInLestoria.cpp index 006ecd79..9315daa6 100644 --- a/Adventures in Lestoria/AdventuresInLestoria.cpp +++ b/Adventures in Lestoria/AdventuresInLestoria.cpp @@ -105,6 +105,7 @@ InputGroup AiL::KEY_START; InputGroup AiL::KEY_SELECT; InputGroup AiL::KEY_SCROLL; +InputGroup AiL::KEY_CHANGE_LOADOUT; #ifndef __EMSCRIPTEN__ ::discord::Core*Discord{}; @@ -2381,7 +2382,7 @@ void AiL::InitializeDefaultKeybinds(){ KEY_DOWN.AddKeybind({CONTROLLER,static_cast(GPButtons::DPAD_D)}); KEY_CONFIRM.AddKeybind({MOUSE,Mouse::LEFT}); KEY_CONFIRM.AddKeybind({CONTROLLER,static_cast(GPButtons::FACE_R)}); - KEY_CONFIRM.AddKeybind({CONTROLLER,static_cast(GPButtons::START)}); + //KEY_CONFIRM.AddKeybind({CONTROLLER,static_cast(GPButtons::START)}); KEY_CONFIRM.AddKeybind({KEY,Z}); KEY_CONFIRM.AddKeybind({KEY,SPACE}); KEY_CONFIRM.AddKeybind({KEY,ENTER}); @@ -2403,6 +2404,9 @@ void AiL::InitializeDefaultKeybinds(){ KEY_SCROLLDOWN.AddKeybind({CONTROLLER,static_cast(GPButtons::R1)}); KEY_SCROLLDOWN.AddKeybind({CONTROLLER,static_cast(GPButtons::R2)}); + KEY_CHANGE_LOADOUT.AddKeybind({KEY,X}); + KEY_CHANGE_LOADOUT.AddKeybind({CONTROLLER,static_cast(GPButtons::FACE_U)}); + KEY_START.AddKeybind({KEY,RETURN}); KEY_START.AddKeybind({CONTROLLER,static_cast(GPButtons::START)}); KEY_SELECT.AddKeybind({KEY,ESCAPE}); diff --git a/Adventures in Lestoria/AdventuresInLestoria.h b/Adventures in Lestoria/AdventuresInLestoria.h index 8c936881..3daef371 100644 --- a/Adventures in Lestoria/AdventuresInLestoria.h +++ b/Adventures in Lestoria/AdventuresInLestoria.h @@ -88,6 +88,7 @@ public: static InputGroup KEY_SELECT; static InputGroup KEY_SCROLL; + static InputGroup KEY_CHANGE_LOADOUT; static float SIZE_CHANGE_SPEED; double levelTime; diff --git a/Adventures in Lestoria/InputHelper.cpp b/Adventures in Lestoria/InputHelper.cpp index 31e818b5..60a1bdcc 100644 --- a/Adventures in Lestoria/InputHelper.cpp +++ b/Adventures in Lestoria/InputHelper.cpp @@ -70,7 +70,7 @@ void InputHelper::Draw(){ #pragma region Populate all buttons to display for(auto&[group,displayName]:inputGroups){ auto&primaryKey=group.GetPrimaryKey(mode); - if(primaryKey.has_value()){ + 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()); @@ -88,7 +88,7 @@ void InputHelper::Draw(){ float buttonDescriptionScaleX=1.0f; if(buttonImgWidth+buttonDescriptionWidth>WINDOW_SIZE.x){ - buttonDescriptionScaleX=(WINDOW_SIZE.x-buttonImgWidth)/buttonDescriptionWidth; + buttonDescriptionScaleX=(WINDOW_SIZE.x-buttonImgWidth)/(buttonDescriptionWidth+buttonImgWidth); } #pragma region Draw all button inputs and descriptions diff --git a/Adventures in Lestoria/LoadGameWindow.cpp b/Adventures in Lestoria/LoadGameWindow.cpp index e9cb8b7c..a0672f1f 100644 --- a/Adventures in Lestoria/LoadGameWindow.cpp +++ b/Adventures in Lestoria/LoadGameWindow.cpp @@ -52,72 +52,72 @@ void Menu::InitializeLoadGameWindow(){ #define ScrollWindow(amount) \ [](MenuType type){ \ auto scrollWindow=Component(type,"Game Files List"); \ - float scrollAmt=amount*game->GetElapsedTime()*"Interface.AnalogScrollSpeed"_F; \ - \ - scrollWindow->SetScrollAmount(scrollWindow->GetTargetScrollAmount()-vf2d{0,amount*game->GetElapsedTime()*"Interface.AnalogScrollSpeed"_F}); \ - \ + scrollWindow->Scroll(amount); \ + float scrollAmt=amount*game->GetElapsedTime()*"Interface.AnalogScrollSpeed"_F;\ /*Height of these buttons is 48.*/ \ scrollWindow->IncreaseSelectionIndex(scrollAmt/48.f); \ } #pragma endregion - loadGameWindow->SetupKeyboardNavigation( - [](MenuType type,Data&returnData){ //On Open - if(SaveFile::GetSaveFileCount()>0){ - returnData=Component(type,"Game Files List")->GetComponents()[0]; - }else{ - returnData="Go Back Button"; + #pragma region Keyboard Navigation Rules + loadGameWindow->SetupKeyboardNavigation( + [](MenuType type,Data&returnData){ //On Open + if(SaveFile::GetSaveFileCount()>0){ + returnData=Component(type,"Game Files List")->GetComponents()[0]; + }else{ + returnData="Go Back Button"; + } + }, + { //Button Key + {{game->KEY_BACK},{"Back to Title Screen",[](MenuType type){ + Component(type,"Go Back Button")->Click(); + }}}, + {{game->KEY_SCROLL,Analog},{"Scroll",ScrollWindow(game->KEY_SCROLL.Analog())}}, + {{game->KEY_SCROLLUP,Held},{"Scroll Up",ScrollWindow(-1.0f)}}, + {{game->KEY_SCROLLDOWN,Held},{"Scroll Down",ScrollWindow(1.0f)}}, } - }, - { //Button Key - {{game->KEY_BACK},{"Back to Title Screen",[](MenuType type){ - Component(type,"Go Back Button")->Click(); - }}}, - {{game->KEY_SCROLL,Analog},{"Scroll",ScrollWindow(game->KEY_SCROLL.Analog())}}, - {{game->KEY_SCROLLUP,Held},{"Scroll Up",ScrollWindow(-1.0f)}}, - {{game->KEY_SCROLLDOWN,Held},{"Scroll Down",ScrollWindow(1.0f)}}, - } - ,{ //Button Navigation Rules - {"Game Files List",{ - .up=[](MenuType type,Data&returnData){ - auto&selection=Menu::menus[type]->GetSelection(); - auto&gameFilesList=Component(type,"Game Files List")->GetComponents(); - auto component=std::find_if(gameFilesList.begin(),gameFilesList.end(),[&](auto&comp){return comp.lock()==selection.lock();}); - if(component==gameFilesList.begin()){ - returnData="Go Back Button"; - }else{ - returnData=*(--component); - } - }, - .down=[](MenuType type,Data&returnData){ - auto&selection=Menu::menus[type]->GetSelection(); - auto&gameFilesList=Component(type,"Game Files List")->GetComponents(); - auto component=std::find_if(gameFilesList.begin(),gameFilesList.end(),[&](auto&comp){return comp.lock()==selection.lock();}); - if(component==gameFilesList.end()-1){ - returnData="Go Back Button"; - }else{ - returnData=*(++component); - } - },}}, - {"Go Back Button",{ - .up=[](MenuType type,Data&returnData){ - auto&selection=Menu::menus[type]->GetSelection(); - auto&gameFilesList=Component(type,"Game Files List")->GetComponents(); - if(gameFilesList.size()==0){ - returnData="Go Back Button"; - return; - } - returnData=*(gameFilesList.end()-1); - }, - .down=[](MenuType type,Data&returnData){ - auto&selection=Menu::menus[type]->GetSelection(); - auto&gameFilesList=Component(type,"Game Files List")->GetComponents(); - if(gameFilesList.size()==0){ - returnData="Go Back Button"; - return; - } - returnData=*(gameFilesList.begin()); - }, - }}, - }); + ,{ //Button Navigation Rules + {"Game Files List",{ + .up=[](MenuType type,Data&returnData){ + auto&selection=Menu::menus[type]->GetSelection(); + auto&gameFilesList=Component(type,"Game Files List")->GetComponents(); + auto component=std::find_if(gameFilesList.begin(),gameFilesList.end(),[&](auto&comp){return comp.lock()==selection.lock();}); + if(component==gameFilesList.begin()){ + returnData="Go Back Button"; + }else{ + returnData=*(--component); + } + }, + .down=[](MenuType type,Data&returnData){ + auto&selection=Menu::menus[type]->GetSelection(); + auto&gameFilesList=Component(type,"Game Files List")->GetComponents(); + auto component=std::find_if(gameFilesList.begin(),gameFilesList.end(),[&](auto&comp){return comp.lock()==selection.lock();}); + if(component==gameFilesList.end()-1){ + returnData="Go Back Button"; + }else{ + returnData=*(++component); + } + },}}, + {"Go Back Button",{ + .up=[](MenuType type,Data&returnData){ + auto&selection=Menu::menus[type]->GetSelection(); + auto&gameFilesList=Component(type,"Game Files List")->GetComponents(); + if(gameFilesList.size()==0){ + returnData="Go Back Button"; + return; + } + returnData=*(gameFilesList.end()-1); + }, + .down=[](MenuType type,Data&returnData){ + auto&selection=Menu::menus[type]->GetSelection(); + auto&gameFilesList=Component(type,"Game Files List")->GetComponents(); + if(gameFilesList.size()==0){ + returnData="Go Back Button"; + return; + } + returnData=*(gameFilesList.begin()); + }, + }}, + }); + #pragma endregion } \ No newline at end of file diff --git a/Adventures in Lestoria/Menu.cpp b/Adventures in Lestoria/Menu.cpp index 19356da8..87b7dba2 100644 --- a/Adventures in Lestoria/Menu.cpp +++ b/Adventures in Lestoria/Menu.cpp @@ -292,7 +292,7 @@ void Menu::OpenMenu(MenuType menu,bool cover){ if(menus[menu]->onOpenFunc){ Data returnData; menus[menu]->onOpenFunc(menu,returnData); - menus[menu]->SetSelection(returnData,true); + if(std::holds_alternative(returnData)&&std::get(returnData).length()>0||std::holds_alternative>(returnData))menus[menu]->SetSelection(returnData,true); } stack.push_back(menus[menu]); } diff --git a/Adventures in Lestoria/MenuComponent.cpp b/Adventures in Lestoria/MenuComponent.cpp index b828931c..ed05c27e 100644 --- a/Adventures in Lestoria/MenuComponent.cpp +++ b/Adventures in Lestoria/MenuComponent.cpp @@ -246,5 +246,15 @@ void MenuComponent::OnPlayerMoneyUpdate(uint32_t newMoney){} void MenuComponent::OnChapterUpdate(uint8_t newChapter){} void MenuComponent::Click(){ - onClick(MenuFuncData{*Menu::menus[parentMenu],game,Menu::menus[parentMenu]->components[name]}); + bool buttonStillValid=onClick(MenuFuncData{*Menu::menus[parentMenu],game,Menu::menus[parentMenu]->components[name]}); + if(buttonStillValid){ + if(menuDest!=MenuType::ENUM_END){ + if(Menu::stack.size()<32){ + Menu::stack.push_back(Menu::menus[menuDest]);//Navigate to the next menu. + }else{ + ERR("WARNING! Exceeded menu stack size limit!") + } + } + } + } \ No newline at end of file diff --git a/Adventures in Lestoria/OverworldMapLevelWindow.cpp b/Adventures in Lestoria/OverworldMapLevelWindow.cpp index e9c8ea3d..ba3f2861 100644 --- a/Adventures in Lestoria/OverworldMapLevelWindow.cpp +++ b/Adventures in Lestoria/OverworldMapLevelWindow.cpp @@ -62,4 +62,29 @@ void Menu::InitializeOverworldMapLevelWindow(){ levelSelectWindow->ADD("Change Loadout Button",MenuComponent)(geom2d::rect{{0,152},{windowSize.x-1,12}},"Change Loadout",ITEM_LOADOUT,[](MenuFuncData data){return true;})END; levelSelectWindow->ADD("Enter Button",MenuComponent)(geom2d::rect{{0,166},{windowSize.x-1,16}},"Enter",[](MenuFuncData data){State_OverworldMap::StartLevel();return true;})END; + + #pragma region Keyboard Navigation Rules + levelSelectWindow->SetupKeyboardNavigation( + [](MenuType type,Data&returnData){ /*On Open*/}, + { //Button Key + {{game->KEY_MENU},{"Game Menu",[](MenuType type){}}}, + {{game->KEY_CONFIRM},{"Start Stage",[](MenuType type){ + Component(type,"Enter Button")->Click(); + }}}, + {{game->KEY_CHANGE_LOADOUT},{"Change Loadout",[](MenuType type){ + Component(type,"Change Loadout Button")->Click(); + }}}, + {{game->KEY_SCROLL,Analog},{"Scroll Encounters",[](MenuType type){ + Component(type,"Spawns List")->Scroll(game->KEY_SCROLL.Analog()); + }}}, + {{game->KEY_SCROLLUP,Held},{"",[](MenuType type){ + Component(type,"Spawns List")->Scroll(1.f); + }}}, + {{game->KEY_SCROLLDOWN,Held},{"",[](MenuType type){ + Component(type,"Spawns List")->Scroll(-1.f); + }}}, + } + ,{ //Button Navigation Rules + }); + #pragma endregion } \ No newline at end of file diff --git a/Adventures in Lestoria/ScrollableWindowComponent.h b/Adventures in Lestoria/ScrollableWindowComponent.h index 720c083f..5390e39f 100644 --- a/Adventures in Lestoria/ScrollableWindowComponent.h +++ b/Adventures in Lestoria/ScrollableWindowComponent.h @@ -92,6 +92,10 @@ public: virtual inline void SetScrollAmount(vf2d scrollOffset){ this->targetScrollOffset=scrollOffset; } + //Provide an amount to scroll by in units/sec. + virtual inline void Scroll(float amt){ + SetScrollAmount(GetTargetScrollAmount()-vf2d{0,amt*game->GetElapsedTime()*"Interface.AnalogScrollSpeed"_F}); + } //Use this when you need to add more scrolling offset to a previous amount as GetScrollAmount() is used to get the internal scroll offset specifically. virtual inline vf2d GetTargetScrollAmount()const{ return targetScrollOffset; diff --git a/Adventures in Lestoria/State_OverworldMap.cpp b/Adventures in Lestoria/State_OverworldMap.cpp index 60865726..1fcad1d8 100644 --- a/Adventures in Lestoria/State_OverworldMap.cpp +++ b/Adventures in Lestoria/State_OverworldMap.cpp @@ -95,17 +95,6 @@ void State_OverworldMap::OnUserUpdate(AiL*game){ Menu::OpenMenu(OVERWORLD_MENU); } - if(game->GetKey(S).bPressed){ - Menu::OpenMenu(MERCHANT); - } - - if(game->GetKey(B).bPressed){ - Menu::OpenMenu(BLACKSMITH); - } - if(game->GetKey(C).bPressed){ - Menu::OpenMenu(CRAFT_CONSUMABLE); - } - #pragma region Audio Test if(game->GetKey(K1).bPressed){ Audio::Play("sfx100v2_loop_water_01.mp3"_SFX); diff --git a/Adventures in Lestoria/Version.h b/Adventures in Lestoria/Version.h index 0d7f9305..386297ab 100644 --- a/Adventures in Lestoria/Version.h +++ b/Adventures in Lestoria/Version.h @@ -39,7 +39,7 @@ All rights reserved. #define VERSION_MAJOR 0 #define VERSION_MINOR 3 #define VERSION_PATCH 0 -#define VERSION_BUILD 6168 +#define VERSION_BUILD 6177 #define stringify(a) stringify_(a) #define stringify_(a) #a