From 5f98d83fdc20d7ba8e600f92236bc7c79af66d4b Mon Sep 17 00:00:00 2001 From: tomaabe <19969980+tomaabe@users.noreply.github.com> Date: Mon, 8 Jun 2026 16:58:23 +0900 Subject: [PATCH] Prevent restarting haptic playback from gamepad/keyboard while a clip plays The Play WASAPI (L1) and Play XAudio2 (R1) buttons were wrapped in ImGui::BeginDisabled while a clip was playing, but that only suppresses mouse interaction. The gamepad shortcut ("|| IsButtonPressed(...)") bypassed the disabled state entirely, and ImGui still activates a keyboard Space/Enter on a button that already had focus. As a result a clip could be restarted from the start, or the other engine started, mid-playback via the controller or the keyboard. Gate both play actions on !IsPlaying() for every input path (mouse, keyboard and gamepad) so playback cannot be retriggered while active, and clear the window focus on play so the now-disabled button stops showing a pressed animation when Space is held. --- Samples/System/Haptics/haptics.cpp | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/Samples/System/Haptics/haptics.cpp b/Samples/System/Haptics/haptics.cpp index a9fe6275..20535a75 100644 --- a/Samples/System/Haptics/haptics.cpp +++ b/Samples/System/Haptics/haptics.cpp @@ -202,16 +202,31 @@ void Sample_Draw(float uiScale) ImGui::Dummy(ImVec2(0, 20)); - ImGui::BeginDisabled(hapticsDevice->IsPlaying()); - if(ImGui::Button("Play WASAPI (L1)", ImVec2(160*uiScale, 50*uiScale)) || IsButtonPressed(state.buttons, context.lastGamepadState.buttons, GameInputGamepadButtons::GameInputGamepadLeftShoulder)) + const bool isPlaying = hapticsDevice->IsPlaying(); + + ImGui::BeginDisabled(isPlaying); + // Always draw the buttons (ImGui requires the call every frame), but gate the + // resulting action on !isPlaying. ImGui::BeginDisabled only blocks new mouse/Tab + // focus; it does NOT block keyboard Space/Enter on an item that already had focus, + // and it does not affect the "|| IsButtonPressed(...)" gamepad shortcut at all. + // Gating the action closes both gaps so a clip cannot be restarted (or the other + // engine started) mid-play, and SetWindowFocus(nullptr) drops focus so the + // disabled button does not keep showing a pressed animation when Space is held. + const bool wasapiPressed = ImGui::Button("Play WASAPI (L1)", ImVec2(160*uiScale, 50*uiScale)) + || IsButtonPressed(state.buttons, context.lastGamepadState.buttons, GameInputGamepadButtons::GameInputGamepadLeftShoulder); + if(!isPlaying && wasapiPressed) { hapticsDevice->PlayWAVFile(context.mediaItem.filename.c_str(), HapticPlaybackEngine::WASAPI); + ImGui::SetWindowFocus(nullptr); } ImGui::SameLine(); - if(ImGui::Button("Play XAudio2 (R1)", ImVec2(160*uiScale, 50*uiScale)) || IsButtonPressed(state.buttons, context.lastGamepadState.buttons, GameInputGamepadButtons::GameInputGamepadRightShoulder)) + const bool xaudioPressed = ImGui::Button("Play XAudio2 (R1)", ImVec2(160*uiScale, 50*uiScale)) + || IsButtonPressed(state.buttons, context.lastGamepadState.buttons, GameInputGamepadButtons::GameInputGamepadRightShoulder); + if(!isPlaying && xaudioPressed) { hapticsDevice->PlayWAVFile(context.mediaItem.filename.c_str(), HapticPlaybackEngine::XAudio2); + ImGui::SetWindowFocus(nullptr); } ImGui::EndDisabled();