Fix ImGui Gamepad Input (#6055)

This fixes gamepad input for both of our ImGui integrations (which should probably be combined at some point).

Signed-off-by: bosnichd <bosnichd@amazon.com>
monroegm-disable-blank-issue-2
bosnichd 4 years ago committed by GitHub
parent 58be7c27ed
commit b2c13b24ff
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -140,9 +140,18 @@ namespace AZ
m_drawData.push_back(drawData); m_drawData.push_back(drawData);
} }
int ImGuiPass::GetTickOrder()
{
// We have to call ImGui::NewFrame (which happens in ImGuiPass::OnTick) after setting
// ImGui::GetIO().NavInputs (which happens in ImGuiPass::OnInputChannelEventFiltered),
// but before ImGui::Render (which happens in ImGuiPass::SetupFrameGraphDependencies).
return AZ::ComponentTickBus::TICK_PRE_RENDER;
}
void ImGuiPass::OnTick(float deltaTime, [[maybe_unused]] AZ::ScriptTimePoint timePoint) void ImGuiPass::OnTick(float deltaTime, [[maybe_unused]] AZ::ScriptTimePoint timePoint)
{ {
auto imguiContextScope = ImguiContextScope(m_imguiContext); auto imguiContextScope = ImguiContextScope(m_imguiContext);
ImGui::NewFrame();
auto& io = ImGui::GetIO(); auto& io = ImGui::GetIO();
io.DeltaTime = deltaTime; io.DeltaTime = deltaTime;
@ -413,6 +422,7 @@ namespace AZ
void ImGuiPass::Init() void ImGuiPass::Init()
{ {
auto imguiContextScope = ImguiContextScope(m_imguiContext);
auto& io = ImGui::GetIO(); auto& io = ImGui::GetIO();
// ImGui IO Setup // ImGui IO Setup
@ -421,7 +431,6 @@ namespace AZ
{ {
io.KeyMap[static_cast<ImGuiKey_>(i)] = static_cast<int>(i); io.KeyMap[static_cast<ImGuiKey_>(i)] = static_cast<int>(i);
} }
io.NavActive = true;
// Touch input // Touch input
const AzFramework::InputDevice* inputDevice = nullptr; const AzFramework::InputDevice* inputDevice = nullptr;
@ -434,6 +443,17 @@ namespace AZ
io.ConfigFlags |= ImGuiConfigFlags_IsTouchScreen; io.ConfigFlags |= ImGuiConfigFlags_IsTouchScreen;
} }
// Gamepad input
inputDevice = nullptr;
AzFramework::InputDeviceRequestBus::EventResult(inputDevice,
AzFramework::InputDeviceGamepad::IdForIndex0,
&AzFramework::InputDeviceRequests::GetInputDevice);
if (inputDevice && inputDevice->IsSupported())
{
io.BackendFlags |= ImGuiBackendFlags_HasGamepad;
io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad;
}
// Set initial display size to something reasonable (this will be updated in FramePrepare) // Set initial display size to something reasonable (this will be updated in FramePrepare)
io.DisplaySize.x = 1920; io.DisplaySize.x = 1920;
io.DisplaySize.y = 1080; io.DisplaySize.y = 1080;
@ -571,7 +591,6 @@ namespace AZ
auto imguiContextScope = ImguiContextScope(m_imguiContext); auto imguiContextScope = ImguiContextScope(m_imguiContext);
ImGui::GetIO().MouseWheel = m_lastFrameMouseWheel; ImGui::GetIO().MouseWheel = m_lastFrameMouseWheel;
m_lastFrameMouseWheel = 0.0; m_lastFrameMouseWheel = 0.0;
ImGui::NewFrame();
} }
void ImGuiPass::BuildCommandListInternal(const RHI::FrameGraphExecuteContext& context) void ImGuiPass::BuildCommandListInternal(const RHI::FrameGraphExecuteContext& context)

@ -77,6 +77,7 @@ namespace AZ
void RenderImguiDrawData(const ImDrawData& drawData); void RenderImguiDrawData(const ImDrawData& drawData);
// TickBus::Handler overrides... // TickBus::Handler overrides...
int GetTickOrder() override;
void OnTick(float deltaTime, AZ::ScriptTimePoint timePoint) override; void OnTick(float deltaTime, AZ::ScriptTimePoint timePoint) override;
// AzFramework::InputTextEventListener overrides... // AzFramework::InputTextEventListener overrides...

@ -172,7 +172,6 @@ void ImGuiManager::Initialize()
// Broadcast ImGui Ready to Listeners // Broadcast ImGui Ready to Listeners
ImGuiUpdateListenerBus::Broadcast(&IImGuiUpdateListener::OnImGuiInitialize); ImGuiUpdateListenerBus::Broadcast(&IImGuiUpdateListener::OnImGuiInitialize);
m_currentControllerIndex = -1;
m_button1Pressed = m_button2Pressed = false; m_button1Pressed = m_button2Pressed = false;
m_menuBarStatusChanged = false; m_menuBarStatusChanged = false;
@ -227,6 +226,7 @@ void ImGui::ImGuiManager::RestoreRenderWindowSizeToDefault()
void ImGui::ImGuiManager::SetDpiScalingFactor(float dpiScalingFactor) void ImGui::ImGuiManager::SetDpiScalingFactor(float dpiScalingFactor)
{ {
ImGui::ImGuiContextScope contextScope(m_imguiContext);
ImGuiIO& io = ImGui::GetIO(); ImGuiIO& io = ImGui::GetIO();
// Set the global font scale to size our UI to the scaling factor // Set the global font scale to size our UI to the scaling factor
// Note: Currently we use the default, 13px fixed-size IMGUI font, so this can get somewhat blurry // Note: Currently we use the default, 13px fixed-size IMGUI font, so this can get somewhat blurry
@ -235,6 +235,7 @@ void ImGui::ImGuiManager::SetDpiScalingFactor(float dpiScalingFactor)
float ImGui::ImGuiManager::GetDpiScalingFactor() const float ImGui::ImGuiManager::GetDpiScalingFactor() const
{ {
ImGui::ImGuiContextScope contextScope(m_imguiContext);
ImGuiIO& io = ImGui::GetIO(); ImGuiIO& io = ImGui::GetIO();
return io.FontGlobalScale; return io.FontGlobalScale;
} }
@ -406,7 +407,7 @@ bool ImGuiManager::OnInputChannelEventFiltered(const InputChannel& inputChannel)
// Cycle through ImGui Menu Bar States on Home button press // Cycle through ImGui Menu Bar States on Home button press
if (inputChannelId == InputDeviceKeyboard::Key::NavigationHome) if (inputChannelId == InputDeviceKeyboard::Key::NavigationHome)
{ {
ToggleThroughImGuiVisibleState(-1); ToggleThroughImGuiVisibleState();
} }
// Cycle through Standalone Editor Window States // Cycle through Standalone Editor Window States
@ -453,19 +454,10 @@ bool ImGuiManager::OnInputChannelEventFiltered(const InputChannel& inputChannel)
} }
// Handle Controller Inputs // Handle Controller Inputs
int inputControllerIndex = -1;
bool controllerInput = false;
if (InputDeviceGamepad::IsGamepadDevice(inputDeviceId)) if (InputDeviceGamepad::IsGamepadDevice(inputDeviceId))
{ {
inputControllerIndex = inputDeviceId.GetIndex(); // Only pipe in Controller Nav Inputs when at least 1 of the two controller modes are enabled.
controllerInput = true; if (m_controllerModeFlags)
}
if (controllerInput)
{
// Only pipe in Controller Nav Inputs if we are the current Controller Index and at least 1 of the two controller modes are enabled.
if (m_currentControllerIndex == inputControllerIndex && m_controllerModeFlags)
{ {
const auto lyButtonToImGuiNav = s_lyInputToImGuiNavIndexMap.find(inputChannelId); const auto lyButtonToImGuiNav = s_lyInputToImGuiNavIndexMap.find(inputChannelId);
if (lyButtonToImGuiNav != s_lyInputToImGuiNavIndexMap.end()) if (lyButtonToImGuiNav != s_lyInputToImGuiNavIndexMap.end())
@ -476,7 +468,7 @@ bool ImGuiManager::OnInputChannelEventFiltered(const InputChannel& inputChannel)
} }
//Switch menu bar display only if two buttons are pressed at the same time //Switch menu bar display only if two buttons are pressed at the same time
if (inputChannelId == InputDeviceGamepad::Button::L3) if (inputChannelId == InputDeviceGamepad::Button::L1)
{ {
if (inputChannel.IsStateBegan()) if (inputChannel.IsStateBegan())
{ {
@ -488,7 +480,7 @@ bool ImGuiManager::OnInputChannelEventFiltered(const InputChannel& inputChannel)
m_menuBarStatusChanged = false; m_menuBarStatusChanged = false;
} }
} }
if (inputChannelId == InputDeviceGamepad::Button::R3) if (inputChannelId == InputDeviceGamepad::Button::R1)
{ {
if (inputChannel.IsStateBegan()) if (inputChannel.IsStateBegan())
{ {
@ -502,7 +494,7 @@ bool ImGuiManager::OnInputChannelEventFiltered(const InputChannel& inputChannel)
} }
if (!m_menuBarStatusChanged && m_button1Pressed && m_button2Pressed) if (!m_menuBarStatusChanged && m_button1Pressed && m_button2Pressed)
{ {
ToggleThroughImGuiVisibleState(inputControllerIndex); ToggleThroughImGuiVisibleState();
} }
// If we have the Discrete Input Mode Enabled.. and we are in the Visible State, then consume input here // If we have the Discrete Input Mode Enabled.. and we are in the Visible State, then consume input here
@ -627,14 +619,13 @@ bool ImGuiManager::OnInputTextEventFiltered(const AZStd::string& textUTF8)
return io.WantTextInput && m_clientMenuBarState == DisplayState::Visible;; return io.WantTextInput && m_clientMenuBarState == DisplayState::Visible;;
} }
void ImGuiManager::ToggleThroughImGuiVisibleState(int controllerIndex) void ImGuiManager::ToggleThroughImGuiVisibleState()
{ {
ImGui::ImGuiContextScope contextScope(m_imguiContext); ImGui::ImGuiContextScope contextScope(m_imguiContext);
switch (m_clientMenuBarState) switch (m_clientMenuBarState)
{ {
case DisplayState::Hidden: case DisplayState::Hidden:
m_currentControllerIndex = controllerIndex;
m_clientMenuBarState = DisplayState::Visible; m_clientMenuBarState = DisplayState::Visible;
// Draw the ImGui Mouse cursor if either the hardware mouse is connected, or the controller mouse is enabled. // Draw the ImGui Mouse cursor if either the hardware mouse is connected, or the controller mouse is enabled.
@ -669,7 +660,6 @@ void ImGuiManager::ToggleThroughImGuiVisibleState(int controllerIndex)
default: default:
m_clientMenuBarState = DisplayState::Hidden; m_clientMenuBarState = DisplayState::Hidden;
m_currentControllerIndex = -1;
// Enable system cursor if it's in editor and it's not editor game mode // Enable system cursor if it's in editor and it's not editor game mode
if (gEnv->IsEditor() && !gEnv->IsEditorGameMode()) if (gEnv->IsEditor() && !gEnv->IsEditorGameMode())
@ -686,12 +676,6 @@ void ImGuiManager::ToggleThroughImGuiVisibleState(int controllerIndex)
m_setEnabledEvent.Signal(m_clientMenuBarState == DisplayState::Hidden); m_setEnabledEvent.Signal(m_clientMenuBarState == DisplayState::Hidden);
} }
void ImGuiManager::ToggleThroughImGuiVisibleState()
{
ToggleThroughImGuiVisibleState(-1);
}
void ImGuiManager::RenderImGuiBuffers(const ImVec2& scaleRects) void ImGuiManager::RenderImGuiBuffers(const ImVec2& scaleRects)
{ {
ImGui::ImGuiContextScope contextScope(m_imguiContext); ImGui::ImGuiContextScope contextScope(m_imguiContext);

@ -76,9 +76,6 @@ namespace ImGui
// Sets up initial window size and listens for changes // Sets up initial window size and listens for changes
void InitWindowSize(); void InitWindowSize();
// A function to toggle through the available ImGui Visibility States
void ToggleThroughImGuiVisibleState(int controllerIndex);
private: private:
ImGuiContext* m_imguiContext = nullptr; ImGuiContext* m_imguiContext = nullptr;
DisplayState m_clientMenuBarState = DisplayState::Hidden; DisplayState m_clientMenuBarState = DisplayState::Hidden;
@ -96,8 +93,6 @@ namespace ImGui
std::vector<uint16> m_idxBuffer; std::vector<uint16> m_idxBuffer;
//Controller navigation //Controller navigation
static const int MaxControllerNumber = 4;
int m_currentControllerIndex;
bool m_button1Pressed, m_button2Pressed, m_menuBarStatusChanged; bool m_button1Pressed, m_button2Pressed, m_menuBarStatusChanged;
bool m_hardwardeMouseConnected = false; bool m_hardwardeMouseConnected = false;

Loading…
Cancel
Save