Skip to content

Commit

Permalink
Merge pull request #197 from OpenROV/fix/chrome-gamepad
Browse files Browse the repository at this point in the history
Chrome Gamepad patch
  • Loading branch information
gilborty authored Jan 25, 2017
2 parents 6dc2d28 + eae922d commit 763a6e2
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 14 deletions.
11 changes: 9 additions & 2 deletions src/plugins/input-configurator/public/js/input-configurator.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@

for(var i = 0; i < presetsIn.length; ++i)
{
self.savePreset(presetsIn[i]);
self.savePreset(presetsIn[i], false);
}
});

Expand Down Expand Up @@ -153,7 +153,7 @@
return;
}
};
savePreset(presetIn)
savePreset(presetIn, lastPreset = true)
{
var self = this;

Expand All @@ -174,6 +174,13 @@

//Add the preset
self.settings.presets.push(presetIn);

//They loaded a preset, set the lastPreset to this name
if(lastPreset)
{
self.settings.lastPreset = presetIn.name;
}


//Update the server settings to reflect this new preset
self.cockpit.rov.emit('plugin.settings-manager.saveSettings', {inputConfigurator: self.settings});
Expand Down
4 changes: 2 additions & 2 deletions src/plugins/rovpilot/public/js/rovpilot.js
Original file line number Diff line number Diff line change
Expand Up @@ -343,10 +343,10 @@
}
}
},
"LEFT_TRIGGER": { type: "button",
"LB": { type: "button",
action: 'rovPilot.decrementPowerLevel'
},
"RIGHT_TRIGGER": { type: "button",
"RB": { type: "button",
action: 'rovPilot.incrementPowerLevel'
}
}
Expand Down
54 changes: 45 additions & 9 deletions src/static/js/libs/gamepad.js
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,15 @@
* Sets up webkit platform.
*/
Gamepad.prototype._setupWebkit = function () {
var self = this;

window.addEventListener('webkitgamepadconnected', function(e) {
self._connect(e.gamepad);
});
window.addEventListener('webkitgamepaddisconnected', function(e) {
self._disconnect(e.gamepad);
});

};
/**
* Sets up filefox platform.
Expand Down Expand Up @@ -475,19 +484,46 @@
return Gamepad.Type.UNSUPPORTED;
}
};

/* Periodically scan for gamepads for Chrome support */
Gamepad.prototype._scanGamepads = function() {
var gamepads = navigator.getGamepads ? navigator.getGamepads() : (navigator.webkitGetGamepads ? navigator.webkitGetGamepads() : []);

for(let i = 0; i < gamepads.length; ++i)
{
let gamepad = gamepads[i];

//If we get a valid gamepad
if(gamepad != null)
{
//If this gamepad isn't already registered
if(this.gamepads[i] == null)
{
//Register it
this._connect(gamepad);
}
}
else
{
//If there is a gamepad registered in this spot, we need to deregister (it has been removed)
if(this.gamepads[i] != null)
{
gamepad = this.gamepads[i];
this._disconnect(gamepad);
}
}
}
}


/**
* Updates the controllers, triggering TICK events.
*/
Gamepad.prototype._update = function () {
var self = this, controlName, isDown, lastDown, downBtnIndex, mapping, value, i, j;
switch (this.platform) {
case Gamepad.Platform.WEBKIT:
this._updateWebkit();
break;
case Gamepad.Platform.FIREFOX:
this._updateFirefox();
break;
}

//Support chrome, scan for gamepads
this._scanGamepads();
for (i = 0; i < this.gamepads.length; i++) {
if (typeof this.gamepads[i] === 'undefined') {
continue;
Expand All @@ -508,7 +544,7 @@
for (j = 0; j < this.gamepads[i].downButtons.length; j++) {
if (this.gamepads[i].downButtons[j] === controlName) {
lastDown = true;
downBtnIndex = i;
downBtnIndex = j;
break;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@
var presetToSave = self.convertToPreset(presetToSave);

self.presets.set(presetToSave.name, presetToSave);
self.handleChangePreset(presetToSave.name);
self.addPreset(presetToSave);
}
};

Expand Down Expand Up @@ -344,6 +344,13 @@
newPreset.addAction(actionName);
});

//Check if this is a map object
if(!Array.isArray(presetIn.actions))
{
presetIn = self.convertToObject(presetIn);
}


for(var actionName in presetIn.actions)
{
var action = presetIn.actions[actionName];
Expand Down

0 comments on commit 763a6e2

Please sign in to comment.