Home Reference Source Repository

js/GameController/GCGamepad.js

  1. 'use strict'
  2.  
  3. import NSObject from '../ObjectiveC/NSObject'
  4. import GCController from './GCController'
  5. //import GCGamepadValueChangedHandler from './GCGamepadValueChangedHandler'
  6. import GCControllerButtonInput from './GCControllerButtonInput'
  7. import GCControllerDirectionPad from './GCControllerDirectionPad'
  8. //import GCGamepadSnapshot from './GCGamepadSnapshot'
  9.  
  10. const _defaultMapping = {
  11. A: 0,
  12. B: 1,
  13. X: 2,
  14. Y: 3,
  15. L1: 4,
  16. R1: 5,
  17. UP: 12,
  18. DOWN: 13,
  19. LEFT: 14,
  20. RIGHT: 15
  21. }
  22.  
  23. if(typeof navigator !== 'undefined' && navigator.userAgent.indexOf('Firefox') !== -1){
  24. // Is this a bug or something?
  25. _defaultMapping.A = 1
  26. _defaultMapping.B = 2
  27. _defaultMapping.X = 0
  28. _defaultMapping.Y = 3
  29. _defaultMapping.UP = 14
  30. _defaultMapping.DOWN = 15
  31. _defaultMapping.LEFT = 16
  32. _defaultMapping.RIGHT = 17
  33. }
  34.  
  35. /**
  36. * The standard set of gamepad controls.
  37. * @access public
  38. * @extends {NSObject}
  39. * @see https://developer.apple.com/documentation/gamecontroller/gcgamepad
  40. */
  41. export default class GCGamepad extends NSObject {
  42.  
  43. /**
  44. * constructor
  45. * @access public
  46. */
  47. constructor() {
  48. super()
  49.  
  50. // Determining the Controller That Owns This Profile
  51.  
  52. this._controller = null
  53.  
  54. // Determining When Any Element in the Profile Changes
  55.  
  56. /**
  57. * A block called when any element in the profile changes.
  58. * @type {?GCGamepadValueChangedHandler}
  59. * @see https://developer.apple.com/documentation/gamecontroller/gcgamepad/1497421-valuechangedhandler
  60. */
  61. this.valueChangedHandler = null
  62.  
  63. // Reading Shoulder Button Inputs
  64.  
  65. this._leftShoulder = new GCControllerButtonInput()
  66. this._rightShoulder = new GCControllerButtonInput()
  67.  
  68. // Reading Directional Pad Inputs
  69.  
  70. /**
  71. * @type {GCControllerDirectionPad}
  72. */
  73. this._dpad = new GCControllerDirectionPad()
  74.  
  75. // Reading Face Button Inputs
  76.  
  77. this._buttonA = new GCControllerButtonInput()
  78. this._buttonB = new GCControllerButtonInput()
  79. this._buttonX = new GCControllerButtonInput()
  80. this._buttonY = new GCControllerButtonInput()
  81.  
  82. this._buttonMapping = new Map([
  83. [ this._leftShoulder, _defaultMapping.L1 ],
  84. [ this._rightShoulder, _defaultMapping.R1 ],
  85. [ this._buttonA, _defaultMapping.A ],
  86. [ this._buttonB, _defaultMapping.B ],
  87. [ this._buttonX, _defaultMapping.X ],
  88. [ this._buttonY, _defaultMapping.Y ]
  89. ])
  90. this._dpadMapping = new Map([
  91. [ this._dpad._up, _defaultMapping.UP ],
  92. [ this._dpad._down, _defaultMapping.DOWN ],
  93. [ this._dpad._left, _defaultMapping.LEFT ],
  94. [ this._dpad._right, _defaultMapping.RIGHT ]
  95. ])
  96. }
  97.  
  98. _update() {
  99. this._buttonMapping.forEach((index, c) => {
  100. c._value = this._controller._state.buttons[index]
  101. c._isPressed = this._controller._state.pressed[index]
  102. })
  103. this._dpadMapping.forEach((index, c) => {
  104. c._value = this._controller._state.buttons[index]
  105. c._isPressed = this._controller._state.pressed[index]
  106. })
  107. this._dpad._xAxis._value = this._dpad._right._value - this._dpad._left._value
  108. this._dpad._yAxis._value = this._dpad._down._value - this._dpad._up._value
  109.  
  110. this._buttonMapping.forEach((index, c) => {
  111. if(c.pressedChangedHandler && this._controller._updated.pressed[index]){
  112. c.pressedChangedHandler(c, c._value, c._isPressed)
  113. }
  114. if(this._controller._updated.buttons[index]){
  115. if(c.valueChangedHandler){
  116. c.valueChangedHandler(c, c._value, c._isPressed)
  117. }
  118. if(this.valueChangedHandler){
  119. this.valueChangedHandler(this, c)
  120. }
  121. }
  122. })
  123. let dpadChanged = false
  124. this._dpadMapping.forEach((index, c) => {
  125. if(this._controller._updated.pressed[index]){
  126. dpadChanged = true
  127. if(c.pressedChangedHandler){
  128. c.pressedChangedHandler(c, c._value, c._isPressed)
  129. }
  130. }
  131. if(this._controller._updated.buttons[index]){
  132. dpadChanged = true
  133. if(c.valueChangedHandler){
  134. c.valueChangedHandler(c, c._value, c._isPressed)
  135. }
  136. if(this.valueChangedHandler){
  137. this.valueChangedHandler(this, c)
  138. }
  139. }
  140. })
  141. if(dpadChanged){
  142. if(this._dpad.valueChangedHandler){
  143. this._dpad.valueChangedHandler(this._dpad, this._dpad._xAxis.value, this._dpad._yAxis.value)
  144. }
  145. if(this.valueChangedHandler){
  146. this.valueChangedHandler(this, this._dpad)
  147. }
  148. }
  149. }
  150.  
  151. _getValue(button) {
  152. let index = this._buttonMapping.get(button)
  153. if(typeof index === 'undefined'){
  154. index = this._dpadMapping.get(button)
  155. }
  156. if(typeof index === 'undefined'){
  157. return null
  158. }
  159.  
  160. return this._controller._state.buttons[index]
  161. }
  162.  
  163.  
  164. // Determining the Controller That Owns This Profile
  165.  
  166. /**
  167. * The controller this profile is associated with.
  168. * @type {?GCController}
  169. * @desc
  170. * @see https://developer.apple.com/documentation/gamecontroller/gcgamepad/1497428-controller
  171. */
  172. get controller() {
  173. return this._controller
  174. }
  175.  
  176. // Reading Shoulder Button Inputs
  177.  
  178. /**
  179. * The left shoulder button element.
  180. * @type {GCControllerButtonInput}
  181. * @desc The shoulder buttons in the gamepad profile are analog buttons.
  182. * @see https://developer.apple.com/documentation/gamecontroller/gcgamepad/1497423-leftshoulder
  183. */
  184. get leftShoulder() {
  185. return this._leftShoulder
  186. }
  187.  
  188. /**
  189. * The right shoulder button element.
  190. * @type {GCControllerButtonInput}
  191. * @desc The shoulder buttons in the gamepad profile are analog buttons.
  192. * @see https://developer.apple.com/documentation/gamecontroller/gcgamepad/1497429-rightshoulder
  193. */
  194. get rightShoulder() {
  195. return this._rightShoulder
  196. }
  197.  
  198. // Reading Directional Pad Inputs
  199.  
  200. /**
  201. * The D-pad element.
  202. * @type {GCControllerDirectionPad}
  203. * @desc The directional pad in the gamepad profile is an analog control.
  204. * @see https://developer.apple.com/documentation/gamecontroller/gcgamepad/1497425-dpad
  205. */
  206. get dpad() {
  207. return this._dpad
  208. }
  209.  
  210. // Reading Face Button Inputs
  211.  
  212. /**
  213. * The bottom face button.
  214. * @type {GCControllerButtonInput}
  215. * @desc The face buttons in the gamepad profile are analog buttons.
  216. * @see https://developer.apple.com/documentation/gamecontroller/gcgamepad/1497427-buttona
  217. */
  218. get buttonA() {
  219. return this._buttonA
  220. }
  221.  
  222. /**
  223. * The right face button.
  224. * @type {GCControllerButtonInput}
  225. * @desc The face buttons in the gamepad profile are analog buttons.
  226. * @see https://developer.apple.com/documentation/gamecontroller/gcgamepad/1497418-buttonb
  227. */
  228. get buttonB() {
  229. return this._buttonB
  230. }
  231.  
  232. /**
  233. * The left face button.
  234. * @type {GCControllerButtonInput}
  235. * @desc The face buttons in the gamepad profile are analog buttons.
  236. * @see https://developer.apple.com/documentation/gamecontroller/gcgamepad/1497417-buttonx
  237. */
  238. get buttonX() {
  239. return this._buttonX
  240. }
  241.  
  242. /**
  243. * The top face button.
  244. * @type {GCControllerButtonInput}
  245. * @desc The face buttons in the gamepad profile are analog buttons.
  246. * @see https://developer.apple.com/documentation/gamecontroller/gcgamepad/1497431-buttony
  247. */
  248. get buttonY() {
  249. return this._buttonY
  250. }
  251.  
  252. // Saving a Snapshot
  253.  
  254. /**
  255. * Saves a snapshot of all of the profile’s elements.
  256. * @access public
  257. * @returns {GCGamepadSnapshot} -
  258. * @see https://developer.apple.com/documentation/gamecontroller/gcgamepad/1497415-savesnapshot
  259. */
  260. saveSnapshot() {
  261. return null
  262. }
  263. }