Event-based button handlers
This commit is contained in:
parent
1d1ea665ef
commit
b991ee03ce
|
@ -3,6 +3,7 @@ class Gamepad
|
|||
constructor: (pad) ->
|
||||
@gamepad = pad
|
||||
@map = @detectMapping(pad.id, navigator.userAgent)
|
||||
@pressed = @getPressed()
|
||||
detectMapping: (id, browser) ->
|
||||
for map in salet.view.gamepadmappings
|
||||
for device in map.supported
|
||||
|
@ -30,7 +31,7 @@ class Gamepad
|
|||
map = @map.buttons[name]
|
||||
unless map?
|
||||
return 0
|
||||
if map.index?
|
||||
if map.index? and @gamepad.buttons[map.index]?
|
||||
return @gamepad.buttons[map.index].pressed
|
||||
if map.axis?
|
||||
if map.direction < 0
|
||||
|
@ -38,3 +39,17 @@ class Gamepad
|
|||
else
|
||||
return @gamepad.axes[map.axis] > 0.75
|
||||
return false
|
||||
# Return the name of the pressed button
|
||||
getPressed: () ->
|
||||
for buttonname, button of @map.buttons
|
||||
if (
|
||||
@gamepad.buttons[button.index]? and
|
||||
@gamepad.buttons[button.index].pressed
|
||||
) or (
|
||||
@gamepad.axes[button.axis] < -0.75 and
|
||||
button.direction < 0
|
||||
) or (
|
||||
@gamepad.axes[button.axis] > 0.75 and
|
||||
button.direction > 0
|
||||
)
|
||||
return buttonname
|
||||
|
|
|
@ -1,35 +1,9 @@
|
|||
$(document).on("viewinit", () ->
|
||||
# An array containing the connected gamepads (see Gamepad class)
|
||||
salet.view.gamepads = []
|
||||
jQuery.ajax({
|
||||
dataType: 'json',
|
||||
url: salet.view.gamepadmappings,
|
||||
success: (data) ->
|
||||
salet.view.gamepadmappings = data
|
||||
window.addEventListener("gamepadconnected", (e) ->
|
||||
salet.view.gamepads[e.gamepad.index] = new Gamepad(e.gamepad)
|
||||
)
|
||||
window.addEventListener("gamepaddisconnected", (e) ->
|
||||
salet.view.gamepads[e.gamepad.index] = undefined
|
||||
)
|
||||
if (typeof navigator.getGamepads == "function")
|
||||
for pad in navigator.getGamepads()
|
||||
if pad?
|
||||
salet.view.gamepads[pad.index] = new Gamepad(pad)
|
||||
})
|
||||
)
|
||||
|
||||
joystick = () ->
|
||||
salet.view.gamepads = []
|
||||
if typeof navigator.getGamepads == "function"
|
||||
for pad in navigator.getGamepads()
|
||||
if pad?
|
||||
salet.view.gamepads[pad.index] = new Gamepad(pad)
|
||||
gamepad = salet.view.gamepads[0]
|
||||
if $(".options").length == 0
|
||||
# Select the previous choice
|
||||
selectUp = (event, button) ->
|
||||
if button != "dpad up" and button != "left stick up"
|
||||
return
|
||||
if $(".options li").length == 0
|
||||
return
|
||||
if gamepad.button("dpad up") or
|
||||
gamepad.button("left stick up")
|
||||
$(".options li").removeClass("active")
|
||||
count = $(".options li").length
|
||||
window.selectedoption ?= count + 1
|
||||
|
@ -37,8 +11,13 @@ joystick = () ->
|
|||
if window.selectedoption < 0
|
||||
window.selectedoption = count
|
||||
$(".options li:nth-child(#{window.selectedoption}").addClass("active")
|
||||
if gamepad.button("dpad down") or
|
||||
gamepad.button("left stick down")
|
||||
|
||||
# Select the next choice
|
||||
selectDown = (event, button) ->
|
||||
if button != "dpad down" and button != "left stick down"
|
||||
return
|
||||
if $(".options li").length == 0
|
||||
return
|
||||
$(".options li").removeClass("active")
|
||||
window.selectedoption ?= -1
|
||||
window.selectedoption++
|
||||
|
@ -46,7 +25,40 @@ joystick = () ->
|
|||
if window.selectedoption > count
|
||||
window.selectedoption = 1
|
||||
$(".options li:nth-child(#{window.selectedoption})").addClass("active")
|
||||
if gamepad.button("a")
|
||||
|
||||
selectOption = (event, button) ->
|
||||
if button != "a"
|
||||
return
|
||||
$(".options li.active").click()
|
||||
|
||||
setInterval(joystick, 100)
|
||||
# Joystick check, fires events
|
||||
joystick = () ->
|
||||
if typeof navigator.getGamepads == "function"
|
||||
for pad in navigator.getGamepads()
|
||||
if pad?
|
||||
temp = new Gamepad(pad)
|
||||
if temp.pressed?
|
||||
$(document).trigger("press", temp.pressed)
|
||||
salet.view.gamepads[pad.id] = temp
|
||||
|
||||
$(document).on("viewinit", () ->
|
||||
# Connected gamepads (see Gamepad class)
|
||||
salet.view.gamepads = {}
|
||||
jQuery.ajax({
|
||||
dataType: 'json',
|
||||
url: salet.view.gamepadmappings,
|
||||
success: (data) ->
|
||||
salet.view.gamepadmappings = data
|
||||
window.addEventListener("gamepadconnected", (e) ->
|
||||
salet.view.gamepads[e.gamepad.id] = new Gamepad(e.gamepad)
|
||||
)
|
||||
window.addEventListener("gamepaddisconnected", (e) ->
|
||||
salet.view.gamepads[e.gamepad.id] = undefined
|
||||
)
|
||||
setInterval(joystick, 50) # 50 = 1000 / 20 fps
|
||||
})
|
||||
)
|
||||
|
||||
$(document).on("press", selectUp)
|
||||
$(document).on("press", selectDown)
|
||||
$(document).on("press", selectOption)
|
||||
|
|
Loading…
Reference in New Issue