From ef63a07f5c5e5bfb2d61428bebce7171c8eef04c Mon Sep 17 00:00:00 2001 From: Nic0Nic0Nii <89110903+Nic0Nic0Nii@users.noreply.github.com> Date: Mon, 5 Feb 2024 21:30:27 +0000 Subject: [PATCH] Added any keybind detection to PGE and began implementing menu helper innterfaces Co-authored-by: sigonasr2 --- .../AdventuresInLestoria.cpp | 5 ++ Adventures in Lestoria/AdventuresInLestoria.h | 1 + Adventures in Lestoria/GameState.cpp | 4 +- Adventures in Lestoria/GameState.h | 2 + .../InputNewKeybindWindow.cpp | 8 ++- Adventures in Lestoria/Key.cpp | 8 +++ Adventures in Lestoria/Key.h | 3 + Adventures in Lestoria/MonsterAttribute.h | 2 + Adventures in Lestoria/State_Keybind.cpp | 59 +++++++++++++++++++ Adventures in Lestoria/State_Keybind.h | 46 +++++++++++++++ Adventures in Lestoria/State_OverworldMap.cpp | 33 ----------- Adventures in Lestoria/State_OverworldMap.h | 2 - Adventures in Lestoria/olcPixelGameEngine.h | 5 ++ 13 files changed, 140 insertions(+), 38 deletions(-) create mode 100644 Adventures in Lestoria/State_Keybind.cpp create mode 100644 Adventures in Lestoria/State_Keybind.h diff --git a/Adventures in Lestoria/AdventuresInLestoria.cpp b/Adventures in Lestoria/AdventuresInLestoria.cpp index 75c5ba88..18f744d9 100644 --- a/Adventures in Lestoria/AdventuresInLestoria.cpp +++ b/Adventures in Lestoria/AdventuresInLestoria.cpp @@ -3075,4 +3075,9 @@ const std::string_view AiL::GetCurrentMapDisplayName()const{ const uint8_t AiL::BossEncounterMobCount()const{ return totalBossEncounterMobs; +} + + +void AiL::GetAnyKeyPress(Key key){ + GameState::STATE->GetAnyKeyPress(key); } \ No newline at end of file diff --git a/Adventures in Lestoria/AdventuresInLestoria.h b/Adventures in Lestoria/AdventuresInLestoria.h index f2e0e96e..97767c0c 100644 --- a/Adventures in Lestoria/AdventuresInLestoria.h +++ b/Adventures in Lestoria/AdventuresInLestoria.h @@ -154,6 +154,7 @@ public: bool OnUserCreate() override; bool OnUserUpdate(float fElapsedTime) override; bool OnUserDestroy() override; + void GetAnyKeyPress(Key key)override; public: geom2d::rectNO_COLLISION={{0.f,0.f,},{0.f,0.f}}; TileTransformedView view; diff --git a/Adventures in Lestoria/GameState.cpp b/Adventures in Lestoria/GameState.cpp index 8e6d1acc..c503040c 100644 --- a/Adventures in Lestoria/GameState.cpp +++ b/Adventures in Lestoria/GameState.cpp @@ -77,4 +77,6 @@ void GameState::ChangeState(States::State newState,float fadeOutDuration){ } } -GameState::~GameState(){} \ No newline at end of file +GameState::~GameState(){} + +void GameState::GetAnyKeyPress(Key k){} \ No newline at end of file diff --git a/Adventures in Lestoria/GameState.h b/Adventures in Lestoria/GameState.h index 01dd86b6..e6bb62b8 100644 --- a/Adventures in Lestoria/GameState.h +++ b/Adventures in Lestoria/GameState.h @@ -50,6 +50,7 @@ namespace States{ MAIN_MENU, LEVEL_COMPLETE, STORY, + KEYBIND, }; }; @@ -65,5 +66,6 @@ public: virtual void OnStateChange(GameState*prevState)=0; virtual void OnUserUpdate(AiL*game)=0; virtual void Draw(AiL*game)=0; + virtual void GetAnyKeyPress(Key k); static void ChangeState(States::State newState,float fadeOutDuration=0); }; \ No newline at end of file diff --git a/Adventures in Lestoria/InputNewKeybindWindow.cpp b/Adventures in Lestoria/InputNewKeybindWindow.cpp index 5c2d2793..22d4dc84 100644 --- a/Adventures in Lestoria/InputNewKeybindWindow.cpp +++ b/Adventures in Lestoria/InputNewKeybindWindow.cpp @@ -30,7 +30,7 @@ CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 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 +Portions of this software are copyright � 2024 The FreeType Project (www.freetype.org). Please see LICENSE_FT.txt for more information. All rights reserved. */ @@ -39,8 +39,12 @@ All rights reserved. #include "Menu.h" #include "MenuLabel.h" +using A=Attribute; + void Menu::InitializeNewKeybindInputWindow(){ Menu*newKeybindWindow=CreateMenu(NEW_INPUT,CENTERED,vf2d{192,96}); - newKeybindWindow->ADD("New Keybind Label",MenuLabel)(geom2d::rect{{0.f,24.f},{192.f,24.f}},"Press a new key for {}",2.f,ComponentAttr::BACKGROUND|ComponentAttr::OUTLINE|ComponentAttr::SHADOW)END; + newKeybindWindow->B(A::IS_KEYBOARD)=true; + newKeybindWindow->S(A::KEYBIND)=""; + newKeybindWindow->ADD("New Keybind Label",MenuLabel)(geom2d::rect{{0.f,24.f},{192.f,96.f}},"Press a new key for '{}'\n\nPress Esc to cancel.",1.5f,ComponentAttr::BACKGROUND|ComponentAttr::OUTLINE|ComponentAttr::SHADOW)END; } \ No newline at end of file diff --git a/Adventures in Lestoria/Key.cpp b/Adventures in Lestoria/Key.cpp index 2b746ebc..1bca6f30 100644 --- a/Adventures in Lestoria/Key.cpp +++ b/Adventures in Lestoria/Key.cpp @@ -535,4 +535,12 @@ InputGroup&InputEngageGroup::GetGroup()const{ const InputEngageGroup InputEngageGroup::operator=(const InputEngageGroup&rhs){ return InputEngageGroup{rhs.group,rhs.type}; +} + + +void InputGroup::RemovePrimaryKeybind(InputType type){ + std::erase(keyOrder.front()); +} +void InputGroup::AddPrimaryKeybind(InputType type){ + } \ No newline at end of file diff --git a/Adventures in Lestoria/Key.h b/Adventures in Lestoria/Key.h index 20a91714..f6e6e387 100644 --- a/Adventures in Lestoria/Key.h +++ b/Adventures in Lestoria/Key.h @@ -85,6 +85,7 @@ public: class InputGroup{ friend class AiL; friend class Menu; + friend class State_Keybind; std::setkeys; std::vectorkeyOrder; //A list of keys inserted in order, for primary key mapping. static safemapmenuNamesToInputGroups; @@ -92,6 +93,8 @@ public: InputGroup(); void AddKeybind(Input bind); void RemoveKeybind(Input bind); + void RemovePrimaryKeybind(InputType type); + void AddPrimaryKeybind(InputType type); const bool Pressed()const; const bool Held()const; const bool Released()const; diff --git a/Adventures in Lestoria/MonsterAttribute.h b/Adventures in Lestoria/MonsterAttribute.h index 2e018792..d0ec619f 100644 --- a/Adventures in Lestoria/MonsterAttribute.h +++ b/Adventures in Lestoria/MonsterAttribute.h @@ -95,4 +95,6 @@ enum class Attribute{ COLLIDED_WITH_PLAYER, //A boolean flag that is set to true when an enemy makes contact with the player. LAST_BGM_VOLUME, LAST_SFX_VOLUME, + IS_KEYBOARD, + KEYBIND, }; \ No newline at end of file diff --git a/Adventures in Lestoria/State_Keybind.cpp b/Adventures in Lestoria/State_Keybind.cpp new file mode 100644 index 00000000..d49c36d1 --- /dev/null +++ b/Adventures in Lestoria/State_Keybind.cpp @@ -0,0 +1,59 @@ +#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 "State_Keybind.h" +#include "VisualNovel.h" +#include "Menu.h" +#include "Key.h" + +using A=Attribute; + +void State_Keybind::OnStateChange(GameState*prevState){ + Menu::CloseAllMenus(); +}; +void State_Keybind::OnUserUpdate(AiL*game){ + +}; +void State_Keybind::Draw(AiL*game){ + +}; +void State_Keybind::GetAnyKeyPress(Key key){ + Input previousPrimaryKey=InputGroup::menuNamesToInputGroups[Menu::menus[NEW_INPUT].S(A::KEYBIND)]->keyOrder[0]; + InputGroup::menuNamesToInputGroups[Menu::menus[NEW_INPUT].S(A::KEYBIND)]->RemoveKeybind({KEY,previousPrimaryKey}); + + +} \ No newline at end of file diff --git a/Adventures in Lestoria/State_Keybind.h b/Adventures in Lestoria/State_Keybind.h new file mode 100644 index 00000000..768d31c9 --- /dev/null +++ b/Adventures in Lestoria/State_Keybind.h @@ -0,0 +1,46 @@ +#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 +#pragma once +#include "GameState.h" + +class State_Keybind:public GameState{ + virtual void OnStateChange(GameState*prevState)override final; + virtual void OnUserUpdate(AiL*game)override final; + virtual void Draw(AiL*game)override final; + virtual void GetAnyKeyPress(Key key)override final; +}; \ No newline at end of file diff --git a/Adventures in Lestoria/State_OverworldMap.cpp b/Adventures in Lestoria/State_OverworldMap.cpp index 48473e65..f63dff1f 100644 --- a/Adventures in Lestoria/State_OverworldMap.cpp +++ b/Adventures in Lestoria/State_OverworldMap.cpp @@ -97,39 +97,6 @@ void State_OverworldMap::OnUserUpdate(AiL*game){ Menu::OpenMenu(OVERWORLD_MENU); } - #pragma region Audio Test - if(game->GetKey(K1).bPressed){ - Audio::Play("sfx100v2_loop_water_01.mp3"_SFX); - } - if(game->GetKey(F1).bPressed){ - Audio::PlayBGM("foresty1_1"); - lastAudioTime=0.f; - }else - if(game->GetKey(F2).bPressed){ - Audio::PlayBGM("foresty0"); - lastAudioTime=0.f; - } - if(game->GetKey(K2).bPressed){ - Audio::SetAudioEvent("Default Volume"); - lastEventTime=0.f; - } - if(game->GetKey(K3).bPressed){ - Audio::SetAudioEvent("LowHealth"); - lastEventTime=0.f; - } - if(game->GetKey(K4).bPressed){ - Audio::SetAudioEvent("InCombat"); - lastEventTime=0.f; - } - if(game->GetKey(K5).bPressed){ - Audio::SetAudioEvent("Underwater"); - lastEventTime=0.f; - } - - lastEventTime=std::clamp(lastEventTime+game->GetElapsedTime(),0.f,5.0f); - lastAudioTime=std::clamp(lastAudioTime+game->GetElapsedTime(),0.f,5.0f); - #pragma endregion - #pragma region Handle Connection Point Clicking and Movement for(ConnectionPoint&cp:connections){ if(game->GetMouse(Mouse::LEFT).bPressed&&geom2d::overlaps(game->GetWorldMousePos(),cp.rect) diff --git a/Adventures in Lestoria/State_OverworldMap.h b/Adventures in Lestoria/State_OverworldMap.h index dc20371f..ad9a46a0 100644 --- a/Adventures in Lestoria/State_OverworldMap.h +++ b/Adventures in Lestoria/State_OverworldMap.h @@ -46,8 +46,6 @@ class State_OverworldMap:public GameState{ float currentTime=0; vf2d playerTargetPos; const float playerMoveSpd=48.0; - float lastEventTime=0.f; - float lastAudioTime=0.f; public: State_OverworldMap(); static std::vectorconnections; diff --git a/Adventures in Lestoria/olcPixelGameEngine.h b/Adventures in Lestoria/olcPixelGameEngine.h index 81dea74b..34ce16c5 100644 --- a/Adventures in Lestoria/olcPixelGameEngine.h +++ b/Adventures in Lestoria/olcPixelGameEngine.h @@ -994,6 +994,7 @@ namespace olc virtual bool OnUserUpdate(float fElapsedTime); // Called once on application termination, so you can be one clean coder virtual bool OnUserDestroy(); + virtual void GetAnyKeyPress(Key key); // Called when a text entry is confirmed with "enter" key virtual void OnTextEntryComplete(const std::string& sText); @@ -4413,6 +4414,9 @@ namespace olc bool PixelGameEngine::OnUserDestroy() { return true; } + void PixelGameEngine::GetAnyKeyPress(Key key) + { } + void PixelGameEngine::OnTextEntryComplete(const std::string& sText) { UNUSED(sText); } bool PixelGameEngine::OnConsoleCommand(const std::string& sCommand) { UNUSED(sCommand); return false; } @@ -4639,6 +4643,7 @@ namespace olc { pKeys[i].bPressed = !pKeys[i].bHeld; pKeys[i].bHeld = true; + GetAnyKeyPress(i); } else {