New controller input system

This commit is contained in:
Mors 2022-10-29 00:59:24 +03:00
parent 798756edf4
commit f612c787b2
8 changed files with 146 additions and 146 deletions

View File

@ -1106,103 +1106,87 @@
"title": "INPUT MAPPING",
"items": [{
"internal_name": "button_a",
"description": "The action assigned to the A button of your controller.\nIt might correspond to a different button on some controllers.",
"description": "The controller button assigned to the A button.",
"os": OS_ANY,
"type": TYPE_MOUSE_BUTTON,
"type": TYPE_CONTROLLER_BUTTON,
"enabled_by": -1,
"disabled_by": -1,
"name": "A"
"name": "A (Jump)"
}, {
"internal_name": "button_b",
"description": "The action assigned to the B button of your controller.\nIt might correspond to a different button on some controllers.",
"description": "The controller button assigned to the B button.",
"os": OS_ANY,
"type": TYPE_MOUSE_BUTTON,
"type": TYPE_CONTROLLER_BUTTON,
"enabled_by": -1,
"disabled_by": -1,
"name": "B"
}, {
"internal_name": "button_x",
"description": "The action assigned to the X button of your controller.\nIt might correspond to a different button on some controllers.",
"os": OS_ANY,
"type": TYPE_MOUSE_BUTTON,
"enabled_by": -1,
"disabled_by": -1,
"name": "X"
}, {
"internal_name": "button_y",
"description": "The action assigned to the Y button of your controller.\nIt might correspond to a different button on some controllers.",
"os": OS_ANY,
"type": TYPE_MOUSE_BUTTON,
"enabled_by": -1,
"disabled_by": -1,
"name": "Y"
"name": "B (Punch)"
}, {
"internal_name": "button_start",
"description": "The action assigned to the Start button of your controller.\nIt might correspond to a different button on some controllers.",
"description": "The controller button assigned to the Start button.",
"os": OS_ANY,
"type": TYPE_MOUSE_BUTTON,
"type": TYPE_CONTROLLER_BUTTON,
"enabled_by": -1,
"disabled_by": -1,
"name": "Start"
}, {
"internal_name": "button_select",
"description": "The action assigned to the Back button of your controller.\nIt might correspond to a different button on some controllers.",
"os": OS_ANY,
"type": TYPE_MOUSE_BUTTON,
"enabled_by": -1,
"disabled_by": -1,
"name": "Back"
"name": "Start (Pause)"
}, {
"internal_name": "button_l",
"description": "The action assigned to the L button of your controller.\nIt might correspond to a different button on some controllers.",
"description": "The controller button assigned to the L trigger.",
"os": OS_ANY,
"type": TYPE_MOUSE_BUTTON,
"type": TYPE_CONTROLLER_BUTTON,
"enabled_by": -1,
"disabled_by": -1,
"name": "L"
"name": "L (Center Camera)"
}, {
"internal_name": "button_r",
"description": "The action assigned to the R button of your controller.\nIt might correspond to a different button on some controllers.",
"description": "The controller button assigned to the R trigger.",
"os": OS_ANY,
"type": TYPE_MOUSE_BUTTON,
"type": TYPE_CONTROLLER_BUTTON,
"enabled_by": -1,
"disabled_by": -1,
"name": "R"
"name": "R (Change Camera)"
}, {
"internal_name": "button_zl",
"description": "The action assigned to the ZL trigger of your controller.\nIt might correspond to a different button on some controllers.",
"internal_name": "button_z",
"description": "The controller button assigned to the Z trigger.",
"os": OS_ANY,
"type": TYPE_MOUSE_BUTTON,
"type": TYPE_CONTROLLER_BUTTON,
"enabled_by": -1,
"disabled_by": -1,
"name": "ZL"
"name": "Z (Crouch)"
}, {
"internal_name": "button_zr",
"description": "The action assigned to the ZR trigger of your controller.\nIt might correspond to a different button on some controllers.",
"internal_name": "button_cup",
"description": "The controller button assigned to the C-Up button.",
"os": OS_ANY,
"type": TYPE_MOUSE_BUTTON,
"type": TYPE_CONTROLLER_BUTTON,
"enabled_by": -1,
"disabled_by": -1,
"name": "ZR"
"name": "C-Up (Zoom In)"
}, {
"internal_name": "button_thumbleft",
"description": "The action assigned to the press of left thumbstick. of your controller.\nIt might correspond to a different button on some controllers.",
"internal_name": "button_cdown",
"description": "The controller button assigned to the C-Down button.",
"os": OS_ANY,
"type": TYPE_MOUSE_BUTTON,
"type": TYPE_CONTROLLER_BUTTON,
"enabled_by": -1,
"disabled_by": -1,
"name": "Left Stick Press"
"name": "C-Down (Zoom Out)"
}, {
"internal_name": "button_thumbright",
"description": "The action assigned to the press of right thumbstick. of your controller.\nIt might correspond to a different button on some controllers.",
"internal_name": "button_cleft",
"description": "The controller button assigned to the C-Left button.",
"os": OS_ANY,
"type": TYPE_MOUSE_BUTTON,
"type": TYPE_CONTROLLER_BUTTON,
"enabled_by": -1,
"disabled_by": -1,
"name": "Right Stick Press"
"name": "C-Left (Rotate Left)"
}, {
"internal_name": "button_cright",
"description": "The controller button assigned to the C-Right button.",
"os": OS_ANY,
"type": TYPE_CONTROLLER_BUTTON,
"enabled_by": -1,
"disabled_by": -1,
"name": "C-Right (Rotate Right)"
}, {
"internal_name": "left_analog_stick_deadzone",
"description": "Lets you adjust the left analog stick deadzone on your controller.\nThe default value is 512.",
"description": "Adjusts the left analog stick deadzone of your controller.\nThe default value is 512.",
"os": OS_ANY,
"type": TYPE_UINT,
"enabled_by": -1,
@ -1210,7 +1194,7 @@
"name": "Left Analog Stick Deadzone"
}, {
"internal_name": "right_analog_stick_deadzone",
"description": "Lets you adjust the right analog stick deadzone on your controller.\nThe default value is 512.",
"description": "Adjusts the right analog stick deadzone of your controller.\nThe default value is 512.",
"os": OS_ANY,
"type": TYPE_UINT,
"enabled_by": -1,
@ -1218,7 +1202,7 @@
"name": "Right Analog Stick Deadzone"
}, {
"internal_name": "rumble_strength",
"description": "Enables and sets the strenght of the controller rumble effect.\nWorks the same way as the Super Mario 3D All-Stars and the Shindou Edition.",
"description": "Enables and sets the strength of the controller rumble effect.\nWorks the same way as the Super Mario 3D All-Stars and the Shindou Edition.",
"os": OS_ANY,
"type": TYPE_PERCENTAGE,
"enabled_by": -1,
@ -1285,7 +1269,7 @@
"type": TYPE_KEY,
"enabled_by": -1,
"disabled_by": -1,
"name": "C-Up"
"name": "C-Up (Zoom In)"
}, {
"internal_name": "key_cdown",
"description": "The keyboard key assigned to the C-Down button.\nThe value is a DirectInput scan code.",
@ -1293,7 +1277,7 @@
"type": TYPE_KEY,
"enabled_by": -1,
"disabled_by": -1,
"name": "C-Down"
"name": "C-Down (Zoom Out)"
}, {
"internal_name": "key_cleft",
"description": "The keyboard key assigned to the C-Left button.\nThe value is a DirectInput scan code.",
@ -1301,7 +1285,7 @@
"type": TYPE_KEY,
"enabled_by": -1,
"disabled_by": -1,
"name": "C-Left"
"name": "C-Left (Rotate Left)"
}, {
"internal_name": "key_cright",
"description": "The keyboard key assigned to the C-Right button.\nThe value is a DirectInput scan code.",
@ -1309,7 +1293,7 @@
"type": TYPE_KEY,
"enabled_by": -1,
"disabled_by": -1,
"name": "C-Right"
"name": "C-Right (Rotate Right)"
}, {
"internal_name": "key_stickup",
"description": "The keyboard key assigned to the stick up action.\n\nThe value is a DirectInput scan code.",

View File

@ -168,21 +168,19 @@ fullscreen_refresh_rate = "60"
custom_camera_distance = "100.000000"
zoomed_out_custom_camera_distance = "150.000000"
[INPUT MAPPING]
button_a = "32768"
button_b = "4"
button_x = "16384"
button_y = "8"
button_start = "4096"
button_select = "32"
button_l = "8192"
button_r = "16"
button_zl = "8192"
button_zr = "16"
button_thumbleft = "0"
button_thumbright = "32"
button_a = "2"
button_b = "8"
button_start = "128"
button_l = "1024"
button_r = "2048"
button_z = "12582912"
button_cup = "16"
button_cdown = "4"
button_cleft = "0"
button_cright = "0"
left_analog_stick_deadzone = "512"
right_analog_stick_deadzone = "512"
rumble_strength = "0.250000"
rumble_strength = "0.25"
[KEY MAPPING]
key_a = "38"
key_b = "51"

View File

@ -168,21 +168,19 @@ fullscreen_refresh_rate = "60"
custom_camera_distance = "100.000000"
zoomed_out_custom_camera_distance = "150.000000"
[INPUT MAPPING]
button_a = "32768"
button_b = "4"
button_x = "16384"
button_y = "8"
button_start = "4096"
button_select = "32"
button_l = "8192"
button_r = "16"
button_zl = "8192"
button_zr = "16"
button_thumbleft = "0"
button_thumbright = "32"
button_a = "2"
button_b = "8"
button_start = "128"
button_l = "1024"
button_r = "2048"
button_z = "12582912"
button_cup = "16"
button_cdown = "4"
button_cleft = "0"
button_cright = "0"
left_analog_stick_deadzone = "512"
right_analog_stick_deadzone = "512"
rumble_strength = "0.250000"
rumble_strength = "0.25"
[KEY MAPPING]
key_a = "38"
key_b = "51"

View File

@ -168,21 +168,19 @@ fullscreen_refresh_rate = "60"
custom_camera_distance = "100.000000"
zoomed_out_custom_camera_distance = "150.000000"
[INPUT MAPPING]
button_a = "32768"
button_b = "4"
button_x = "16384"
button_y = "8"
button_start = "4096"
button_select = "32"
button_l = "8192"
button_r = "16"
button_zl = "8192"
button_zr = "16"
button_thumbleft = "0"
button_thumbright = "32"
button_a = "2"
button_b = "8"
button_start = "128"
button_l = "1024"
button_r = "2048"
button_z = "12582912"
button_cup = "16"
button_cdown = "4"
button_cleft = "0"
button_cright = "0"
left_analog_stick_deadzone = "512"
right_analog_stick_deadzone = "512"
rumble_strength = "0.000000"
rumble_strength = "0.25"
[KEY MAPPING]
key_a = "38"
key_b = "51"

View File

@ -288,18 +288,16 @@ unsigned int configFullscreenRefreshRate = 60;
float configCustomCameraDistance = 100.0f;
float configCustomCameraDistanceZoomedOut = 150.0f;
unsigned int configButtonA = A_BUTTON;
unsigned int configButtonB = D_CBUTTONS;
unsigned int configButtonX = B_BUTTON;
unsigned int configButtonY = U_CBUTTONS;
unsigned int configButtonStart = START_BUTTON;
unsigned int configButtonSelect = L_TRIG;
unsigned int configButtonL = Z_TRIG;
unsigned int configButtonR = R_TRIG;
unsigned int configButtonZL = Z_TRIG;
unsigned int configButtonZR = R_TRIG;
unsigned int configButtonThumbLeft = 0;
unsigned int configButtonThumbRight = L_TRIG;
unsigned int configButtonA = 2;
unsigned int configButtonB = 8;
unsigned int configButtonStart = 128;
unsigned int configButtonL = 1024;
unsigned int configButtonR = 2048;
unsigned int configButtonZ = 12582912;
unsigned int configButtonCUp = 16;
unsigned int configButtonCDown = 4;
unsigned int configButtonCLeft = 0;
unsigned int configButtonCRight = 0;
unsigned int gControllerLeftDeadzone = 512;
unsigned int gControllerRightDeadzone = 512;
float configRumbleStrength = 0.5f;

View File

@ -139,16 +139,14 @@ extern float configCustomCameraDistanceZoomedOut;
extern unsigned int configButtonA;
extern unsigned int configButtonB;
extern unsigned int configButtonX;
extern unsigned int configButtonY;
extern unsigned int configButtonStart;
extern unsigned int configButtonSelect;
extern unsigned int configButtonL;
extern unsigned int configButtonR;
extern unsigned int configButtonZL;
extern unsigned int configButtonZR;
extern unsigned int configButtonThumbLeft;
extern unsigned int configButtonThumbRight;
extern unsigned int configButtonZ;
extern unsigned int configButtonCUp;
extern unsigned int configButtonCDown;
extern unsigned int configButtonCLeft;
extern unsigned int configButtonCRight;
extern unsigned int gControllerLeftDeadzone;
extern unsigned int gControllerRightDeadzone;
extern float configRumbleStrength;

View File

@ -222,16 +222,14 @@ static const struct ConfigOption options[] = {
{ .name = "INPUT MAPPING", .type = CONFIG_TYPE_SECTION },
{ .name = "button_a", .type = CONFIG_TYPE_UINT, .uintValue = &configButtonA },
{ .name = "button_b", .type = CONFIG_TYPE_UINT, .uintValue = &configButtonB },
{ .name = "button_x", .type = CONFIG_TYPE_UINT, .uintValue = &configButtonX },
{ .name = "button_y", .type = CONFIG_TYPE_UINT, .uintValue = &configButtonY },
{ .name = "button_start", .type = CONFIG_TYPE_UINT, .uintValue = &configButtonStart },
{ .name = "button_select", .type = CONFIG_TYPE_UINT, .uintValue = &configButtonSelect },
{ .name = "button_l", .type = CONFIG_TYPE_UINT, .uintValue = &configButtonL },
{ .name = "button_r", .type = CONFIG_TYPE_UINT, .uintValue = &configButtonR },
{ .name = "button_zl", .type = CONFIG_TYPE_UINT, .uintValue = &configButtonZL },
{ .name = "button_zr", .type = CONFIG_TYPE_UINT, .uintValue = &configButtonZR },
{ .name = "button_thumbleft", .type = CONFIG_TYPE_UINT, .uintValue = &configButtonThumbLeft },
{ .name = "button_thumbright", .type = CONFIG_TYPE_UINT, .uintValue = &configButtonThumbRight },
{ .name = "button_z", .type = CONFIG_TYPE_UINT, .uintValue = &configButtonZ },
{ .name = "button_cup", .type = CONFIG_TYPE_UINT, .uintValue = &configButtonCUp },
{ .name = "button_cdown", .type = CONFIG_TYPE_UINT, .uintValue = &configButtonCDown },
{ .name = "button_cleft", .type = CONFIG_TYPE_UINT, .uintValue = &configButtonCLeft },
{ .name = "button_cright", .type = CONFIG_TYPE_UINT, .uintValue = &configButtonCRight },
{ .name = "left_analog_stick_deadzone", .type = CONFIG_TYPE_UINT, .uintValue = &gControllerLeftDeadzone },
{ .name = "right_analog_stick_deadzone", .type = CONFIG_TYPE_UINT, .uintValue = &gControllerRightDeadzone },
{ .name = "rumble_strength", .type = CONFIG_TYPE_FLOAT, .floatValue = &configRumbleStrength },

View File

@ -18,6 +18,11 @@
#define DEADZONE_LEFT gControllerLeftDeadzone * 10
#define DEADZONE_RIGHT gControllerRightDeadzone * 10
// Checks if the button is pressed, and if so adds it to the pressedButtons variable
#define SET_BUTTON(SDL_BUTTON) if (SDL_GameControllerGetButton(sdl_cntrl, SDL_BUTTON)) pressedButtons |= 1 << (SDL_BUTTON + 1)
//Checks if the button is in the pressedButtons variable, and if so passes it to the emulated controls
#define CHECK_BUTTON(CONFIG_BUTTON, GAME_BUTTON) if ((pressedButtons & CONFIG_BUTTON) != 0) pad->button |= GAME_BUTTON
static bool init_ok;
static SDL_GameController *sdl_cntrl;
static bool haptics_enabled;
@ -85,30 +90,53 @@ static void controller_sdl_read(OSContPad *pad) {
}
}
if (SDL_GameControllerGetButton(sdl_cntrl, SDL_CONTROLLER_BUTTON_START)) pad->button |= configButtonStart;
if (SDL_GameControllerGetButton(sdl_cntrl, SDL_CONTROLLER_BUTTON_BACK)) pad->button |= configButtonSelect;
if (SDL_GameControllerGetButton(sdl_cntrl, SDL_CONTROLLER_BUTTON_LEFTSHOULDER)) pad->button |= configButtonL;
if (SDL_GameControllerGetButton(sdl_cntrl, SDL_CONTROLLER_BUTTON_RIGHTSHOULDER)) pad->button |= configButtonR;
if (SDL_GameControllerGetButton(sdl_cntrl, SDL_CONTROLLER_BUTTON_A)) pad->button |= configButtonA;
if (SDL_GameControllerGetButton(sdl_cntrl, SDL_CONTROLLER_BUTTON_B)) pad->button |= configButtonB;
if (SDL_GameControllerGetButton(sdl_cntrl, SDL_CONTROLLER_BUTTON_X)) pad->button |= configButtonX;
if (SDL_GameControllerGetButton(sdl_cntrl, SDL_CONTROLLER_BUTTON_Y)) pad->button |= configButtonY;
if (SDL_GameControllerGetButton(sdl_cntrl, SDL_CONTROLLER_BUTTON_LEFTSTICK)) pad->button |= configButtonThumbLeft;
if (SDL_GameControllerGetButton(sdl_cntrl, SDL_CONTROLLER_BUTTON_RIGHTSTICK)) pad->button |= configButtonThumbRight;
int16_t ltrig = SDL_GameControllerGetAxis(sdl_cntrl, SDL_CONTROLLER_AXIS_TRIGGERLEFT);
int16_t rtrig = SDL_GameControllerGetAxis(sdl_cntrl, SDL_CONTROLLER_AXIS_TRIGGERRIGHT);
if (SDL_GameControllerGetButton(sdl_cntrl, SDL_CONTROLLER_BUTTON_DPAD_UP)) pad->button |= U_JPAD;
if (SDL_GameControllerGetButton(sdl_cntrl, SDL_CONTROLLER_BUTTON_DPAD_DOWN)) pad->button |= D_JPAD;
if (SDL_GameControllerGetButton(sdl_cntrl, SDL_CONTROLLER_BUTTON_DPAD_LEFT)) pad->button |= L_JPAD;
if (SDL_GameControllerGetButton(sdl_cntrl, SDL_CONTROLLER_BUTTON_DPAD_RIGHT)) pad->button |= R_JPAD;
int pressedButtons = 0;
SET_BUTTON(SDL_CONTROLLER_BUTTON_START);
SET_BUTTON(SDL_CONTROLLER_BUTTON_BACK);
SET_BUTTON(SDL_CONTROLLER_BUTTON_LEFTSHOULDER);
SET_BUTTON(SDL_CONTROLLER_BUTTON_RIGHTSHOULDER);
SET_BUTTON(SDL_CONTROLLER_BUTTON_A);
SET_BUTTON(SDL_CONTROLLER_BUTTON_B);
SET_BUTTON(SDL_CONTROLLER_BUTTON_X);
SET_BUTTON(SDL_CONTROLLER_BUTTON_Y);
SET_BUTTON(SDL_CONTROLLER_BUTTON_LEFTSTICK);
SET_BUTTON(SDL_CONTROLLER_BUTTON_RIGHTSTICK);
SET_BUTTON(SDL_CONTROLLER_BUTTON_DPAD_UP);
SET_BUTTON(SDL_CONTROLLER_BUTTON_DPAD_DOWN);
SET_BUTTON(SDL_CONTROLLER_BUTTON_DPAD_LEFT);
SET_BUTTON(SDL_CONTROLLER_BUTTON_DPAD_RIGHT);
if (ltrig > 30 * 256) pressedButtons |= 1 << 22;
if (rtrig > 30 * 256) pressedButtons |= 1 << 23;
CHECK_BUTTON(configButtonA, A_BUTTON);
CHECK_BUTTON(configButtonB, B_BUTTON);
CHECK_BUTTON(configButtonZ, Z_TRIG);
CHECK_BUTTON(configButtonStart, START_BUTTON);
CHECK_BUTTON(configButtonL, L_TRIG);
CHECK_BUTTON(configButtonR, R_TRIG);
CHECK_BUTTON(configButtonCUp, U_CBUTTONS);
CHECK_BUTTON(configButtonCDown, D_CBUTTONS);
CHECK_BUTTON(configButtonCLeft, L_CBUTTONS);
CHECK_BUTTON(configButtonCRight, R_CBUTTONS);
if (SDL_GameControllerGetButton(sdl_cntrl, SDL_CONTROLLER_BUTTON_DPAD_UP))
pad->button |= U_JPAD;
if (SDL_GameControllerGetButton(sdl_cntrl, SDL_CONTROLLER_BUTTON_DPAD_DOWN))
pad->button |= D_JPAD;
if (SDL_GameControllerGetButton(sdl_cntrl, SDL_CONTROLLER_BUTTON_DPAD_LEFT))
pad->button |= L_JPAD;
if (SDL_GameControllerGetButton(sdl_cntrl, SDL_CONTROLLER_BUTTON_DPAD_RIGHT))
pad->button |= R_JPAD;
int16_t leftx = SDL_GameControllerGetAxis(sdl_cntrl, SDL_CONTROLLER_AXIS_LEFTX);
int16_t lefty = SDL_GameControllerGetAxis(sdl_cntrl, SDL_CONTROLLER_AXIS_LEFTY);
int16_t rightx = SDL_GameControllerGetAxis(sdl_cntrl, SDL_CONTROLLER_AXIS_RIGHTX);
int16_t righty = SDL_GameControllerGetAxis(sdl_cntrl, SDL_CONTROLLER_AXIS_RIGHTY);
int16_t ltrig = SDL_GameControllerGetAxis(sdl_cntrl, SDL_CONTROLLER_AXIS_TRIGGERLEFT);
int16_t rtrig = SDL_GameControllerGetAxis(sdl_cntrl, SDL_CONTROLLER_AXIS_TRIGGERRIGHT);
#ifdef TARGET_WEB
// Firefox has a bug: https://bugzilla.mozilla.org/show_bug.cgi?id=1606562
// It sets down y to 32768.0f / 32767.0f, which is greater than the allowed 1.0f,
@ -145,8 +173,8 @@ static void controller_sdl_read(OSContPad *pad) {
if (righty > 0x4000) pad->button |= D_CBUTTONS;
}
if (ltrig > 30 * 256) pad->button |= configButtonZL;
if (rtrig > 30 * 256) pad->button |= configButtonZR;
//if (ltrig > 30 * 256) pad->button |= configButtonZL;
//if (rtrig > 30 * 256) pad->button |= configButtonZR;
uint32_t magnitude_sq = (uint32_t)(leftx * leftx) + (uint32_t)(lefty * lefty);
if (magnitude_sq > (uint32_t)(DEADZONE_LEFT * DEADZONE_LEFT)) {