-
-
Notifications
You must be signed in to change notification settings - Fork 21
feat: Support for Web #48
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
zhuhaichao518
wants to merge
9
commits into
flame-engine:main
Choose a base branch
from
zhuhaichao518:PR_to_upstream
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
d508623
add support for web.
zhuhaichao518 f1ee9e5
use js_interop and package:web instead of package:js and dart:html
zhuhaichao518 c50fc2c
resolve comments && use package:web for gamepad
zhuhaichao518 f738725
make GamePadState private
zhuhaichao518 e2f0219
Move gamepad_detector to /src
zhuhaichao518 a113e29
Merge branch 'main' into PR_to_upstream
zhuhaichao518 787abae
Update packages/gamepads_web/lib/gamepads_web.dart
zhuhaichao518 0a29ad9
fix lint
zhuhaichao518 aab7c90
Update packages/gamepads_web/pubspec.yaml
zhuhaichao518 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,29 @@ | ||
# Miscellaneous | ||
*.class | ||
*.log | ||
*.pyc | ||
*.swp | ||
.DS_Store | ||
.atom/ | ||
.buildlog/ | ||
.history | ||
.svn/ | ||
migrate_working_dir/ | ||
|
||
# IntelliJ related | ||
*.iml | ||
*.ipr | ||
*.iws | ||
.idea/ | ||
|
||
# The .vscode folder contains launch configuration and tasks you configure in | ||
# VS Code which you may wish to be included in version control, so this line | ||
# is commented out by default. | ||
#.vscode/ | ||
|
||
# Flutter/Dart/Pub related | ||
# Libraries should not include pubspec.lock, per https://dart.dev/guides/libraries/private-files#pubspeclock. | ||
/pubspec.lock | ||
**/doc/api/ | ||
.dart_tool/ | ||
build/ |
This file contains hidden or 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 @@ | ||
../../LICENSE |
This file contains hidden or 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 @@ | ||
../../README.md |
This file contains hidden or 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 @@ | ||
include: package:flame_lint/analysis_options.yaml |
This file contains hidden or 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,131 @@ | ||
import 'dart:async'; | ||
import 'dart:js_interop'; | ||
|
||
import 'package:flutter/foundation.dart'; | ||
import 'package:flutter_web_plugins/flutter_web_plugins.dart'; | ||
import 'package:gamepads_platform_interface/api/gamepad_controller.dart'; | ||
import 'package:gamepads_platform_interface/api/gamepad_event.dart'; | ||
import 'package:gamepads_platform_interface/gamepads_platform_interface.dart'; | ||
import 'package:gamepads_web/gamepad_detector.dart'; | ||
import 'package:web/web.dart' as web; | ||
|
||
class _GamePadState { | ||
_GamePadState(int length) { | ||
keyStates = List<dynamic>.filled(length, null); | ||
axesStates = List<double>.filled(4, 0); | ||
} | ||
|
||
List<dynamic>? keyStates; | ||
List<double>? axesStates; | ||
} | ||
|
||
/// A web implementation of the GamepadsWebPlatform of the GamepadsWeb plugin. | ||
class GamepadsWeb extends GamepadsPlatformInterface { | ||
int _gamepadCount = 0; | ||
Timer? _gamepadPollingTimer; | ||
|
||
final Map<String, _GamePadState> _lastGamePadstates = {}; | ||
|
||
void updateGamepadsStatus() { | ||
final gamepads = getGamepadList(); | ||
for (final gamepad in gamepads) { | ||
final buttonlist = gamepad!.buttons.toDart; | ||
final axeslist = gamepad.axes.toDart; | ||
final gamepadId = gamepad.index.toString(); | ||
_GamePadState lastState; | ||
if (_lastGamePadstates.containsKey(gamepadId) && | ||
_lastGamePadstates[gamepadId]?.keyStates?.length == | ||
buttonlist.length) { | ||
lastState = _lastGamePadstates[gamepadId]!; | ||
} else { | ||
_lastGamePadstates[gamepadId] = _GamePadState(buttonlist.length); | ||
lastState = _lastGamePadstates[gamepadId]!; | ||
} | ||
for (var i = 0; i < buttonlist.length; i++) { | ||
if (lastState.keyStates?[i] != buttonlist[i].value) { | ||
lastState.keyStates?[i] = buttonlist[i].value; | ||
emitGamepadEvent( | ||
GamepadEvent( | ||
gamepadId: gamepadId, | ||
timestamp: DateTime.now().millisecondsSinceEpoch, | ||
type: KeyType.button, | ||
key: 'button $i', | ||
value: buttonlist[i].value, | ||
), | ||
); | ||
} | ||
} | ||
for (var i = 0; i < 4; i++) { | ||
if ((lastState.axesStates![i] - axeslist[i].toDartDouble).abs() > | ||
0.03) { | ||
lastState.axesStates?[i] = axeslist[i].toDartDouble; | ||
emitGamepadEvent( | ||
GamepadEvent( | ||
gamepadId: gamepadId, | ||
timestamp: DateTime.now().millisecondsSinceEpoch, | ||
type: KeyType.analog, | ||
key: 'analog $i', | ||
value: axeslist[i].toDartDouble, | ||
), | ||
); | ||
} | ||
} | ||
} | ||
} | ||
|
||
GamepadsWeb() { | ||
web.window.addEventListener( | ||
'gamepadconnected', | ||
(web.Event event) { | ||
_gamepadCount++; | ||
if (_gamepadCount == 1) { | ||
// The game pad state for web is not event driven. We need to | ||
// query the game pad state by ourself. | ||
// By default we set the query interval is 8 ms. | ||
_gamepadPollingTimer = | ||
Timer.periodic(const Duration(milliseconds: 8), (timer) { | ||
updateGamepadsStatus(); | ||
}); | ||
} | ||
}.toJS, | ||
); | ||
|
||
web.window.addEventListener( | ||
'gamepaddisconnected', | ||
(web.Event event) { | ||
_gamepadCount--; | ||
if (_gamepadCount == 0) { | ||
_gamepadPollingTimer?.cancel(); | ||
} | ||
}.toJS, | ||
); | ||
} | ||
|
||
static void registerWith(Registrar registrar) { | ||
GamepadsPlatformInterface.instance = GamepadsWeb(); | ||
} | ||
|
||
List<GamepadController>? controllers; | ||
|
||
@override | ||
Future<List<GamepadController>> listGamepads() async { | ||
controllers = getGamepads(this); | ||
return controllers!; | ||
} | ||
|
||
void emitGamepadEvent(GamepadEvent event) { | ||
_gamepadEventsStreamController.add(event); | ||
} | ||
|
||
final StreamController<GamepadEvent> _gamepadEventsStreamController = | ||
StreamController<GamepadEvent>.broadcast(); | ||
|
||
@override | ||
Stream<GamepadEvent> get gamepadEventsStream => | ||
_gamepadEventsStreamController.stream; | ||
|
||
@mustCallSuper | ||
Future<void> dispose() async { | ||
_gamepadEventsStreamController.close(); | ||
} | ||
} |
This file contains hidden or 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,27 @@ | ||
import 'dart:js_interop'; | ||
|
||
import 'package:gamepads_platform_interface/api/gamepad_controller.dart'; | ||
import 'package:gamepads_platform_interface/gamepads_platform_interface.dart'; | ||
import 'package:web/web.dart'; | ||
|
||
List<GamepadController> getGamepads(GamepadsPlatformInterface plugin) { | ||
final controllers = <GamepadController>[]; | ||
final gamepads = getGamepadList(); | ||
for (var i = 0; i < gamepads.length; i++) { | ||
final gamepad = gamepads[i]; | ||
controllers.add( | ||
GamepadController( | ||
id: gamepad!.index.toString(), | ||
name: gamepad.id, | ||
plugin: plugin, | ||
), | ||
); | ||
} | ||
return controllers; | ||
} | ||
|
||
List<Gamepad?> getGamepadList() { | ||
final gamepads = window.navigator.getGamepads().toDart; | ||
gamepads.removeWhere((item) => item == null); | ||
return gamepads; | ||
} |
This file contains hidden or 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,33 @@ | ||
name: gamepads_web | ||
zhuhaichao518 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
resolution: workspace | ||
description: Web implementation of gamepads, a Flutter plugin to handle gamepad input across multiple platforms. | ||
version: 0.1.0 | ||
homepage: https://github.com/flame-engine/gamepads | ||
repository: https://github.com/flame-engine/gamepads/tree/main/packages/gamepads_web | ||
|
||
flutter: | ||
plugin: | ||
implements: gamepads | ||
platforms: | ||
web: | ||
pluginClass: GamepadsWeb | ||
fileName: gamepads_web.dart | ||
|
||
environment: | ||
sdk: '>=2.19.0 <3.0.0' | ||
flutter: ">=3.3.0" | ||
|
||
dependencies: | ||
flutter: | ||
sdk: flutter | ||
flutter_web_plugins: | ||
sdk: flutter | ||
gamepads_platform_interface: ^0.1.2+1 | ||
js_interop: ^0.0.1 | ||
plugin_platform_interface: ^2.1.8 | ||
web: ^1.1.0 | ||
|
||
dev_dependencies: | ||
flame_lint: ^0.2.0 | ||
flutter_test: | ||
sdk: flutter |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.