mirror of
https://github.com/hrydgard/ppsspp.git
synced 2024-10-07 02:43:32 +00:00
Move the mouse event processing code out from NativeApp
This commit is contained in:
parent
812b1024f0
commit
7b0ee5440d
@ -2,11 +2,13 @@
|
||||
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
#include <mutex>
|
||||
|
||||
#include "Common/Math/math_util.h"
|
||||
#include "Common/Math/lin/vec3.h"
|
||||
#include "Common/Math/lin/matrix4x4.h"
|
||||
#include "Common/Log.h"
|
||||
#include "Common/System/Display.h"
|
||||
|
||||
#include "Core/Config.h"
|
||||
#include "Core/ConfigValues.h"
|
||||
@ -301,3 +303,46 @@ void ResetTiltEvents() {
|
||||
}
|
||||
|
||||
} // namespace TiltEventProcessor
|
||||
|
||||
namespace MouseEventProcessor {
|
||||
|
||||
// Technically, we may be OK without a mutex here.
|
||||
// But, the cost isn't high.
|
||||
std::mutex g_mouseMutex;
|
||||
|
||||
float g_mouseDeltaX = 0;
|
||||
float g_mouseDeltaY = 0;
|
||||
|
||||
void ProcessDelta(float dx, float dy) {
|
||||
std::unique_lock<std::mutex> lock(g_mouseMutex);
|
||||
// Accumulate mouse deltas, for some kind of smoothing.
|
||||
g_mouseDeltaX += dx;
|
||||
g_mouseDeltaY += dy;
|
||||
}
|
||||
|
||||
void MouseDeltaToAxes(double now, float *mx, float *my) {
|
||||
std::unique_lock<std::mutex> lock(g_mouseMutex);
|
||||
|
||||
static double lastTime = 0.0f;
|
||||
if (lastTime == 0.0) {
|
||||
lastTime = now;
|
||||
*mx = 0.0f;
|
||||
*my = 0.0f;
|
||||
return;
|
||||
}
|
||||
double dt = now - lastTime;
|
||||
lastTime = now;
|
||||
|
||||
float scaleFactor_x = g_display.dpi_scale_x * 0.1 * g_Config.fMouseSensitivity;
|
||||
float scaleFactor_y = g_display.dpi_scale_y * 0.1 * g_Config.fMouseSensitivity;
|
||||
|
||||
*mx = clamp_value(g_mouseDeltaX * scaleFactor_x, -1.0f, 1.0f);
|
||||
*my = clamp_value(g_mouseDeltaY * scaleFactor_y, -1.0f, 1.0f);
|
||||
|
||||
// Decay the mouse deltas. This is where we should use dt.
|
||||
float decay = expf(-dt * 50.0f * (1.0f - g_Config.fMouseSmoothing));
|
||||
g_mouseDeltaX *= decay;
|
||||
g_mouseDeltaY *= decay;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
@ -16,3 +16,10 @@ extern float rawTiltAnalogX;
|
||||
extern float rawTiltAnalogY;
|
||||
|
||||
} // namespace
|
||||
|
||||
namespace MouseEventProcessor {
|
||||
|
||||
void ProcessDelta(float dx, float dy);
|
||||
void MouseDeltaToAxes(double now, float *mx, float *my);
|
||||
|
||||
} // namespace
|
||||
|
@ -1324,40 +1324,18 @@ void NativeAxis(const AxisInput *axes, size_t count) {
|
||||
}
|
||||
}
|
||||
|
||||
float g_mouseDeltaX = 0;
|
||||
float g_mouseDeltaY = 0;
|
||||
|
||||
void NativeMouseDelta(float dx, float dy) {
|
||||
// Remap, shared code. Then send it as a regular axis event.
|
||||
if (!g_Config.bMouseControl)
|
||||
return;
|
||||
|
||||
// Accumulate mouse deltas, for some kind of smoothing.
|
||||
g_mouseDeltaX += dx;
|
||||
g_mouseDeltaY += dy;
|
||||
MouseEventProcessor::ProcessDelta(dx, dy);
|
||||
}
|
||||
|
||||
// Called from NativeFrame.
|
||||
static void SendMouseDeltaAxis() {
|
||||
static double lastTime = 0.0f;
|
||||
double now = time_now_d();
|
||||
if (lastTime == 0.0) {
|
||||
lastTime = now;
|
||||
return;
|
||||
}
|
||||
double dt = now - lastTime;
|
||||
lastTime = now;
|
||||
|
||||
float scaleFactor_x = g_display.dpi_scale_x * 0.1 * g_Config.fMouseSensitivity;
|
||||
float scaleFactor_y = g_display.dpi_scale_y * 0.1 * g_Config.fMouseSensitivity;
|
||||
|
||||
float mx = clamp_value(g_mouseDeltaX * scaleFactor_x, -1.0f, 1.0f);
|
||||
float my = clamp_value(g_mouseDeltaY * scaleFactor_y, -1.0f, 1.0f);
|
||||
|
||||
// Decay the mouse deltas. This is where we should use dt.
|
||||
float decay = expf(-dt * 50.0f * (1.0f - g_Config.fMouseSmoothing));
|
||||
g_mouseDeltaX *= decay;
|
||||
g_mouseDeltaY *= decay;
|
||||
float mx, my;
|
||||
MouseEventProcessor::MouseDeltaToAxes(time_now_d(), &mx, &my);
|
||||
|
||||
AxisInput axis[2];
|
||||
axis[0].axisId = JOYSTICK_AXIS_MOUSE_REL_X;
|
||||
|
Loading…
Reference in New Issue
Block a user