Files
flame/lib/components/mixins/tapable.dart
2019-09-24 19:03:36 -03:00

38 lines
937 B
Dart

import 'dart:ui';
import 'package:flutter/gestures.dart';
mixin Tapable {
Rect toRect();
void onTapCancel() {}
void onTapDown(TapDownDetails details) {}
void onTapUp(TapUpDetails details) {}
bool checkTapOverlap(Offset o) => toRect().contains(o);
void handleTapDown(TapDownDetails details) {
if (checkTapOverlap(details.globalPosition)) {
onTapDown(details);
}
tapableChildren().forEach((c) => c.handleTapDown(details));
}
void handleTapUp(TapUpDetails details) {
if (checkTapOverlap(details.globalPosition)) {
onTapUp(details);
}
tapableChildren().forEach((c) => c.handleTapUp(details));
}
void handleTapCancel() {
onTapCancel();
tapableChildren().forEach((c) => c.handleTapCancel());
}
/// Overwrite this to add children to this [Tapable].
///
/// If a [Tapable] has children, its children be taped as well.
Iterable<Tapable> tapableChildren() => [];
}