Repository navigation
Expand file tree
/
Copy pathfree_example.dart
More file actions
130 lines (118 loc) · 3.49 KB
/
Copy pathfree_example.dart
File metadata and controls
130 lines (118 loc) · 3.49 KB
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:helgoboss_pointer_lock/helgoboss_pointer_lock.dart';
import 'package:helgoboss_pointer_lock_example/info_panel.dart';
class FreeExample extends StatefulWidget {
final PointerLockCursor cursor;
final PointerLockWindowsMode windowsMode;
const FreeExample({
super.key,
required this.cursor,
required this.windowsMode,
});
@override
State<FreeExample> createState() => _FreeExampleState();
}
class _FreeExampleState extends State<FreeExample> {
StreamSubscription<PointerLockMoveEvent>? _subscription;
Offset _firstPointerDelta = Offset.zero;
Offset _lastPointerDelta = Offset.zero;
Offset _accumulation = Offset.zero;
@override
void dispose() {
_subscription?.cancel();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Listener(
behavior: HitTestBehavior.translucent,
onPointerCancel: _debugPointerEvent,
onPointerDown: _debugPointerEvent,
onPointerUp: _debugPointerEvent,
onPointerPanZoomEnd: _debugPointerEvent,
onPointerPanZoomStart: _debugPointerEvent,
onPointerPanZoomUpdate: _debugPointerEvent,
onPointerSignal: _debugPointerEvent,
child: _subscription == null
? Center(
child: ElevatedButton(
onPressed: _startSession,
child: const Text("Lock pointer!"),
),
)
: Actions(
actions: {
DismissIntent: CallbackAction(onInvoke: (intent) {
_stopSession();
return null;
})
},
child: Focus(
autofocus: true,
child: InfoPanel(
firstPointerDelta: _firstPointerDelta,
lastPointerDelta: _lastPointerDelta,
accumulation: _accumulation,
additionalText:
"Unlock the pointer by pressing the Escape key!",
),
),
));
}
void _startSession() {
if (_subscription != null) {
return;
}
final deltaStream = pointerLock.createSession(
windowsMode: widget.windowsMode,
cursor: widget.cursor,
);
final subscription = deltaStream.listen(
(event) {
_processMoveDelta(event.delta);
},
onDone: () {
// Stream closed naturally
_setSubscription(null);
},
onError: (error) {
// Handle any errors
debugPrint('Pointer lock error: $error');
_setSubscription(null);
},
);
_setSubscription(subscription);
}
void _stopSession() async {
final subscription = _subscription;
if (subscription == null) {
return;
}
_setSubscription(null);
await subscription.cancel();
}
void _setSubscription(StreamSubscription<PointerLockMoveEvent>? value) {
setState(() {
_firstPointerDelta = Offset.zero;
_lastPointerDelta = Offset.zero;
_accumulation = Offset.zero;
_subscription = value;
});
}
void _processMoveDelta(Offset delta) {
if (!mounted) {
return;
}
setState(() {
if (_firstPointerDelta == Offset.zero) {
_firstPointerDelta = delta;
}
_lastPointerDelta = delta;
_accumulation += delta;
});
}
}
void _debugPointerEvent(PointerEvent evt) {
debugPrint(evt.toString(minLevel: DiagnosticLevel.fine));
}