Gamepad: WIP towards customising axis behaviour and sensitivity

This commit is contained in:
UnknownShadow200 2024-04-02 22:18:13 +11:00
parent d056f8c2ff
commit cfa3676e44
3 changed files with 86 additions and 83 deletions

View File

@ -6,74 +6,74 @@ Copyright 2014-2023 ClassiCube | Licensed under BSD-3
*/ */
#if _MSC_VER #if _MSC_VER
typedef signed __int8 cc_int8; typedef signed __int8 cc_int8;
typedef signed __int16 cc_int16; typedef signed __int16 cc_int16;
typedef signed __int32 cc_int32; typedef signed __int32 cc_int32;
typedef signed __int64 cc_int64; typedef signed __int64 cc_int64;
typedef unsigned __int8 cc_uint8; typedef unsigned __int8 cc_uint8;
typedef unsigned __int16 cc_uint16; typedef unsigned __int16 cc_uint16;
typedef unsigned __int32 cc_uint32; typedef unsigned __int32 cc_uint32;
typedef unsigned __int64 cc_uint64; typedef unsigned __int64 cc_uint64;
#ifdef _WIN64 #ifdef _WIN64
typedef unsigned __int64 cc_uintptr; typedef unsigned __int64 cc_uintptr;
#else #else
typedef unsigned __int32 cc_uintptr; typedef unsigned __int32 cc_uintptr;
#endif #endif
#define CC_INLINE inline #define CC_INLINE inline
#define CC_NOINLINE __declspec(noinline) #define CC_NOINLINE __declspec(noinline)
#ifndef CC_API #ifndef CC_API
#define CC_API __declspec(dllexport, noinline) #define CC_API __declspec(dllexport, noinline)
#define CC_VAR __declspec(dllexport) #define CC_VAR __declspec(dllexport)
#endif #endif
#define CC_HAS_TYPES #define CC_HAS_TYPES
#define CC_HAS_MISC #define CC_HAS_MISC
#elif __GNUC__ #elif __GNUC__
/* really old GCC/clang might not have these defined */ /* really old GCC/clang might not have these defined */
#ifdef __INT8_TYPE__ #ifdef __INT8_TYPE__
/* avoid including <stdint.h> because it breaks defining UNICODE in Platform.c with MinGW */ /* avoid including <stdint.h> because it breaks defining UNICODE in Platform.c with MinGW */
typedef __INT8_TYPE__ cc_int8; typedef __INT8_TYPE__ cc_int8;
typedef __INT16_TYPE__ cc_int16; typedef __INT16_TYPE__ cc_int16;
typedef __INT32_TYPE__ cc_int32; typedef __INT32_TYPE__ cc_int32;
typedef __INT64_TYPE__ cc_int64; typedef __INT64_TYPE__ cc_int64;
#ifdef __UINT8_TYPE__ #ifdef __UINT8_TYPE__
typedef __UINT8_TYPE__ cc_uint8; typedef __UINT8_TYPE__ cc_uint8;
typedef __UINT16_TYPE__ cc_uint16; typedef __UINT16_TYPE__ cc_uint16;
typedef __UINT32_TYPE__ cc_uint32; typedef __UINT32_TYPE__ cc_uint32;
typedef __UINT64_TYPE__ cc_uint64; typedef __UINT64_TYPE__ cc_uint64;
typedef __UINTPTR_TYPE__ cc_uintptr; typedef __UINTPTR_TYPE__ cc_uintptr;
#else #else
/* clang doesn't define the __UINT8_TYPE__ */ /* clang doesn't define the __UINT8_TYPE__ */
typedef unsigned __INT8_TYPE__ cc_uint8; typedef unsigned __INT8_TYPE__ cc_uint8;
typedef unsigned __INT16_TYPE__ cc_uint16; typedef unsigned __INT16_TYPE__ cc_uint16;
typedef unsigned __INT32_TYPE__ cc_uint32; typedef unsigned __INT32_TYPE__ cc_uint32;
typedef unsigned __INT64_TYPE__ cc_uint64; typedef unsigned __INT64_TYPE__ cc_uint64;
typedef unsigned __INTPTR_TYPE__ cc_uintptr; typedef unsigned __INTPTR_TYPE__ cc_uintptr;
#endif #endif
#define CC_HAS_TYPES #define CC_HAS_TYPES
#endif #endif
#define CC_INLINE inline #define CC_INLINE inline
#define CC_NOINLINE __attribute__((noinline)) #define CC_NOINLINE __attribute__((noinline))
#ifndef CC_API #ifndef CC_API
#ifdef _WIN32 #ifdef _WIN32
#define CC_API __attribute__((dllexport, noinline)) #define CC_API __attribute__((dllexport, noinline))
#define CC_VAR __attribute__((dllexport)) #define CC_VAR __attribute__((dllexport))
#else #else
#define CC_API __attribute__((visibility("default"), noinline)) #define CC_API __attribute__((visibility("default"), noinline))
#define CC_VAR __attribute__((visibility("default"))) #define CC_VAR __attribute__((visibility("default")))
#endif #endif
#endif #endif
#define CC_HAS_MISC #define CC_HAS_MISC
#ifdef __BIG_ENDIAN__ #ifdef __BIG_ENDIAN__
#define CC_BIG_ENDIAN #define CC_BIG_ENDIAN
#endif #endif
#elif __MWERKS__ #elif __MWERKS__
/* TODO: Is there actual attribute support for CC_API etc somewhere? */ /* TODO: Is there actual attribute support for CC_API etc somewhere? */
#define CC_BIG_ENDIAN #define CC_BIG_ENDIAN
#endif #endif
/* Unrecognised compiler, so just go with some sensible default typdefs */ /* Unrecognised compiler, so just go with some sensible default typdefs */
@ -146,6 +146,7 @@ typedef cc_uint8 cc_bool;
#define CC_BUILD_WIN #define CC_BUILD_WIN
#define CC_BUILD_D3D9 #define CC_BUILD_D3D9
#define CC_BUILD_WINGUI #define CC_BUILD_WINGUI
#define CC_BUILD_HTTPCLIENT
#define CC_BUILD_SCHANNEL #define CC_BUILD_SCHANNEL
#define CC_BUILD_WINMM #define CC_BUILD_WINMM
#elif defined __ANDROID__ #elif defined __ANDROID__

View File

@ -420,6 +420,10 @@ static void KeyBind_Init(void) {
#define GAMEPAD_BEG_BTN CCPAD_A #define GAMEPAD_BEG_BTN CCPAD_A
static float pad_holdtime[INPUT_COUNT - GAMEPAD_BEG_BTN]; static float pad_holdtime[INPUT_COUNT - GAMEPAD_BEG_BTN];
int Gamepad_AxisBehaviour[2] = { AXIS_BEHAVIOUR_MOVEMENT, AXIS_BEHAVIOUR_CAMERA };
int Gamepad_AxisSensitivity[2] = { AXIS_SENSI_NORMAL, AXIS_SENSI_NORMAL };
static const float axis_sensiFactor[] = { 0.25f, 0.5f, 1.0f, 2.0f, 4.0f };
void Gamepad_SetButton(int btn, int pressed) { void Gamepad_SetButton(int btn, int pressed) {
/* Reset hold tracking time */ /* Reset hold tracking time */
if (pressed && !Input.Pressed[btn]) pad_holdtime[btn - GAMEPAD_BEG_BTN] = 0; if (pressed && !Input.Pressed[btn]) pad_holdtime[btn - GAMEPAD_BEG_BTN] = 0;
@ -427,25 +431,18 @@ void Gamepad_SetButton(int btn, int pressed) {
Input_SetNonRepeatable(btn, pressed); Input_SetNonRepeatable(btn, pressed);
} }
static void Gamepad_SetLAxis(float x, float y, double delta) {
if (x == 0 && y == 0) return;
Input.JoystickMovement = true;
Input.JoystickAngle = Math_Atan2(x, -y);
}
static void Gamepad_SetRAxis(float x, float y, double delta) {
float scale = delta * 60.0;
Event_RaiseRawMove(&ControllerEvents.RawMoved, x * scale, y * scale);
}
void Gamepad_SetAxis(int axis, float x, float y, double delta) { void Gamepad_SetAxis(int axis, float x, float y, double delta) {
if (!Input.RawMode) return; if (!Input.RawMode) return;
if (axis == PAD_AXIS_LEFT) { if (Gamepad_AxisBehaviour[axis] == AXIS_BEHAVIOUR_MOVEMENT) {
Gamepad_SetLAxis(x, y, delta); if (x == 0 && y == 0) return;
Input.JoystickMovement = true;
Input.JoystickAngle = Math_Atan2(x, -y);
} else { } else {
Gamepad_SetRAxis(x, y, delta); int sensi = Gamepad_AxisSensitivity[axis];
float scale = delta * 60.0 * axis_sensiFactor[sensi];
Event_RaiseRawMove(&ControllerEvents.RawMoved, x * scale, y * scale);
} }
} }

View File

@ -187,9 +187,14 @@ void KeyBind_Set(KeyBind binding, int key, cc_uint8* binds);
/* Gamepad axes. Default behaviour is: */ /* Gamepad axes. Default behaviour is: */
/* - left axis: player movement */ /* - left axis: player movement */
/* - right axis: camera movement */ /* - right axis: camera movement */
enum PAD_AXIS { PAD_AXIS_LEFT, PAD_AXIS_RIGHT }; enum PAD_AXIS { PAD_AXIS_LEFT, PAD_AXIS_RIGHT };
enum AXIS_SENSITIVITY { AXIS_SENSI_LOWER, AXIS_SENSI_LOW, AXIS_SENSI_NORMAL, AXIS_SENSI_HIGH, AXIS_SENSI_HIGHER };
enum AXIS_BEHAVIOUR { AXIS_BEHAVIOUR_MOVEMENT, AXIS_BEHAVIOUR_CAMERA };
extern int Gamepad_AxisBehaviour[2];
extern int Gamepad_AxisSensitivity[2];
/* Sets value of the given gamepad button */ /* Sets value of the given gamepad button */
void Gamepad_SetButton(int btn, int pressed); void Gamepad_SetButton(int btn, int pressed);
/* Sets value of the given axis */ /* Sets value of the given axis */