Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Analog binds #2924

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions src/common/console/c_buttons.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
#include "printf.h"
#include "cmdlib.h"
#include "c_console.h"
#include "m_joy.h"

ButtonMap buttonMap;

Expand Down Expand Up @@ -150,6 +151,23 @@ void ButtonMap::ResetButtonStates ()
//
//=============================================================================

void ButtonMap::GetAxes ()
{
float joyaxes[NUM_KEYS];
I_GetAxes(joyaxes);

for (auto &btn : Buttons)
{
btn.AddAxes(joyaxes);
}
}

//=============================================================================
//
//
//
//=============================================================================

bool FButtonStatus::PressKey (int keynum)
{
int i, open;
Expand Down Expand Up @@ -246,6 +264,37 @@ bool FButtonStatus::ReleaseKey (int keynum)
//
//=============================================================================

void FButtonStatus::AddAxes (float joyaxes[NUM_KEYS])
{
int i, open;

bIsAxis = false;
Axis = 0.0f;

for (i = 0; i < MAX_KEYS; i++)
{
if (Keys[i] == 0)
{
break;
}

float axis_value = joyaxes[ Keys[i] ];
if (axis_value > 0.0f)
{
bIsAxis = true;
Axis += axis_value;
}
}

Axis = clamp<float>(Axis, 0.0f, 1.0f);
}

//=============================================================================
//
//
//
//=============================================================================

void ButtonMap::AddButtonTabCommands()
{
// Add all the action commands for tab completion
Expand Down
26 changes: 25 additions & 1 deletion src/common/console/c_buttons.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <stdint.h>
#include "tarray.h"
#include "name.h"
#include "keydef.h"

// Actions
struct FButtonStatus
Expand All @@ -14,13 +15,18 @@ struct FButtonStatus
bool bWentDown; // Button went down this tic
bool bWentUp; // Button went up this tic
bool bReleaseLock; // Lock ReleaseKey call in ResetButtonStates

bool bIsAxis; // Whenever or not this button is being controlled by any axis.
float Axis; // How far the button has been pressed. Updated by I_GetAxes.

void (*PressHandler)(); // for optional game-side customization
void (*ReleaseHandler)();

bool PressKey (int keynum); // Returns true if this key caused the button to be pressed.
bool ReleaseKey (int keynum); // Returns true if this key is no longer pressed.
void AddAxes (float joyaxes[NUM_KEYS]); // Update joystick axis information.
void ResetTriggers () { bWentDown = bWentUp = false; }
void Reset () { bDown = bWentDown = bWentUp = false; }
void Reset () { bDown = bWentDown = bWentUp = bIsAxis = false; Axis = 0.0f; }
};

class ButtonMap
Expand Down Expand Up @@ -53,6 +59,7 @@ class ButtonMap

void ResetButtonTriggers(); // Call ResetTriggers for all buttons
void ResetButtonStates(); // Same as above, but also clear bDown
void GetAxes(); // Call AddAxes for all buttons
int ListActionCommands(const char* pattern);
void AddButtonTabCommands();

Expand All @@ -62,6 +69,23 @@ class ButtonMap
return Buttons[x].bDown;
}

bool DigitalButtonDown(int x) const
{
// Like ButtonDown, but only for digital buttons. Axes are excluded.
return (Buttons[x].bIsAxis == false && Buttons[x].bDown == true);
}

float AnalogButtonDown(int x) const
{
// Gets the analog value of a button when it is bound to an axis.
if (Buttons[x].bIsAxis == true)
{
return Buttons[x].Axis;
}

return 0.0f;
}

bool ButtonPressed(int x) const
{
return Buttons[x].bWentDown;
Expand Down
18 changes: 0 additions & 18 deletions src/common/engine/m_joy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,18 +162,6 @@ bool M_LoadJoystickConfig(IJoystickConfig *joy)
{
joy->SetAxisScale(i, (float)atof(value));
}

mysnprintf(key + axislen, countof(key) - axislen, "map");
value = GameConfig->GetValueForKey(key);
if (value)
{
EJoyAxis gameaxis = (EJoyAxis)atoi(value);
if (gameaxis < JOYAXIS_None || gameaxis >= NUM_JOYAXIS)
{
gameaxis = JOYAXIS_None;
}
joy->SetAxisMap(i, gameaxis);
}
}
return true;
}
Expand Down Expand Up @@ -227,12 +215,6 @@ void M_SaveJoystickConfig(IJoystickConfig *joy)
mysnprintf(value, countof(value), "%g", joy->GetAxisScale(i));
GameConfig->SetValueForKey(key, value);
}
if (!joy->IsAxisMapDefault(i))
{
mysnprintf(key + axislen, countof(key) - axislen, "map");
mysnprintf(value, countof(value), "%d", joy->GetAxisMap(i));
GameConfig->SetValueForKey(key, value);
}
}
// If the joystick is entirely at its defaults, delete this section
// so that we don't write out a lone section header.
Expand Down
18 changes: 2 additions & 16 deletions src/common/engine/m_joy.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,7 @@
#include "basics.h"
#include "tarray.h"
#include "c_cvars.h"

enum EJoyAxis
{
JOYAXIS_None = -1,
JOYAXIS_Yaw,
JOYAXIS_Pitch,
JOYAXIS_Forward,
JOYAXIS_Side,
JOYAXIS_Up,
// JOYAXIS_Roll, // Ha ha. No roll for you.
NUM_JOYAXIS,
};
#include "keydef.h"

// Generic configuration interface for a controller.
struct IJoystickConfig
Expand All @@ -28,12 +17,10 @@ struct IJoystickConfig

virtual int GetNumAxes() = 0;
virtual float GetAxisDeadZone(int axis) = 0;
virtual EJoyAxis GetAxisMap(int axis) = 0;
virtual const char *GetAxisName(int axis) = 0;
virtual float GetAxisScale(int axis) = 0;

virtual void SetAxisDeadZone(int axis, float zone) = 0;
virtual void SetAxisMap(int axis, EJoyAxis gameaxis) = 0;
virtual void SetAxisScale(int axis, float scale) = 0;

virtual bool GetEnabled() = 0;
Expand All @@ -46,7 +33,6 @@ struct IJoystickConfig
// Used by the saver to not save properties that are at their defaults.
virtual bool IsSensitivityDefault() = 0;
virtual bool IsAxisDeadZoneDefault(int axis) = 0;
virtual bool IsAxisMapDefault(int axis) = 0;
virtual bool IsAxisScaleDefault(int axis) = 0;

virtual void SetDefaultConfig() = 0;
Expand All @@ -64,7 +50,7 @@ int Joy_XYAxesToButtons(double x, double y);
double Joy_RemoveDeadZone(double axisval, double deadzone, uint8_t *buttons);

// These ought to be provided by a system-specific i_input.cpp.
void I_GetAxes(float axes[NUM_JOYAXIS]);
void I_GetAxes(float axes[NUM_KEYS]);
void I_GetJoysticks(TArray<IJoystickConfig *> &sticks);
IJoystickConfig *I_UpdateDeviceList();
extern void UpdateJoystickMenu(IJoystickConfig *);
Expand Down
4 changes: 2 additions & 2 deletions src/common/menu/joystickmenu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,15 +88,15 @@ DEFINE_ACTION_FUNCTION(IJoystickConfig, GetAxisMap)
{
PARAM_SELF_STRUCT_PROLOGUE(IJoystickConfig);
PARAM_INT(axis);
ACTION_RETURN_INT(self->GetAxisMap(axis));
ACTION_RETURN_INT(0);
}

DEFINE_ACTION_FUNCTION(IJoystickConfig, SetAxisMap)
{
PARAM_SELF_STRUCT_PROLOGUE(IJoystickConfig);
PARAM_INT(axis);
PARAM_INT(map);
self->SetAxisMap(axis, (EJoyAxis)map);
//self->SetAxisMap(axis, (EJoyAxis)map);
return 0;
}

Expand Down
Loading