|
@@ -265,6 +265,43 @@ static void ImGui_ImplGlfw_UpdateMouseCursor()
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+static void ImGui_ImplGlfw_UpdateGamepads()
|
|
|
+{
|
|
|
+ ImGuiIO& io = ImGui::GetIO();
|
|
|
+ memset(io.NavInputs, 0, sizeof(io.NavInputs));
|
|
|
+ if ((io.ConfigFlags & ImGuiConfigFlags_NavEnableGamepad) == 0)
|
|
|
+ return;
|
|
|
+
|
|
|
+ // Update gamepad inputs
|
|
|
+ #define MAP_BUTTON(NAV_NO, BUTTON_NO) { if (buttons_count > BUTTON_NO && buttons[BUTTON_NO] == GLFW_PRESS) io.NavInputs[NAV_NO] = 1.0f; }
|
|
|
+ #define MAP_ANALOG(NAV_NO, AXIS_NO, V0, V1) { float v = (axes_count > AXIS_NO) ? axes[AXIS_NO] : V0; v = (v - V0) / (V1 - V0); if (v > 1.0f) v = 1.0f; if (io.NavInputs[NAV_NO] < v) io.NavInputs[NAV_NO] = v; }
|
|
|
+ int axes_count = 0, buttons_count = 0;
|
|
|
+ const float* axes = glfwGetJoystickAxes(GLFW_JOYSTICK_1, &axes_count);
|
|
|
+ const unsigned char* buttons = glfwGetJoystickButtons(GLFW_JOYSTICK_1, &buttons_count);
|
|
|
+ MAP_BUTTON(ImGuiNavInput_Activate, 0); // Cross / A
|
|
|
+ MAP_BUTTON(ImGuiNavInput_Cancel, 1); // Circle / B
|
|
|
+ MAP_BUTTON(ImGuiNavInput_Menu, 2); // Square / X
|
|
|
+ MAP_BUTTON(ImGuiNavInput_Input, 3); // Triangle / Y
|
|
|
+ MAP_BUTTON(ImGuiNavInput_DpadLeft, 13); // D-Pad Left
|
|
|
+ MAP_BUTTON(ImGuiNavInput_DpadRight, 11); // D-Pad Right
|
|
|
+ MAP_BUTTON(ImGuiNavInput_DpadUp, 10); // D-Pad Up
|
|
|
+ MAP_BUTTON(ImGuiNavInput_DpadDown, 12); // D-Pad Down
|
|
|
+ MAP_BUTTON(ImGuiNavInput_FocusPrev, 4); // L1 / LB
|
|
|
+ MAP_BUTTON(ImGuiNavInput_FocusNext, 5); // R1 / RB
|
|
|
+ MAP_BUTTON(ImGuiNavInput_TweakSlow, 4); // L1 / LB
|
|
|
+ MAP_BUTTON(ImGuiNavInput_TweakFast, 5); // R1 / RB
|
|
|
+ MAP_ANALOG(ImGuiNavInput_LStickLeft, 0, -0.3f, -0.9f);
|
|
|
+ MAP_ANALOG(ImGuiNavInput_LStickRight,0, +0.3f, +0.9f);
|
|
|
+ MAP_ANALOG(ImGuiNavInput_LStickUp, 1, +0.3f, +0.9f);
|
|
|
+ MAP_ANALOG(ImGuiNavInput_LStickDown, 1, -0.3f, -0.9f);
|
|
|
+ #undef MAP_BUTTON
|
|
|
+ #undef MAP_ANALOG
|
|
|
+ if (axes_count > 0 && buttons_count > 0)
|
|
|
+ io.BackendFlags |= ImGuiBackendFlags_HasGamepad;
|
|
|
+ else
|
|
|
+ io.BackendFlags &= ~ImGuiBackendFlags_HasGamepad;
|
|
|
+}
|
|
|
+
|
|
|
void ImGui_ImplGlfw_NewFrame()
|
|
|
{
|
|
|
ImGuiIO& io = ImGui::GetIO();
|
|
@@ -287,36 +324,5 @@ void ImGui_ImplGlfw_NewFrame()
|
|
|
ImGui_ImplGlfw_UpdateMouseCursor();
|
|
|
|
|
|
// Gamepad navigation mapping
|
|
|
- memset(io.NavInputs, 0, sizeof(io.NavInputs));
|
|
|
- if (io.ConfigFlags & ImGuiConfigFlags_NavEnableGamepad)
|
|
|
- {
|
|
|
- // Update gamepad inputs
|
|
|
- #define MAP_BUTTON(NAV_NO, BUTTON_NO) { if (buttons_count > BUTTON_NO && buttons[BUTTON_NO] == GLFW_PRESS) io.NavInputs[NAV_NO] = 1.0f; }
|
|
|
- #define MAP_ANALOG(NAV_NO, AXIS_NO, V0, V1) { float v = (axes_count > AXIS_NO) ? axes[AXIS_NO] : V0; v = (v - V0) / (V1 - V0); if (v > 1.0f) v = 1.0f; if (io.NavInputs[NAV_NO] < v) io.NavInputs[NAV_NO] = v; }
|
|
|
- int axes_count = 0, buttons_count = 0;
|
|
|
- const float* axes = glfwGetJoystickAxes(GLFW_JOYSTICK_1, &axes_count);
|
|
|
- const unsigned char* buttons = glfwGetJoystickButtons(GLFW_JOYSTICK_1, &buttons_count);
|
|
|
- MAP_BUTTON(ImGuiNavInput_Activate, 0); // Cross / A
|
|
|
- MAP_BUTTON(ImGuiNavInput_Cancel, 1); // Circle / B
|
|
|
- MAP_BUTTON(ImGuiNavInput_Menu, 2); // Square / X
|
|
|
- MAP_BUTTON(ImGuiNavInput_Input, 3); // Triangle / Y
|
|
|
- MAP_BUTTON(ImGuiNavInput_DpadLeft, 13); // D-Pad Left
|
|
|
- MAP_BUTTON(ImGuiNavInput_DpadRight, 11); // D-Pad Right
|
|
|
- MAP_BUTTON(ImGuiNavInput_DpadUp, 10); // D-Pad Up
|
|
|
- MAP_BUTTON(ImGuiNavInput_DpadDown, 12); // D-Pad Down
|
|
|
- MAP_BUTTON(ImGuiNavInput_FocusPrev, 4); // L1 / LB
|
|
|
- MAP_BUTTON(ImGuiNavInput_FocusNext, 5); // R1 / RB
|
|
|
- MAP_BUTTON(ImGuiNavInput_TweakSlow, 4); // L1 / LB
|
|
|
- MAP_BUTTON(ImGuiNavInput_TweakFast, 5); // R1 / RB
|
|
|
- MAP_ANALOG(ImGuiNavInput_LStickLeft, 0, -0.3f, -0.9f);
|
|
|
- MAP_ANALOG(ImGuiNavInput_LStickRight,0, +0.3f, +0.9f);
|
|
|
- MAP_ANALOG(ImGuiNavInput_LStickUp, 1, +0.3f, +0.9f);
|
|
|
- MAP_ANALOG(ImGuiNavInput_LStickDown, 1, -0.3f, -0.9f);
|
|
|
- #undef MAP_BUTTON
|
|
|
- #undef MAP_ANALOG
|
|
|
- if (axes_count > 0 && buttons_count > 0)
|
|
|
- io.BackendFlags |= ImGuiBackendFlags_HasGamepad;
|
|
|
- else
|
|
|
- io.BackendFlags &= ~ImGuiBackendFlags_HasGamepad;
|
|
|
- }
|
|
|
+ ImGui_ImplGlfw_UpdateGamepads();
|
|
|
}
|