public void handleTouchpad(byte[] data, State sensors, int touchPacketOffset = 0) { var touchPadIsDown = sensors.TouchButton; if (!PacketChanged(data, touchPacketOffset) && touchPadIsDown == lastTouchPadIsDown) { TouchUnchanged?.Invoke(this, EventArgs.Empty); return; } var touchID1 = (byte)(data[0 + TOUCHPAD_DATA_OFFSET + touchPacketOffset] & 0x7F); var touchID2 = (byte)(data[4 + TOUCHPAD_DATA_OFFSET + touchPacketOffset] & 0x7F); var currentX1 = data[1 + TOUCHPAD_DATA_OFFSET + touchPacketOffset] + (data[2 + TOUCHPAD_DATA_OFFSET + touchPacketOffset] & 0xF) * 255; var currentY1 = ((data[2 + TOUCHPAD_DATA_OFFSET + touchPacketOffset] & 0xF0) >> 4) + data[3 + TOUCHPAD_DATA_OFFSET + touchPacketOffset] * 16; var currentX2 = data[5 + TOUCHPAD_DATA_OFFSET + touchPacketOffset] + (data[6 + TOUCHPAD_DATA_OFFSET + touchPacketOffset] & 0xF) * 255; var currentY2 = ((data[6 + TOUCHPAD_DATA_OFFSET + touchPacketOffset] & 0xF0) >> 4) + data[7 + TOUCHPAD_DATA_OFFSET + touchPacketOffset] * 16; TouchpadEventArgs args; if (sensors.Touch1 || sensors.Touch2) { if (sensors.Touch1 && !lastIsActive1 || sensors.Touch2 && !lastIsActive2) { if (TouchesBegan != null) { if (sensors.Touch1 && sensors.Touch2) { args = new TouchpadEventArgs(sensors.ReportTimeStamp, sensors.TouchButton, new TouchReadings(currentX1, currentY1, touchID1), new TouchReadings(currentX2, currentY2, touchID2)); } else if (sensors.Touch1) { args = new TouchpadEventArgs(sensors.ReportTimeStamp, sensors.TouchButton, new TouchReadings(currentX1, currentY1, touchID1)); } else { args = new TouchpadEventArgs(sensors.ReportTimeStamp, sensors.TouchButton, new TouchReadings(currentX2, currentY2, touchID2)); } TouchesBegan(this, args); } } else if (sensors.Touch1 == lastIsActive1 && sensors.Touch2 == lastIsActive2 && TouchesMoved != null) { TouchReadings tPrev, t0, t1; if (sensors.Touch1 && sensors.Touch2) { tPrev = new TouchReadings(lastTouchPadX1, lastTouchPadY1, lastTouchID1); t0 = new TouchReadings(currentX1, currentY1, touchID1, tPrev); tPrev = new TouchReadings(lastTouchPadX2, lastTouchPadY2, lastTouchID2); t1 = new TouchReadings(currentX2, currentY2, touchID2, tPrev); } else if (sensors.Touch1) { tPrev = new TouchReadings(lastTouchPadX1, lastTouchPadY1, lastTouchID1); t0 = new TouchReadings(currentX1, currentY1, touchID1, tPrev); t1 = null; } else { tPrev = new TouchReadings(lastTouchPadX2, lastTouchPadY2, lastTouchID2); t0 = new TouchReadings(currentX2, currentY2, touchID2, tPrev); t1 = null; } args = new TouchpadEventArgs(sensors.ReportTimeStamp, sensors.TouchButton, t0, t1); TouchesMoved(this, args); } if (!lastTouchPadIsDown && touchPadIsDown && TouchButtonDown != null) { if (sensors.Touch1 && sensors.Touch2) { args = new TouchpadEventArgs(sensors.ReportTimeStamp, sensors.TouchButton, new TouchReadings(currentX1, currentY1, touchID1), new TouchReadings(currentX2, currentY2, touchID2)); } else if (sensors.Touch1) { args = new TouchpadEventArgs(sensors.ReportTimeStamp, sensors.TouchButton, new TouchReadings(currentX1, currentY1, touchID1)); } else { args = new TouchpadEventArgs(sensors.ReportTimeStamp, sensors.TouchButton, new TouchReadings(currentX2, currentY2, touchID2)); } TouchButtonDown(this, args); } else if (lastTouchPadIsDown && !touchPadIsDown && TouchButtonUp != null) { if (sensors.Touch1 && sensors.Touch2) { args = new TouchpadEventArgs(sensors.ReportTimeStamp, sensors.TouchButton, new TouchReadings(currentX1, currentY1, touchID1), new TouchReadings(currentX2, currentY2, touchID2)); } else if (sensors.Touch1) { args = new TouchpadEventArgs(sensors.ReportTimeStamp, sensors.TouchButton, new TouchReadings(currentX1, currentY1, touchID1)); } else { args = new TouchpadEventArgs(sensors.ReportTimeStamp, sensors.TouchButton, new TouchReadings(currentX2, currentY2, touchID2)); } TouchButtonUp(this, args); } if (sensors.Touch1) { lastTouchPadX1 = currentX1; lastTouchPadY1 = currentY1; } if (sensors.Touch2) { lastTouchPadX2 = currentX2; lastTouchPadY2 = currentY2; } lastTouchPadIsDown = touchPadIsDown; } else { if (touchPadIsDown && !lastTouchPadIsDown) { TouchButtonDown?.Invoke(this, new TouchpadEventArgs(sensors.ReportTimeStamp, sensors.TouchButton, null)); } else if (!touchPadIsDown && lastTouchPadIsDown) { TouchButtonUp?.Invoke(this, new TouchpadEventArgs(sensors.ReportTimeStamp, sensors.TouchButton, null)); } if ((lastIsActive1 || lastIsActive2) && TouchesEnded != null) { if (lastIsActive1 && lastIsActive2) { args = new TouchpadEventArgs(sensors.ReportTimeStamp, sensors.TouchButton, new TouchReadings(lastTouchPadX1, lastTouchPadY1, touchID1), new TouchReadings(lastTouchPadX2, lastTouchPadY2, touchID2)); } else if (lastIsActive1) { args = new TouchpadEventArgs(sensors.ReportTimeStamp, sensors.TouchButton, new TouchReadings(lastTouchPadX1, lastTouchPadY1, touchID1)); } else { args = new TouchpadEventArgs(sensors.ReportTimeStamp, sensors.TouchButton, new TouchReadings(lastTouchPadX2, lastTouchPadY2, touchID2)); } TouchesEnded(this, args); } } lastIsActive1 = sensors.Touch1; lastIsActive2 = sensors.Touch2; lastTouchID1 = touchID1; lastTouchID2 = touchID2; lastTouchPadIsDown = touchPadIsDown; }
public void handleTouchpad(byte[] data, DS4State sensors, int touchPacketOffset = 0) { PreTouchProcess?.Invoke(this, EventArgs.Empty); bool touchPadIsDown = sensors.TouchButton; if (!PacketChanged(data, touchPacketOffset) && touchPadIsDown == lastTouchPadIsDown) { TouchUnchanged?.Invoke(this, EventArgs.Empty); return; } Array.Copy(data, TOUCHPAD_DATA_OFFSET + touchPacketOffset, previousPacket, 0, 8); byte touchID1 = (byte)(data[0 + TOUCHPAD_DATA_OFFSET + touchPacketOffset] & 0x7F); byte touchID2 = (byte)(data[4 + TOUCHPAD_DATA_OFFSET + touchPacketOffset] & 0x7F); int currentX1 = ((data[2 + TOUCHPAD_DATA_OFFSET + touchPacketOffset] & 0x0F) << 8) | data[1 + TOUCHPAD_DATA_OFFSET + touchPacketOffset]; int currentY1 = (data[3 + TOUCHPAD_DATA_OFFSET + touchPacketOffset] << 4) | ((data[2 + TOUCHPAD_DATA_OFFSET + touchPacketOffset] & 0xF0) >> 4); int currentX2 = ((data[6 + TOUCHPAD_DATA_OFFSET + touchPacketOffset] & 0x0F) << 8) | data[5 + TOUCHPAD_DATA_OFFSET + touchPacketOffset]; int currentY2 = (data[7 + TOUCHPAD_DATA_OFFSET + touchPacketOffset] << 4) | ((data[6 + TOUCHPAD_DATA_OFFSET + touchPacketOffset] & 0xF0) >> 4); TouchpadEventArgs args; if (sensors.Touch1 || sensors.Touch2) { if ((sensors.Touch1 && !lastIsActive1) || (sensors.Touch2 && !lastIsActive2)) { if (TouchesBegan != null) { if (sensors.Touch1 && sensors.Touch2) { t0.populate(currentX1, currentY1, touchID1); t1.populate(currentX2, currentY2, touchID2); args = new TouchpadEventArgs(sensors.ReportTimeStamp, sensors.TouchButton, t0, t1); } else if (sensors.Touch1) { t0.populate(currentX1, currentY1, touchID1); args = new TouchpadEventArgs(sensors.ReportTimeStamp, sensors.TouchButton, t0); } else { t0.populate(currentX2, currentY2, touchID2); args = new TouchpadEventArgs(sensors.ReportTimeStamp, sensors.TouchButton, t0); } TouchesBegan(this, args); } } else if (sensors.Touch1 == lastIsActive1 && sensors.Touch2 == lastIsActive2 && TouchesMoved != null) { Touch currentT0, currentT1; if (sensors.Touch1 && sensors.Touch2) { tPrev0.populate(lastTouchPadX1, lastTouchPadY1, lastTouchID1); t0.populate(currentX1, currentY1, touchID1, tPrev0); currentT0 = t0; tPrev1.populate(lastTouchPadX2, lastTouchPadY2, lastTouchID2); t1.populate(currentX2, currentY2, touchID2, tPrev1); currentT1 = t1; } else if (sensors.Touch1) { tPrev0.populate(lastTouchPadX1, lastTouchPadY1, lastTouchID1); t0.populate(currentX1, currentY1, touchID1, tPrev0); currentT0 = t0; currentT1 = null; } else { tPrev0.populate(lastTouchPadX2, lastTouchPadY2, lastTouchID2); t0.populate(currentX2, currentY2, touchID2, tPrev0); currentT0 = t0; currentT1 = null; } args = new TouchpadEventArgs(sensors.ReportTimeStamp, sensors.TouchButton, currentT0, currentT1); TouchesMoved(this, args); } if (!lastTouchPadIsDown && touchPadIsDown && TouchButtonDown != null) { if (sensors.Touch1 && sensors.Touch2) { t0.populate(currentX1, currentY1, touchID1); t1.populate(currentX2, currentY2, touchID2); args = new TouchpadEventArgs(sensors.ReportTimeStamp, sensors.TouchButton, t0, t1); } else if (sensors.Touch1) { t0.populate(currentX1, currentY1, touchID1); args = new TouchpadEventArgs(sensors.ReportTimeStamp, sensors.TouchButton, t0); } else { t0.populate(currentX2, currentY2, touchID2); args = new TouchpadEventArgs(sensors.ReportTimeStamp, sensors.TouchButton, t0); } TouchButtonDown(this, args); } else if (lastTouchPadIsDown && !touchPadIsDown && TouchButtonUp != null) { if (sensors.Touch1 && sensors.Touch2) { t0.populate(currentX1, currentY1, touchID1); t1.populate(currentX2, currentY2, touchID2); args = new TouchpadEventArgs(sensors.ReportTimeStamp, sensors.TouchButton, t0, t1); } else if (sensors.Touch1) { t0.populate(currentX1, currentY1, touchID1); args = new TouchpadEventArgs(sensors.ReportTimeStamp, sensors.TouchButton, t0); } else { t0.populate(currentX2, currentY2, touchID2); args = new TouchpadEventArgs(sensors.ReportTimeStamp, sensors.TouchButton, t0); } TouchButtonUp(this, args); } if (sensors.Touch1) { lastTouchPadX1 = currentX1; lastTouchPadY1 = currentY1; } if (sensors.Touch2) { lastTouchPadX2 = currentX2; lastTouchPadY2 = currentY2; } lastTouchPadIsDown = touchPadIsDown; } else { if (touchPadIsDown && !lastTouchPadIsDown) { TouchButtonDown?.Invoke(this, new TouchpadEventArgs(sensors.ReportTimeStamp, sensors.TouchButton, null, null)); } else if (!touchPadIsDown && lastTouchPadIsDown) { TouchButtonUp?.Invoke(this, new TouchpadEventArgs(sensors.ReportTimeStamp, sensors.TouchButton, null, null)); } if ((lastIsActive1 || lastIsActive2) && TouchesEnded != null) { if (lastIsActive1 && lastIsActive2) { t0.populate(lastTouchPadX1, lastTouchPadY1, touchID1); t1.populate(lastTouchPadX2, lastTouchPadY2, touchID2); args = new TouchpadEventArgs(sensors.ReportTimeStamp, sensors.TouchButton, t0, t1); } else if (lastIsActive1) { t0.populate(lastTouchPadX1, lastTouchPadY1, touchID1); args = new TouchpadEventArgs(sensors.ReportTimeStamp, sensors.TouchButton, t0); } else { t0.populate(lastTouchPadX2, lastTouchPadY2, touchID2); args = new TouchpadEventArgs(sensors.ReportTimeStamp, sensors.TouchButton, t0); } TouchesEnded(this, args); } } lastIsActive1 = sensors.Touch1; lastIsActive2 = sensors.Touch2; lastTouchID1 = touchID1; lastTouchID2 = touchID2; lastTouchPadIsDown = touchPadIsDown; }