Get joystick nonlinear parameters from user defaults
This commit is contained in:
parent
5d93530a90
commit
ebcdbca0fb
@ -101,16 +101,7 @@ enum {
|
||||
#define STICK_NOFUNCTION -1
|
||||
#define STICK_AXISUNASSIGNED -10.0
|
||||
|
||||
// Nonlinear options:
|
||||
// 0: normal gameplay
|
||||
// 1: regular normal mode, nonlinear precision mode
|
||||
// 2: nonlinear normal mode, regular precision mode
|
||||
// 3: nonlinear both modes, with cubic transform for normal, quintic transform for precision
|
||||
#define STICK_NONLINEAR_OPTION 1
|
||||
#define STICK_PRECISIONFAC 3
|
||||
//STICK_NONLINEAR1 needs to be a double between 0 and 1
|
||||
#define STICK_NONLINEAR1 0.2
|
||||
#define STICK_NONLINEAR2 (1.0-STICK_NONLINEAR1)
|
||||
#define STICK_NORMALDIV 32768
|
||||
#define STICK_PRECISIONDIV (STICK_PRECISIONFAC*STICK_NORMALDIV)
|
||||
|
||||
@ -137,6 +128,8 @@ enum {
|
||||
#define STICK_AXBUT @"stickAxBt" // Axis or button number
|
||||
#define STICK_FUNCTION @"stickFunc" // Function of axis/button
|
||||
#define STICK_DEADZONE_SETTING @"JoystickAxesDeadzone" // Deadzone setting double 0.0 to 1.0.
|
||||
#define STICK_PRECISION_SETTING @"JoystickPrecision" // Precision mode
|
||||
#define STICK_NONLINEAR_PARAMETER @"JoystickNonlinear" // Nonlinear parameter double from 0.0 to 1.0
|
||||
// shortcut to make code more readable when using enum as key for
|
||||
// an NSDictionary
|
||||
#define ENUMKEY(x) [NSString stringWithFormat: @"%d", x]
|
||||
@ -243,6 +236,11 @@ typedef struct
|
||||
char cbHardware;
|
||||
BOOL invertPitch;
|
||||
double deadzone;
|
||||
|
||||
// parameter for nonlinear settings. This is a double between 0.0 and 1.0. 1.0 - nonlinear_parameter is the
|
||||
// gradient of the transform function at zero. 0.0 means the gradient is 1.0, which makes the stick linear.
|
||||
// Higher values reduce the stick response for more accuracy at the centre.
|
||||
double nonlinear_parameter;
|
||||
}
|
||||
|
||||
+ (id) sharedStickHandler;
|
||||
|
@ -87,9 +87,7 @@ static id sSharedStickHandler = nil;
|
||||
// axes and buttons are set to unassigned (STICK_NOFUNCTION).
|
||||
[self loadStickSettings];
|
||||
|
||||
precisionMode = NO;
|
||||
invertPitch = NO;
|
||||
deadzone = STICK_DEADZONE;
|
||||
}
|
||||
return self;
|
||||
}
|
||||
@ -162,48 +160,19 @@ static id sSharedStickHandler = nil;
|
||||
- (double) axisTransform: (double)axisvalue;
|
||||
{
|
||||
if (fabs(axisvalue) < deadzone) return 0.0;
|
||||
|
||||
#if STICK_NONLINEAR_OPTION == 0
|
||||
if (precisionMode)
|
||||
{
|
||||
return axisvalue / STICK_PRECISIONFAC;
|
||||
}
|
||||
return axisvalue;
|
||||
#endif
|
||||
|
||||
#if STICK_NONLINEAR_OPTION == 1
|
||||
if (precisionMode)
|
||||
{
|
||||
// since we're mucking around with nonlinear stuff, we may as well throw in a smooth transition from deadzone to non-deadzone
|
||||
if (axisvalue < 0.0) axisvalue = -(-axisvalue - deadzone)/(1 - deadzone);
|
||||
else axisvalue = (axisvalue - deadzone)/(1 - deadzone);
|
||||
return axisvalue*(STICK_NONLINEAR1 + STICK_NONLINEAR2*axisvalue*axisvalue);
|
||||
}
|
||||
return axisvalue;
|
||||
#endif
|
||||
|
||||
#if STICK_NONLINEAR_OPTION == 2
|
||||
if (precisionMode)
|
||||
{
|
||||
return axisvalue / STICK_PRECISIONFAC;
|
||||
}
|
||||
if (axisvalue < 0.0) axisvalue = -(-axisvalue - deadzone)/(1 - deadzone);
|
||||
else axisvalue = (axisvalue - deadzone)/(1 - deadzone);
|
||||
return axisvalue*(STICK_NONLINEAR1 + STICK_NONLINEAR2*axisvalue*axisvalue);
|
||||
#endif
|
||||
|
||||
#if STICK_NONLINEAR_OPTION == 3
|
||||
|
||||
// since we're mucking around with nonlinear stuff, we may as well throw in a smooth transition
|
||||
// from deadzone to non-deadzone
|
||||
if (axisvalue < 0.0) axisvalue = -(-axisvalue - deadzone)/(1 - deadzone);
|
||||
else axisvalue = (axisvalue - deadzone)/(1 - deadzone);
|
||||
if (precisionMode)
|
||||
{
|
||||
return axisvalue*STICK_NONLINEAR1 + STICK_NONLINEAR2*pow(axisvalue,5);
|
||||
return axisvalue*((1.0-nonlinear_parameter) + nonlinear_parameter*axisvalue*axisvalue);
|
||||
}
|
||||
return axisvalue*(STICK_NONLINEAR1 + axisvalue*axisvalue*STICK_NONLINEAR2);
|
||||
#endif
|
||||
|
||||
return axisvalue;
|
||||
}
|
||||
|
||||
|
||||
- (NSArray *)listSticks
|
||||
{
|
||||
NSUInteger i, stickCount = [self joystickCount];
|
||||
@ -533,6 +502,7 @@ static id sSharedStickHandler = nil;
|
||||
bs = YES;
|
||||
if(function == BUTTON_PRECISION)
|
||||
precisionMode = !precisionMode;
|
||||
[[NSUserDefaults standardUserDefaults] setBool: precisionMode forKey: STICK_PRECISION_SETTING];
|
||||
}
|
||||
|
||||
if (function >= 0)
|
||||
@ -579,7 +549,9 @@ static id sSharedStickHandler = nil;
|
||||
forKey:AXIS_SETTINGS];
|
||||
[defaults setObject:[self buttonFunctions]
|
||||
forKey:BUTTON_SETTINGS];
|
||||
[defaults setFloat: deadzone forKey: STICK_DEADZONE_SETTING];
|
||||
[defaults setDouble: deadzone forKey: STICK_DEADZONE_SETTING];
|
||||
[defaults setDouble: nonlinear_parameter forKey: STICK_NONLINEAR_PARAMETER];
|
||||
[defaults setBool: !!precisionMode forKey: STICK_PRECISION_SETTING];
|
||||
[defaults synchronize];
|
||||
}
|
||||
|
||||
@ -621,6 +593,16 @@ static id sSharedStickHandler = nil;
|
||||
{
|
||||
deadzone = STICK_DEADZONE;
|
||||
}
|
||||
nonlinear_parameter = [defaults oo_doubleForKey: STICK_NONLINEAR_PARAMETER defaultValue: 1.0];
|
||||
if (nonlinear_parameter < 0.0)
|
||||
{
|
||||
nonlinear_parameter = 0.0;
|
||||
}
|
||||
if (nonlinear_parameter > 1.0)
|
||||
{
|
||||
nonlinear_parameter = 1.0;
|
||||
}
|
||||
precisionMode = [defaults oo_boolForKey: STICK_PRECISION_SETTING defaultValue:NO];
|
||||
}
|
||||
|
||||
// These get overidden by subclasses
|
||||
|
Loading…
x
Reference in New Issue
Block a user