Adjust how deadzones work
Some checks failed
Some checks failed
This commit is contained in:
@@ -30,7 +30,7 @@ elseif DOLPHIN then
|
||||
inputBind("lstick_left", INPUT_ACTION_LEFT)
|
||||
inputBind("lstick_right", INPUT_ACTION_RIGHT)
|
||||
|
||||
else
|
||||
elseif LINUX then
|
||||
if INPUT_KEYBOARD then
|
||||
inputBind("w", INPUT_ACTION_UP)
|
||||
inputBind("s", INPUT_ACTION_DOWN)
|
||||
@@ -70,6 +70,8 @@ else
|
||||
inputBind("mouse_x", INPUT_ACTION_POINTERX)
|
||||
inputBind("mouse_y", INPUT_ACTION_POINTERY)
|
||||
end
|
||||
else
|
||||
print("Unknown platform, no default input bindings set.")
|
||||
end
|
||||
|
||||
sceneSet('scene/minesweeper.lua')
|
||||
@@ -178,21 +178,8 @@ function sceneDispose()
|
||||
end
|
||||
|
||||
function sceneUpdate()
|
||||
if inputIsDown(INPUT_ACTION_RIGHT) then
|
||||
x = x + 1
|
||||
end
|
||||
|
||||
if inputIsDown(INPUT_ACTION_LEFT) then
|
||||
x = x - 1
|
||||
end
|
||||
|
||||
if inputIsDown(INPUT_ACTION_DOWN) then
|
||||
y = y + 1
|
||||
end
|
||||
|
||||
if inputIsDown(INPUT_ACTION_UP) then
|
||||
y = y - 1
|
||||
end
|
||||
x = x + inputAxis(INPUT_ACTION_LEFT, INPUT_ACTION_RIGHT)
|
||||
y = y + inputAxis(INPUT_ACTION_UP, INPUT_ACTION_DOWN)
|
||||
end
|
||||
|
||||
|
||||
|
||||
@@ -23,10 +23,6 @@ errorret_t inputInit(void) {
|
||||
INPUT.actions[i].currentValue = 0.0f;
|
||||
}
|
||||
|
||||
#ifdef DUSK_INPUT_GAMEPAD
|
||||
INPUT.deadzone = 0.2f;
|
||||
#endif
|
||||
|
||||
#ifdef inputInitPlatform
|
||||
errorChain(inputInitPlatform());
|
||||
#endif
|
||||
@@ -195,3 +191,8 @@ void inputBind(const inputbutton_t button, const inputaction_t act) {
|
||||
// Bind the action.
|
||||
data->action = act;
|
||||
}
|
||||
|
||||
float_t inputDeadzone(const float_t rawValue, const float_t deadzone) {
|
||||
if(rawValue < deadzone) return 0.0f;
|
||||
return (rawValue - deadzone) / (1.0f - deadzone);
|
||||
}
|
||||
@@ -27,10 +27,6 @@ typedef struct {
|
||||
event_t eventReleased;
|
||||
|
||||
inputplatform_t platform;
|
||||
|
||||
#ifdef DUSK_INPUT_GAMEPAD
|
||||
float_t deadzone;
|
||||
#endif
|
||||
} input_t;
|
||||
|
||||
extern input_t INPUT;
|
||||
@@ -131,3 +127,14 @@ float_t inputAxis(const inputaction_t neg, const inputaction_t pos);
|
||||
* @param action The input action to bind the button to.
|
||||
*/
|
||||
void inputBind(const inputbutton_t button, const inputaction_t act);
|
||||
|
||||
/**
|
||||
* Applies deadzone formula to a raw input value. This will;
|
||||
* return 0 if input is less than the deadzone.
|
||||
* re-map the range between 0 and 1 in deadzone-space.
|
||||
*
|
||||
* @param rawValue The raw input value to apply the deadzone to.
|
||||
* @param deadzone The deadzone threshold (0.0f to 1.0f).
|
||||
* @return The input value after applying the deadzone.
|
||||
*/
|
||||
float_t inputDeadzone(const float_t rawValue, const float_t deadzone);
|
||||
@@ -53,6 +53,7 @@ void moduleInput(scriptcontext_t *context) {
|
||||
lua_register(context->luaState, "inputPressed", moduleInputPressed);
|
||||
lua_register(context->luaState, "inputReleased", moduleInputReleased);
|
||||
lua_register(context->luaState, "inputGetValue", moduleInputGetValue);
|
||||
lua_register(context->luaState, "inputAxis", moduleInputAxis);
|
||||
}
|
||||
|
||||
int moduleInputIndex(lua_State *l) {
|
||||
@@ -190,3 +191,27 @@ int moduleInputGetValue(lua_State *L) {
|
||||
lua_pushnumber(L, inputGetCurrentValue(action));
|
||||
return 1;
|
||||
}
|
||||
|
||||
int moduleInputAxis(lua_State *L) {
|
||||
assertNotNull(L, "Lua state cannot be NULL");
|
||||
|
||||
if(!lua_isnumber(L, 1) || !lua_isnumber(L, 2)) {
|
||||
luaL_error(L, "inputAxis: Expected two action IDs as arguments (neg, pos)");
|
||||
return 0;
|
||||
}
|
||||
|
||||
const inputaction_t neg = (inputaction_t)lua_tonumber(L, 1);
|
||||
const inputaction_t pos = (inputaction_t)lua_tonumber(L, 2);
|
||||
|
||||
if(neg < INPUT_ACTION_NULL || neg >= INPUT_ACTION_COUNT) {
|
||||
luaL_error(L, "inputAxis: Invalid negative action ID %d", neg);
|
||||
return 0;
|
||||
}
|
||||
if(pos < INPUT_ACTION_NULL || pos >= INPUT_ACTION_COUNT) {
|
||||
luaL_error(L, "inputAxis: Invalid positive action ID %d", pos);
|
||||
return 0;
|
||||
}
|
||||
|
||||
lua_pushnumber(L, inputAxis(neg, pos));
|
||||
return 1;
|
||||
}
|
||||
@@ -61,3 +61,11 @@ int moduleInputReleased(lua_State *L);
|
||||
* @return Number of return values on the Lua stack.
|
||||
*/
|
||||
int moduleInputGetValue(lua_State *L);
|
||||
|
||||
/**
|
||||
* Script binding for inputAxis.
|
||||
*
|
||||
* @param L The Lua state.
|
||||
* @return Number of return values on the Lua stack.
|
||||
*/
|
||||
int moduleInputAxis(lua_State *L);
|
||||
@@ -229,3 +229,10 @@ bool_t stringEndsWithCaseInsensitive(
|
||||
if(suffixLen > strLen) return false;
|
||||
return strcasecmp(str + strLen - suffixLen, suffix) == 0;
|
||||
}
|
||||
|
||||
bool_t stringIncludesString(const char_t *haystack, const char_t *needle) {
|
||||
assertNotNull(haystack, "haystack must not be NULL");
|
||||
assertNotNull(needle, "needle must not be NULL");
|
||||
|
||||
return strstr(haystack, needle) != NULL;
|
||||
}
|
||||
@@ -161,3 +161,12 @@ bool_t stringEndsWith(const char_t *str, const char_t *suffix);
|
||||
* @return TRUE if the string ends with the suffix, FALSE otherwise.
|
||||
*/
|
||||
bool_t stringEndsWithCaseInsensitive(const char_t *str, const char_t *suffix);
|
||||
|
||||
/**
|
||||
* Determines if a string includes a specified substring.
|
||||
*
|
||||
* @param haystack The string to check.
|
||||
* @param needle The substring to check for.
|
||||
* @return TRUE if the string includes the substring, FALSE otherwise.
|
||||
*/
|
||||
bool_t stringIncludesString(const char_t *haystack, const char_t *needle);
|
||||
@@ -7,6 +7,8 @@
|
||||
|
||||
#include "input/input.h"
|
||||
#include "assert/assert.h"
|
||||
#include "log/log.h"
|
||||
#include "util/string.h"
|
||||
|
||||
inputbuttondata_t INPUT_BUTTON_DATA[] = {
|
||||
#ifdef DUSK_INPUT_GAMEPAD
|
||||
@@ -26,13 +28,14 @@ inputbuttondata_t INPUT_BUTTON_DATA[] = {
|
||||
{ .name = "lstick_up", { .type = INPUT_BUTTON_TYPE_GAMEPAD_AXIS, .gpAxis = { .axis = INPUT_GAMEPAD_AXIS_LEFT_X, .positive = true } } },
|
||||
{ .name = "lstick_down", { .type = INPUT_BUTTON_TYPE_GAMEPAD_AXIS, .gpAxis = { .axis = INPUT_GAMEPAD_AXIS_LEFT_X, .positive = false } } },
|
||||
{ .name = "lstick_left", { .type = INPUT_BUTTON_TYPE_GAMEPAD_AXIS, .gpAxis = { .axis = INPUT_GAMEPAD_AXIS_LEFT_Y, .positive = true } } },
|
||||
{ .name = "lstick_right", { .type = INPUT_BUTTON_TYPE_GAMEPAD_AXIS, .gpAxis = { .axis = INPUT_GAMEPAD_AXIS_LEFT_Y, .positive = false } } },
|
||||
{ .name = "rstick_up", { .type = INPUT_BUTTON_TYPE_GAMEPAD_AXIS, .gpAxis = { .axis = INPUT_GAMEPAD_AXIS_C_X, .positive = true } } },
|
||||
{ .name = "rstick_down", { .type = INPUT_BUTTON_TYPE_GAMEPAD_AXIS, .gpAxis = { .axis = INPUT_GAMEPAD_AXIS_C_X, .positive = false } } },
|
||||
{ .name = "rstick_left", { .type = INPUT_BUTTON_TYPE_GAMEPAD_AXIS, .gpAxis = { .axis = INPUT_GAMEPAD_AXIS_C_Y, .positive = true } } },
|
||||
{ .name = "rstick_right", { .type = INPUT_BUTTON_TYPE_GAMEPAD_AXIS, .gpAxis = { .axis = INPUT_GAMEPAD_AXIS_C_Y, .positive = false } } },
|
||||
{ .name = "lstick_right", { .type = INPUT_BUTTON_TYPE_GAMEPAD_AXIS, .gpAxis = { .axis = INPUT_GAMEPAD_AXIS_LEFT_Y, .positive = false } } },
|
||||
{ .name = "ltrigger", { .type = INPUT_BUTTON_TYPE_GAMEPAD_AXIS, .gpAxis = { .axis = INPUT_GAMEPAD_AXIS_TRIGGER_LEFT, .positive = true } } },
|
||||
{ .name = "rtrigger", { .type = INPUT_BUTTON_TYPE_GAMEPAD_AXIS, .gpAxis = { .axis = INPUT_GAMEPAD_AXIS_TRIGGER_RIGHT, .positive = true } } },
|
||||
|
||||
#endif
|
||||
|
||||
{ .name = NULL }
|
||||
@@ -51,29 +54,13 @@ void inputUpdateDolphin(void) {
|
||||
for(uint8_t i = 0; i < INPUT_DOLPHIN_PAD_COUNT; i++) {
|
||||
INPUT.platform.padState[i] = PAD_ButtonsHeld(i);
|
||||
|
||||
INPUT.platform.pads[i][INPUT_GAMEPAD_AXIS_LEFT_X] = (
|
||||
INPUT_DOLPHIN_AXIS(PAD_StickX(i))
|
||||
);
|
||||
|
||||
INPUT.platform.pads[i][INPUT_GAMEPAD_AXIS_LEFT_Y] = (
|
||||
INPUT_DOLPHIN_AXIS(PAD_StickY(i))
|
||||
);
|
||||
|
||||
INPUT.platform.pads[i][INPUT_GAMEPAD_AXIS_C_X] = (
|
||||
INPUT_DOLPHIN_AXIS(PAD_SubStickX(i))
|
||||
);
|
||||
|
||||
INPUT.platform.pads[i][INPUT_GAMEPAD_AXIS_C_Y] = (
|
||||
INPUT_DOLPHIN_AXIS(PAD_SubStickY(i))
|
||||
);
|
||||
|
||||
INPUT.platform.pads[i][INPUT_GAMEPAD_AXIS_TRIGGER_LEFT] = (
|
||||
INPUT_DOLPHIN_AXIS(PAD_TriggerL(i))
|
||||
);
|
||||
|
||||
INPUT.platform.pads[i][INPUT_GAMEPAD_AXIS_TRIGGER_RIGHT] = (
|
||||
INPUT_DOLPHIN_AXIS(PAD_TriggerR(i))
|
||||
);
|
||||
INPUT.platform.pads[i][INPUT_GAMEPAD_AXIS_LEFT_X] = PAD_StickX(i);
|
||||
INPUT.platform.pads[i][INPUT_GAMEPAD_AXIS_LEFT_Y] = PAD_StickY(i);
|
||||
INPUT.platform.pads[i][INPUT_GAMEPAD_AXIS_C_X] = PAD_SubStickX(i);
|
||||
INPUT.platform.pads[i][INPUT_GAMEPAD_AXIS_C_Y] = PAD_SubStickY(i);
|
||||
INPUT.platform.pads[i][INPUT_GAMEPAD_AXIS_TRIGGER_LEFT]=PAD_TriggerL(i);
|
||||
INPUT.platform.pads[i][INPUT_GAMEPAD_AXIS_TRIGGER_RIGHT]=PAD_TriggerR(i);
|
||||
// Because gamecube sticks are hexagons this is going to be weird.
|
||||
}
|
||||
#endif
|
||||
}
|
||||
@@ -87,10 +74,11 @@ float_t inputButtonGetValueDolphin(const inputbutton_t button) {
|
||||
}
|
||||
|
||||
case INPUT_BUTTON_TYPE_GAMEPAD_AXIS: {
|
||||
float_t value = INPUT.platform.pads[0][button.gpAxis.axis];
|
||||
float_t axis = INPUT.platform.pads[0][button.gpAxis.axis];
|
||||
float_t value = axis / 128.0f;
|
||||
if(!button.gpAxis.positive) value = -value;
|
||||
if(value >= INPUT.deadzone) return value;
|
||||
return 0.0f;
|
||||
value = inputDeadzone(value, inputGetDeadzoneDolphin(button));
|
||||
return value;
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -100,3 +88,7 @@ float_t inputButtonGetValueDolphin(const inputbutton_t button) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
float_t inputGetDeadzoneDolphin(const inputbutton_t button) {
|
||||
return 0.2f;
|
||||
}
|
||||
@@ -64,3 +64,11 @@ void inputUpdateDolphin(void);
|
||||
* @return The value of the button, between 0 and 1.
|
||||
*/
|
||||
float_t inputButtonGetValueDolphin(const inputbutton_t button);
|
||||
|
||||
/**
|
||||
* Returns the deadzone for the given button.
|
||||
*
|
||||
* @param button The button to get the deadzone of.
|
||||
* @return The deadzone for the button, between 0 and 1.
|
||||
*/
|
||||
float_t inputGetDeadzoneDolphin(const inputbutton_t button);
|
||||
@@ -183,3 +183,7 @@ inputbuttondata_t INPUT_BUTTON_DATA[] = {
|
||||
|
||||
{ .name = NULL },
|
||||
};
|
||||
|
||||
float_t inputGetDeadzoneSDL2(const inputbutton_t button) {
|
||||
return 0.17f;
|
||||
}
|
||||
@@ -21,10 +21,14 @@ inputbuttondata_t INPUT_BUTTON_DATA[] = {
|
||||
{ .name = "l", { .type = INPUT_BUTTON_TYPE_GAMEPAD, .gpButton = SDL_CONTROLLER_BUTTON_LEFTSHOULDER } },
|
||||
{ .name = "r", { .type = INPUT_BUTTON_TYPE_GAMEPAD, .gpButton = SDL_CONTROLLER_BUTTON_RIGHTSHOULDER } },
|
||||
|
||||
{ .name = "lstick_down", { .type = INPUT_BUTTON_TYPE_GAMEPAD_AXIS, .gpAxis = { .axis = SDL_CONTROLLER_AXIS_LEFTX, .positive = true } } },
|
||||
{ .name = "lstick_up", { .type = INPUT_BUTTON_TYPE_GAMEPAD_AXIS, .gpAxis = { .axis = SDL_CONTROLLER_AXIS_LEFTX, .positive = false } } },
|
||||
{ .name = "lstick_right", { .type = INPUT_BUTTON_TYPE_GAMEPAD_AXIS, .gpAxis = { .axis = SDL_CONTROLLER_AXIS_LEFTY, .positive = true } } },
|
||||
{ .name = "lstick_left", { .type = INPUT_BUTTON_TYPE_GAMEPAD_AXIS, .gpAxis = { .axis = SDL_CONTROLLER_AXIS_LEFTY, .positive = false } } },
|
||||
{ .name = "lstick_down", { .type = INPUT_BUTTON_TYPE_GAMEPAD_AXIS, .gpAxis = { .axis = SDL_CONTROLLER_AXIS_LEFTY, .positive = true } } },
|
||||
{ .name = "lstick_up", { .type = INPUT_BUTTON_TYPE_GAMEPAD_AXIS, .gpAxis = { .axis = SDL_CONTROLLER_AXIS_LEFTY, .positive = false } } },
|
||||
{ .name = "lstick_right", { .type = INPUT_BUTTON_TYPE_GAMEPAD_AXIS, .gpAxis = { .axis = SDL_CONTROLLER_AXIS_LEFTX, .positive = true } } },
|
||||
{ .name = "lstick_left", { .type = INPUT_BUTTON_TYPE_GAMEPAD_AXIS, .gpAxis = { .axis = SDL_CONTROLLER_AXIS_LEFTX, .positive = false } } },
|
||||
|
||||
{ .name = NULL }
|
||||
};
|
||||
|
||||
float_t inputGetDeadzoneSDL2(const inputbutton_t button) {
|
||||
return 0.17f;
|
||||
}
|
||||
@@ -88,10 +88,9 @@ float_t inputButtonGetValueSDL2(const inputbutton_t button) {
|
||||
INPUT.platform.controller, button.gpAxis.axis
|
||||
);
|
||||
value = (float_t)axis / 32767.0f;
|
||||
|
||||
if(!button.gpAxis.positive) value = -value;
|
||||
if(value >= INPUT.deadzone) return value;
|
||||
return 0.0f;
|
||||
value = inputDeadzone(value, inputGetDeadzoneSDL2(button));
|
||||
return value;
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
@@ -49,6 +49,15 @@ typedef struct {
|
||||
*/
|
||||
void inputUpdateSDL2(void);
|
||||
|
||||
/**
|
||||
* Returns the deadzone for the given gamepad axis. Requires implementation by
|
||||
* the host platform.
|
||||
*
|
||||
* @param button The button to get the deadzone of.
|
||||
* @return The deadzone for the given gamepad axis.
|
||||
*/
|
||||
float_t inputGetDeadzoneSDL2(const inputbutton_t button);
|
||||
|
||||
/**
|
||||
* Returns the input value (between 0 and 1) of the given button.
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user