-
Notifications
You must be signed in to change notification settings - Fork 0
/
twine_gamepad.twee
executable file
·59 lines (53 loc) · 13.4 KB
/
twine_gamepad.twee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
:: Style - selected link [stylesheet]
/* Keep surrounding text from jittering when 2px borders are added. */
a.internalLink, a.externalLink {
padding: 2px;
}
/* Set the style for the currently-selected link. */
a.gamepadSelected {
border: 2px dotted blue;
padding: 0px;
}
:: Script loader [script]
/* @license
* Copyright 2012 Priit Kallas <[email protected]>
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
!function(a){"use strict";var b=function(){},c={getType:function(){return"null"},isSupported:function(){return!1},update:b},d=function(a){var c=this,d=window;this.update=b,this.requestAnimationFrame=a||d.requestAnimationFrame||d.webkitRequestAnimationFrame||d.mozRequestAnimationFrame,this.tickFunction=function(){c.update(),c.startTicker()},this.startTicker=function(){c.requestAnimationFrame.apply(d,[c.tickFunction])}};d.prototype.start=function(a){this.update=a||b,this.startTicker()};var e=function(){};e.prototype.update=b,e.prototype.start=function(a){this.update=a||b};var f=function(a,b){this.listener=a,this.gamepadGetter=b,this.knownGamepads=[]};f.factory=function(a){var b=c,d=window&&window.navigator;return d&&(void 0!==d.getGamepads?b=new f(a,function(){return d.getGamepads()}):void 0!==d.webkitGamepads?b=new f(a,function(){return d.webkitGamepads()}):void 0!==d.webkitGetGamepads&&(b=new f(a,function(){return d.webkitGetGamepads()}))),b},f.getType=function(){return"WebKit"},f.prototype.getType=function(){return f.getType()},f.prototype.isSupported=function(){return!0},f.prototype.update=function(){var a,b,c=this,d=Array.prototype.slice.call(this.gamepadGetter(),0);for(b=this.knownGamepads.length-1;0<=b;b--)a=this.knownGamepads[b],d.indexOf(a)<0&&(this.knownGamepads.splice(b,1),this.listener._disconnect(a));for(b=0;b<d.length;b++)(a=d[b])&&c.knownGamepads.indexOf(a)<0&&(c.knownGamepads.push(a),c.listener._connect(a))};var g=function(a){this.listener=a,window.addEventListener("gamepadconnected",function(b){a._connect(b.gamepad)}),window.addEventListener("gamepaddisconnected",function(b){a._disconnect(b.gamepad)})};g.factory=function(a){var b=c;return window&&void 0!==window.addEventListener&&(b=new g(a)),b},g.getType=function(){return"Firefox"},g.prototype.getType=function(){return g.getType()},g.prototype.isSupported=function(){return-1!==(window&&window.navigator).userAgent.indexOf("Firefox")},g.prototype.update=b;var h=function(a){this.updateStrategy=a||new d,this.gamepads=[],this.listeners={},this.platform=c,this.deadzone=.03,this.maximizeThreshold=.97};h.UpdateStrategies={AnimFrameUpdateStrategy:d,ManualUpdateStrategy:e},h.PlatformFactories=[g.factory,f.factory],h.Type={N64:"n64",PLAYSTATION:"playstation",LOGITECH:"logitech",XBOX:"xbox",UNKNOWN:"unknown"},h.Event={CONNECTED:"connected",UNSUPPORTED:"unsupported",DISCONNECTED:"disconnected",TICK:"tick",BUTTON_DOWN:"button-down",BUTTON_UP:"button-up",AXIS_CHANGED:"axis-changed"},h.StandardButtons=["FACE_1","FACE_2","FACE_3","FACE_4","LEFT_TOP_SHOULDER","RIGHT_TOP_SHOULDER","LEFT_BOTTOM_SHOULDER","RIGHT_BOTTOM_SHOULDER","SELECT_BACK","START_FORWARD","LEFT_STICK","RIGHT_STICK","DPAD_UP","DPAD_DOWN","DPAD_LEFT","DPAD_RIGHT","HOME"],h.StandardAxes=["LEFT_STICK_X","LEFT_STICK_Y","RIGHT_STICK_X","RIGHT_STICK_Y"];var i=function(a,b,c){return b<a.length?a[b]:c+(b-a.length+1)};h.StandardMapping={env:{},buttons:{byButton:[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]},axes:{byAxis:[0,1,2,3]}},h.Mappings=[{env:{platform:g.getType(),type:h.Type.N64},buttons:{byButton:[2,1,3,0,4,5,-1,-1,8,9,-1,-1,12,13,14,15,-1]},axes:{byAxis:[1,2,-1,-1]}},{env:{platform:f.getType(),type:h.Type.N64},buttons:{byButton:[2,1,3,0,4,5,-1,-1,8,9,-1,-1,12,13,14,15,-1]},axes:{byAxis:[0,1,-1,-1]}},{env:{platform:g.getType(),type:h.Type.XBOX},buttons:{byButton:[0,1,2,3,4,5,15,16,9,8,6,7,11,12,13,14,10]},axes:{byAxis:[0,1,2,3]}},{env:{platform:g.getType(),type:h.Type.PLAYSTATION},buttons:{byButton:[14,13,15,12,10,11,8,9,0,3,1,2,4,6,7,5,16]},axes:{byAxis:[0,1,2,3]}},{env:{platform:f.getType(),type:h.Type.LOGITECH},buttons:{byButton:[1,2,0,3,4,5,6,7,8,9,10,11,11,12,13,14,10]},axes:{byAxis:[0,1,2,3]}},{env:{platform:g.getType(),type:h.Type.LOGITECH},buttons:{byButton:[0,1,2,3,4,5,-1,-1,6,7,8,9,11,12,13,14,10],byAxis:[-1,-1,-1,-1,-1,-1,[2,0,1],[2,0,-1]]},axes:{byAxis:[0,1,3,4]}}],h.prototype.init=function(){var a=h.resolvePlatform(this),b=this;return this.platform=a,this.updateStrategy.start(function(){b._update()}),a.isSupported()},h.prototype.bind=function(a,b){return void 0===this.listeners[a]&&(this.listeners[a]=[]),this.listeners[a].push(b),this},h.prototype.unbind=function(a,b){if(void 0!==a){if(void 0!==b){if(void 0===this.listeners[a])return!1;for(var c=0;c<this.listeners[a].length;c++)if(this.listeners[a][c]===b)return this.listeners[a].splice(c,1),!0;return!1}this.listeners[a]=[]}else this.listeners={}},h.prototype.count=function(){return this.gamepads.length},h.prototype._fire=function(a,b){if(void 0!==this.listeners[a])for(var c=0;c<this.listeners[a].length;c++)this.listeners[a][c].apply(this.listeners[a][c],[b])},h.getNullPlatform=function(){return Object.create(c)},h.resolvePlatform=function(a){var b,d=c;for(b=0;!d.isSupported()&&b<h.PlatformFactories.length;b++)d=h.PlatformFactories[b](a);return d},h.prototype._connect=function(a){var b,c,d=this._resolveMapping(a);for(a.state={},a.lastState={},a.updater=[],b=d.buttons.byButton.length,c=0;c<b;c++)this._addButtonUpdater(a,d,c);for(b=d.axes.byAxis.length,c=0;c<b;c++)this._addAxisUpdater(a,d,c);this.gamepads[a.index]=a,this._fire(h.Event.CONNECTED,a)},h.prototype._addButtonUpdater=function(a,c,d){var e,f=i(h.StandardButtons,d,"EXTRA_BUTTON_"),g=this._createButtonGetter(a,c.buttons,d),j=this,k={gamepad:a,control:f};a.state[f]=0,a.lastState[f]=0,e=function(){var b=g(),c=a.lastState[f],d=.5<b,e=.5<c;a.state[f]=b,d&&!e?j._fire(h.Event.BUTTON_DOWN,Object.create(k)):!d&&e&&j._fire(h.Event.BUTTON_UP,Object.create(k)),0!==b&&1!==b&&b!==c&&j._fireAxisChangedEvent(a,f,b),a.lastState[f]=b},a.updater.push(e)},h.prototype._addAxisUpdater=function(a,c,d){var e,f=i(h.StandardAxes,d,"EXTRA_AXIS_"),g=this._createAxisGetter(a,c.axes,d),j=this;a.state[f]=0,a.lastState[f]=0,e=function(){var b=g(),c=a.lastState[f];(a.state[f]=b)!==c&&j._fireAxisChangedEvent(a,f,b),a.lastState[f]=b},a.updater.push(e)},h.prototype._fireAxisChangedEvent=function(a,b,c){var d={gamepad:a,axis:b,value:c};this._fire(h.Event.AXIS_CHANGED,d)},h.prototype._createButtonGetter=function(){var a=function(){return 0};return function(d,e,f){var g,h=a,i=this;return-1!==(g=e.byButton[f])?"number"==typeof g&&g<d.buttons.length&&(h=function(){var a=d.buttons[g];return"number"==typeof a?a:"number"==typeof a.value?a.value:0}):e.byAxis&&f<e.byAxis.length&&function(a){return"[object Array]"===Object.prototype.toString.call(a)}(g=e.byAxis[f])&&3==g.length&&g[0]<d.axes.length&&(h=function(b,c,d){var e=a;return c<d?e=function(){var a=d-c,e=b();return(e=(e-c)/a)<0?0:e}:d<c&&(e=function(){var a=c-d,e=b();return 1<(e=(e-d)/a)?0:1-e}),e}(h=function(){var a=d.axes[g[0]];return i._applyDeadzoneMaximize(a)},g[1],g[2])),h}}(),h.prototype._createAxisGetter=function(){var a=function(){return 0};return function(b,c,d){var e,f=a,g=this;return-1!==(e=c.byAxis[d])&&"number"==typeof e&&e<b.axes.length&&(f=function(){var a=b.axes[e];return g._applyDeadzoneMaximize(a)}),f}}(),h.prototype._disconnect=function(a){var b,c=[];for(void 0!==this.gamepads[a.index]&&delete this.gamepads[a.index],b=0;b<this.gamepads.length;b++)void 0!==this.gamepads[b]&&(c[b]=this.gamepads[b]);this.gamepads=c,this._fire(h.Event.DISCONNECTED,a)},h.prototype._resolveControllerType=function(a){return-1!==(a=a.toLowerCase().replace(/\s+/g," ").replace(/^\s+|\s+$/g,"")).indexOf("playstation")?h.Type.PLAYSTATION:-1!==a.indexOf("logitech")||-1!==a.indexOf("wireless gamepad")?h.Type.LOGITECH:-1!==a.indexOf("xbox")||-1!==a.indexOf("360")?h.Type.XBOX:-1!==a.indexOf("79-6-generic")&&-1!==a.indexOf("joystick")||-1!==a.indexOf("vendor: 0079 product: 0006")&&-1!==a.indexOf("generic usb joystick")?h.Type.N64:h.Type.UNKNOWN},h.prototype._resolveMapping=function(a){var b,c,d=h.Mappings,e=null,f={platform:this.platform.getType(),type:this._resolveControllerType(a.id)};for(b=0;!e&&b<d.length;b++)c=d[b],h.envMatchesFilter(c.env,f)&&(e=c);return e||h.StandardMapping},h.envMatchesFilter=function(a,b){var c,d=!0;for(c in a)a[c]!==b[c]&&(d=!1);return d},h.prototype._update=function(){this.platform.update(),this.gamepads.forEach(function(a){a&&a.updater.forEach(function(a){a()})}),0<this.gamepads.length&&this._fire(h.Event.TICK,this.gamepads)},h.prototype._applyDeadzoneMaximize=function(a,b,c){return b=void 0!==b?b:this.deadzone,c=void 0!==c?c:this.maximizeThreshold,0<=a?a<b?a=0:c<a&&(a=1):-b<a?a=0:a<-c&&(a=-1),a},a.Gamepad=h}("undefined"!=typeof module&&module.exports||window),
/* @license
The MIT License (MIT)
Copyright (c) 2014 mildmojo
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
function(exports){var Gamepad=window.Gamepad,gamepad=new Gamepad,axes={LEFT_STICK_X:0,LEFT_STICK_Y:0};function showLink(){var selected=document.querySelector(".gamepadSelected");if(selected&&!function(el){var offset=el.getBoundingClientRect();if(offset.left<0||offset.top<0)return!1;if(offset.left>window.innerWidth||offset.top>window.innerHeight)return!1;return!0}(selected)){var position=selected.getBoundingClientRect().top<0;selected.scrollIntoView(position)}}function getTwineLinks(){return document.querySelectorAll("a.internalLink, a.externalLink")}function findSelectedIndex(links){for(var i=0;i<links.length;i++)if(hasClass(links[i],"gamepadSelected"))return i;return null}function hasClass(el,className){return el.classList?el.classList.contains(className):new RegExp("(^| )"+className+"( |$)","gi").test(el.className)}function removeClass(el,className){el.classList?el.classList.remove(className):el.className=el.className.replace(new RegExp("(^|\\b)"+className.split(" ").join("|")+"(\\b|$)","gi")," ")}function addClass(el,className){el.classList?el.classList.add(className):el.className+=" "+className}gamepad.bind(Gamepad.Event.CONNECTED,function(_device){console.log("Gamepad connected.")}),gamepad.bind(Gamepad.Event.DISCONNECTED,function(_device){console.log("Gamepad disconnected.")}),gamepad.bind(Gamepad.Event.BUTTON_DOWN,function(e){switch(e.control){case"DPAD_DOWN":case"DPAD_RIGHT":!function(){var links=getTwineLinks();if(null===links)return;var selectedIndex=findSelectedIndex(links),newIndex=0;null!==selectedIndex&&(removeClass(links[selectedIndex],"gamepadSelected"),newIndex=(selectedIndex+1)%links.length);addClass(links[newIndex],"gamepadSelected"),showLink()}();break;case"DPAD_UP":case"DPAD_LEFT":!function(){var links=getTwineLinks();if(null===links)return;var selectedIndex=findSelectedIndex(links),newIndex=0;null!==selectedIndex&&(removeClass(links[selectedIndex],"gamepadSelected"),newIndex=0<selectedIndex?selectedIndex-1:links.length-1);addClass(links[newIndex],"gamepadSelected"),showLink()}();break;case"FACE_1":case"FACE_2":case"FACE_3":case"FACE_4":var selected=document.querySelector(".gamepadSelected");selected&&(removeClass(selected,"gamepadSelected"),selected.dispatchEvent(new MouseEvent("click")))}}),gamepad.bind(Gamepad.Event.AXIS_CHANGED,function(data){var newValue=0;.5<Math.abs(data.value)&&(newValue=data.value<0?-1:1,axes[data.axis]!==newValue&&("LEFT_STICK_X"===data.axis?-1===newValue?gamepad._fire(Gamepad.Event.BUTTON_DOWN,{control:"DPAD_LEFT"}):gamepad._fire(Gamepad.Event.BUTTON_DOWN,{control:"DPAD_RIGHT"}):"LEFT_STICK_Y"===data.axis&&(1===newValue?gamepad._fire(Gamepad.Event.BUTTON_DOWN,{control:"DPAD_DOWN"}):gamepad._fire(Gamepad.Event.BUTTON_DOWN,{control:"DPAD_UP"})))),axes[data.axis]=newValue}),gamepad.init()}("undefined"!=typeof module&&module.exports||window);