From cbc4fa5470c9ae4315778ae66de7542d586dc7c4 Mon Sep 17 00:00:00 2001 From: Nic0Nic0Nii <89110903+Nic0Nic0Nii@users.noreply.github.com> Date: Fri, 9 Feb 2024 18:03:23 +0000 Subject: [PATCH] Add DAs input functions Co-authored-by: sigonasr2 --- Adventures in Lestoria/Key.cpp | 62 +++++++++++++++++++ Adventures in Lestoria/Key.h | 7 +++ Adventures in Lestoria/Menu.cpp | 8 +-- .../assets/config/Interface.txt | 7 ++- emscripten_build.sh | 2 +- emscripten_debug_build.sh | 2 +- 6 files changed, 80 insertions(+), 8 deletions(-) diff --git a/Adventures in Lestoria/Key.cpp b/Adventures in Lestoria/Key.cpp index b4cfbfe8..7b222714 100644 --- a/Adventures in Lestoria/Key.cpp +++ b/Adventures in Lestoria/Key.cpp @@ -79,6 +79,60 @@ bool Input::Pressed(){ return false; } +bool Input::PressedDAS(){ + if(!game->IsFocused())return false; + bool inputPressed=false; + + auto HandleDAS=[&](const HWButton&button){ + if(button.bPressed){ + if(initialHoldDownTime==0.f){ + initialHoldDownTime="Interface.InitialScrollDelay"_F; + } + return true; + }else + if(button.bHeld&&initialHoldDownTime>0.f){ + initialHoldDownTime-=game->GetElapsedTime(); + if(initialHoldDownTime<=0.f){ + holdDownTime="Interface.ScrollDelay"_F; + return true; + } + }else + if(button.bHeld&&holdDownTime>0.f){ + holdDownTime-=game->GetElapsedTime(); + if(holdDownTime<=0.f){ + holdDownTime="Interface.ScrollDelay"_F; + return true; + } + } + return false; + }; + + switch(type){ + case KEY:{ + if(HandleDAS(game->GetKey(Key(key))))inputPressed=true; + }break; + case MOUSE:{ + if(HandleDAS(game->GetMouse(key)))inputPressed=true; + }break; + case CONTROLLER:{ + for(GamePad*gamepad:GamePad::getGamepads()){ + if(gamepad->stillConnected)if(HandleDAS(gamepad->getButton(static_cast(key))))inputPressed=true; + } + }break; + case ANALOG:{ + //An analog input can never be "pressed". No-op. + }break; + default:{ + ERR("Invalid Control Scheme detected! We shouldn't be here!! Type is "<IsFocused())return false; bool inputHeld=false; @@ -132,6 +186,7 @@ bool Input::Released(){ } if(inputReleased){ usingGamepad=type==CONTROLLER; + initialHoldDownTime=holdDownTime=0.f; //Reset hold times if we release the key. return true; } return false; @@ -209,6 +264,13 @@ const bool InputGroup::Pressed()const{ return false; } +const bool InputGroup::PressedDAS()const{ + for(Input input:keys){ + if(input.PressedDAS())return true; + } + return false; +} + const bool InputGroup::Held()const{ for(Input input:keys){ if(input.Held())return true; diff --git a/Adventures in Lestoria/Key.h b/Adventures in Lestoria/Key.h index fcdfea26..13e52004 100644 --- a/Adventures in Lestoria/Key.h +++ b/Adventures in Lestoria/Key.h @@ -61,9 +61,14 @@ class Input{ int key; //This will be interpreted differently depending on input type. static bool usingGamepad; static void SetUsingGamepad(const bool usingGamepad); + + float holdDownTime=0.f; + float initialHoldDownTime=0.f; public: Input(InputType type,int key); bool Pressed(); + //A version of Pressed() that will continuously return true after holding down for some time. Useful for scrolling through menus. + bool PressedDAS(); bool Held(); bool Released(); float Analog(); @@ -100,6 +105,8 @@ public: void RemoveKeybind(Input bind); void SetNewPrimaryKeybind(Input key); const bool Pressed()const; + //A version of Pressed() that will continuously return true after holding down for some time. Useful for scrolling through menus. + const bool PressedDAS()const; const bool Held()const; const bool Released()const; const float Analog()const; diff --git a/Adventures in Lestoria/Menu.cpp b/Adventures in Lestoria/Menu.cpp index 9233d217..681283e1 100644 --- a/Adventures in Lestoria/Menu.cpp +++ b/Adventures in Lestoria/Menu.cpp @@ -342,7 +342,7 @@ void Menu::KeyboardButtonNavigation(AiL*game,vf2d menuPos){ if(navigationGroups.count(selectionButtonName)){ Navigation nav=navigationGroups[selectionButtonName]; - if(game->KEY_UP.Pressed()){ + if(game->KEY_UP.PressedDAS()){ SetMouseNavigation(false); if(std::holds_alternative(nav.up)&&std::get(nav.up).length()>0)SetSelection(std::string_view(std::get(nav.up))); else @@ -352,7 +352,7 @@ void Menu::KeyboardButtonNavigation(AiL*game,vf2d menuPos){ SetSelection(returnData); } } - if(game->KEY_DOWN.Pressed()){ + if(game->KEY_DOWN.PressedDAS()){ SetMouseNavigation(false); if(std::holds_alternative(nav.down)&&std::get(nav.down).length()>0)SetSelection(std::string_view(std::get(nav.down))); else @@ -362,7 +362,7 @@ void Menu::KeyboardButtonNavigation(AiL*game,vf2d menuPos){ SetSelection(returnData); } } - if(game->KEY_LEFT.Pressed()){ + if(game->KEY_LEFT.PressedDAS()){ SetMouseNavigation(false); if(std::holds_alternative(nav.left)&&std::get(nav.left).length()>0)SetSelection(std::string_view(std::get(nav.left))); else @@ -372,7 +372,7 @@ void Menu::KeyboardButtonNavigation(AiL*game,vf2d menuPos){ SetSelection(returnData); } } - if(game->KEY_RIGHT.Pressed()){ + if(game->KEY_RIGHT.PressedDAS()){ SetMouseNavigation(false); if(std::holds_alternative(nav.right)&&std::get(nav.right).length()>0)SetSelection(std::string_view(std::get(nav.right))); else diff --git a/Adventures in Lestoria/assets/config/Interface.txt b/Adventures in Lestoria/assets/config/Interface.txt index 2cc4c511..df4b8aa8 100644 --- a/Adventures in Lestoria/assets/config/Interface.txt +++ b/Adventures in Lestoria/assets/config/Interface.txt @@ -1,7 +1,10 @@ Interface { - # In pixels/sec - AnalogScrollSpeed = 220 + # How long to wait for the first initial delay auto shift to occur. + InitialScrollDelay = 0.4s + + # How long to wait for subsequent scrolling through menu items. + ScrollDelay = 0.2s # The size of each side of the 9-patch menu sprite. 9PatchSize = 24,24 diff --git a/emscripten_build.sh b/emscripten_build.sh index 7aab7278..a1634dc0 100755 --- a/emscripten_build.sh +++ b/emscripten_build.sh @@ -1,4 +1,4 @@ clear source ./emsdk/emsdk_env.sh emcmake cmake -DCMAKE_BUILD_TYPE=Release . -cmake --build . -j 8 \ No newline at end of file +cmake --build . -j 20 \ No newline at end of file diff --git a/emscripten_debug_build.sh b/emscripten_debug_build.sh index 4a0a7900..96fedf06 100755 --- a/emscripten_debug_build.sh +++ b/emscripten_debug_build.sh @@ -1,4 +1,4 @@ clear source ./emsdk/emsdk_env.sh emcmake cmake -DCMAKE_BUILD_TYPE=Debug -D_DEBUG=1 . -cmake --build . -j 8 \ No newline at end of file +cmake --build . -j 20 \ No newline at end of file