mirror of
https://github.com/flame-engine/flame.git
synced 2025-11-02 03:15:43 +08:00
Fixing gestures conflicts and adding a gestures example
This commit is contained in:
committed by
Erick (CptBlackPixel)
parent
655a70b0a6
commit
6f44e0514b
72
doc/examples/gestures/.gitignore
vendored
Normal file
72
doc/examples/gestures/.gitignore
vendored
Normal file
@ -0,0 +1,72 @@
|
|||||||
|
# Miscellaneous
|
||||||
|
*.class
|
||||||
|
*.log
|
||||||
|
*.pyc
|
||||||
|
*.swp
|
||||||
|
.DS_Store
|
||||||
|
.atom/
|
||||||
|
.buildlog/
|
||||||
|
.history
|
||||||
|
.svn/
|
||||||
|
|
||||||
|
# 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
|
||||||
|
**/doc/api/
|
||||||
|
.dart_tool/
|
||||||
|
.flutter-plugins
|
||||||
|
.packages
|
||||||
|
.pub-cache/
|
||||||
|
.pub/
|
||||||
|
/build/
|
||||||
|
|
||||||
|
# Android related
|
||||||
|
**/android/**/gradle-wrapper.jar
|
||||||
|
**/android/.gradle
|
||||||
|
**/android/captures/
|
||||||
|
**/android/gradlew
|
||||||
|
**/android/gradlew.bat
|
||||||
|
**/android/local.properties
|
||||||
|
**/android/**/GeneratedPluginRegistrant.java
|
||||||
|
|
||||||
|
# iOS/XCode related
|
||||||
|
**/ios/**/*.mode1v3
|
||||||
|
**/ios/**/*.mode2v3
|
||||||
|
**/ios/**/*.moved-aside
|
||||||
|
**/ios/**/*.pbxuser
|
||||||
|
**/ios/**/*.perspectivev3
|
||||||
|
**/ios/**/*sync/
|
||||||
|
**/ios/**/.sconsign.dblite
|
||||||
|
**/ios/**/.tags*
|
||||||
|
**/ios/**/.vagrant/
|
||||||
|
**/ios/**/DerivedData/
|
||||||
|
**/ios/**/Icon?
|
||||||
|
**/ios/**/Pods/
|
||||||
|
**/ios/**/.symlinks/
|
||||||
|
**/ios/**/profile
|
||||||
|
**/ios/**/xcuserdata
|
||||||
|
**/ios/.generated/
|
||||||
|
**/ios/Flutter/App.framework
|
||||||
|
**/ios/Flutter/Flutter.framework
|
||||||
|
**/ios/Flutter/Generated.xcconfig
|
||||||
|
**/ios/Flutter/app.flx
|
||||||
|
**/ios/Flutter/app.zip
|
||||||
|
**/ios/Flutter/flutter_assets/
|
||||||
|
**/ios/ServiceDefinitions.json
|
||||||
|
**/ios/Runner/GeneratedPluginRegistrant.*
|
||||||
|
|
||||||
|
# Exceptions to above rules.
|
||||||
|
!**/ios/**/default.mode1v3
|
||||||
|
!**/ios/**/default.mode2v3
|
||||||
|
!**/ios/**/default.pbxuser
|
||||||
|
!**/ios/**/default.perspectivev3
|
||||||
|
!/packages/flutter_tools/test/data/dart_dependencies_test/**/.packages
|
||||||
10
doc/examples/gestures/.metadata
Normal file
10
doc/examples/gestures/.metadata
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
# This file tracks properties of this Flutter project.
|
||||||
|
# Used by Flutter tool to assess capabilities and perform upgrades etc.
|
||||||
|
#
|
||||||
|
# This file should be version controlled and should not be manually edited.
|
||||||
|
|
||||||
|
version:
|
||||||
|
revision: 841707365a9be08f2219cbafc52c52d6af5355aa
|
||||||
|
channel: master
|
||||||
|
|
||||||
|
project_type: app
|
||||||
3
doc/examples/gestures/README.md
Normal file
3
doc/examples/gestures/README.md
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
# gestures
|
||||||
|
|
||||||
|
A flame game showcasing the use of gestures callbacks
|
||||||
50
doc/examples/gestures/lib/main.dart
Normal file
50
doc/examples/gestures/lib/main.dart
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:flame/game.dart';
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
final game = MyGame();
|
||||||
|
runApp(game.widget);
|
||||||
|
}
|
||||||
|
|
||||||
|
class MyGame extends Game {
|
||||||
|
final _whitePaint = Paint()..color = const Color(0xFFFFFFFF);
|
||||||
|
final _bluePaint = Paint()..color = const Color(0xFF0000FF);
|
||||||
|
final _greenPaint = Paint()..color = const Color(0xFF00FF00);
|
||||||
|
|
||||||
|
Paint _paint;
|
||||||
|
|
||||||
|
Rect _rect = const Rect.fromLTWH(50, 50, 50, 50);
|
||||||
|
|
||||||
|
MyGame() {
|
||||||
|
_paint = _whitePaint;
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
void onTap() {
|
||||||
|
_paint = _paint == _whitePaint ? _bluePaint : _whitePaint;
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
bool useDoubleTapDetectors() => true;
|
||||||
|
|
||||||
|
@override
|
||||||
|
void onDoubleTap() {
|
||||||
|
_paint = _greenPaint;
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
bool usePanDetectors() => true;
|
||||||
|
|
||||||
|
@override
|
||||||
|
void onPanUpdate(DragUpdateDetails details) {
|
||||||
|
_rect = _rect.translate(details.delta.dx, details.delta.dy);
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
void update(double dt) {}
|
||||||
|
|
||||||
|
@override
|
||||||
|
void render(Canvas canvas) {
|
||||||
|
canvas.drawRect(_rect, _paint);
|
||||||
|
}
|
||||||
|
}
|
||||||
16
doc/examples/gestures/pubspec.yaml
Normal file
16
doc/examples/gestures/pubspec.yaml
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
name: gestures
|
||||||
|
description: A flame game showcasing the use of gestures callbacks
|
||||||
|
|
||||||
|
version: 1.0.0+1
|
||||||
|
|
||||||
|
environment:
|
||||||
|
sdk: ">=2.1.0 <3.0.0"
|
||||||
|
|
||||||
|
dependencies:
|
||||||
|
flutter:
|
||||||
|
sdk: flutter
|
||||||
|
flame:
|
||||||
|
path: ../../../
|
||||||
|
|
||||||
|
flutter:
|
||||||
|
uses-material-design: false
|
||||||
105
lib/game.dart
105
lib/game.dart
@ -19,38 +19,55 @@ import 'position.dart';
|
|||||||
/// Subclass this to implement the [update] and [render] methods.
|
/// Subclass this to implement the [update] and [render] methods.
|
||||||
/// Flame will deal with calling these methods properly when the game's widget is rendered.
|
/// Flame will deal with calling these methods properly when the game's widget is rendered.
|
||||||
abstract class Game {
|
abstract class Game {
|
||||||
|
bool useTapDetectors() => true;
|
||||||
void onTap() {}
|
void onTap() {}
|
||||||
void onTapCancel() {}
|
void onTapCancel() {}
|
||||||
void onTapDown(TapDownDetails details) {}
|
void onTapDown(TapDownDetails details) {}
|
||||||
void onTapUp(TapUpDetails details) {}
|
void onTapUp(TapUpDetails details) {}
|
||||||
|
|
||||||
|
bool useSecondaryTapDetectors() => false;
|
||||||
void onSecondaryTapDown(TapDownDetails details) {}
|
void onSecondaryTapDown(TapDownDetails details) {}
|
||||||
void onSecondaryTapUp(TapUpDetails details) {}
|
void onSecondaryTapUp(TapUpDetails details) {}
|
||||||
void onSecondaryTapCancel() {}
|
void onSecondaryTapCancel() {}
|
||||||
|
|
||||||
|
bool useDoubleTapDetectors() => false;
|
||||||
void onDoubleTap() {}
|
void onDoubleTap() {}
|
||||||
|
|
||||||
|
bool useLongPressDetectors() => false;
|
||||||
void onLongPress() {}
|
void onLongPress() {}
|
||||||
void onLongPressStart(LongPressStartDetails details) {}
|
void onLongPressStart(LongPressStartDetails details) {}
|
||||||
void onLongPressMoveUpdate(LongPressMoveUpdateDetails details) {}
|
void onLongPressMoveUpdate(LongPressMoveUpdateDetails details) {}
|
||||||
void onLongPressUp() {}
|
void onLongPressUp() {}
|
||||||
void onLongPressEnd(LongPressEndDetails details) {}
|
void onLongPressEnd(LongPressEndDetails details) {}
|
||||||
|
|
||||||
|
bool useVerticalDragDetectors() => false;
|
||||||
void onVerticalDragDown(DragDownDetails details) {}
|
void onVerticalDragDown(DragDownDetails details) {}
|
||||||
void onVerticalDragStart(DragStartDetails details) {}
|
void onVerticalDragStart(DragStartDetails details) {}
|
||||||
void onVerticalDragUpdate(DragUpdateDetails details) {}
|
void onVerticalDragUpdate(DragUpdateDetails details) {}
|
||||||
void onVerticalDragEnd(DragEndDetails details) {}
|
void onVerticalDragEnd(DragEndDetails details) {}
|
||||||
void onVerticalDragCancel() {}
|
void onVerticalDragCancel() {}
|
||||||
|
|
||||||
|
bool useHorizontalDragDetectors() => false;
|
||||||
void onHorizontalDragDown(DragDownDetails details) {}
|
void onHorizontalDragDown(DragDownDetails details) {}
|
||||||
void onHorizontalDragStart(DragStartDetails details) {}
|
void onHorizontalDragStart(DragStartDetails details) {}
|
||||||
void onHorizontalDragUpdate(DragUpdateDetails details) {}
|
void onHorizontalDragUpdate(DragUpdateDetails details) {}
|
||||||
void onHorizontalDragEnd(DragEndDetails details) {}
|
void onHorizontalDragEnd(DragEndDetails details) {}
|
||||||
void onHorizontalDragCancel() {}
|
void onHorizontalDragCancel() {}
|
||||||
|
|
||||||
|
bool useForcePressDetectors() => false;
|
||||||
void onForcePressStart(ForcePressDetails details) {}
|
void onForcePressStart(ForcePressDetails details) {}
|
||||||
void onForcePressPeak(ForcePressDetails details) {}
|
void onForcePressPeak(ForcePressDetails details) {}
|
||||||
void onForcePressUpdate(ForcePressDetails details) {}
|
void onForcePressUpdate(ForcePressDetails details) {}
|
||||||
void onForcePressEnd(ForcePressDetails details) {}
|
void onForcePressEnd(ForcePressDetails details) {}
|
||||||
|
|
||||||
|
bool usePanDetectors() => false;
|
||||||
void onPanDown(DragDownDetails details) {}
|
void onPanDown(DragDownDetails details) {}
|
||||||
void onPanStart(DragStartDetails details) {}
|
void onPanStart(DragStartDetails details) {}
|
||||||
void onPanUpdate(DragUpdateDetails details) {}
|
void onPanUpdate(DragUpdateDetails details) {}
|
||||||
void onPanEnd(DragEndDetails details) {}
|
void onPanEnd(DragEndDetails details) {}
|
||||||
void onPanCancel() {}
|
void onPanCancel() {}
|
||||||
|
|
||||||
|
bool useScaleDetectors() => false;
|
||||||
void onScaleStart(ScaleStartDetails details) {}
|
void onScaleStart(ScaleStartDetails details) {}
|
||||||
void onScaleUpdate(ScaleUpdateDetails details) {}
|
void onScaleUpdate(ScaleUpdateDetails details) {}
|
||||||
void onScaleEnd(ScaleEndDetails details) {}
|
void onScaleEnd(ScaleEndDetails details) {}
|
||||||
@ -96,45 +113,63 @@ class WidgetBuilder {
|
|||||||
|
|
||||||
Widget build(Game game) {
|
Widget build(Game game) {
|
||||||
return GestureDetector(
|
return GestureDetector(
|
||||||
onTap: () => game.onTap(),
|
// Taps
|
||||||
onTapCancel: () => game.onTapCancel(),
|
onTap: !game.useTapDetectors() ? null : () => game.onTap(),
|
||||||
onTapDown: (TapDownDetails d) => game.onTapDown(d),
|
onTapCancel: !game.useTapDetectors() ? null : () => game.onTapCancel(),
|
||||||
onTapUp: (TapUpDetails d) => game.onTapUp(d),
|
onTapDown: !game.useTapDetectors() ? null : (TapDownDetails d) => game.onTapDown(d),
|
||||||
onSecondaryTapDown: (TapDownDetails d) => game.onSecondaryTapDown(d),
|
onTapUp: !game.useTapDetectors() ? null : (TapUpDetails d) => game.onTapUp(d),
|
||||||
onSecondaryTapUp: (TapUpDetails d) => game.onSecondaryTapUp(d),
|
|
||||||
onSecondaryTapCancel: () => game.onSecondaryTapCancel(),
|
// Secondary taps
|
||||||
onDoubleTap: () => game.onDoubleTap(),
|
onSecondaryTapDown: !game.useSecondaryTapDetectors() ? null : (TapDownDetails d) => game.onSecondaryTapDown(d),
|
||||||
onLongPress: () => game.onLongPress(),
|
onSecondaryTapUp: !game.useSecondaryTapDetectors() ? null : (TapUpDetails d) => game.onSecondaryTapUp(d),
|
||||||
onLongPressStart: (LongPressStartDetails d) => game.onLongPressStart(d),
|
onSecondaryTapCancel: !game.useSecondaryTapDetectors() ? null : () => game.onSecondaryTapCancel(),
|
||||||
onLongPressMoveUpdate: (LongPressMoveUpdateDetails d) =>
|
|
||||||
|
// Double tap
|
||||||
|
onDoubleTap: !game.useDoubleTapDetectors() ? null : () => game.onDoubleTap(),
|
||||||
|
|
||||||
|
// Long presses
|
||||||
|
onLongPress: !game.useLongPressDetectors() ? null : () => game.onLongPress(),
|
||||||
|
onLongPressStart: !game.useLongPressDetectors() ? null : (LongPressStartDetails d) => game.onLongPressStart(d),
|
||||||
|
onLongPressMoveUpdate: !game.useLongPressDetectors() ? null : (LongPressMoveUpdateDetails d) =>
|
||||||
game.onLongPressMoveUpdate(d),
|
game.onLongPressMoveUpdate(d),
|
||||||
onLongPressUp: () => game.onLongPressUp(),
|
onLongPressUp: !game.useLongPressDetectors() ? null : () => game.onLongPressUp(),
|
||||||
onLongPressEnd: (LongPressEndDetails d) => game.onLongPressEnd(d),
|
onLongPressEnd: !game.useLongPressDetectors() ? null : (LongPressEndDetails d) => game.onLongPressEnd(d),
|
||||||
onVerticalDragDown: (DragDownDetails d) => game.onVerticalDragDown(d),
|
|
||||||
onVerticalDragStart: (DragStartDetails d) => game.onVerticalDragStart(d),
|
// Vertical drag
|
||||||
onVerticalDragUpdate: (DragUpdateDetails d) =>
|
onVerticalDragDown: !game.useVerticalDragDetectors() ? null : (DragDownDetails d) => game.onVerticalDragDown(d),
|
||||||
|
onVerticalDragStart: !game.useVerticalDragDetectors() ? null : (DragStartDetails d) => game.onVerticalDragStart(d),
|
||||||
|
onVerticalDragUpdate: !game.useVerticalDragDetectors() ? null : (DragUpdateDetails d) =>
|
||||||
game.onVerticalDragUpdate(d),
|
game.onVerticalDragUpdate(d),
|
||||||
onVerticalDragEnd: (DragEndDetails d) => game.onVerticalDragEnd(d),
|
onVerticalDragEnd: !game.useVerticalDragDetectors() ? null : (DragEndDetails d) => game.onVerticalDragEnd(d),
|
||||||
onVerticalDragCancel: () => game.onVerticalDragCancel(),
|
onVerticalDragCancel: !game.useVerticalDragDetectors() ? null : () => game.onVerticalDragCancel(),
|
||||||
onHorizontalDragDown: (DragDownDetails d) => game.onHorizontalDragDown(d),
|
|
||||||
onHorizontalDragStart: (DragStartDetails d) =>
|
// Horizontal drag
|
||||||
|
onHorizontalDragDown: !game.useHorizontalDragDetectors() ? null : (DragDownDetails d) => game.onHorizontalDragDown(d),
|
||||||
|
onHorizontalDragStart: !game.useHorizontalDragDetectors() ? null : (DragStartDetails d) =>
|
||||||
game.onHorizontalDragStart(d),
|
game.onHorizontalDragStart(d),
|
||||||
onHorizontalDragUpdate: (DragUpdateDetails d) =>
|
onHorizontalDragUpdate: !game.useHorizontalDragDetectors() ? null : (DragUpdateDetails d) =>
|
||||||
game.onHorizontalDragUpdate(d),
|
game.onHorizontalDragUpdate(d),
|
||||||
onHorizontalDragEnd: (DragEndDetails d) => game.onHorizontalDragEnd(d),
|
onHorizontalDragEnd: !game.useHorizontalDragDetectors() ? null : (DragEndDetails d) => game.onHorizontalDragEnd(d),
|
||||||
onHorizontalDragCancel: () => game.onHorizontalDragCancel(),
|
onHorizontalDragCancel: !game.useHorizontalDragDetectors() ? null : () => game.onHorizontalDragCancel(),
|
||||||
onForcePressStart: (ForcePressDetails d) => game.onForcePressStart(d),
|
|
||||||
onForcePressPeak: (ForcePressDetails d) => game.onForcePressPeak(d),
|
// Force presses
|
||||||
onForcePressUpdate: (ForcePressDetails d) => game.onForcePressUpdate(d),
|
onForcePressStart: !game.useForcePressDetectors() ? null : (ForcePressDetails d) => game.onForcePressStart(d),
|
||||||
onForcePressEnd: (ForcePressDetails d) => game.onForcePressEnd(d),
|
onForcePressPeak: !game.useForcePressDetectors() ? null : (ForcePressDetails d) => game.onForcePressPeak(d),
|
||||||
onPanDown: (DragDownDetails d) => game.onPanDown(d),
|
onForcePressUpdate: !game.useForcePressDetectors() ? null : (ForcePressDetails d) => game.onForcePressUpdate(d),
|
||||||
onPanStart: (DragStartDetails d) => game.onPanStart(d),
|
onForcePressEnd: !game.useForcePressDetectors() ? null : (ForcePressDetails d) => game.onForcePressEnd(d),
|
||||||
onPanUpdate: (DragUpdateDetails d) => game.onPanUpdate(d),
|
|
||||||
onPanEnd: (DragEndDetails d) => game.onPanEnd(d),
|
// Pan
|
||||||
onPanCancel: () => game.onPanCancel(),
|
onPanDown: !game.usePanDetectors() ? null : (DragDownDetails d) => game.onPanDown(d),
|
||||||
onScaleStart: (ScaleStartDetails d) => game.onScaleStart(d),
|
onPanStart: !game.usePanDetectors() ? null : (DragStartDetails d) => game.onPanStart(d),
|
||||||
onScaleUpdate: (ScaleUpdateDetails d) => game.onScaleUpdate(d),
|
onPanUpdate: !game.usePanDetectors() ? null : (DragUpdateDetails d) => game.onPanUpdate(d),
|
||||||
onScaleEnd: (ScaleEndDetails d) => game.onScaleEnd(d),
|
onPanEnd: !game.usePanDetectors() ? null : (DragEndDetails d) => game.onPanEnd(d),
|
||||||
|
onPanCancel: !game.usePanDetectors() ? null : () => game.onPanCancel(),
|
||||||
|
|
||||||
|
// Scales
|
||||||
|
onScaleStart: !game.useScaleDetectors() ? null : (ScaleStartDetails d) => game.onScaleStart(d),
|
||||||
|
onScaleUpdate: !game.useScaleDetectors() ? null : (ScaleUpdateDetails d) => game.onScaleUpdate(d),
|
||||||
|
onScaleEnd: !game.useScaleDetectors() ? null : (ScaleEndDetails d) => game.onScaleEnd(d),
|
||||||
|
|
||||||
child: Container(
|
child: Container(
|
||||||
color: const Color(0xFF000000),
|
color: const Color(0xFF000000),
|
||||||
child: Directionality(
|
child: Directionality(
|
||||||
|
|||||||
Reference in New Issue
Block a user