完善遥控权限、控制发送和急停反馈
This commit is contained in:
@@ -63,6 +63,8 @@ class CCJoystick extends StatefulWidget {
|
||||
final AxisHint axisHint;
|
||||
final Function(JoystickValue) onValueChanged;
|
||||
final VoidCallback? onPress;
|
||||
/// Optional observer only; failures in diagnostics cannot affect gestures.
|
||||
final void Function(String event, Map<String, Object?> fields)? onDiagnostic;
|
||||
final Future<void> Function()? onPanEnd; // 🔥 修复:改为异步回调
|
||||
final double radius;
|
||||
|
||||
@@ -74,6 +76,7 @@ class CCJoystick extends StatefulWidget {
|
||||
required this.axisHint,
|
||||
required this.onValueChanged,
|
||||
this.onPress,
|
||||
this.onDiagnostic,
|
||||
this.onPanEnd, // 🔥 新增
|
||||
this.radius = 80,
|
||||
this.skin,
|
||||
@@ -89,6 +92,16 @@ class _CustomJoystickState extends State<CCJoystick>
|
||||
late AnimationController _floatController;
|
||||
bool _isPanning = false; // 🔥 跟踪是否正在拖拽
|
||||
|
||||
void _diagnose(String event, [Map<String, Object?> fields = const {}]) {
|
||||
try {
|
||||
widget.onDiagnostic?.call(event, {
|
||||
'axis': widget.axisHint.name,
|
||||
'panning': _isPanning,
|
||||
...fields,
|
||||
});
|
||||
} catch (_) {}
|
||||
}
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
@@ -116,7 +129,10 @@ class _CustomJoystickState extends State<CCJoystick>
|
||||
y = ((-_offset.dy / widget.radius) * 100).clamp(-100, 100).toInt();
|
||||
}
|
||||
|
||||
widget.onValueChanged(JoystickValue(_applyDeadZone(x), _applyDeadZone(y)));
|
||||
final outputX = _applyDeadZone(x);
|
||||
final outputY = _applyDeadZone(y);
|
||||
_diagnose('value', {'rawX': x, 'rawY': y, 'x': outputX, 'y': outputY});
|
||||
widget.onValueChanged(JoystickValue(outputX, outputY));
|
||||
}
|
||||
|
||||
int _applyDeadZone(int value, {int deadZone = 5}) {
|
||||
@@ -130,16 +146,17 @@ class _CustomJoystickState extends State<CCJoystick>
|
||||
onPointerUp: (_) async {
|
||||
if (_isPanning) {
|
||||
debugPrint('🛡️ [兜底] 全局指针抬起,强制归零');
|
||||
await _handlePanEnd();
|
||||
await _handlePanEnd(reason: 'pointer_up');
|
||||
}
|
||||
},
|
||||
onPointerCancel: (_) async {
|
||||
await _handlePanEnd();
|
||||
await _handlePanEnd(reason: 'pointer_cancel');
|
||||
},
|
||||
child: GestureDetector(
|
||||
behavior: HitTestBehavior.opaque,
|
||||
onPanStart: (_) {
|
||||
_isPanning = true;
|
||||
_diagnose('gesture_start');
|
||||
widget.onPress?.call();
|
||||
},
|
||||
onPanUpdate: (details) {
|
||||
@@ -165,12 +182,12 @@ class _CustomJoystickState extends State<CCJoystick>
|
||||
},
|
||||
onPanEnd: (_) async {
|
||||
debugPrint('✋ [正常] onPanEnd 触发');
|
||||
await _handlePanEnd();
|
||||
await _handlePanEnd(reason: 'pan_end');
|
||||
},
|
||||
onPanCancel: () async {
|
||||
// 🔥 新增:处理手势取消(如来电中断)
|
||||
debugPrint('⚠️ [异常] onPanCancel 触发');
|
||||
await _handlePanEnd();
|
||||
await _handlePanEnd(reason: 'pan_cancel');
|
||||
},
|
||||
child: AnimatedBuilder(
|
||||
animation: _floatController,
|
||||
@@ -192,7 +209,8 @@ class _CustomJoystickState extends State<CCJoystick>
|
||||
}
|
||||
|
||||
// 🔥 统一的松手处理逻辑
|
||||
Future<void> _handlePanEnd() async {
|
||||
Future<void> _handlePanEnd({required String reason}) async {
|
||||
_diagnose('gesture_end', {'reason': reason, 'willStop': _isPanning});
|
||||
if (!_isPanning) return;
|
||||
_isPanning = false;
|
||||
setState(() => _offset = Offset.zero); // 归位
|
||||
|
||||
Reference in New Issue
Block a user