-
-
Notifications
You must be signed in to change notification settings - Fork 873
/
drag_callbacks.dart
75 lines (68 loc) · 2.91 KB
/
drag_callbacks.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import 'package:flame/src/components/core/component.dart';
import 'package:flame/src/events/flame_game_mixins/multi_drag_dispatcher.dart';
import 'package:flame/src/events/messages/drag_cancel_event.dart';
import 'package:flame/src/events/messages/drag_end_event.dart';
import 'package:flame/src/events/messages/drag_start_event.dart';
import 'package:flame/src/events/messages/drag_update_event.dart';
import 'package:meta/meta.dart';
/// This mixin can be added to a [Component] allowing it to receive drag events.
///
/// In addition to adding this mixin, the component must also implement the
/// [containsLocalPoint] method -- the component will only register the start of
/// a drag if the point where the initial touch event has occurred was inside
/// the component.
///
/// This mixin is the replacement of the Draggable mixin.
mixin DragCallbacks on Component {
bool _isDragged = false;
/// Returns true while the component is being dragged.
bool get isDragged => _isDragged;
/// The user initiated a drag gesture on top of this component.
///
/// By default, only one component will receive a drag event. However, setting
/// the property `event.continuePropagation` to true will allow the event to
/// also reach the components below this one.
///
/// Once a component receives the [onDragStart] event, the subsequent events
/// [onDragUpdate], [onDragEnd], and [onDragCancel] with the same pointer id
/// will be delivered to the same component. If multiple components have
/// received the initial [onDragStart] event, then all of them will be
/// receiving the follow-up events.
@mustCallSuper
void onDragStart(DragStartEvent event) {
_isDragged = true;
}
/// The user has moved the pointer that initiated the drag gesture.
///
/// This event will be delivered to the component(s) that captured the initial
/// [onDragStart], even if the point of touch moves outside of the boundaries
/// of the component. In the latter case `event.localPosition` will contain a
/// NaN point.
void onDragUpdate(DragUpdateEvent event) {}
/// The drag event has ended.
///
/// This event will be delivered to the component(s) that captured the initial
/// [onDragStart], even if the point of touch moves outside of the boundaries
/// of the component.
@mustCallSuper
void onDragEnd(DragEndEvent event) {
_isDragged = false;
}
/// The drag was cancelled.
///
/// This is a very rare event, so we provide a default implementation that
/// converts it into an [onDragEnd] event.
@mustCallSuper
void onDragCancel(DragCancelEvent event) => onDragEnd(event.toDragEnd());
@override
@mustCallSuper
void onMount() {
super.onMount();
final game = findRootGame()!;
if (game.findByKey(const MultiDragDispatcherKey()) == null) {
final dispatcher = MultiDragDispatcher();
game.registerKey(const MultiDragDispatcherKey(), dispatcher);
game.add(dispatcher);
}
}
}