Skip to content

Commit

Permalink
feat: Add custom long tap delay (#3110)
Browse files Browse the repository at this point in the history
Currently `longTapDelay` is hardcoded to 300ms.
This is good for most of the games. But same games required custom long
press values.
This PR allows it.

---------

Co-authored-by: Lukas Klingsbo <lukas.klingsbo@gmail.com>
  • Loading branch information
lucasnlm and spydon authored Apr 1, 2024
1 parent a717bcb commit a95d7df
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 2 deletions.
4 changes: 4 additions & 0 deletions doc/flame/inputs/tap_events.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ in `MultiTapDispatcher`), "long tap" will be triggered. This event invokes the
`void onLongTapDown(TapDownEvent)` handler on those components that previously received the
`onTapDown` event.

By default, the `.longTapDelay` is set to 300 milliseconds, what may be different of the system default.
You can change this value by setting the `TapConfig.longTapDelay` value.
It may also be useful for specific accessibility needs.


### onTapUp

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import 'package:flame/src/events/messages/tap_cancel_event.dart';
import 'package:flame/src/events/messages/tap_down_event.dart';
import 'package:flame/src/events/messages/tap_up_event.dart';
import 'package:flame/src/events/tagged_component.dart';
import 'package:flame/src/events/tap_config.dart';
import 'package:flame/src/game/flame_game.dart';
import 'package:flame/src/game/game_render_box.dart';
import 'package:flutter/gestures.dart';
Expand Down Expand Up @@ -120,7 +121,7 @@ class MultiTapDispatcher extends Component implements MultiTapListener {

/// The delay (in seconds) after which a tap is considered a long tap.
@override
double get longTapDelay => 0.300;
double get longTapDelay => TapConfig.longTapDelay;

@override
void handleTap(int pointerId) {}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:flame/events.dart';
import 'package:flame/src/events/tap_config.dart';
import 'package:flame/src/game/game.dart';
import 'package:flutter/gestures.dart';

Expand All @@ -22,7 +23,7 @@ mixin MultiTouchTapDetector on Game implements MultiTapListener {

//#region MultiTapListener API
@override
double get longTapDelay => 0.300;
double get longTapDelay => TapConfig.longTapDelay;

@override
void handleTap(int pointerId) => onTap(pointerId);
Expand Down
27 changes: 27 additions & 0 deletions packages/flame/lib/src/events/tap_config.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import 'dart:math';

/// [TapConfig] is used to expose specific configurations
/// related to the tap dispatcher.
final class TapConfig {
TapConfig._();

static double _longTapDelay = _defaultLongTapDelay;

/// The delay (in seconds) after which a tap is considered a long tap.
static double get longTapDelay => _longTapDelay;

/// Set the delay (in seconds) after which a tap is considered a long tap.
/// Same games may want to change the long tap delay to better
/// fit their needs or accessibility requirements.
static set longTapDelay(double value) {
_longTapDelay = max(_minLongTapDelay, value);
}

/// Min delay to long tap delay is defined below.
/// Values too low don't make sense because they
/// would be basically a regular tap.
static const double _minLongTapDelay = 0.150;

/// Default long tap delay is 0.3 seconds.
static const double _defaultLongTapDelay = 0.3;
}
20 changes: 20 additions & 0 deletions packages/flame/test/events/tap_config_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import 'package:flame/src/events/tap_config.dart';
import 'package:flutter_test/flutter_test.dart';

void main() {
group('TapConfig', () {
test('default longTapDelay is 0.3', () {
expect(TapConfig.longTapDelay, 0.3);
});

test('longTapDelay can be set new values', () {
TapConfig.longTapDelay = 0.5;
expect(TapConfig.longTapDelay, 0.5);
});

test('longTapDelay cannot be set to a value lower than 0.150', () {
TapConfig.longTapDelay = 0.1;
expect(TapConfig.longTapDelay, 0.150);
});
});
}

0 comments on commit a95d7df

Please sign in to comment.