Browse Source

allow rebinding of joystick and mouse buttons

master
fgsfds 2 years ago
parent
commit
690283da13
  1. 24
      src/pc/configfile.c
  2. 12
      src/pc/configfile.h
  3. 20
      src/pc/controller/controller_sdl.c

24
src/pc/configfile.c

@ -45,6 +45,19 @@ unsigned int configKeyStickUp = 0x11;
unsigned int configKeyStickDown = 0x1F;
unsigned int configKeyStickLeft = 0x1E;
unsigned int configKeyStickRight = 0x20;
// Gamepad mappings (SDL_GameControllerButton values)
unsigned int configJoyA = 0;
unsigned int configJoyB = 2;
unsigned int configJoyStart = 6;
unsigned int configJoyL = 9;
unsigned int configJoyR = 10;
unsigned int configJoyZ = 7;
// Mouse button mappings (0 for none, 1 for left, 2 for middle, 3 for right)
unsigned int configMouseA = 3;
unsigned int configMouseB = 1;
unsigned int configMouseL = 4;
unsigned int configMouseR = 5;
unsigned int configMouseZ = 2;
static const struct ConfigOption options[] = {
@ -63,6 +76,17 @@ static const struct ConfigOption options[] = {
{.name = "key_stickdown", .type = CONFIG_TYPE_UINT, .uintValue = &configKeyStickDown},
{.name = "key_stickleft", .type = CONFIG_TYPE_UINT, .uintValue = &configKeyStickLeft},
{.name = "key_stickright", .type = CONFIG_TYPE_UINT, .uintValue = &configKeyStickRight},
{.name = "joy_a", .type = CONFIG_TYPE_UINT, .uintValue = &configJoyA},
{.name = "joy_b", .type = CONFIG_TYPE_UINT, .uintValue = &configJoyB},
{.name = "joy_start", .type = CONFIG_TYPE_UINT, .uintValue = &configJoyStart},
{.name = "joy_l", .type = CONFIG_TYPE_UINT, .uintValue = &configJoyL},
{.name = "joy_r", .type = CONFIG_TYPE_UINT, .uintValue = &configJoyR},
{.name = "joy_z", .type = CONFIG_TYPE_UINT, .uintValue = &configJoyZ},
{.name = "mouse_a", .type = CONFIG_TYPE_UINT, .uintValue = &configMouseA},
{.name = "mouse_b", .type = CONFIG_TYPE_UINT, .uintValue = &configMouseB},
{.name = "mouse_l", .type = CONFIG_TYPE_UINT, .uintValue = &configMouseL},
{.name = "mouse_r", .type = CONFIG_TYPE_UINT, .uintValue = &configMouseR},
{.name = "mouse_z", .type = CONFIG_TYPE_UINT, .uintValue = &configMouseZ},
};
// Reads an entire line from a file (excluding the newline character) and returns an allocated string

12
src/pc/configfile.h

@ -16,6 +16,18 @@ extern unsigned int configKeyStickUp;
extern unsigned int configKeyStickDown;
extern unsigned int configKeyStickLeft;
extern unsigned int configKeyStickRight;
extern unsigned int configJoyA;
extern unsigned int configJoyB;
extern unsigned int configJoyStart;
extern unsigned int configJoyL;
extern unsigned int configJoyR;
extern unsigned int configJoyZ;
extern unsigned int configMouseA;
extern unsigned int configMouseB;
extern unsigned int configMouseStart;
extern unsigned int configMouseL;
extern unsigned int configMouseR;
extern unsigned int configMouseZ;
void configfile_load(const char *filename);
void configfile_save(const char *filename);

20
src/pc/controller/controller_sdl.c

@ -12,6 +12,8 @@
#include "controller_api.h"
#include "../configfile.h"
extern int16_t rightx;
extern int16_t righty;
@ -53,9 +55,11 @@ static void controller_sdl_read(OSContPad *pad) {
const u32 mbuttons = SDL_GetRelativeMouseState(&mouse_x, &mouse_y);
if (mbuttons & SDL_BUTTON_LMASK) pad->button |= B_BUTTON;
if (mbuttons & SDL_BUTTON_RMASK) pad->button |= A_BUTTON;
if (mbuttons & SDL_BUTTON_MMASK) pad->button |= Z_TRIG;
if (configMouseA && (mbuttons & SDL_BUTTON(configMouseA))) pad->button |= A_BUTTON;
if (configMouseB && (mbuttons & SDL_BUTTON(configMouseB))) pad->button |= B_BUTTON;
if (configMouseL && (mbuttons & SDL_BUTTON(configMouseL))) pad->button |= L_TRIG;
if (configMouseR && (mbuttons & SDL_BUTTON(configMouseR))) pad->button |= R_TRIG;
if (configMouseZ && (mbuttons & SDL_BUTTON(configMouseZ))) pad->button |= Z_TRIG;
#endif
SDL_GameControllerUpdate();
@ -78,11 +82,11 @@ static void controller_sdl_read(OSContPad *pad) {
}
}
if (SDL_GameControllerGetButton(sdl_cntrl, SDL_CONTROLLER_BUTTON_START)) pad->button |= START_BUTTON;
if (SDL_GameControllerGetButton(sdl_cntrl, SDL_CONTROLLER_BUTTON_LEFTSHOULDER)) pad->button |= Z_TRIG;
if (SDL_GameControllerGetButton(sdl_cntrl, SDL_CONTROLLER_BUTTON_RIGHTSHOULDER)) pad->button |= R_TRIG;
if (SDL_GameControllerGetButton(sdl_cntrl, SDL_CONTROLLER_BUTTON_A)) pad->button |= A_BUTTON;
if (SDL_GameControllerGetButton(sdl_cntrl, SDL_CONTROLLER_BUTTON_X)) pad->button |= B_BUTTON;
if (SDL_GameControllerGetButton(sdl_cntrl, configJoyStart)) pad->button |= START_BUTTON;
if (SDL_GameControllerGetButton(sdl_cntrl, configJoyL)) pad->button |= Z_TRIG;
if (SDL_GameControllerGetButton(sdl_cntrl, configJoyR)) pad->button |= R_TRIG;
if (SDL_GameControllerGetButton(sdl_cntrl, configJoyA)) pad->button |= A_BUTTON;
if (SDL_GameControllerGetButton(sdl_cntrl, configJoyB)) pad->button |= B_BUTTON;
int16_t leftx = SDL_GameControllerGetAxis(sdl_cntrl, SDL_CONTROLLER_AXIS_LEFTX);
int16_t lefty = SDL_GameControllerGetAxis(sdl_cntrl, SDL_CONTROLLER_AXIS_LEFTY);

Loading…
Cancel
Save