-
-
Notifications
You must be signed in to change notification settings - Fork 935
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from erickzanardo/gamepad
Initial Gamepad support
- Loading branch information
Showing
4 changed files
with
128 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# Gamepad | ||
|
||
## Gamepad is only support on Android at the moment. | ||
|
||
To listen to gamepad events use the 'Flame.gamepad' instance, to add a listener use following snippet. | ||
|
||
```dart | ||
Flame.gamepad.addListener((String evtType, String key) { | ||
print(key) | ||
if (evtType == GAMEPAD_BUTTON_UP) { | ||
print('is up') | ||
} else { | ||
print('is down') | ||
} | ||
}); | ||
``` | ||
|
||
To check for specific keys use the following constants avaiable on the package: `flame/game.dart` | ||
|
||
``` | ||
GAMEPAD_BUTTON_UP | ||
GAMEPAD_BUTTON_DOWN | ||
GAMEPAD_DPAD_UP | ||
GAMEPAD_DPAD_DOWN | ||
GAMEPAD_DPAD_LEFT | ||
GAMEPAD_DPAD_RIGHT | ||
GAMEPAD_BUTTON_A | ||
GAMEPAD_BUTTON_B | ||
GAMEPAD_BUTTON_X | ||
GAMEPAD_BUTTON_Y | ||
GAMEPAD_BUTTON_L1 | ||
GAMEPAD_BUTTON_L2 | ||
GAMEPAD_BUTTON_R1 | ||
GAMEPAD_BUTTON_R2 | ||
GAMEPAD_BUTTON_START | ||
GAMEPAD_BUTTON_SELECT | ||
``` | ||
|
||
A functional example can be found [here](https://github.com/erickzanardo/flame-gamepad-example) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import 'package:flutter/services.dart'; | ||
|
||
typedef void KeyListener (RawKeyEvent event); | ||
typedef void GamepadListener (String evtType, String key); | ||
|
||
const GAMEPAD_BUTTON_UP = "UP"; | ||
const GAMEPAD_BUTTON_DOWN = "DOWN"; | ||
|
||
const GAMEPAD_DPAD_UP = "UP"; | ||
const GAMEPAD_DPAD_DOWN = "DOWN"; | ||
const GAMEPAD_DPAD_LEFT = "LEFT"; | ||
const GAMEPAD_DPAD_RIGHT = "RIGHT"; | ||
|
||
const GAMEPAD_BUTTON_A = "A"; | ||
const GAMEPAD_BUTTON_B = "B"; | ||
const GAMEPAD_BUTTON_X = "X"; | ||
const GAMEPAD_BUTTON_Y = "Y"; | ||
|
||
const GAMEPAD_BUTTON_L1 = "L1"; | ||
const GAMEPAD_BUTTON_L2 = "L2"; | ||
|
||
const GAMEPAD_BUTTON_R1 = "R1"; | ||
const GAMEPAD_BUTTON_R2 = "R2"; | ||
|
||
const GAMEPAD_BUTTON_START = "START"; | ||
const GAMEPAD_BUTTON_SELECT = "SELECT"; | ||
|
||
const ANDROID_MAPPING = { | ||
19: GAMEPAD_DPAD_UP, | ||
20: GAMEPAD_DPAD_DOWN, | ||
21: GAMEPAD_DPAD_LEFT, | ||
22: GAMEPAD_DPAD_RIGHT, | ||
96: GAMEPAD_BUTTON_A, | ||
97: GAMEPAD_BUTTON_B, | ||
99: GAMEPAD_BUTTON_X, | ||
100: GAMEPAD_BUTTON_Y, | ||
102: GAMEPAD_BUTTON_L1, | ||
103: GAMEPAD_BUTTON_R1, | ||
104: GAMEPAD_BUTTON_L2, | ||
105: GAMEPAD_BUTTON_R2, | ||
108: GAMEPAD_BUTTON_START, | ||
109: GAMEPAD_BUTTON_SELECT | ||
}; | ||
|
||
/// Gampad functionalities | ||
/// | ||
/// To use this class, access it via [Flame.gamepad]. | ||
class Gamepad { | ||
|
||
KeyListener listener; | ||
|
||
void addListener(GamepadListener gamepadListener) { | ||
this.listener = (RawKeyEvent e) { | ||
String evtType = e is RawKeyDownEvent ? GAMEPAD_BUTTON_DOWN : GAMEPAD_BUTTON_UP; | ||
|
||
if (e.data is RawKeyEventDataAndroid) { | ||
RawKeyEventDataAndroid androidEvent = e.data as RawKeyEventDataAndroid; | ||
|
||
String key = ANDROID_MAPPING[androidEvent.keyCode]; | ||
if (key != null) { | ||
gamepadListener(evtType, key); | ||
} | ||
} | ||
}; | ||
|
||
RawKeyboard.instance.addListener(this.listener); | ||
} | ||
|
||
void removeListener() { | ||
RawKeyboard.instance.removeListener(this.listener); | ||
} | ||
} |