Added toggle mouse LS + RS

This commit is contained in:
devl0rd 2020-04-28 13:14:59 -06:00
parent 31dcebb0ef
commit f7fe8998f2

View file

@ -164,50 +164,29 @@ function heldKeysBitmask(HeldKeys) {
Down: isOdd(HeldKeys >> 15) Down: isOdd(HeldKeys >> 15)
} }
} }
function convertAnalog(axis) {
var na;
if (axis) {
na = axis / 32767.5
}
if (na > 1) {
na = 2 - na
na = -na
}
return na;
}
function convertAnalogXY(x, y) {
return { x: convertAnalog(x), y: convertAnalog(y) };
}
function handleControllerInput(hid, controllerId, playerNumber) { function handleControllerInput(hid, controllerId, playerNumber) {
var heldKeys = hid.get("HeldKeys" + playerNumber); var heldKeys = hid.get("HeldKeys" + playerNumber);
var LJoyX = hid.get("LJoyX" + playerNumber); var LJoyX = convertAnalog(hid.get("LJoyX" + playerNumber));
var LJoyY = hid.get("LJoyY" + playerNumber); var LJoyY = convertAnalog(hid.get("LJoyY" + playerNumber));
var RJoyX = hid.get("RJoyX" + playerNumber); var RJoyX = convertAnalog(hid.get("RJoyX" + playerNumber));
var RJoyY = hid.get("RJoyY" + playerNumber); var RJoyY = convertAnalog(hid.get("RJoyY" + playerNumber));
var nljx; vgen.setAxisL(controllerId, LJoyX, LJoyY);
var nljy; vgen.setAxisR(controllerId, RJoyX, RJoyY);
if (LJoyX) {
nljx = LJoyX / 32767.5
}
if (nljx > 1) {
nljx = 2 - nljx
nljx = -nljx
}
if (LJoyY) {
nljy = LJoyY / 32767.5
}
if (nljy > 1) {
nljy = 2 - nljy
nljy = -nljy
}
vgen.setAxisL(controllerId, nljx, nljy);
var nrjx;
var nrjy;
if (RJoyX) {
nrjx = RJoyX / 32767.5
}
if (nrjx > 1) {
nrjx = 2 - nrjx
nrjx = -nrjx
}
if (RJoyY) {
nrjy = RJoyY / 32767.5
}
if (nrjy > 1) {
nrjy = 2 - nrjy
nrjy = -nrjy
}
vgen.setAxisR(controllerId, nrjx, nrjy);
var inputStates = heldKeysBitmask(heldKeys); var inputStates = heldKeysBitmask(heldKeys);
//Button mapping //Button mapping
if (!abxySwap) { if (!abxySwap) {
@ -273,51 +252,45 @@ function handleControllerInput(hid, controllerId, playerNumber) {
} }
var touchX1old = 0; var touchX1old = 0;
var touchY1old = 0; var touchY1old = 0;
var leftClicking = false; var leftClicking = false;
var rightTouchTime = 0; var rightTouchTime = 0;
var leftTouchTime = 0; var leftTouchTime = 0;
var rightClicking = false; var rightClicking = false;
var scrolling = false; var scrolling = false;
hidStreamClient.on('data', function (data) { var toggledMouseInput = false;
switchHidBuffer = new Buffer.from(data); var mouseInput = false;
var hid = parseInputStruct(switchHidBuffer) function handleMouseInputToggling(hid, playerNumber) {
var controllerCount = hid.get("controllerCount"); var heldKeys = hid.get("HeldKeys" + playerNumber);
if (controllerCount > controllerIds.length) { var inputStates = heldKeysBitmask(heldKeys);
plugControllerIn(); if (inputStates.LS && inputStates.RS) {
if (!toggledMouseInput) {
if (mouseInput) {
mouseInput = false;
} else {
mouseInput = true;
} }
for (i in controllerIds) { toggledMouseInput = true;
handleControllerInput(hid, controllerIds[i], parseInt(i) + 1);
} }
var gyro = { x: hid.get("gyroX"), y: hid.get("gyroY"), z: hid.get("gyroZ") } } else {
if (mouseControl == "ANALOG") { toggledMouseInput = false;
var RJoyX = hid.get("RJoyX1");
var RJoyY = hid.get("RJoyY1");
var nrjx;
var nrjy;
if (RJoyX) {
nrjx = RJoyX / 32767.5
}
if (nrjx > 1) {
nrjx = 2 - nrjx
nrjx = -nrjx
}
if (RJoyY) {
nrjy = RJoyY / 32767.5
}
if (nrjy > 1) {
nrjy = 2 - nrjy
nrjy = -nrjy
} }
}
function handleAnalogMouse(hid, playerNumber) {
var RJoyX = convertAnalog(hid.get("RJoyX" + playerNumber));
var RJoyY = convertAnalog(hid.get("RJoyY" + playerNumber));
var LJoyX = convertAnalog(hid.get("LJoyX" + playerNumber));
var LJoyY = convertAnalog(hid.get("LJoyY" + playerNumber));
var heldKeys = hid.get("HeldKeys" + playerNumber);
var inputStates = heldKeysBitmask(heldKeys);
var mouse = robot.getMousePos(); var mouse = robot.getMousePos();
mx = mouse.x + (nrjx * 16); mx = mouse.x + (RJoyX * 25);
my = mouse.y - (nrjy * 16); my = mouse.y - (RJoyY * 25);
if (mx && my) { if (mx && my) {
robot.moveMouse(mx, my); robot.moveMouse(mx, my);
} }
var heldKeys = hid.get("HeldKeys1"); if (LJoyX || LJoyY) {
var inputStates = heldKeysBitmask(heldKeys); robot.scrollMouse(LJoyX, LJoyY);
}
if (inputStates.ZR) { if (inputStates.ZR) {
if (!leftClicking) { if (!leftClicking) {
robot.mouseToggle("down"); robot.mouseToggle("down");
@ -340,7 +313,13 @@ hidStreamClient.on('data', function (data) {
rightClicking = false; rightClicking = false;
} }
} }
} else if (mouseControl == "GYRO") { }
function handleGyroMouse(hid, playerNumber) {
var RJoyX = convertAnalog(hid.get("RJoyX" + playerNumber));
var RJoyY = convertAnalog(hid.get("RJoyY" + playerNumber));
var heldKeys = hid.get("HeldKeys" + playerNumber);
var inputStates = heldKeysBitmask(heldKeys);
var gyro = { x: hid.get("gyroX"), y: hid.get("gyroY"), z: hid.get("gyroZ") }
var mouse = robot.getMousePos(); var mouse = robot.getMousePos();
var ngx = gyro.x * -1; var ngx = gyro.x * -1;
var ngz = gyro.z * -1 var ngz = gyro.z * -1
@ -349,8 +328,9 @@ hidStreamClient.on('data', function (data) {
if (mx && my) { if (mx && my) {
robot.moveMouse(mx, my); robot.moveMouse(mx, my);
} }
var heldKeys = hid.get("HeldKeys1"); if (RJoyX || RJoyY) {
var inputStates = heldKeysBitmask(heldKeys); robot.scrollMouse(LJoyX, LJoyY);
}
if (inputStates.ZR) { if (inputStates.ZR) {
if (!leftClicking) { if (!leftClicking) {
robot.mouseToggle("down"); robot.mouseToggle("down");
@ -373,7 +353,8 @@ hidStreamClient.on('data', function (data) {
rightClicking = false; rightClicking = false;
} }
} }
} else { }
function handleTouchInput(hid) {
var touchX1 = hid.get("touchX1"); var touchX1 = hid.get("touchX1");
var touchY1 = hid.get("touchY1"); var touchY1 = hid.get("touchY1");
if (touchX1 && touchY1) { if (touchX1 && touchY1) {
@ -428,14 +409,37 @@ hidStreamClient.on('data', function (data) {
leftTouchTime = 0; leftTouchTime = 0;
rightTouchTime = 0; rightTouchTime = 0;
} }
} }
function handleGyroAndAccel(hid) {
var gyro = { x: hid.get("gyroX"), y: hid.get("gyroY"), z: hid.get("gyroZ") }
var accel = { x: hid.get("accelX"), y: hid.get("accelY"), z: hid.get("accelZ") }
for (axis in gyro) { for (axis in gyro) {
gyro[axis] *= 250; gyro[axis] *= 250;
} }
gyro.y *= -1; gyro.y *= -1;
var accel = { x: hid.get("accelX"), y: hid.get("accelY"), z: hid.get("accelZ") }
GyroServ.sendMotionData(gyro, accel); GyroServ.sendMotionData(gyro, accel);
}
hidStreamClient.on('data', function (data) {
switchHidBuffer = new Buffer.from(data);
var hid = parseInputStruct(switchHidBuffer)
var controllerCount = hid.get("controllerCount");
if (controllerCount > controllerIds.length) {
plugControllerIn();
}
var playerNumber;
for (i in controllerIds) {
playerNumber = parseInt(i) + 1;
handleControllerInput(hid, controllerIds[i], playerNumber);
}
handleMouseInputToggling(hid, 1);
if (mouseControl == "ANALOG" && mouseInput) {
handleAnalogMouse(hid, 1);
} else if (mouseControl == "GYRO" && mouseInput) {
handleGyroMouse(hid, 1);
}
handleTouchInput(hid);
handleGyroAndAccel(hid);
}); });
hidStreamClient.on('end', function () { hidStreamClient.on('end', function () {
console.log('hidStreamClient Disconnected.'); console.log('hidStreamClient Disconnected.');