diff --git a/src/gamepad.coffee b/src/gamepad.coffee index 29e294a..07edc16 100644 --- a/src/gamepad.coffee +++ b/src/gamepad.coffee @@ -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 diff --git a/src/init.coffee b/src/init.coffee index 656b7eb..c4e31de 100644 --- a/src/init.coffee +++ b/src/init.coffee @@ -1,52 +1,64 @@ +# Select the previous choice +selectUp = (event, button) -> + if button != "dpad up" and button != "left stick up" + return + if $(".options li").length == 0 + return + $(".options li").removeClass("active") + count = $(".options li").length + window.selectedoption ?= count + 1 + window.selectedoption-- + if window.selectedoption < 0 + window.selectedoption = count + $(".options li:nth-child(#{window.selectedoption}").addClass("active") + +# 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++ + count = $(".options li").length + if window.selectedoption > count + window.selectedoption = 1 + $(".options li:nth-child(#{window.selectedoption})").addClass("active") + +selectOption = (event, button) -> + if button != "a" + return + $(".options li.active").click() + +# 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", () -> - # An array containing the connected gamepads (see Gamepad class) - salet.view.gamepads = [] + # 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) + salet.view.gamepads[e.gamepad.id] = new Gamepad(e.gamepad) ) window.addEventListener("gamepaddisconnected", (e) -> - salet.view.gamepads[e.gamepad.index] = undefined + salet.view.gamepads[e.gamepad.id] = undefined ) - if (typeof navigator.getGamepads == "function") - for pad in navigator.getGamepads() - if pad? - salet.view.gamepads[pad.index] = new Gamepad(pad) + setInterval(joystick, 50) # 50 = 1000 / 20 fps }) ) -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 - if gamepad.button("dpad up") or - gamepad.button("left stick up") - $(".options li").removeClass("active") - count = $(".options li").length - window.selectedoption ?= count + 1 - window.selectedoption-- - 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") - $(".options li").removeClass("active") - window.selectedoption ?= -1 - window.selectedoption++ - count = $(".options li").length - if window.selectedoption > count - window.selectedoption = 1 - $(".options li:nth-child(#{window.selectedoption})").addClass("active") - if gamepad.button("a") - $(".options li.active").click() - -setInterval(joystick, 100) +$(document).on("press", selectUp) +$(document).on("press", selectDown) +$(document).on("press", selectOption)