Giter Club home page Giter Club logo

snes-gamepad-cljs's Introduction

SNES Gamepad

A ClojureScript library providing a simple event handler interface for using USB SNES gamepads in HTML5 game projects.

Clojars Project

For more on the ๐ŸŽฎ Gamepad API, see https://w3c.github.io/gamepad/ and https://developer.mozilla.org/en-US/docs/Web/API/Gamepad_API.

Quick Start

The SNES Gamepad library allows you to bind handler functions to SNES gamepad events, such as on-connected or on-start-button-pressed.

(ns your.app.namespace
  (:require [snes-gamepad.mapping :as button-mapping]
            [snes-gamepad.gamepad :as snes-gamepad]))

(def gamepad (-> (snes-gamepad/snes-gamepad button-mapping/kiwitata)
                 (snes-gamepad/on-connected #(.log js/console "SNES gamepad connected!"))
                 (snes-gamepad/on-disconnected #(.log js/console "SNES gamepad disconnected."))
                 (snes-gamepad/on-b-button-pressed #(.log js/console "Samus jumps into the air!"))))

(snes-gamepad/enable! gamepad)

Manually Reading Pressed Buttons

If you need to read the list of pressed gamepad buttons manually, you can do so with the pressed-buttons! function.

(snes-gamepad/pressed-buttons! gamepad)

The pressed-buttons! function returns a vector of keywords representing each button that is currently pressed:

[:up :down :left :right :a :b :x :y :left-bumper :right-bumper :select :start]

If no buttons are being pressed on the gamepad, or if there is no gamepad connected, this function returns an empty vector.

You are encouraged to place your manual checking for button presses inside an on-connected callback, preferably within a .setInterval or .requestAnimationFrame function that can be cancelled if the on-disconnected callback fires.

Function Reference

Function Parameters Description
snes-gamepad button-mapping Returns an SNES gamepad with the given button mapping and no button press handlers.
enable! gamepad Enables a connected SNES gamepad. If no gamepad is connected, the browser will wait for one to be connected before firing an on-connected event.
disable! gamepad Disables an SNES gamepad. The browser will stop listening for gamepad connections and button press events until enable! is called.
on-connected gamepad handler Binds a handler function to execute when an SNES gamepad is connected.
on-disconnected gamepad handler Binds a handler function to execute when an SNES gamepad is disconnected.
on-up-button-pressed gamepad handler Binds a handler function to execute when the up button is pressed on the directional pad.
on-down-button-pressed gamepad handler Binds a handler function to execute when the down button is pressed on the directional pad.
on-left-button-pressed gamepad handler Binds a handler function to execute when the left button is pressed on the directional pad.
on-right-button-pressed gamepad handler Binds a handler function to execute when the right button is pressed on the directional pad.
on-a-button-pressed gamepad handler Binds a handler function to execute when the A button is pressed.
on-b-button-pressed gamepad handler Binds a handler function to execute when the B button is pressed.
on-x-button-pressed gamepad handler Binds a handler function to execute when the X button is pressed.
on-y-button-pressed gamepad handler Binds a handler function to execute when the Y button is pressed.
on-left-bumper-pressed gamepad handler Binds a handler function to execute when the left bumper is pressed.
on-right-bumper-pressed gamepad handler Binds a handler function to execute when the right bumper is pressed.
on-select-button-pressed gamepad handler Binds a handler function to execute when the select button is pressed.
on-start-button-pressed gamepad handler Binds a handler function to execute when the start button is pressed.
pressed-buttons! gamepad Returns a vector of keywords representing the buttons currently being pressed on the gamepad.

Available Mappings

The SNES Gamepad library comes with two button mappings in the snes-gamepad.mapping namespace:

Pull requests for additional mappings are welcome!

Single-Player Only

The SNES Gamepad library does not currently support multiple gamepads connected to the same computer.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.