Include functions for monitoring distance/roll.

This commit is contained in:
Ryan McGrath 2011-07-10 04:53:38 -04:00
parent 31b3d9c0bb
commit 434f069185
4 changed files with 113 additions and 8 deletions

View file

@ -26,6 +26,16 @@ Wii.Remote = function(remote_id, opts) {
this.remote_id = remote_id;
this.opts = opts;
/**
* Default these properties to undefined, since that's what
* the Wii returns anyway, and it's worth it to try and stay (somewhat)
* close to the core tech.
*/
this.x = undefined;
this.y = undefined;
this.roll = undefined;
this.last_known_distance_from_screen = undefined;
/**
* If this is the "main" wii_remote, then the bitwise checks will fail
* because it's treated more as a "browsing" device. For these events,

View file

@ -81,9 +81,13 @@ Wii.listen = function() {
if(wii_remoteCurrStatus.isBrowsing) {
Wii.currentBrowsingRemote = wii_remote;
} else {
/* Update these on each poll, since we've got the data anyway. */
wii_remote.x = wii_remoteCurrStatus.dpdScreenX;
wii_remote.y = wii_remoteCurrStatus.dpdScreenY;
for(var evt in wii_remote.evtsInterestedIn) {
var evtHappened = Wii.DISPATCHER[evt](wii_remote, wii_remoteCurrStatus);
if(evtHappened) try { wii_remote.evtsInterestedIn[evt](wii_remote, wii_remoteCurrStatus); } catch(e) { alert(e.message); }
if(evtHappened) wii_remote.evtsInterestedIn[evt](wii_remote, wii_remoteCurrStatus);
}
}
}
@ -128,6 +132,10 @@ Wii.parsePrimaryWiimote = function(e) {
wii_remoteCurrStatus = wii_remote.isEnabled(),
buttonPressed = Wii.PRIMARY_CONTROLLER_DISPATCHER[wii_remote.opts.horizontal ? 'horizontal' : 'vertical'][e.keyCode];
/* Grab these first, and on every pass. */
wii_remote.x = wii_remoteCurrStatus.dpdScreenX;
wii_remote.y = wii_remoteCurrStatus.dpdScreenY;
/**
* Filter down and figure out which "event" we're really looking at based on code
* matchups; this gets messy pretty quickly...
@ -136,6 +144,22 @@ Wii.parsePrimaryWiimote = function(e) {
wii_remote.evtsInterestedIn[buttonPressed](wii_remote, wii_remoteCurrStatus);
}
/**
* Due to the difference in how these controls are caught, we need a second set of roll/distance-changes
* run here. Luckily, we can just re-use the dispatcher functions.
*/
if(typeof wii_remote.evtsInterestedIn['roll_change'] === 'function') {
if(Wii.DISPATCHER['roll_change'](wii_remote, wii_remoteCurrStatus)) {
wii_remote.evtsInterestedIn['roll_change'](wii_remote, wii_remoteCurrStatus);
}
}
if(typeof wii_remote.evtsInterestedIn['distance_change'] === 'function') {
if(Wii.DISPATCHER['distance_change'](wii_remote, wii_remoteCurrStatus)) {
wii_remote.evtsInterestedIn['distance_change'](wii_remote, wii_remoteCurrStatus);
}
}
/* Doing this in conjunction with preventDefault() halts an odd clicking bug or two. */
return false;
};
@ -145,7 +169,7 @@ Wii.parsePrimaryWiimote = function(e) {
*
* In order to keep things as performant as possible, we want DOM events (for the primary controller)
* to also be a 1:1 hash map lookup. This is PRIMARILY for the primary ("browsing") controller; all other
* controllers get their operations routed through the DISPATCHER below.
* controllers get their operations routed through the DISPATCHER below. The keys below are keyCodes.
*/
Wii.PRIMARY_CONTROLLER_DISPATCHER = {
vertical: {
@ -214,6 +238,25 @@ Wii.DISPATCHER = {
'pressed_b': function(wii_remote, wii_remoteStatus) { return wii_remoteStatus.hold & 1024; },
'pressed_a': function(wii_remote, wii_remoteStatus) { return wii_remoteStatus.hold & 2048; },
'roll_change': function(wii_remote, wii_remoteStatus) {
var roll = Math.atan2(wii_remoteStatus.dpdRollY, wii_remoteStatus.dpdRollX);
if(roll !== wii_remote.roll) {
wii_remote.roll = roll;
return true;
}
return false;
},
'distance_change': function(wii_remote, wii_remoteStatus) {
if(wii_remoteStatus.dpdDistance !== wii_remote.last_known_distance_from_screen) {
wii_remote.last_known_distance_from_screen = wii_remoteStatus.dpdDistance;
return true;
}
return false;
},
/**
* I'm keeping these noted here for legacy reasons, but by and large it's just not even
* worth trying to use the Nunchuk with anything in the browser; the primary controller

View file

@ -104,9 +104,13 @@ Wii.listen = function() {
if(wii_remoteCurrStatus.isBrowsing) {
Wii.currentBrowsingRemote = wii_remote;
} else {
/* Update these on each poll, since we've got the data anyway. */
wii_remote.x = wii_remoteCurrStatus.dpdScreenX;
wii_remote.y = wii_remoteCurrStatus.dpdScreenY;
for(var evt in wii_remote.evtsInterestedIn) {
var evtHappened = Wii.DISPATCHER[evt](wii_remote, wii_remoteCurrStatus);
if(evtHappened) try { wii_remote.evtsInterestedIn[evt](wii_remote, wii_remoteCurrStatus); } catch(e) { alert(e.message); }
if(evtHappened) wii_remote.evtsInterestedIn[evt](wii_remote, wii_remoteCurrStatus);
}
}
}
@ -151,6 +155,10 @@ Wii.parsePrimaryWiimote = function(e) {
wii_remoteCurrStatus = wii_remote.isEnabled(),
buttonPressed = Wii.PRIMARY_CONTROLLER_DISPATCHER[wii_remote.opts.horizontal ? 'horizontal' : 'vertical'][e.keyCode];
/* Grab these first, and on every pass. */
wii_remote.x = wii_remoteCurrStatus.dpdScreenX;
wii_remote.y = wii_remoteCurrStatus.dpdScreenY;
/**
* Filter down and figure out which "event" we're really looking at based on code
* matchups; this gets messy pretty quickly...
@ -159,6 +167,22 @@ Wii.parsePrimaryWiimote = function(e) {
wii_remote.evtsInterestedIn[buttonPressed](wii_remote, wii_remoteCurrStatus);
}
/**
* Due to the difference in how these controls are caught, we need a second set of roll/distance-changes
* run here. Luckily, we can just re-use the dispatcher functions.
*/
if(typeof wii_remote.evtsInterestedIn['roll_change'] === 'function') {
if(Wii.DISPATCHER['roll_change'](wii_remote, wii_remoteCurrStatus)) {
wii_remote.evtsInterestedIn['roll_change'](wii_remote, wii_remoteCurrStatus);
}
}
if(typeof wii_remote.evtsInterestedIn['distance_change'] === 'function') {
if(Wii.DISPATCHER['distance_change'](wii_remote, wii_remoteCurrStatus)) {
wii_remote.evtsInterestedIn['distance_change'](wii_remote, wii_remoteCurrStatus);
}
}
/* Doing this in conjunction with preventDefault() halts an odd clicking bug or two. */
return false;
};
@ -168,7 +192,7 @@ Wii.parsePrimaryWiimote = function(e) {
*
* In order to keep things as performant as possible, we want DOM events (for the primary controller)
* to also be a 1:1 hash map lookup. This is PRIMARILY for the primary ("browsing") controller; all other
* controllers get their operations routed through the DISPATCHER below.
* controllers get their operations routed through the DISPATCHER below. The keys below are keyCodes.
*/
Wii.PRIMARY_CONTROLLER_DISPATCHER = {
vertical: {
@ -237,6 +261,25 @@ Wii.DISPATCHER = {
'pressed_b': function(wii_remote, wii_remoteStatus) { return wii_remoteStatus.hold & 1024; },
'pressed_a': function(wii_remote, wii_remoteStatus) { return wii_remoteStatus.hold & 2048; },
'roll_change': function(wii_remote, wii_remoteStatus) {
var roll = Math.atan2(wii_remoteStatus.dpdRollY, wii_remoteStatus.dpdRollX);
if(roll !== wii_remote.roll) {
wii_remote.roll = roll;
return true;
}
return false;
},
'distance_change': function(wii_remote, wii_remoteStatus) {
if(wii_remoteStatus.dpdDistance !== wii_remote.last_known_distance_from_screen) {
wii_remote.last_known_distance_from_screen = wii_remoteStatus.dpdDistance;
return true;
}
return false;
},
/**
* I'm keeping these noted here for legacy reasons, but by and large it's just not even
* worth trying to use the Nunchuk with anything in the browser; the primary controller
@ -244,8 +287,7 @@ Wii.DISPATCHER = {
*/
'pressed_z': function(wii_remote, wii_remoteStatus) { return wii_remoteStatus.hold & 8192; },
'pressed_c': function(wii_remote, wii_remoteStatus) { return wii_remoteStatus.hold & 16384; }
};
/**
};/**
* util.js
*
* A basic utility wrapper; anything extra that's often re-used should
@ -339,6 +381,16 @@ Wii.Remote = function(remote_id, opts) {
this.remote_id = remote_id;
this.opts = opts;
/**
* Default these properties to undefined, since that's what
* the Wii returns anyway, and it's worth it to try and stay (somewhat)
* close to the core tech.
*/
this.x = undefined;
this.y = undefined;
this.roll = undefined;
this.last_known_distance_from_screen = undefined;
/**
* If this is the "main" wii_remote, then the bitwise checks will fail
* because it's treated more as a "browsing" device. For these events,

2
js/wii.min.js vendored
View file

@ -1 +1 @@
(function(a){if(!a){return false}var b={extraRemotes:[],currentBrowsingRemote:null,setKeyListeners:false,debuggerDiv:null};b.installKeyListeners=function(){document.addEventListener("mouseup",b.parsePrimaryWiimote,false);document.addEventListener("keydown",b.parsePrimaryWiimote,false);document.addEventListener("mousedown",b.parsePrimaryWiimote,false);document.addEventListener("keyup",b.parsePrimaryWiimote,false);document.addEventListener("keypress",b.parsePrimaryWiimote,false);return true};b.listen=function(){if(!b.setKeyListeners){b.setKeyListeners=b.installKeyListeners()}var f=b.extraRemotes.length;while(f--){var j=b.extraRemotes[f],h=j.isEnabled();if(h){if(h.isBrowsing){b.currentBrowsingRemote=j}else{for(var c in j.evtsInterestedIn){var d=b.DISPATCHER[c](j,h);if(d){try{j.evtsInterestedIn[c](j,h)}catch(g){alert(g.message)}}}}}}return setTimeout(b.listen,100)};b.parsePrimaryWiimote=function(g){g.preventDefault();var f=b.currentBrowsingRemote,d=f.isEnabled(),c=b.PRIMARY_CONTROLLER_DISPATCHER[f.opts.horizontal?"horizontal":"vertical"][g.keyCode];if(typeof c!=="undefined"&&typeof f.evtsInterestedIn[c]==="function"){f.evtsInterestedIn[c](f,d)}return false};b.PRIMARY_CONTROLLER_DISPATCHER={vertical:{0:"pressed_a",13:"pressed_a",170:"pressed_minus",171:"pressed_b",172:"pressed_1",173:"pressed_2",174:"pressed_plus",175:"pressed_up",176:"pressed_down",177:"pressed_right",178:"pressed_left"},horizontal:{0:"pressed_a",13:"pressed_a",170:"pressed_minus",171:"pressed_b",172:"pressed_1",173:"pressed_2",174:"pressed_plus",175:"pressed_left",176:"pressed_right",177:"pressed_up",178:"pressed_down"}};b.DISPATCHER={pressed_up:function(d,c){if(d.opts.horizontal){return c.hold&2}return c.hold&8},pressed_right:function(d,c){if(d.opts.horizontal){return c.hold&4}return c.hold&2},pressed_down:function(d,c){if(d.opts.horizontal){return c.hold&1}return c.hold&4},pressed_left:function(d,c){if(d.opts.horizontal){return c.hold&8}return c.hold&1},pressed_plus:function(d,c){return c.hold&16},pressed_minus:function(d,c){return c.hold&4096},pressed_2:function(d,c){return c.hold&256},pressed_1:function(d,c){return c.hold&512},pressed_b:function(d,c){return c.hold&1024},pressed_a:function(d,c){return c.hold&2048},pressed_z:function(d,c){return c.hold&8192},pressed_c:function(d,c){return c.hold&16384}};b.util={debug:function(c){if(b.debuggerDiv===null){b.debuggerDiv=document.createElement("div");b.debuggerDiv.style.cssText=["width: 90%;","height: 90%;","padding: 10px;","font-size: 26px;","font-family: monospace;","overflow: scroll","position: absolute;","top: 10px;","left: 10px;","color: #f9f9f9;","background-color: #010101;"].join("");document.body.appendChild(b.debuggerDiv)}if(typeof c==="string"){b.debuggerDiv.innerHTML=c}else{var f="";for(var d in c){f+=d+"="+c[d]+"<br>"}b.debuggerDiv.innerHTML=f}b.debuggerDiv.style.display="block"},bind:function(c,d){return function(){return d.apply(c,arguments)}}};b.Remote=function(e,d){this.remote_id=e;this.opts=d;var c=this.isEnabled();if(c){if(!c.isBrowsing){b.extraRemotes.push(this)}else{b.currentBrowsingRemote=this}}};b.Remote.prototype={opts:{horizontal:false},evtsInterestedIn:undefined,isEnabled:function(){var c=opera.wiiremote.update(this.remote_id-1);return(c.isEnabled&&c.isDataValid?c:false)},when:function(d,c){if(typeof b.DISPATCHER[d]!=="undefined"){if(this.evtsInterestedIn===undefined){this.evtsInterestedIn={}}this.evtsInterestedIn[d]=b.util.bind(this,c);return this}return undefined}};window.Wii=b})(window.opera&&opera.wiiremote);
(function(a){if(!a){return false}var b={extraRemotes:[],currentBrowsingRemote:null,setKeyListeners:false,debuggerDiv:null};b.installKeyListeners=function(){document.addEventListener("mouseup",b.parsePrimaryWiimote,false);document.addEventListener("keydown",b.parsePrimaryWiimote,false);document.addEventListener("mousedown",b.parsePrimaryWiimote,false);document.addEventListener("keyup",b.parsePrimaryWiimote,false);document.addEventListener("keypress",b.parsePrimaryWiimote,false);return true};b.listen=function(){if(!b.setKeyListeners){b.setKeyListeners=b.installKeyListeners()}var e=b.extraRemotes.length;while(e--){var g=b.extraRemotes[e],f=g.isEnabled();if(f){if(f.isBrowsing){b.currentBrowsingRemote=g}else{g.x=f.dpdScreenX;g.y=f.dpdScreenY;for(var c in g.evtsInterestedIn){var d=b.DISPATCHER[c](g,f);if(d){g.evtsInterestedIn[c](g,f)}}}}}return setTimeout(b.listen,100)};b.parsePrimaryWiimote=function(g){g.preventDefault();var f=b.currentBrowsingRemote,d=f.isEnabled(),c=b.PRIMARY_CONTROLLER_DISPATCHER[f.opts.horizontal?"horizontal":"vertical"][g.keyCode];f.x=d.dpdScreenX;f.y=d.dpdScreenY;if(typeof c!=="undefined"&&typeof f.evtsInterestedIn[c]==="function"){f.evtsInterestedIn[c](f,d)}if(typeof f.evtsInterestedIn.roll_change==="function"){if(b.DISPATCHER.roll_change(f,d)){f.evtsInterestedIn.roll_change(f,d)}}if(typeof f.evtsInterestedIn.distance_change==="function"){if(b.DISPATCHER.distance_change(f,d)){f.evtsInterestedIn.distance_change(f,d)}}return false};b.PRIMARY_CONTROLLER_DISPATCHER={vertical:{0:"pressed_a",13:"pressed_a",170:"pressed_minus",171:"pressed_b",172:"pressed_1",173:"pressed_2",174:"pressed_plus",175:"pressed_up",176:"pressed_down",177:"pressed_right",178:"pressed_left"},horizontal:{0:"pressed_a",13:"pressed_a",170:"pressed_minus",171:"pressed_b",172:"pressed_1",173:"pressed_2",174:"pressed_plus",175:"pressed_left",176:"pressed_right",177:"pressed_up",178:"pressed_down"}};b.DISPATCHER={pressed_up:function(d,c){if(d.opts.horizontal){return c.hold&2}return c.hold&8},pressed_right:function(d,c){if(d.opts.horizontal){return c.hold&4}return c.hold&2},pressed_down:function(d,c){if(d.opts.horizontal){return c.hold&1}return c.hold&4},pressed_left:function(d,c){if(d.opts.horizontal){return c.hold&8}return c.hold&1},pressed_plus:function(d,c){return c.hold&16},pressed_minus:function(d,c){return c.hold&4096},pressed_2:function(d,c){return c.hold&256},pressed_1:function(d,c){return c.hold&512},pressed_b:function(d,c){return c.hold&1024},pressed_a:function(d,c){return c.hold&2048},roll_change:function(e,d){var c=Math.atan2(d.dpdRollY,d.dpdRollX);if(c!==e.roll){e.roll=c;return true}return false},distance_change:function(d,c){if(c.dpdDistance!==d.last_known_distance_from_screen){d.last_known_distance_from_screen=c.dpdDistance;return true}return false},pressed_z:function(d,c){return c.hold&8192},pressed_c:function(d,c){return c.hold&16384}};b.util={debug:function(c){if(b.debuggerDiv===null){b.debuggerDiv=document.createElement("div");b.debuggerDiv.style.cssText=["width: 90%;","height: 90%;","padding: 10px;","font-size: 26px;","font-family: monospace;","overflow: scroll","position: absolute;","top: 10px;","left: 10px;","color: #f9f9f9;","background-color: #010101;"].join("");document.body.appendChild(b.debuggerDiv)}if(typeof c==="string"){b.debuggerDiv.innerHTML=c}else{var f="";for(var d in c){f+=d+"="+c[d]+"<br>"}b.debuggerDiv.innerHTML=f}b.debuggerDiv.style.display="block"},bind:function(c,d){return function(){return d.apply(c,arguments)}}};b.Remote=function(e,d){this.remote_id=e;this.opts=d;this.x=undefined;this.y=undefined;this.roll=undefined;this.last_known_distance_from_screen=undefined;var c=this.isEnabled();if(c){if(!c.isBrowsing){b.extraRemotes.push(this)}else{b.currentBrowsingRemote=this}}};b.Remote.prototype={opts:{horizontal:false},evtsInterestedIn:undefined,isEnabled:function(){var c=opera.wiiremote.update(this.remote_id-1);return(c.isEnabled&&c.isDataValid?c:false)},when:function(d,c){if(typeof b.DISPATCHER[d]!=="undefined"){if(this.evtsInterestedIn===undefined){this.evtsInterestedIn={}}this.evtsInterestedIn[d]=b.util.bind(this,c);return this}return undefined}};window.Wii=b})(window.opera&&opera.wiiremote);