Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tilt improvements: Add visualizer, better defaults #16889

Merged
merged 7 commits into from
Feb 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1283,14 +1283,14 @@ list(APPEND NativeAppSource
UI/GPUDriverTestScreen.cpp
UI/TiltAnalogSettingsScreen.h
UI/TiltAnalogSettingsScreen.cpp
UI/TiltEventProcessor.h
UI/TiltEventProcessor.cpp
UI/TouchControlLayoutScreen.h
UI/TouchControlLayoutScreen.cpp
UI/TouchControlVisibilityScreen.h
UI/TouchControlVisibilityScreen.cpp
UI/GamepadEmu.h
UI/GamepadEmu.cpp
UI/JoystickHistoryView.h
UI/JoystickHistoryView.cpp
UI/OnScreenDisplay.h
UI/OnScreenDisplay.cpp
UI/ControlMappingScreen.h
Expand Down Expand Up @@ -1704,6 +1704,8 @@ add_library(${CoreLibName} ${CoreLinkType}
Core/KeyMapDefaults.cpp
Core/KeyMapDefaults.h
Core/ThreadEventQueue.h
Core/TiltEventProcessor.h
Core/TiltEventProcessor.cpp
Core/WebServer.cpp
Core/WebServer.h
Core/Debugger/Breakpoints.cpp
Expand Down
6 changes: 0 additions & 6 deletions Common/Input/KeyCodes.h
Original file line number Diff line number Diff line change
Expand Up @@ -321,12 +321,6 @@ enum AndroidJoystickAxis {
JOYSTICK_AXIS_MOUSE_REL_X = 26,
JOYSTICK_AXIS_MOUSE_REL_Y = 27,

// Getting these on Ouya, no clue what they are.
JOYSTICK_AXIS_OUYA_UNKNOWN1 = 32,
JOYSTICK_AXIS_OUYA_UNKNOWN2 = 33,
JOYSTICK_AXIS_OUYA_UNKNOWN3 = 34,
JOYSTICK_AXIS_OUYA_UNKNOWN4 = 35,

// Mobile device accelerometer/gyro
JOYSTICK_AXIS_ACCELEROMETER_X = 40,
JOYSTICK_AXIS_ACCELEROMETER_Y = 41,
Expand Down
6 changes: 4 additions & 2 deletions Common/UI/UIScreen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -231,8 +231,10 @@ void PopupScreen::touch(const TouchInput &touch) {
UIDialogScreen::touch(touch);
}

if (!box_->GetBounds().Contains(touch.x, touch.y)) {
TriggerFinish(DR_BACK);
// Extra bounds to avoid closing the dialog while trying to aim for something
// near the edge.
if (!box_->GetBounds().Expand(100.0f, 60.0f).Contains(touch.x, touch.y)) {
TriggerFinish(DR_CANCEL);
}

UIDialogScreen::touch(touch);
Expand Down
6 changes: 3 additions & 3 deletions Core/Config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -975,9 +975,9 @@ static const ConfigSetting controlSettings[] = {
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),
ConfigSetting("TiltSensitivityY", &g_Config.iTiltSensitivityY, 100, true, true),
ConfigSetting("DeadzoneRadius", &g_Config.fDeadzoneRadius, 0.2f, true, true),
ConfigSetting("TiltSensitivityX", &g_Config.iTiltSensitivityX, 50, true, true),
ConfigSetting("TiltSensitivityY", &g_Config.iTiltSensitivityY, 50, true, true),
ConfigSetting("DeadzoneRadius", &g_Config.fDeadzoneRadius, 0.05f, true, true),
ConfigSetting("TiltDeadzoneSkip", &g_Config.fTiltDeadzoneSkip, 0.0f, true, true),
ConfigSetting("TiltInputType", &g_Config.iTiltInputType, 0, true, true),
#endif
Expand Down
2 changes: 2 additions & 0 deletions Core/Core.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -1041,6 +1041,7 @@
<ClCompile Include="Screenshot.cpp" />
<ClCompile Include="System.cpp" />
<ClCompile Include="ThreadPools.cpp" />
<ClCompile Include="TiltEventProcessor.cpp" />
<ClCompile Include="Util\AudioFormat.cpp" />
<ClCompile Include="Util\BlockAllocator.cpp" />
<ClCompile Include="Util\DisArm64.cpp" />
Expand Down Expand Up @@ -1397,6 +1398,7 @@
<ClInclude Include="System.h" />
<ClInclude Include="ThreadEventQueue.h" />
<ClInclude Include="ThreadPools.h" />
<ClInclude Include="TiltEventProcessor.h" />
<ClInclude Include="Util\AudioFormat.h" />
<ClInclude Include="Util\BlockAllocator.h" />
<ClInclude Include="Util\DisArm64.h" />
Expand Down
6 changes: 6 additions & 0 deletions Core/Core.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -1195,6 +1195,9 @@
<ClCompile Include="..\ext\riscv-disas.cpp">
<Filter>Ext</Filter>
</ClCompile>
<ClCompile Include="TiltEventProcessor.cpp">
<Filter>Core</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="ELF\ElfReader.h">
Expand Down Expand Up @@ -1932,6 +1935,9 @@
<ClInclude Include="..\ext\riscv-disas.h">
<Filter>Ext</Filter>
</ClInclude>
<ClInclude Include="TiltEventProcessor.h">
<Filter>Core</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<None Include="..\LICENSE.TXT" />
Expand Down
59 changes: 30 additions & 29 deletions UI/TiltEventProcessor.cpp → Core/TiltEventProcessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,17 @@

#include "Core/Config.h"
#include "Core/HLE/sceCtrl.h"
#include "UI/TiltEventProcessor.h"
#include "Core/TiltEventProcessor.h"

using namespace TiltEventProcessor;
namespace TiltEventProcessor {

static u32 tiltButtonsDown = 0;
static bool tiltAnalogSet = false;
float rawTiltAnalogX;
float rawTiltAnalogY;

//deadzone is normalized - 0 to 1
//sensitivity controls how fast the deadzone reaches max value
inline float tiltInputCurve (float x, float deadzone, float sensitivity) {
inline float tiltInputCurve(float x, float deadzone, float sensitivity) {
const float factor = sensitivity * 1.0f / (1.0f - deadzone);

if (x > deadzone) {
Expand All @@ -37,22 +38,21 @@ inline float clamp(float f) {
if (f > 1.0f) return 1.0f;
if (f < -1.0f) return -1.0f;
return f;
}
}

Tilt TiltEventProcessor::NormalizeTilt(const Tilt &tilt){
Tilt NormalizeTilt(const Tilt &tilt) {
// Normalise the accelerometer manually per-platform, to 'g'
#if defined(__ANDROID__)
// Values are in metres per second. Divide by 9.8 to get 'g' value
float maxX = 9.8f, maxY = 9.8f;
#else
float maxX = 1.0f, maxY = 1.0f;
#endif
#if defined(__ANDROID__)
// Values are in metres per second. Divide by 9.8 to get 'g' value
float maxX = 9.8f, maxY = 9.8f;
#else
float maxX = 1.0f, maxY = 1.0f;
#endif

return Tilt(tilt.x_ / maxX, tilt.y_ / maxY);

}

Tilt TiltEventProcessor::GenTilt(const Tilt &baseTilt, const Tilt &currentTilt, bool invertX, bool invertY, float deadzone, float xSensitivity, float ySensitivity) {
Tilt GenTilt(const Tilt &baseTilt, const Tilt &currentTilt, bool invertX, bool invertY, float deadzone, float xSensitivity, float ySensitivity) {
//first convert to the correct coordinate system
Tilt transformedTilt(currentTilt.x_ - baseTilt.x_, currentTilt.y_ - baseTilt.y_);

Expand All @@ -67,12 +67,15 @@ Tilt TiltEventProcessor::GenTilt(const Tilt &baseTilt, const Tilt &currentTilt,

//next, normalize the tilt values
transformedTilt = NormalizeTilt(transformedTilt);

//finally, dampen the tilt according to our curve.
return dampTilt(transformedTilt, deadzone, xSensitivity, ySensitivity);
}

void TiltEventProcessor::TranslateTiltToInput(const Tilt &tilt) {
void TranslateTiltToInput(const Tilt &tilt) {
rawTiltAnalogX = tilt.x_;
rawTiltAnalogY = tilt.y_;

switch (g_Config.iTiltInputType) {
case TILT_NULL:
break;
Expand All @@ -95,13 +98,12 @@ void TiltEventProcessor::TranslateTiltToInput(const Tilt &tilt) {
}
}

void TiltEventProcessor::GenerateAnalogStickEvent(const Tilt &tilt) {
void GenerateAnalogStickEvent(const Tilt &tilt) {
__CtrlSetAnalogXY(CTRL_STICK_LEFT, clamp(tilt.x_), clamp(tilt.y_));
tiltAnalogSet = true;
}

void TiltEventProcessor::GenerateDPadEvent(const Tilt &tilt) {
static const int dir[4] = {CTRL_RIGHT, CTRL_DOWN, CTRL_LEFT, CTRL_UP};
void GenerateDPadEvent(const Tilt &tilt) {
static const int dir[4] = { CTRL_RIGHT, CTRL_DOWN, CTRL_LEFT, CTRL_UP };

if (tilt.x_ == 0) {
__CtrlButtonUp(tiltButtonsDown & (CTRL_RIGHT | CTRL_LEFT));
Expand Down Expand Up @@ -129,13 +131,14 @@ 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;
}

void TiltEventProcessor::GenerateActionButtonEvent(const Tilt &tilt) {
static const int buttons[4] = {CTRL_CIRCLE, CTRL_CROSS, CTRL_SQUARE, CTRL_TRIANGLE};
void GenerateActionButtonEvent(const Tilt &tilt) {
static const int buttons[4] = { CTRL_CIRCLE, CTRL_CROSS, CTRL_SQUARE, CTRL_TRIANGLE };

if (tilt.x_ == 0) {
__CtrlButtonUp(tiltButtonsDown & (CTRL_SQUARE | CTRL_CIRCLE));
Expand All @@ -157,7 +160,7 @@ void TiltEventProcessor::GenerateActionButtonEvent(const Tilt &tilt) {
tiltButtonsDown |= downButtons;
}

void TiltEventProcessor::GenerateTriggerButtonEvent(const Tilt &tilt) {
void GenerateTriggerButtonEvent(const Tilt &tilt) {
u32 upButtons = 0;
u32 downButtons = 0;
// Y axis for both
Expand All @@ -179,13 +182,11 @@ void TiltEventProcessor::GenerateTriggerButtonEvent(const Tilt &tilt) {
tiltButtonsDown = (tiltButtonsDown & ~upButtons) | downButtons;
}

void TiltEventProcessor::ResetTiltEvents() {
void ResetTiltEvents() {
// Reset the buttons we have marked pressed.
__CtrlButtonUp(tiltButtonsDown);
tiltButtonsDown = 0;

if (tiltAnalogSet) {
__CtrlSetAnalogXY(CTRL_STICK_LEFT, 0.0f, 0.0f);
tiltAnalogSet = false;
}
__CtrlSetAnalogXY(CTRL_STICK_LEFT, 0.0f, 0.0f);
}

} // namespace TiltEventProcessor
46 changes: 46 additions & 0 deletions Core/TiltEventProcessor.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#pragma once

namespace TiltEventProcessor {

enum TiltTypes{
TILT_NULL = 0,
TILT_ANALOG,
TILT_DPAD,
TILT_ACTION_BUTTON,
TILT_TRIGGER_BUTTON,
};


//Represents a generic Tilt event
struct Tilt {
Tilt() : x_(0), y_(0) {}
Tilt(const float x, const float y) : x_(x), y_(y) {}

float x_, y_;
};


Tilt NormalizeTilt(const Tilt &tilt);

// generates a tilt in the correct coordinate system based on
// calibration. BaseTilt is the "base" / "zero" tilt. currentTilt is the
// sensor tilt reading at this moment.
// NOTE- both base and current tilt *MUST BE NORMALIZED* by calling the NormalizeTilt() function.
Tilt GenTilt(const Tilt &baseTilt, const Tilt &currentTilt, bool invertX, bool invertY, float deadzone, float xSensitivity, float ySensitivity);

void TranslateTiltToInput(const Tilt &tilt);

// These functions generate tilt events given the current Tilt amount,
// and the deadzone radius.
void GenerateAnalogStickEvent(const Tilt &tilt);
void GenerateDPadEvent(const Tilt &tilt);
void GenerateActionButtonEvent(const Tilt &tilt);
void GenerateTriggerButtonEvent(const Tilt &tilt);

void ResetTiltEvents();

// Lets you preview the amount of tilt in TiltAnalogSettingsScreen.
extern float rawTiltAnalogX;
extern float rawTiltAnalogY;

} // namespace
28 changes: 14 additions & 14 deletions Tools/langtool/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading