Skip to content

Commit

Permalink
Merge pull request #12530 from iota97/tilt
Browse files Browse the repository at this point in the history
Allow tilt input on Z instead of X
  • Loading branch information
hrydgard authored Aug 21, 2021
2 parents 0c34c93 + ba98e54 commit 7733d8a
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 13 deletions.
1 change: 1 addition & 0 deletions Core/Config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -969,6 +969,7 @@ static ConfigSetting controlSettings[] = {
#ifdef MOBILE_DEVICE
ConfigSetting("TiltBaseX", &g_Config.fTiltBaseX, 0.0f, true, true),
ConfigSetting("TiltBaseY", &g_Config.fTiltBaseY, 0.0f, true, true),
ConfigSetting("TiltOrientation", &g_Config.iTiltOrientation, 0, true, true),
ConfigSetting("InvertTiltX", &g_Config.bInvertTiltX, false, true, true),
ConfigSetting("InvertTiltY", &g_Config.bInvertTiltY, true, true, true),
ConfigSetting("TiltSensitivityX", &g_Config.iTiltSensitivityX, 100, true, true),
Expand Down
1 change: 1 addition & 0 deletions Core/Config.h
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,7 @@ struct Config {
//the base x and y tilt. this inclination is treated as (0,0) and the tilt input
//considers this orientation to be equal to no movement of the analog stick.
float fTiltBaseX, fTiltBaseY;
int iTiltOrientation;
//whether the x axes and y axes should invert directions (left becomes right, top becomes bottom.)
bool bInvertTiltX, bInvertTiltY;
//the sensitivity of the tilt in the x direction
Expand Down
29 changes: 26 additions & 3 deletions UI/NativeApp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1360,13 +1360,27 @@ bool NativeAxis(const AxisInput &axis) {
// This is static, since we need to remember where we last were (in terms of orientation)
static Tilt currentTilt;

// tilt on x or y?
static bool verticalTilt;
if (g_Config.iTiltOrientation == 0)
verticalTilt = false;
else if (g_Config.iTiltOrientation == 1)
verticalTilt = true;

// x and y are flipped if we are in landscape orientation. The events are
// sent with respect to the portrait coordinate system, while we
// take all events in landscape.
// see [http://developer.android.com/guide/topics/sensors/sensors_overview.html] for details
bool portrait = dp_yres > dp_xres;
switch (axis.axisId) {
//TODO: make this generic.
case JOYSTICK_AXIS_ACCELEROMETER_X:
if (verticalTilt) {
if (fabs(axis.value) < 0.8f && g_Config.iTiltOrientation == 2) // Auto tilt switch
verticalTilt = false;
else
return false; // Tilt on Z instead
}
if (portrait) {
currentTilt.x_ = axis.value;
} else {
Expand All @@ -1383,9 +1397,18 @@ bool NativeAxis(const AxisInput &axis) {
break;

case JOYSTICK_AXIS_ACCELEROMETER_Z:
//don't handle this now as only landscape is enabled.
//TODO: make this generic.
return false;
if (!verticalTilt) {
if (fabs(axis.value) < 0.8f && g_Config.iTiltOrientation == 2) // Auto tilt switch
verticalTilt = true;
else
return false; // Tilt on X instead
}
if (portrait) {
currentTilt.x_ = -axis.value;
} else {
currentTilt.y_ = -axis.value;
}
break;

case JOYSTICK_AXIS_OUYA_UNKNOWN1:
case JOYSTICK_AXIS_OUYA_UNKNOWN2:
Expand Down
2 changes: 2 additions & 0 deletions UI/TiltAnalogSettingsScreen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ void TiltAnalogSettingsScreen::CreateViews() {
settings->Add(new ItemHeader(co->T("Invert Axes")));
settings->Add(new CheckBox(&g_Config.bInvertTiltX, co->T("Invert Tilt along X axis")));
settings->Add(new CheckBox(&g_Config.bInvertTiltY, co->T("Invert Tilt along Y axis")));
static const char* tiltMode[] = { "Screen aligned to ground", "Screen at right angle to ground", "Auto-switch" };
settings->Add(new PopupMultiChoice(&g_Config.iTiltOrientation, co->T("Base tilt position"), tiltMode, 0, ARRAY_SIZE(tiltMode), co->GetName(), screenManager()));

settings->Add(new ItemHeader(co->T("Sensitivity")));
//TODO: allow values greater than 100? I'm not sure if that's needed.
Expand Down
24 changes: 14 additions & 10 deletions UI/TiltEventProcessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,12 @@ void TiltEventProcessor::GenerateDPadEvent(const Tilt &tilt) {
static const int dir[4] = {CTRL_RIGHT, CTRL_DOWN, CTRL_LEFT, CTRL_UP};

if (tilt.x_ == 0) {
__CtrlButtonUp(CTRL_RIGHT | CTRL_LEFT);
__CtrlButtonUp(tiltButtonsDown & (CTRL_RIGHT | CTRL_LEFT));
tiltButtonsDown &= ~(CTRL_LEFT | CTRL_RIGHT);
}

if (tilt.y_ == 0) {
__CtrlButtonUp(CTRL_UP | CTRL_DOWN);
__CtrlButtonUp(tiltButtonsDown & (CTRL_UP | CTRL_DOWN));
tiltButtonsDown &= ~(CTRL_UP | CTRL_DOWN);
}

Expand All @@ -129,7 +129,7 @@ void TiltEventProcessor::GenerateDPadEvent(const Tilt &tilt) {
case 6: ctrlMask |= CTRL_UP; break;
case 7: ctrlMask |= CTRL_UP | CTRL_RIGHT; break;
}

ctrlMask &= ~__CtrlPeekButtons();
__CtrlButtonDown(ctrlMask);
tiltButtonsDown |= ctrlMask;
}
Expand All @@ -138,12 +138,12 @@ void TiltEventProcessor::GenerateActionButtonEvent(const Tilt &tilt) {
static const int buttons[4] = {CTRL_CIRCLE, CTRL_CROSS, CTRL_SQUARE, CTRL_TRIANGLE};

if (tilt.x_ == 0) {
__CtrlButtonUp(CTRL_SQUARE | CTRL_CIRCLE);
__CtrlButtonUp(tiltButtonsDown & (CTRL_SQUARE | CTRL_CIRCLE));
tiltButtonsDown &= ~(CTRL_SQUARE | CTRL_CIRCLE);
}

if (tilt.y_ == 0) {
__CtrlButtonUp(CTRL_TRIANGLE | CTRL_CROSS);
__CtrlButtonUp(tiltButtonsDown & (CTRL_TRIANGLE | CTRL_CROSS));
tiltButtonsDown &= ~(CTRL_TRIANGLE | CTRL_CROSS);
}

Expand All @@ -152,15 +152,18 @@ void TiltEventProcessor::GenerateActionButtonEvent(const Tilt &tilt) {
}

int direction = (int)(floorf((atan2f(tilt.y_, tilt.x_) / (2.0f * (float)M_PI) * 4.0f) + 0.5f)) & 3;
__CtrlButtonDown(buttons[direction]);
tiltButtonsDown |= buttons[direction];
int downButtons = buttons[direction] & ~__CtrlPeekButtons();
__CtrlButtonDown(downButtons);
tiltButtonsDown |= downButtons;
}

void TiltEventProcessor::GenerateTriggerButtonEvent(const Tilt &tilt) {
u32 upButtons = 0;
u32 downButtons = 0;
// KISS, let's only look at X. Expect deadzone to already be applied.
if (tilt.x_ == 0.0f) {
// Y axis for both
if (tilt.y_ < 0.0f) {
downButtons = CTRL_LTRIGGER | CTRL_RTRIGGER;
} else if (tilt.x_ == 0.0f) {
upButtons = CTRL_LTRIGGER | CTRL_RTRIGGER;
} else if (tilt.x_ < 0.0f) {
downButtons = CTRL_LTRIGGER;
Expand All @@ -170,7 +173,8 @@ void TiltEventProcessor::GenerateTriggerButtonEvent(const Tilt &tilt) {
upButtons = CTRL_LTRIGGER;
}

__CtrlButtonUp(upButtons);
downButtons &= ~__CtrlPeekButtons();
__CtrlButtonUp(tiltButtonsDown & upButtons);
__CtrlButtonDown(downButtons);
tiltButtonsDown = (tiltButtonsDown & ~upButtons) | downButtons;
}
Expand Down

0 comments on commit 7733d8a

Please sign in to comment.