1 /**
  2  * @fileOverview
  3  * wiiu.enchant.js
  4  * @version 0.1.0
  5  * @require enchant.js v0.6.0+
  6  * @author UEI Corporation
  7  *
  8  * @description
  9  * Library for making game for Nintendo wii U
 10  * ("Wii U" is registered trademark of Nintendo,Inc.)
 11  */
 12 
 13 /**
 14  * plugin namespace object
 15  * @namespace
 16  * @type {Object}
 17  */
 18 enchant.wiiu = {};
 19 
 20 enchant.Event.L_STICK_MOVE = 'lstickmove';
 21 enchant.Event.R_STICK_MOVE = 'rstickmove';
 22 enchant.Event.DEVICE_MOTION = 'devicemotion';
 23 enchant.Event.DEVICE_ORIENTATION = 'deviceorientation';
 24 
 25 enchant.wiiu.Core = enchant.Class.create(enchant.Core, {
 26     initialize: function() {
 27         alert("width: " + window.innerWidth + " height: " + window.innerHeight);
 28         enchant.Core.apply(this, arguments);
 29 
 30         var label = new Label("");
 31         this.rootScene.addChild(label);
 32         var debug = function(str) {
 33             label.text += str + '<br />';
 34         };
 35 
 36         /**
 37          * disable default keybind
 38          */
 39         this._keybind = {};
 40 
 41         var touched = false;
 42         var prevData = {};
 43         var pushed = {};
 44 
 45         var keyEventTable = {
 46             'a': 0x8000,
 47             'b': 0x4000,
 48             'x': 0x2000,
 49             'y': 0x1000,
 50             'l': 0x0020,
 51             'r': 0x0010,
 52             'zl': 0x0080,
 53             'zr': 0x0040,
 54             'up': 0x0200,
 55             'down': 0x0100,
 56             'right': 0x0800,
 57             'left': 0x0400
 58         };
 59         var core = this;
 60         core.input.rstick = {x: 0, y: 0};
 61         core.input.lstick = {x: 0, y: 0};
 62 
 63         debug('wiiu?' + (window.wiiu ? "on" : "off"));
 64         for(var i in window.wiiu){
 65         }
 66 
 67         if (window.wiiu) {
 68             core.addEventListener("enterframe", function() {
 69                 /**
 70                  * watch data from wiiU controller
 71                  */
 72                 var data = window.wiiu.gamepad.update();
 73                 if (!data.isEnabled) {
 74                     console.log('Wii U Gamepad is not connected');
 75                 }
 76 
 77                 var evt, target;
 78 
 79                 core.input = {};
 80 
 81                 if (data.lStickX !== prevData.lStickX || data.lStickY !== prevData.lStickY) {
 82                     evt = new enchant.Event(enchant.Event.L_STICK_MOVE);
 83                     evt.x = data.lStickX;
 84                     evt.y = -data.lStickY;
 85                     core.dispatchEvent(evt);
 86                 }
 87                 core.input['lstick'] = {x: data.lStickX, y: -data.lStickY};
 88 
 89                 if (data.rStickX !== prevData.rStickX || data.rStickY !== prevData.rStickY) {
 90                     evt = new enchant.Event(enchant.Event.R_STICK_MOVE);
 91                     evt.x = data.rStickX;
 92                     evt.y = -data.rStickY;
 93                     core.dispatchEvent(evt);
 94                 }
 95                 core.input['rstick'] = {x: data.rStickX, y: -data.rStickY};
 96 
 97                 evt = new enchant.Event(enchant.Event.DEVICE_MOTION);
 98                 core.input['acc'] = {
 99                     x: evt.x = data.accX,
100                     y: evt.y = data.accY,
101                     z: evt.z = data.accZ
102                 };
103                 core.rootScene.dispatchEvent(evt);
104 
105                 evt = new enchant.Event(enchant.Event.DEVICE_ORIENTATION);
106                 core.input['angle'] = {
107                     x: evt.x = data.angleX,
108                     y: evt.y = data.angleY,
109                     z: evt.z = data.angleZ
110                 };
111                 core.rootScene.dispatchEvent(evt);
112 
113                 for(var type in keyEventTable){
114                     if(keyEventTable.hasOwnProperty(type)){
115                         var bitmask = keyEventTable[type];
116                         var hold = data.hold & bitmask;
117 
118                         if(hold && !core.input[type]){
119                             // Button data: On, Flag: Off -> buttondown
120                             core.input[type] = true;
121                             evt = new enchant.Event(type + 'buttondown');
122                             this.dispatchEvent(evt);
123                             debug(type + 'buttondown');
124                         }else if(!hold && core.input[type]){
125                             // Button data: On, Flag: Off -> buttonup
126                             delete core.input[type];
127                             evt = new enchant.Event(type  + 'buttonup');
128                             this.dispatchEvent(evt);
129                             debug(type + 'buttondown');
130                         }
131                     }
132                 }
133                 prevData = data;
134             });
135         } else {
136             console.log('This browser is not wiiU browser.');
137         }
138 
139     }
140 });