GithubHelp home page GithubHelp logo

kalanjdjordje / the_flutter_gamepad Goto Github PK

View Code? Open in Web Editor NEW

This project forked from rainwayapp/flutter_gamepad

0.0 1.0 0.0 187 KB

The gamepad library for Flutter

License: Apache License 2.0

Ruby 6.67% Objective-C 0.81% Kotlin 45.99% Dart 30.43% Swift 16.10%

the_flutter_gamepad's Introduction

flutter_gamepad

A platform library for listening to hardware gamepads (game controllers) from Flutter.

Features

  • FlutterGamepad.gamepads() returns info about all currently connected gamepads.
  • FlutterGamepad.eventStream reports gamepad events. (See example)
  • Fractional button values, such as those reported by the left and right trigger buttons on most gamepads, are supported.
  • Supports iOS 13+, as well as older versions of iOS.
  • Supports Android.
  • Supports multiple simultaneous gamepads. Events are tagged with an ID you can tell gamepads apart by.

Caveats

Android

  • "Disconnect" events are never fired on Android. "Connect" events are fired initially for already-connected gamepads when listening to the stream, and thereafter on the first input of any new gamepads. This differs from the iOS behavior for non-inital "connect" events: on that platform, a "connect" event can arrive for a gamepad before any button has been pressed.

  • Gamepad button events are just a special kind of key event. This plugin currently patches the FlutterViewController to prevent key-events that originate from a gamepad from reaching the default Flutter handler for key events. This allows you to handle gamepad input all in one place and not getting duplicate key events in your keyboard handlers.

    This has a notable consequence: normally, Android sends "SELECT" events along with "BUTTON_A" events that you might expect on the Flutter side to implement key-based focus navigation. This plugin silences those, so you'll have to listen to the FlutterGamepad event stream for A presses and make navigation work based off of those.

  • Despite all that, the B button still seems to trigger a "back" action in Flutter. You'll want to have a WillPopScope on your game scaffold to prevent this.

  • Android TV exhibits a weird behavior where it transforms presses of the "select" button (the leftmost small button on the face of the gamepad) into KEYCODE_BACK key events. (Normally they are KEYCODE_BUTTON_SELECT.) This plugin does some work to undo this transformation.

Example

import 'package:flutter_gamepad/flutter_gamepad.dart';

class _MyWidgetState extends State<MyWidget> {
  StreamSubscription<GamepadEvent> _gamepadEventSubscription;

  @override
  void initState() {
    super.initState();
    _gamepadEventSubscription = FlutterGamepad.eventStream.listen(onGamepadEvent);
  }

  void dispose() { _gamepadEventSubscription.cancel(); super.dispose(); }

  void onGamepadEvent(GamepadEvent e) {
    if (e is GamepadConnectedEvent) {
      // ...
    } else if (e is GamepadDisconnectedEvent) {
      // ...
    } else if (e is GamepadButtonEvent) {
      // ...
    } else if (e is GamepadThumbstickEvent) {
      // ...
    } else throw ArgumentError('Unknown event: $e');
  }
}

the_flutter_gamepad's People

Contributors

lynn avatar

Watchers

James Cloos avatar

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.