2017-09-05 11:27:24 +02:00
|
|
|
|
using System;
|
|
|
|
|
|
2015-02-08 22:51:52 +01:00
|
|
|
|
namespace DS4Windows
|
2014-03-29 06:56:30 +01:00
|
|
|
|
{
|
|
|
|
|
class MouseCursor
|
|
|
|
|
{
|
|
|
|
|
private readonly int deviceNumber;
|
|
|
|
|
public MouseCursor(int deviceNum)
|
|
|
|
|
{
|
|
|
|
|
deviceNumber = deviceNum;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Keep track of remainders when performing moves or we lose fractional parts.
|
|
|
|
|
private double horizontalRemainder = 0.0, verticalRemainder = 0.0;
|
2015-11-28 06:47:26 +01:00
|
|
|
|
private double hRemainder = 0.0, vRemainder = 0.0;
|
2014-03-31 22:33:16 +02:00
|
|
|
|
/** Indicate x/y direction for doing jitter compensation, etc. */
|
|
|
|
|
public enum Direction { Negative, Neutral, Positive }
|
|
|
|
|
// Track direction vector separately and very trivially for now.
|
2017-07-18 22:37:01 +02:00
|
|
|
|
private Direction horizontalDirection = Direction.Neutral,
|
|
|
|
|
verticalDirection = Direction.Neutral;
|
2015-11-28 06:47:26 +01:00
|
|
|
|
private Direction hDirection = Direction.Neutral, vDirection = Direction.Neutral;
|
|
|
|
|
|
2017-08-07 03:31:55 +02:00
|
|
|
|
private const double GYRO_MOUSE_COEFFICIENT = 0.0095;
|
2019-01-31 17:59:00 +01:00
|
|
|
|
public const int GYRO_MOUSE_DEADZONE = 10;
|
2017-08-07 03:31:55 +02:00
|
|
|
|
private const double GYRO_MOUSE_OFFSET = 0.1463;
|
|
|
|
|
private const double GYRO_SMOOTH_MOUSE_OFFSET = 0.14698;
|
|
|
|
|
private const double TOUCHPAD_MOUSE_OFFSET = 0.015;
|
2017-06-29 06:42:16 +02:00
|
|
|
|
|
|
|
|
|
private const int SMOOTH_BUFFER_LEN = 3;
|
|
|
|
|
private double[] xSmoothBuffer = new double[SMOOTH_BUFFER_LEN];
|
|
|
|
|
private double[] ySmoothBuffer = new double[SMOOTH_BUFFER_LEN];
|
|
|
|
|
private int smoothBufferTail = 0;
|
2019-01-31 17:59:00 +01:00
|
|
|
|
private int gyroCursorDeadZone = GYRO_MOUSE_DEADZONE;
|
|
|
|
|
public int GyroCursorDeadZone { get => gyroCursorDeadZone; set => gyroCursorDeadZone = value; }
|
2017-06-29 06:42:16 +02:00
|
|
|
|
|
2018-01-01 19:21:35 +01:00
|
|
|
|
|
2017-06-29 06:42:16 +02:00
|
|
|
|
double coefficient = 0.0;
|
2017-06-24 11:52:39 +02:00
|
|
|
|
double verticalScale = 0.0;
|
2017-06-29 06:42:16 +02:00
|
|
|
|
bool gyroSmooth = false;
|
2017-07-16 09:22:21 +02:00
|
|
|
|
|
2017-07-13 05:39:46 +02:00
|
|
|
|
int tempInt = 0;
|
2017-07-16 09:22:21 +02:00
|
|
|
|
double tempDouble = 0.0;
|
2017-08-18 09:41:37 +02:00
|
|
|
|
bool tempBool = false;
|
2017-06-24 11:52:39 +02:00
|
|
|
|
|
2015-11-28 06:47:26 +01:00
|
|
|
|
public virtual void sixaxisMoved(SixAxisEventArgs arg)
|
|
|
|
|
{
|
|
|
|
|
int deltaX = 0, deltaY = 0;
|
2017-07-14 14:46:45 +02:00
|
|
|
|
deltaX = Global.getGyroMouseHorizontalAxis(deviceNumber) == 0 ? arg.sixAxis.gyroYawFull :
|
|
|
|
|
arg.sixAxis.gyroRollFull;
|
2017-07-12 15:04:37 +02:00
|
|
|
|
deltaY = -arg.sixAxis.gyroPitchFull;
|
2017-07-26 11:30:50 +02:00
|
|
|
|
//tempDouble = arg.sixAxis.elapsed * 0.001 * 200.0; // Base default speed on 5 ms
|
|
|
|
|
tempDouble = arg.sixAxis.elapsed * 200.0; // Base default speed on 5 ms
|
2015-12-13 22:30:54 +01:00
|
|
|
|
|
2017-06-29 06:42:16 +02:00
|
|
|
|
gyroSmooth = Global.getGyroSmoothing(deviceNumber);
|
|
|
|
|
double gyroSmoothWeight = 0.0;
|
|
|
|
|
|
|
|
|
|
coefficient = (Global.getGyroSensitivity(deviceNumber) * 0.01) * GYRO_MOUSE_COEFFICIENT;
|
|
|
|
|
double offset = GYRO_MOUSE_OFFSET;
|
|
|
|
|
if (gyroSmooth)
|
|
|
|
|
{
|
|
|
|
|
gyroSmoothWeight = Global.getGyroSmoothingWeight(deviceNumber);
|
|
|
|
|
if (gyroSmoothWeight > 0.0)
|
|
|
|
|
{
|
|
|
|
|
offset = GYRO_SMOOTH_MOUSE_OFFSET;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-05 11:27:24 +02:00
|
|
|
|
double tempAngle = Math.Atan2(-deltaY, deltaX);
|
|
|
|
|
double normX = Math.Abs(Math.Cos(tempAngle));
|
|
|
|
|
double normY = Math.Abs(Math.Sin(tempAngle));
|
|
|
|
|
int signX = Math.Sign(deltaX);
|
|
|
|
|
int signY = Math.Sign(deltaY);
|
2017-06-22 03:07:21 +02:00
|
|
|
|
|
2017-07-13 14:27:25 +02:00
|
|
|
|
if (deltaX == 0 || (hRemainder > 0 != deltaX > 0))
|
2017-06-22 03:07:21 +02:00
|
|
|
|
{
|
|
|
|
|
hRemainder = 0.0;
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-13 14:27:25 +02:00
|
|
|
|
if (deltaY == 0 || (vRemainder > 0 != deltaY > 0))
|
2017-06-22 03:07:21 +02:00
|
|
|
|
{
|
|
|
|
|
vRemainder = 0.0;
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-31 17:59:00 +01:00
|
|
|
|
int deadzoneX = (int)Math.Abs(normX * gyroCursorDeadZone);
|
|
|
|
|
int deadzoneY = (int)Math.Abs(normY * gyroCursorDeadZone);
|
2017-06-22 06:18:15 +02:00
|
|
|
|
|
2017-09-05 11:27:24 +02:00
|
|
|
|
if (Math.Abs(deltaX) > deadzoneX)
|
2017-06-22 06:18:15 +02:00
|
|
|
|
{
|
|
|
|
|
deltaX -= signX * deadzoneX;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
deltaX = 0;
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-05 11:27:24 +02:00
|
|
|
|
if (Math.Abs(deltaY) > deadzoneY)
|
2017-06-22 06:18:15 +02:00
|
|
|
|
{
|
|
|
|
|
deltaY -= signY * deadzoneY;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
deltaY = 0;
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-16 09:22:21 +02:00
|
|
|
|
double xMotion = deltaX != 0 ? coefficient * (deltaX * tempDouble)
|
|
|
|
|
+ (normX * (offset * signX)) : 0;
|
|
|
|
|
|
2017-06-22 06:18:15 +02:00
|
|
|
|
int xAction = 0;
|
|
|
|
|
if (xMotion != 0.0)
|
|
|
|
|
{
|
|
|
|
|
xMotion += hRemainder;
|
|
|
|
|
}
|
2017-08-05 05:36:46 +02:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
hRemainder = 0.0;
|
|
|
|
|
}
|
2017-06-22 06:18:15 +02:00
|
|
|
|
|
2017-06-25 04:09:03 +02:00
|
|
|
|
verticalScale = Global.getGyroSensVerticalScale(deviceNumber) * 0.01;
|
2017-07-16 09:22:21 +02:00
|
|
|
|
double yMotion = deltaY != 0 ? (coefficient * verticalScale) * (deltaY * tempDouble)
|
|
|
|
|
+ (normY * (offset * signY)) : 0;
|
|
|
|
|
|
2017-06-22 06:18:15 +02:00
|
|
|
|
int yAction = 0;
|
|
|
|
|
if (yMotion != 0.0)
|
|
|
|
|
{
|
|
|
|
|
yMotion += vRemainder;
|
2017-06-29 06:42:16 +02:00
|
|
|
|
}
|
2017-08-05 05:36:46 +02:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
vRemainder = 0.0;
|
|
|
|
|
}
|
2017-06-29 06:42:16 +02:00
|
|
|
|
|
|
|
|
|
if (gyroSmooth)
|
|
|
|
|
{
|
|
|
|
|
int iIndex = smoothBufferTail % SMOOTH_BUFFER_LEN;
|
|
|
|
|
xSmoothBuffer[iIndex] = xMotion;
|
|
|
|
|
ySmoothBuffer[iIndex] = yMotion;
|
|
|
|
|
smoothBufferTail = iIndex + 1;
|
|
|
|
|
|
|
|
|
|
double currentWeight = 1.0;
|
|
|
|
|
double finalWeight = 0.0;
|
|
|
|
|
double x_out = 0.0, y_out = 0.0;
|
2017-07-16 09:22:21 +02:00
|
|
|
|
int idx = 0;
|
2017-06-29 06:42:16 +02:00
|
|
|
|
for (int i = 0; i < SMOOTH_BUFFER_LEN; i++)
|
|
|
|
|
{
|
2017-09-21 04:39:55 +02:00
|
|
|
|
idx = (smoothBufferTail - i - 1 + SMOOTH_BUFFER_LEN) % SMOOTH_BUFFER_LEN;
|
2017-06-29 06:42:16 +02:00
|
|
|
|
x_out += xSmoothBuffer[idx] * currentWeight;
|
|
|
|
|
y_out += ySmoothBuffer[idx] * currentWeight;
|
|
|
|
|
finalWeight += currentWeight;
|
|
|
|
|
currentWeight *= gyroSmoothWeight;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
x_out /= finalWeight;
|
|
|
|
|
xMotion = x_out;
|
|
|
|
|
y_out /= finalWeight;
|
|
|
|
|
yMotion = y_out;
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-05 05:36:46 +02:00
|
|
|
|
hRemainder = vRemainder = 0.0;
|
2017-06-29 06:42:16 +02:00
|
|
|
|
if (xMotion != 0.0)
|
|
|
|
|
{
|
|
|
|
|
xAction = (int)xMotion;
|
|
|
|
|
hRemainder = xMotion - xAction;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (yMotion != 0.0)
|
|
|
|
|
{
|
2017-06-22 06:18:15 +02:00
|
|
|
|
yAction = (int)yMotion;
|
|
|
|
|
vRemainder = yMotion - yAction;
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-24 11:52:39 +02:00
|
|
|
|
int gyroInvert = Global.getGyroInvert(deviceNumber);
|
2017-07-13 14:27:25 +02:00
|
|
|
|
if ((gyroInvert & 0x02) == 2)
|
2015-11-28 06:47:26 +01:00
|
|
|
|
xAction *= -1;
|
2017-04-16 07:15:54 +02:00
|
|
|
|
|
2017-07-13 14:27:25 +02:00
|
|
|
|
if ((gyroInvert & 0x01) == 1)
|
2015-11-28 06:47:26 +01:00
|
|
|
|
yAction *= -1;
|
2017-04-16 07:15:54 +02:00
|
|
|
|
|
2015-11-28 06:47:26 +01:00
|
|
|
|
if (yAction != 0 || xAction != 0)
|
|
|
|
|
InputMethods.MoveCursorBy(xAction, yAction);
|
|
|
|
|
|
|
|
|
|
hDirection = xMotion > 0.0 ? Direction.Positive : xMotion < 0.0 ? Direction.Negative : Direction.Neutral;
|
|
|
|
|
vDirection = yMotion > 0.0 ? Direction.Positive : yMotion < 0.0 ? Direction.Negative : Direction.Neutral;
|
|
|
|
|
}
|
2014-03-29 06:56:30 +01:00
|
|
|
|
|
2017-06-22 16:36:40 +02:00
|
|
|
|
public void mouseRemainderReset()
|
|
|
|
|
{
|
|
|
|
|
hRemainder = vRemainder = 0.0;
|
2017-06-29 06:42:16 +02:00
|
|
|
|
int iIndex = smoothBufferTail % SMOOTH_BUFFER_LEN;
|
|
|
|
|
xSmoothBuffer[iIndex] = 0.0;
|
|
|
|
|
ySmoothBuffer[iIndex] = 0.0;
|
|
|
|
|
smoothBufferTail = iIndex + 1;
|
2017-06-22 16:36:40 +02:00
|
|
|
|
}
|
|
|
|
|
|
2014-03-29 06:56:30 +01:00
|
|
|
|
public void touchesBegan(TouchpadEventArgs arg)
|
|
|
|
|
{
|
|
|
|
|
if (arg.touches.Length == 1)
|
2014-03-31 22:33:16 +02:00
|
|
|
|
{
|
2014-03-29 06:56:30 +01:00
|
|
|
|
horizontalRemainder = verticalRemainder = 0.0;
|
2014-03-31 22:33:16 +02:00
|
|
|
|
horizontalDirection = verticalDirection = Direction.Neutral;
|
2018-01-01 19:21:35 +01:00
|
|
|
|
|
2014-03-31 22:33:16 +02:00
|
|
|
|
}
|
2014-03-29 06:56:30 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private byte lastTouchID;
|
2017-08-05 05:36:46 +02:00
|
|
|
|
public void touchesMoved(TouchpadEventArgs arg, bool dragging, bool disableInvert = false)
|
2014-03-29 06:56:30 +01:00
|
|
|
|
{
|
2017-04-16 07:15:54 +02:00
|
|
|
|
int touchesLen = arg.touches.Length;
|
|
|
|
|
if ((!dragging && touchesLen != 1) || (dragging && touchesLen < 1))
|
2014-03-29 06:56:30 +01:00
|
|
|
|
return;
|
2017-04-16 07:15:54 +02:00
|
|
|
|
|
2017-08-07 03:31:55 +02:00
|
|
|
|
int deltaX = 0, deltaY = 0;
|
2014-03-29 06:56:30 +01:00
|
|
|
|
if (arg.touches[0].touchID != lastTouchID)
|
|
|
|
|
{
|
|
|
|
|
deltaX = deltaY = 0;
|
|
|
|
|
horizontalRemainder = verticalRemainder = 0.0;
|
2014-03-31 22:33:16 +02:00
|
|
|
|
horizontalDirection = verticalDirection = Direction.Neutral;
|
2014-03-29 06:56:30 +01:00
|
|
|
|
lastTouchID = arg.touches[0].touchID;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2017-04-16 07:15:54 +02:00
|
|
|
|
if (dragging && touchesLen > 1)
|
2015-06-01 21:04:22 +02:00
|
|
|
|
{
|
|
|
|
|
deltaX = arg.touches[1].deltaX;
|
|
|
|
|
deltaY = arg.touches[1].deltaY;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
deltaX = arg.touches[0].deltaX;
|
|
|
|
|
deltaY = arg.touches[0].deltaY;
|
|
|
|
|
}
|
2014-03-29 06:56:30 +01:00
|
|
|
|
}
|
|
|
|
|
|
2018-01-01 19:21:35 +01:00
|
|
|
|
TouchMoveCursor(deltaX, deltaY, disableInvert);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void TouchMoveCursor(int dx, int dy, bool disableInvert = false)
|
|
|
|
|
{
|
|
|
|
|
double tempAngle = Math.Atan2(-dy, dx);
|
2017-09-05 11:27:24 +02:00
|
|
|
|
double normX = Math.Abs(Math.Cos(tempAngle));
|
|
|
|
|
double normY = Math.Abs(Math.Sin(tempAngle));
|
2018-01-01 19:21:35 +01:00
|
|
|
|
int signX = Math.Sign(dx);
|
|
|
|
|
int signY = Math.Sign(dy);
|
2017-08-18 09:41:37 +02:00
|
|
|
|
double coefficient = Global.getTouchSensitivity(deviceNumber) * 0.01;
|
2017-08-19 03:52:11 +02:00
|
|
|
|
bool jitterCompenstation = Global.getTouchpadJitterCompensation(deviceNumber);
|
2017-08-07 03:31:55 +02:00
|
|
|
|
|
2018-01-01 19:21:35 +01:00
|
|
|
|
double xMotion = dx != 0 ?
|
|
|
|
|
coefficient * dx + (normX * (TOUCHPAD_MOUSE_OFFSET * signX)) : 0.0;
|
2017-08-07 03:31:55 +02:00
|
|
|
|
|
2018-01-01 19:21:35 +01:00
|
|
|
|
double yMotion = dy != 0 ?
|
|
|
|
|
coefficient * dy + (normY * (TOUCHPAD_MOUSE_OFFSET * signY)) : 0.0;
|
2017-08-18 09:41:37 +02:00
|
|
|
|
|
2017-08-19 03:52:11 +02:00
|
|
|
|
if (jitterCompenstation)
|
2017-08-18 09:41:37 +02:00
|
|
|
|
{
|
2017-09-05 11:27:24 +02:00
|
|
|
|
double absX = Math.Abs(xMotion);
|
2018-05-20 09:18:28 +02:00
|
|
|
|
if (absX <= normX * 0.15)
|
2017-08-18 09:41:37 +02:00
|
|
|
|
{
|
2018-05-20 09:18:28 +02:00
|
|
|
|
xMotion = signX * Math.Pow(absX / 0.15f, 1.408) * 0.15;
|
2017-08-18 09:41:37 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-09-05 11:27:24 +02:00
|
|
|
|
double absY = Math.Abs(yMotion);
|
2018-05-20 09:18:28 +02:00
|
|
|
|
if (absY <= normY * 0.15)
|
2017-08-18 09:41:37 +02:00
|
|
|
|
{
|
2018-05-20 09:18:28 +02:00
|
|
|
|
yMotion = signY * Math.Pow(absY / 0.15f, 1.408) * 0.15;
|
2017-08-18 09:41:37 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-19 03:52:11 +02:00
|
|
|
|
// Collect rounding errors instead of losing motion.
|
2017-08-07 03:31:55 +02:00
|
|
|
|
if (xMotion > 0.0 && horizontalRemainder > 0.0)
|
2014-03-29 06:56:30 +01:00
|
|
|
|
{
|
2017-08-07 03:31:55 +02:00
|
|
|
|
xMotion += horizontalRemainder;
|
2014-03-29 06:56:30 +01:00
|
|
|
|
}
|
2017-08-07 03:31:55 +02:00
|
|
|
|
else if (xMotion < 0.0 && horizontalRemainder < 0.0)
|
2014-03-29 06:56:30 +01:00
|
|
|
|
{
|
2017-08-07 03:31:55 +02:00
|
|
|
|
xMotion += horizontalRemainder;
|
2014-03-29 06:56:30 +01:00
|
|
|
|
}
|
|
|
|
|
int xAction = (int)xMotion;
|
|
|
|
|
horizontalRemainder = xMotion - xAction;
|
|
|
|
|
|
2017-08-07 03:31:55 +02:00
|
|
|
|
if (yMotion > 0.0 && verticalRemainder > 0.0)
|
2014-03-29 06:56:30 +01:00
|
|
|
|
{
|
2017-08-07 03:31:55 +02:00
|
|
|
|
yMotion += verticalRemainder;
|
2014-03-29 06:56:30 +01:00
|
|
|
|
}
|
2017-08-07 03:31:55 +02:00
|
|
|
|
else if (yMotion < 0.0 && verticalRemainder < 0.0)
|
2014-03-29 06:56:30 +01:00
|
|
|
|
{
|
2017-08-07 03:31:55 +02:00
|
|
|
|
yMotion += verticalRemainder;
|
2014-03-29 06:56:30 +01:00
|
|
|
|
}
|
|
|
|
|
int yAction = (int)yMotion;
|
|
|
|
|
verticalRemainder = yMotion - yAction;
|
|
|
|
|
|
2017-08-05 05:36:46 +02:00
|
|
|
|
if (disableInvert == false)
|
|
|
|
|
{
|
|
|
|
|
int touchpadInvert = tempInt = Global.getTouchpadInvert(deviceNumber);
|
|
|
|
|
if ((touchpadInvert & 0x02) == 2)
|
|
|
|
|
xAction *= -1;
|
2017-07-13 05:39:46 +02:00
|
|
|
|
|
2017-08-05 05:36:46 +02:00
|
|
|
|
if ((touchpadInvert & 0x01) == 1)
|
|
|
|
|
yAction *= -1;
|
|
|
|
|
}
|
2017-07-13 05:39:46 +02:00
|
|
|
|
|
2014-03-29 06:56:30 +01:00
|
|
|
|
if (yAction != 0 || xAction != 0)
|
|
|
|
|
InputMethods.MoveCursorBy(xAction, yAction);
|
2014-03-31 22:33:16 +02:00
|
|
|
|
|
2014-04-27 21:32:09 +02:00
|
|
|
|
horizontalDirection = xMotion > 0.0 ? Direction.Positive : xMotion < 0.0 ? Direction.Negative : Direction.Neutral;
|
|
|
|
|
verticalDirection = yMotion > 0.0 ? Direction.Positive : yMotion < 0.0 ? Direction.Negative : Direction.Neutral;
|
2014-03-29 06:56:30 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|