Skip to content

Commit

Permalink
fix!: Convert PositionEvent.canvasPosition to local coordinates (#2598)
Browse files Browse the repository at this point in the history
This PR makes the `PositionEvent` class take a `Game` instance as a
required parameter. The game is the used
to convert `canvasPosition` lazily as it is done in `EventPosition`.
As a result, i had to change several constructor calls to include the
game, and make a breaking change in the `flame_test` package for all the
`create[...]Events` functions. This may not be the best solution, but it
is the easiest. Feel free to share your opinion and improvement ideas.
  • Loading branch information
tibotix committed Jul 2, 2023
1 parent 985400f commit 87139c8
Show file tree
Hide file tree
Showing 12 changed files with 34 additions and 18 deletions.
Expand Up @@ -42,7 +42,7 @@ class DoubleTapDispatcher extends Component with HasGameRef<FlameGame> {
DoubleTapGestureRecognizer.new,
(DoubleTapGestureRecognizer instance) {
instance.onDoubleTapDown =
(details) => _onDoubleTapDown(DoubleTapDownEvent(details));
(details) => _onDoubleTapDown(DoubleTapDownEvent(game, details));
instance.onDoubleTapCancel =
() => _onDoubleTapCancel(DoubleTapCancelEvent());
instance.onDoubleTap = () => _onDoubleTapUp(DoubleTapEvent());
Expand Down
Expand Up @@ -140,13 +140,13 @@ class MultiDragDispatcher extends Component implements MultiDragListener {
@internal
@override
void handleDragStart(int pointerId, DragStartDetails details) {
onDragStart(DragStartEvent(pointerId, details));
onDragStart(DragStartEvent(pointerId, game, details));
}

@internal
@override
void handleDragUpdate(int pointerId, DragUpdateDetails details) {
onDragUpdate(DragUpdateEvent(pointerId, details));
onDragUpdate(DragUpdateEvent(pointerId, game, details));
}

@internal
Expand Down
Expand Up @@ -160,19 +160,19 @@ class MultiTapDispatcher extends Component implements MultiTapListener {
@internal
@override
void handleTapDown(int pointerId, TapDownDetails details) {
onTapDown(TapDownEvent(pointerId, details));
onTapDown(TapDownEvent(pointerId, game, details));
}

@internal
@override
void handleTapUp(int pointerId, TapUpDetails details) {
onTapUp(TapUpEvent(pointerId, details));
onTapUp(TapUpEvent(pointerId, game, details));
}

@internal
@override
void handleLongTapDown(int pointerId, TapDownDetails details) {
onLongTapDown(TapDownEvent(pointerId, details));
onLongTapDown(TapDownEvent(pointerId, game, details));
}

//#endregion
Expand Down
Expand Up @@ -5,10 +5,9 @@ import 'package:flutter/gestures.dart';
class DoubleTapDownEvent extends PositionEvent {
final PointerDeviceKind deviceKind;

DoubleTapDownEvent(TapDownDetails details)
DoubleTapDownEvent(super.game, TapDownDetails details)
: deviceKind = details.kind ?? PointerDeviceKind.unknown,
super(
canvasPosition: details.localPosition.toVector2(),
devicePosition: details.globalPosition.toVector2(),
);
}
3 changes: 1 addition & 2 deletions packages/flame/lib/src/events/messages/drag_start_event.dart
Expand Up @@ -11,10 +11,9 @@ import 'package:flutter/gestures.dart';
///
/// This is a [PositionEvent], where the position is the point of touch.
class DragStartEvent extends PositionEvent {
DragStartEvent(this.pointerId, DragStartDetails details)
DragStartEvent(this.pointerId, super.game, DragStartDetails details)
: deviceKind = details.kind ?? PointerDeviceKind.unknown,
super(
canvasPosition: details.localPosition.toVector2(),
devicePosition: details.globalPosition.toVector2(),
);

Expand Down
Expand Up @@ -5,11 +5,10 @@ import 'package:flame/src/game/flame_game.dart';
import 'package:flutter/gestures.dart';

class DragUpdateEvent extends PositionEvent {
DragUpdateEvent(this.pointerId, DragUpdateDetails details)
DragUpdateEvent(this.pointerId, super.game, DragUpdateDetails details)
: timestamp = details.sourceTimeStamp ?? Duration.zero,
delta = details.delta.toVector2(),
super(
canvasPosition: details.localPosition.toVector2(),
devicePosition: details.globalPosition.toVector2(),
);

Expand Down
7 changes: 5 additions & 2 deletions packages/flame/lib/src/events/messages/position_event.dart
@@ -1,5 +1,6 @@
import 'package:flame/components.dart';
import 'package:flame/src/events/messages/event.dart';
import 'package:flame/src/game/game.dart';
import 'package:meta/meta.dart';

/// Base class for events that originate at some point on the screen. These
Expand All @@ -8,13 +9,15 @@ import 'package:meta/meta.dart';
/// This class includes properties that describe the position where the event
/// has occurred.
abstract class PositionEvent extends Event {
PositionEvent({required this.canvasPosition, required this.devicePosition});
PositionEvent(this._game, {required this.devicePosition});
final Game _game;

/// Event position in the coordinate space of the game widget, i.e. relative
/// to the game canvas.
///
/// This could be considered the Flame-level global position.
final Vector2 canvasPosition;
late final Vector2 canvasPosition =
_game.convertGlobalToLocalCoordinate(devicePosition);

/// Event position in the coordinate space of the device -- either the phone,
/// or the browser window, or the app.
Expand Down
3 changes: 1 addition & 2 deletions packages/flame/lib/src/events/messages/tap_down_event.dart
Expand Up @@ -15,10 +15,9 @@ import 'package:flutter/gestures.dart';
/// In order for a component to be eligible to receive this event, it must add
/// the [TapCallbacks] mixin.
class TapDownEvent extends PositionEvent {
TapDownEvent(this.pointerId, TapDownDetails details)
TapDownEvent(this.pointerId, super.game, TapDownDetails details)
: deviceKind = details.kind ?? PointerDeviceKind.unknown,
super(
canvasPosition: details.localPosition.toVector2(),
devicePosition: details.globalPosition.toVector2(),
);

Expand Down
3 changes: 1 addition & 2 deletions packages/flame/lib/src/events/messages/tap_up_event.dart
Expand Up @@ -13,10 +13,9 @@ import 'package:flutter/gestures.dart';
///
/// The [TapUpEvent] will only occur if there was a previous [TapDownEvent].
class TapUpEvent extends PositionEvent {
TapUpEvent(this.pointerId, TapUpDetails details)
TapUpEvent(this.pointerId, super.game, TapUpDetails details)
: deviceKind = details.kind,
super(
canvasPosition: details.localPosition.toVector2(),
devicePosition: details.globalPosition.toVector2(),
);

Expand Down
Expand Up @@ -29,6 +29,7 @@ void main() {
expect(game.children.whereType<MultiDragDispatcher>().length, 1);
game.firstChild<MultiDragDispatcher>()!.onDragStart(
createDragStartEvents(
game: game,
localPosition: const Offset(12, 12),
globalPosition: const Offset(12, 12),
),
Expand All @@ -47,6 +48,7 @@ void main() {

dispatcher.onDragStart(
createDragStartEvents(
game: game,
localPosition: const Offset(12, 12),
globalPosition: const Offset(12, 12),
),
Expand All @@ -57,6 +59,7 @@ void main() {

dispatcher.onDragUpdate(
createDragUpdateEvents(
game: game,
localPosition: const Offset(15, 15),
globalPosition: const Offset(15, 15),
),
Expand Down Expand Up @@ -84,6 +87,7 @@ void main() {

dispatcher.onDragUpdate(
createDragUpdateEvents(
game: game,
localPosition: const Offset(15, 15),
globalPosition: const Offset(15, 15),
),
Expand Down
Expand Up @@ -27,6 +27,7 @@ void main() {
expect(game.children.whereType<MultiTapDispatcher>().length, equals(1));
game.firstChild<MultiTapDispatcher>()!.onTapDown(
createTapDownEvents(
game: game,
localPosition: const Offset(12, 12),
globalPosition: const Offset(12, 12),
),
Expand All @@ -48,6 +49,7 @@ void main() {

dispatcher.onTapDown(
createTapDownEvents(
game: game,
localPosition: const Offset(12, 12),
globalPosition: const Offset(12, 12),
),
Expand All @@ -59,6 +61,7 @@ void main() {
// [onTapUp] will call, if there was an [onTapDown] event before
dispatcher.onTapUp(
createTapUpEvents(
game: game,
localPosition: const Offset(12, 12),
globalPosition: const Offset(12, 12),
),
Expand All @@ -82,6 +85,7 @@ void main() {

dispatcher.onTapDown(
createTapDownEvents(
game: game,
localPosition: const Offset(12, 12),
globalPosition: const Offset(12, 12),
),
Expand All @@ -93,6 +97,7 @@ void main() {
// [onTapUp] will call, if there was an [onTapDown] event before
dispatcher.onLongTapDown(
createTapDownEvents(
game: game,
localPosition: const Offset(12, 12),
globalPosition: const Offset(12, 12),
),
Expand Down
9 changes: 9 additions & 0 deletions packages/flame_test/lib/src/mock_tap_drag_events.dart
@@ -1,14 +1,17 @@
import 'package:flame/events.dart';
import 'package:flame/game.dart';
import 'package:flutter/gestures.dart';

TapDownEvent createTapDownEvents({
required Game game,
int? pointerId,
PointerDeviceKind? kind,
Offset? globalPosition,
Offset? localPosition,
}) {
return TapDownEvent(
pointerId ?? 1,
game,
TapDownDetails(
localPosition: localPosition ?? Offset.zero,
globalPosition: globalPosition ?? Offset.zero,
Expand All @@ -18,13 +21,15 @@ TapDownEvent createTapDownEvents({
}

TapUpEvent createTapUpEvents({
required Game game,
int? pointerId,
PointerDeviceKind? kind,
Offset? globalPosition,
Offset? localPosition,
}) {
return TapUpEvent(
pointerId ?? 1,
game,
TapUpDetails(
localPosition: localPosition ?? Offset.zero,
globalPosition: globalPosition ?? Offset.zero,
Expand All @@ -34,13 +39,15 @@ TapUpEvent createTapUpEvents({
}

DragStartEvent createDragStartEvents({
required Game game,
int? pointerId,
PointerDeviceKind? kind,
Offset? globalPosition,
Offset? localPosition,
}) {
return DragStartEvent(
pointerId ?? 1,
game,
DragStartDetails(
localPosition: localPosition ?? Offset.zero,
globalPosition: globalPosition ?? Offset.zero,
Expand All @@ -49,13 +56,15 @@ DragStartEvent createDragStartEvents({
}

DragUpdateEvent createDragUpdateEvents({
required Game game,
int? pointerId,
PointerDeviceKind? kind,
Offset? globalPosition,
Offset? localPosition,
}) {
return DragUpdateEvent(
pointerId ?? 1,
game,
DragUpdateDetails(
localPosition: localPosition ?? Offset.zero,
globalPosition: globalPosition ?? Offset.zero,
Expand Down

0 comments on commit 87139c8

Please sign in to comment.