Skip to content

Commit f838506

Browse files
committed
InputBinding: add timeout for keyboard binding, to allow binding escape
1 parent d67f41c commit f838506

File tree

1 file changed

+22
-7
lines changed

1 file changed

+22
-7
lines changed

src/input_manager.cpp

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
#define SDL_GAMEPAD_BUTTON_X SDL_GAMEPAD_BUTTON_WEST
2121
#define SDL_GAMEPAD_BUTTON_Y SDL_GAMEPAD_BUTTON_NORTH
2222

23+
constexpr float BindScreenTimeout = 5.f;
24+
2325
struct InputState
2426
{
2527
float currentValue = 0.0f;
@@ -577,6 +579,7 @@ class InputManager
577579
WaitForButtonRelease = 1,
578580
Listening = 2
579581
};
582+
580583
struct BindingDialogState
581584
{
582585
ListenState isListeningForInput = ListenState::False;
@@ -588,6 +591,7 @@ class InputManager
588591
bool currentIsNegate = false;
589592
bool isBindingVolume = false;
590593
bool isBindingKeyboard = false;
594+
float bindScreenDisplayTime = 0.f;
591595
};
592596

593597
BindingDialogState bindDialog;
@@ -679,6 +683,15 @@ class InputManager
679683
// Handle keyboard binding
680684
if (bindDialog.isBindingKeyboard)
681685
{
686+
// Update timeout
687+
bindDialog.bindScreenDisplayTime -= ImGui::GetIO().DeltaTime;
688+
if (bindDialog.bindScreenDisplayTime <= 0.0f)
689+
{
690+
bindDialog.isListeningForInput = ListenState::False;
691+
ImGui::CloseCurrentPopup();
692+
return;
693+
}
694+
682695
// Check all possible keys
683696
int key_count = 0;
684697
const bool* key_state = SDL_GetKeyboardState(&key_count);
@@ -688,11 +701,6 @@ class InputManager
688701
{
689702
SDL_Scancode key = static_cast<SDL_Scancode>(i);
690703

691-
// TODO: escape is used to cancel binding atm, but we also need to allow user to bind escape in-game...
692-
// maybe should use overlay key to cancel instead?
693-
if (key == SDL_SCANCODE_ESCAPE)
694-
continue;
695-
696704
// remove existing keyboard bindings
697705
if (bindDialog.isBindingVolume)
698706
{
@@ -962,6 +970,7 @@ class InputManager
962970
bindDialog.currentVolumeChannel = static_cast<ADChannel>(i);
963971
bindDialog.currentlyBinding = volumeNames[i];
964972
bindDialog.currentIsNegate = volChannel == ADChannel::Steering ? true : false;
973+
bindDialog.bindScreenDisplayTime = BindScreenTimeout;
965974
}
966975

967976
if (volChannel == ADChannel::Steering)
@@ -975,6 +984,7 @@ class InputManager
975984
bindDialog.currentVolumeChannel = static_cast<ADChannel>(i);
976985
bindDialog.currentlyBinding = volumeNames[i];
977986
bindDialog.currentIsNegate = false;
987+
bindDialog.bindScreenDisplayTime = BindScreenTimeout;
978988
}
979989
}
980990
}
@@ -1061,6 +1071,7 @@ class InputManager
10611071
bindDialog.isBindingKeyboard = true;
10621072
bindDialog.currentSwitchId = static_cast<SwitchId>(i);
10631073
bindDialog.currentlyBinding = switchNames[i];
1074+
bindDialog.bindScreenDisplayTime = BindScreenTimeout;
10641075
}
10651076
}
10661077
}
@@ -1108,12 +1119,16 @@ class InputManager
11081119
ImGui::Text("Press %s input to bind to %s",
11091120
bindDialog.isBindingKeyboard ? "keyboard" : "controller",
11101121
bindDialog.currentlyBinding.c_str());
1111-
ImGui::Text("Press ESC to cancel");
1122+
1123+
if(bindDialog.isBindingKeyboard)
1124+
ImGui::Text("Aborting in %.1f seconds", bindDialog.bindScreenDisplayTime);
1125+
else
1126+
ImGui::Text("Press ESC to abort");
11121127

11131128
// Check for binding presses
11141129
HandleNewBinding();
11151130

1116-
if (ImGui::IsKeyPressed(ImGuiKey_Escape))
1131+
if (!bindDialog.isBindingKeyboard && ImGui::IsKeyPressed(ImGuiKey_Escape))
11171132
{
11181133
bindDialog.isListeningForInput = ListenState::False;
11191134
ImGui::CloseCurrentPopup();

0 commit comments

Comments
 (0)