mirror of
https://gitlab.com/Oreolek/salet-gamepad-module.git
synced 2024-04-29 23:59:37 +03:00
Event-based button handlers
This commit is contained in:
parent
1d1ea665ef
commit
b991ee03ce
|
@ -3,6 +3,7 @@ class Gamepad
|
||||||
constructor: (pad) ->
|
constructor: (pad) ->
|
||||||
@gamepad = pad
|
@gamepad = pad
|
||||||
@map = @detectMapping(pad.id, navigator.userAgent)
|
@map = @detectMapping(pad.id, navigator.userAgent)
|
||||||
|
@pressed = @getPressed()
|
||||||
detectMapping: (id, browser) ->
|
detectMapping: (id, browser) ->
|
||||||
for map in salet.view.gamepadmappings
|
for map in salet.view.gamepadmappings
|
||||||
for device in map.supported
|
for device in map.supported
|
||||||
|
@ -30,7 +31,7 @@ class Gamepad
|
||||||
map = @map.buttons[name]
|
map = @map.buttons[name]
|
||||||
unless map?
|
unless map?
|
||||||
return 0
|
return 0
|
||||||
if map.index?
|
if map.index? and @gamepad.buttons[map.index]?
|
||||||
return @gamepad.buttons[map.index].pressed
|
return @gamepad.buttons[map.index].pressed
|
||||||
if map.axis?
|
if map.axis?
|
||||||
if map.direction < 0
|
if map.direction < 0
|
||||||
|
@ -38,3 +39,17 @@ class Gamepad
|
||||||
else
|
else
|
||||||
return @gamepad.axes[map.axis] > 0.75
|
return @gamepad.axes[map.axis] > 0.75
|
||||||
return false
|
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", () ->
|
# Select the previous choice
|
||||||
# An array containing the connected gamepads (see Gamepad class)
|
selectUp = (event, button) ->
|
||||||
salet.view.gamepads = []
|
if button != "dpad up" and button != "left stick up"
|
||||||
jQuery.ajax({
|
return
|
||||||
dataType: 'json',
|
if $(".options li").length == 0
|
||||||
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
|
|
||||||
return
|
return
|
||||||
if gamepad.button("dpad up") or
|
|
||||||
gamepad.button("left stick up")
|
|
||||||
$(".options li").removeClass("active")
|
$(".options li").removeClass("active")
|
||||||
count = $(".options li").length
|
count = $(".options li").length
|
||||||
window.selectedoption ?= count + 1
|
window.selectedoption ?= count + 1
|
||||||
|
@ -37,8 +11,13 @@ joystick = () ->
|
||||||
if window.selectedoption < 0
|
if window.selectedoption < 0
|
||||||
window.selectedoption = count
|
window.selectedoption = count
|
||||||
$(".options li:nth-child(#{window.selectedoption}").addClass("active")
|
$(".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")
|
$(".options li").removeClass("active")
|
||||||
window.selectedoption ?= -1
|
window.selectedoption ?= -1
|
||||||
window.selectedoption++
|
window.selectedoption++
|
||||||
|
@ -46,7 +25,40 @@ joystick = () ->
|
||||||
if window.selectedoption > count
|
if window.selectedoption > count
|
||||||
window.selectedoption = 1
|
window.selectedoption = 1
|
||||||
$(".options li:nth-child(#{window.selectedoption})").addClass("active")
|
$(".options li:nth-child(#{window.selectedoption})").addClass("active")
|
||||||
if gamepad.button("a")
|
|
||||||
|
selectOption = (event, button) ->
|
||||||
|
if button != "a"
|
||||||
|
return
|
||||||
$(".options li.active").click()
|
$(".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 a new issue