Skip to content

Commit

Permalink
Add analog bind support for POSIX SDL
Browse files Browse the repository at this point in the history
  • Loading branch information
TehRealSalt committed Jan 31, 2025
1 parent 5fec4d7 commit 04faec6
Showing 1 changed file with 32 additions and 29 deletions.
61 changes: 32 additions & 29 deletions src/common/platform/posix/sdl/i_joystick.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,6 @@ class SDLInputJoystick: public IJoystickConfig
{
return Axes[axis].DeadZone;
}
EJoyAxis GetAxisMap(int axis)
{
return Axes[axis].GameAxis;
}
const char *GetAxisName(int axis)
{
return Axes[axis].Name.GetChars();
Expand All @@ -107,10 +103,6 @@ class SDLInputJoystick: public IJoystickConfig
{
Axes[axis].DeadZone = clamp(zone, MIN_DEADZONE, 1.f);
}
void SetAxisMap(int axis, EJoyAxis gameaxis)
{
Axes[axis].GameAxis = gameaxis;
}
void SetAxisScale(int axis, float scale)
{
Axes[axis].Multiplier = scale;
Expand All @@ -125,12 +117,6 @@ class SDLInputJoystick: public IJoystickConfig
{
return Axes[axis].DeadZone <= MIN_DEADZONE;
}
bool IsAxisMapDefault(int axis)
{
if(axis >= 5)
return Axes[axis].GameAxis == JOYAXIS_None;
return Axes[axis].GameAxis == DefaultAxes[axis];
}
bool IsAxisScaleDefault(int axis)
{
return Axes[axis].Multiplier == 1.0f;
Expand All @@ -149,10 +135,7 @@ class SDLInputJoystick: public IJoystickConfig
info.Multiplier = 1.0f;
info.Value = 0.0;
info.ButtonValue = 0;
if(i >= 5)
info.GameAxis = JOYAXIS_None;
else
info.GameAxis = DefaultAxes[i];
info.Keys[0] = info.Keys[1] = 0;
Axes.Push(info);
}
}
Expand All @@ -178,13 +161,27 @@ class SDLInputJoystick: public IJoystickConfig
return id;
}

void AddAxes(float axes[NUM_JOYAXIS])
void AddAxes(float axes[NUM_KEYS])
{
// Add to game axes.
for (int i = 0; i < GetNumAxes(); ++i)
{
if(Axes[i].GameAxis != JOYAXIS_None)
axes[Axes[i].GameAxis] -= float(Axes[i].Value * Multiplier * Axes[i].Multiplier);
float axis_value = float(Axes[i].Value * Multiplier * Axes[i].Multiplier);
int axis_key = 0;

if (axis_value > 0.0f)
{
axis_key = Axes[i].Keys[0];
}
else if (axis_value < 0.0f)
{
axis_key = Axes[i].Keys[1];
}

if (axis_key > 0 && axis_key < NUM_KEYS)
{
axes[axis_key] += fabs(axis_value);
}
}
}

Expand All @@ -196,6 +193,16 @@ class SDLInputJoystick: public IJoystickConfig
{
buttonstate = 0;

if (i < NUM_JOYAXISBUTTONS)
{
Axes[i].Keys[0] = KEY_JOYAXIS1PLUS + (i * 2);
Axes[i].Keys[1] = KEY_JOYAXIS1PLUS + (i * 2) + 1;
}
else
{
Axes[i].Keys[0] = Axes[i].Keys[1] = 0;
}

Axes[i].Value = SDL_JoystickGetAxis(Device, i)/32767.0;
Axes[i].Value = Joy_RemoveDeadZone(Axes[i].Value, Axes[i].DeadZone, &buttonstate);

Expand Down Expand Up @@ -255,11 +262,10 @@ class SDLInputJoystick: public IJoystickConfig
FString Name;
float DeadZone;
float Multiplier;
EJoyAxis GameAxis;
int Keys[2];
double Value;
uint8_t ButtonValue;
};
static const EJoyAxis DefaultAxes[5];

int DeviceIndex;
SDL_Joystick *Device;
Expand All @@ -273,9 +279,6 @@ class SDLInputJoystick: public IJoystickConfig
friend class SDLInputJoystickManager;
};

// [Nash 4 Feb 2024] seems like on Linux, the third axis is actually the Left Trigger, resulting in the player uncontrollably looking upwards.
const EJoyAxis SDLInputJoystick::DefaultAxes[5] = {JOYAXIS_Side, JOYAXIS_Forward, JOYAXIS_None, JOYAXIS_Yaw, JOYAXIS_Pitch};

class SDLInputJoystickManager
{
public:
Expand All @@ -296,7 +299,7 @@ class SDLInputJoystickManager
delete Joysticks[i];
}

void AddAxes(float axes[NUM_JOYAXIS])
void AddAxes(float axes[NUM_KEYS])
{
for(unsigned int i = 0;i < Joysticks.Size();i++)
Joysticks[i]->AddAxes(axes);
Expand Down Expand Up @@ -344,9 +347,9 @@ void I_GetJoysticks(TArray<IJoystickConfig *> &sticks)
JoystickManager->GetDevices(sticks);
}

void I_GetAxes(float axes[NUM_JOYAXIS])
void I_GetAxes(float axes[NUM_KEYS])
{
for (int i = 0; i < NUM_JOYAXIS; ++i)
for (int i = 0; i < NUM_KEYS; ++i)
{
axes[i] = 0;
}
Expand Down

0 comments on commit 04faec6

Please sign in to comment.