This commit is contained in:
Isuru Samarathunga
2025-10-21 21:03:40 +05:30
parent 86ed9346aa
commit 2d5875d211
11 changed files with 239 additions and 165 deletions

View File

@ -19,6 +19,13 @@
namespace ia::iae
{
struct OnScreenGamepadState
{
Vec2 KnobPosition;
Vec2 ThumbstickPosition{};
FLOAT32 ThumbstickRadius{};
} g_onScreenGamepadState{};
EXTERN SDL_Window *g_windowHandle;
BOOL InputManager::s_keys[256];
@ -42,6 +49,9 @@ namespace ia::iae
{
memset(s_keys, 0, sizeof(s_keys));
memset(s_prevKeys, 0, sizeof(s_prevKeys));
g_onScreenGamepadState.KnobPosition = {};
g_onScreenGamepadState.ThumbstickRadius = 64.0f;
}
VOID InputManager::Terminate()
@ -119,7 +129,10 @@ namespace ia::iae
VOID InputManager::SetupOnScreenGamePad()
{
s_onScreenGamePadEnabled = true;
// Initialize
// Disable till manually enabled by calling Input_EnableOnScreenGamePad
s_onScreenGamePadEnabled = false;
}
VOID InputManager::SetupKeyboardGamePad(IN CONST KeyboardGamePadMapping &mapping)
@ -197,16 +210,15 @@ namespace ia::iae
{
if (s_onScreenGamePadEnabled)
{
// Engine::SetRenderState_Texture(0);
// Engine::SetRenderState_FlippedH(false);
// Engine::SetRenderState_FlippedV(false);
// Engine::SetRenderState_ColorOverlay({0xFF, 0xFF, 0xFF, 0xFF});
// Engine::SetRenderState_TextureOffset({0, 0});
// Engine::SetRenderState_CameraRelative(false);
// Engine::SetRenderState_Transform({300.0f, 500.0f},
// {100.0f, 100.0f},
// 0);
// Engine::DrawGeometry(Engine::GetGeometry_Circle(), 0xFF, 0);
g_onScreenGamepadState.ThumbstickPosition =
Engine::CalculatePercentPosition({92.5f, 90.0f}) - Vec2{0, 32.0f};
Engine::SetRenderState_CameraRelative(false);
Engine::SetRenderState_ColorOverlay({0x80, 0x80, 0x80, 0x80});
Engine::DrawCircle(g_onScreenGamepadState.ThumbstickPosition, 0, g_onScreenGamepadState.ThumbstickRadius, 0,
0xFF, 0);
Engine::SetRenderState_ColorOverlay({0x20, 0x20, 0x20, 0xB0});
Engine::DrawCircle(g_onScreenGamepadState.ThumbstickPosition + g_onScreenGamepadState.KnobPosition, 0, 16,
0, 0xFF, 0);
}
}
@ -221,6 +233,30 @@ namespace ia::iae
if (s_onScreenGamePadEnabled)
{
STATIC CONSTEXPR INT16 DIRECTION_MAP_VERTICAL[] = {0, 1, 1, 1, 0, -1, -1, -1};
STATIC CONSTEXPR INT16 DIRECTION_MAP_HORIZONTAL[] = {1, 1, 0, -1, -1, -1, 0, 1};
if (Engine::Input_IsPointerDown(g_onScreenGamepadState.ThumbstickPosition,
g_onScreenGamepadState.ThumbstickRadius))
g_onScreenGamepadState.KnobPosition =
Engine::Input_GetPointerPosition() - g_onScreenGamepadState.ThumbstickPosition;
else
g_onScreenGamepadState.KnobPosition = {};
auto verticalAxis = abs(g_onScreenGamepadState.KnobPosition.y);
auto horizontalAxis = abs(g_onScreenGamepadState.KnobPosition.x);
verticalAxis = (verticalAxis > FLOAT32_EPSILON) ? g_onScreenGamepadState.KnobPosition.y / ((FLOAT32)g_onScreenGamepadState.ThumbstickRadius) : 0;
horizontalAxis = (horizontalAxis > FLOAT32_EPSILON) ? g_onScreenGamepadState.KnobPosition.x / ((FLOAT32)g_onScreenGamepadState.ThumbstickRadius) : 0;
if ((abs(verticalAxis) > FLOAT32_EPSILON) || (abs(horizontalAxis) > FLOAT32_EPSILON))
{
auto angle = glm::degrees(atan2(verticalAxis, horizontalAxis));
if (angle < 0)
angle += 360;
const auto t = INT32((angle + 22.5) / 45) % 8;
s_verticalAxis = DIRECTION_MAP_VERTICAL[t];
s_horizontalAxis = DIRECTION_MAP_HORIZONTAL[t];
}
}
if (s_keyboardGamePadEnabled)
@ -238,15 +274,31 @@ namespace ia::iae
BOOL InputManager::IsPointerDown(IN CONST Vec2 &start, IN CONST Vec2 &end)
{
if(!s_pointerState) return false;
if (!s_pointerState)
return false;
return (s_pointerPosition.x >= start.x) && (s_pointerPosition.x <= end.x) && (s_pointerPosition.y >= start.y) &&
(s_pointerPosition.y <= end.y);
}
BOOL InputManager::IsPointerDown(IN CONST Vec2 &center, IN FLOAT32 radius)
{
if (!s_pointerState)
return false;
const auto dx = s_pointerPosition.x - center.x;
const auto dy = s_pointerPosition.y - center.y;
const auto d = (dx * dx) + (dy * dy);
return d <= (radius * radius);
}
BOOL InputManager::DidPointerClick(IN CONST Vec2 &start, IN CONST Vec2 &end)
{
return IsPointerDown(start, end) && !s_pointerPrevState;
}
BOOL InputManager::DidPointerClick(IN CONST Vec2 &center, IN FLOAT32 radius)
{
return IsPointerDown(center, radius) && !s_pointerPrevState;
}
} // namespace ia::iae
namespace ia::iae
@ -372,4 +424,24 @@ namespace ia::iae
{
InputManager::SetHorizontalAxis(value);
}
BOOL Engine::Input_DidPointerClick(IN CONST Vec2 &center, IN FLOAT32 radius)
{
return InputManager::DidPointerClick(center, radius);
}
BOOL Engine::Input_IsPointerDown(IN CONST Vec2 &center, IN FLOAT32 radius)
{
return InputManager::IsPointerDown(center, radius);
}
VOID Engine::Input_EnableOnScreenGamePad()
{
InputManager::s_onScreenGamePadEnabled = true;
}
VOID Engine::Input_DisableOnScreenGamePad()
{
InputManager::s_onScreenGamePadEnabled = false;
}
} // namespace ia::iae