Finish up debug wrappers, add note about supported Wii web tech to readme, finalize demo.

This commit is contained in:
Ryan McGrath 2011-07-11 09:27:20 -04:00
parent 7f4f81fa9e
commit e7249a0ed6
6 changed files with 81 additions and 17 deletions

View file

@ -26,6 +26,7 @@ currdir = os.getcwd()
DEPENDENCIES = [ DEPENDENCIES = [
'wii', 'wii',
'util', 'util',
'console',
'remote', 'remote',
] ]

20
js/src/console.js Normal file
View file

@ -0,0 +1,20 @@
/**
* console.js
*
* A lightweight wrapper for the now-common "console" object in browsers.
* Really just maps calls over to the wii-js internal Wii.util.debug() call,
* but exists so that if you use this in production code for whatever reason
* it could still be used to debug on the Wii.
*
* Note that for this to work, you must be listening in debug mode!
*
* @Author: Ryan McGrath <ryan@venodesigns.net>
* @Requires: wii.js, util.js
*/
if(typeof window.console === 'undefined') {
window.console = {
log: Wii.util.debug,
debug: Wii.util.debug
};
}

View file

@ -31,21 +31,25 @@ Wii.util = {
* try { ... } catch(e) { Wii.util.debug(e); } * try { ... } catch(e) { Wii.util.debug(e); }
*/ */
debug: function(err) { debug: function(err) {
if(!Wii.debug) return;
if(Wii.util.msgNode === null) { if(Wii.util.msgNode === null) {
Wii.util.msgNode = document.createElement('div'); Wii.util.msgNode = document.createElement('div');
Wii.util.msgNode.style.cssText = [ Wii.util.msgNode.style.cssText = [
'min-width: 776px;', 'min-width: 780px;',
'padding: 10px;', 'padding: 10px;',
'font-size: 28px;', 'font-size: 28px;',
'line-height: 32px;', 'line-height: 32px;',
'font-family: monospace;', 'font-family: monospace;',
'position: absolute;', 'position: absolute;',
'top: 20px;', 'top: 15px;',
'left: 0;',
'color: #f9f9f9;', 'color: #f9f9f9;',
'background-color: #010101;', 'background-color: #010101;',
'border: 2px solid #42a2cc;', 'border-bottom: 2px solid #42a2cc;',
'opacity: .8', 'opacity: .7;',
'font-weight: bold;'
].join(''); ].join('');
Wii.util.msgNode.addEventListener('click', Wii.util.hideDebugger, false); Wii.util.msgNode.addEventListener('click', Wii.util.hideDebugger, false);

View file

@ -381,21 +381,25 @@ Wii.util = {
* try { ... } catch(e) { Wii.util.debug(e); } * try { ... } catch(e) { Wii.util.debug(e); }
*/ */
debug: function(err) { debug: function(err) {
if(!Wii.debug) return;
if(Wii.util.msgNode === null) { if(Wii.util.msgNode === null) {
Wii.util.msgNode = document.createElement('div'); Wii.util.msgNode = document.createElement('div');
Wii.util.msgNode.style.cssText = [ Wii.util.msgNode.style.cssText = [
'min-width: 776px;', 'min-width: 780px;',
'padding: 10px;', 'padding: 10px;',
'font-size: 28px;', 'font-size: 28px;',
'line-height: 32px;', 'line-height: 32px;',
'font-family: monospace;', 'font-family: monospace;',
'position: absolute;', 'position: absolute;',
'top: 20px;', 'top: 15px;',
'left: 0;',
'color: #f9f9f9;', 'color: #f9f9f9;',
'background-color: #010101;', 'background-color: #010101;',
'border: 2px solid #42a2cc;', 'border-bottom: 2px solid #42a2cc;',
'opacity: .8', 'opacity: .7;',
'font-weight: bold;'
].join(''); ].join('');
Wii.util.msgNode.addEventListener('click', Wii.util.hideDebugger, false); Wii.util.msgNode.addEventListener('click', Wii.util.hideDebugger, false);
@ -436,6 +440,26 @@ Wii.util = {
}; };
} }
}; };
/**
* console.js
*
* A lightweight wrapper for the now-common "console" object in browsers.
* Really just maps calls over to the wii-js internal Wii.util.debug() call,
* but exists so that if you use this in production code for whatever reason
* it could still be used to debug on the Wii.
*
* Note that for this to work, you must be listening in debug mode!
*
* @Author: Ryan McGrath <ryan@venodesigns.net>
* @Requires: wii.js, util.js
*/
if(typeof window.console === 'undefined') {
window.console = {
log: Wii.util.debug,
debug: Wii.util.debug
};
}
/** /**
* remote.js * remote.js
* *

2
js/wii.min.js vendored
View file

@ -1 +1 @@
(function(a){if(!a){return false}var b={extraRemotes:[],currentBrowsingRemote:null,setListeners:false,debug:false,primaryWiimoteEvts:["mouseup","mousedown","keyup","keydown","keypress"]};b.installListeners=function(){for(var d=0,c=b.primaryWiimoteEvts.length;d<c;d++){document.addEventListener(b.primaryWiimoteEvts[d],b.parsePrimaryWiimote,false)}window.onbeforeunload=function(){for(var f=0,e=b.primaryWiimoteEvts.length;f<e;f++){document.removeEventListener(b.primaryWiimoteEvts[f],b.parsePrimaryWiimote,false)}if(b.util.msgNode){b.util.msgNode.removeEventListener("click",b.util.hideDebugger,false)}};if(b.debug){b.util.originalErrFunction=window.Error;window.Error=function(){if(arguments.length>0){b.util.debug(arguments[0])}else{b.util.originalErrFunction.apply(this,arguments)}}}return true};b.listen=function(h){if(typeof h!=="undefined"){if(typeof h.debug!=="undefined"&&h.debug){b.debug=true}}if(!b.setListeners){b.setListeners=b.installListeners()}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={originalErrFunction:null,msgNode:null,debug:function(c){if(b.util.msgNode===null){b.util.msgNode=document.createElement("div");b.util.msgNode.style.cssText=["min-width: 776px;","padding: 10px;","font-size: 28px;","line-height: 32px;","font-family: monospace;","position: absolute;","top: 20px;","color: #f9f9f9;","background-color: #010101;","border: 2px solid #42a2cc;","opacity: .8",].join("");b.util.msgNode.addEventListener("click",b.util.hideDebugger,false);document.body.appendChild(b.util.msgNode)}if(typeof c==="string"){b.util.msgNode.innerHTML=c}else{var f="";for(var d in c){f+='<span style="color: #42a2cc; font-weight: bold;">'+d+"</span>="+c[d]+"<br>"}b.util.msgNode.innerHTML=f}b.util.msgNode.style.display="block"},hideDebugger:function(){this.style.display="none"},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); (function(a){if(!a){return false}var b={extraRemotes:[],currentBrowsingRemote:null,setListeners:false,debug:false,primaryWiimoteEvts:["mouseup","mousedown","keyup","keydown","keypress"]};b.installListeners=function(){for(var d=0,c=b.primaryWiimoteEvts.length;d<c;d++){document.addEventListener(b.primaryWiimoteEvts[d],b.parsePrimaryWiimote,false)}window.onbeforeunload=function(){for(var f=0,e=b.primaryWiimoteEvts.length;f<e;f++){document.removeEventListener(b.primaryWiimoteEvts[f],b.parsePrimaryWiimote,false)}if(b.util.msgNode){b.util.msgNode.removeEventListener("click",b.util.hideDebugger,false)}};if(b.debug){b.util.originalErrFunction=window.Error;window.Error=function(){if(arguments.length>0){b.util.debug(arguments[0])}else{b.util.originalErrFunction.apply(this,arguments)}}}return true};b.listen=function(h){if(typeof h!=="undefined"){if(typeof h.debug!=="undefined"&&h.debug){b.debug=true}}if(!b.setListeners){b.setListeners=b.installListeners()}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={originalErrFunction:null,msgNode:null,debug:function(c){if(!b.debug){return}if(b.util.msgNode===null){b.util.msgNode=document.createElement("div");b.util.msgNode.style.cssText=["min-width: 780px;","padding: 10px;","font-size: 28px;","line-height: 32px;","font-family: monospace;","position: absolute;","top: 15px;","left: 0;","color: #f9f9f9;","background-color: #010101;","border-bottom: 2px solid #42a2cc;","opacity: .7;","font-weight: bold;"].join("");b.util.msgNode.addEventListener("click",b.util.hideDebugger,false);document.body.appendChild(b.util.msgNode)}if(typeof c==="string"){b.util.msgNode.innerHTML=c}else{var f="";for(var d in c){f+='<span style="color: #42a2cc; font-weight: bold;">'+d+"</span>="+c[d]+"<br>"}b.util.msgNode.innerHTML=f}b.util.msgNode.style.display="block"},hideDebugger:function(){this.style.display="none"},bind:function(c,d){return function(){return d.apply(c,arguments)}}};if(typeof window.console==="undefined"){window.console={log:b.util.debug,debug:b.util.debug}}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);

View file

@ -17,6 +17,10 @@ To play with a live example, load up the demo (_index.html_) on your own server,
**wii-js Demo: [http://venodesigns.net/wii/](http://venodesigns.net/wii/)** **wii-js Demo: [http://venodesigns.net/wii/](http://venodesigns.net/wii/)**
Working with the Wii's browser can be odd - it has moderately good support for CSS, so you're never really
as bad off as you'd be with a version of Internet Explorer - that said, if you're looking for a good read
on what's supported, check out **[this article on Opera Wii supported technologies](http://www.opera.com/docs/specs/opera9/?platform=wii)**.
Questions, comments, criticism and praise can be directed to me at the following outlets: Questions, comments, criticism and praise can be directed to me at the following outlets:
- You can email me at **ryan [at] venodesigns (dot) net**. - You can email me at **ryan [at] venodesigns (dot) net**.
@ -102,7 +106,7 @@ isn't able to see the TV/sensor bar, so be sure to check this!
of the current webpage + toolbar height, if enabled. Tinker with this one for your purposes. of the current webpage + toolbar height, if enabled. Tinker with this one for your purposes.
Extra Tips and Tricks Extra Tips and Tricks (Debugging)
------------------------------------------------------------------------------------------------------------------ ------------------------------------------------------------------------------------------------------------------
One semi-useful trick to point out about this library is that each of your callback functions get passed two One semi-useful trick to point out about this library is that each of your callback functions get passed two
arguments by default - a reference to the Wiimote you're working with, and the raw Wiimote status object that the arguments by default - a reference to the Wiimote you're working with, and the raw Wiimote status object that the
@ -118,19 +122,30 @@ wiimote.when('pressed_a', function(wii_remote, wii_remote_status) {
}); });
``` ```
Debugging Javascript on the Wii is also nothing short of incredibly annoying, so I've included a convenience function Debugging Javascript on the Wii is also nothing short of incredibly annoying, so I've made some efforts to patch this
for running through error messages. My typical debugging strategy with any Wii-related code would always start with up and make life a bit earier. My typical debugging strategy with any Wii-related code would always start with
the following: the following. The first thing to do is set the Wii listener to run in debug mode, like so:
``` javascript
Wii.listen({debug: true});
```
With this set, you can log errors with any of the following functions. `error` can be a string or a complex object.
- **console.log(error);** - Tried and true, now supported.
- **console.debug(error);** - Same as console.log here, but syntax is supported.
- **throw new Error(error);** - Throw them, they'll be logged.
- **Wii.util.debug(error);** - The core function that handles logging internally.
If the typical Wii debugging flow isn't enough for you, go aggressive with this - just be aware that you can crash
the Wii's browser if you're using try/catch all over the place, as it's not cheap in Javascript.
``` javascript ``` javascript
try { try {
// Whatever function I execute // Whatever function to execute
} catch(e) { Wii.util.debug(e); } } catch(e) { Wii.util.debug(e); }
``` ```
The rate at which this function will improve is commensurate with how much more time and effort I can afford to put
into this project. Feel free to fork/patch/enhance if you need more. ;)
Why the button limitations? Why the button limitations?
------------------------------------------------------------------------------------------------------------------ ------------------------------------------------------------------------------------------------------------------