fixes for detection with new target styles

This commit is contained in:
2025-12-23 16:30:18 +01:00
parent 99b26893e7
commit e111f0a99b
37 changed files with 391 additions and 14 deletions
+81 -7
View File
@@ -38,7 +38,7 @@ class _PreviewScreenState extends State<PreviewScreen> {
Uint8List? _cachedTargetImage;
Uint8List? _cachedRealImage;
final String targetAssetPath = 'assets/target.png';
late String targetAssetPath;
final Offset fixedRingTargetCenter = const Offset(602.51, 600.43);
final Size fixedRingTargetSize = const Size(355.94, 359.27);
@@ -58,15 +58,23 @@ class _PreviewScreenState extends State<PreviewScreen> {
Color get _activeColorWithOpacity => _activeColor.withOpacity(_bulletOpacity);
String _selectedLanguage = 'English';
Color _backgroundColor = Colors.grey[900]!;
@override
void initState() {
super.initState();
_realImageDetections = widget.detections;
_targetImageFuture = _loadAndDecodeTargetImage();
_loadBulletColor(); // 👈 Add this
_loadLanguage();
_runDetectionAndMapping();
targetAssetPath = 'assets/target.png'; // Default
_initializeScreen();
}
Future<void> _initializeScreen() async {
await Future.wait([
_loadSelectedTarget(),
_loadBulletColor(),
_loadLanguage(),
]);
await _runDetectionAndMapping();
}
String _t(String key) {
@@ -80,6 +88,71 @@ class _PreviewScreenState extends State<PreviewScreen> {
});
}
Future<void> _loadSelectedTarget() async {
final prefs = await SharedPreferences.getInstance();
// For detection mode (cameraWithDetections), always use default target
// For presentation mode (mappedTarget), use selected target
final selectedTarget = _mode == PreviewMode.mappedTarget
? (prefs.getString('selectedTarget') ?? 'assets/target.png')
: 'assets/target.png';
// Update the target path and trigger image load
targetAssetPath = selectedTarget;
_targetImageFuture = _loadAndDecodeTargetImage();
_backgroundColor = _getBackgroundColorForTarget(selectedTarget);
// Await the image decoding to ensure it's populated before rebuilding
_targetImageDecoded = await _targetImageFuture;
if (_targetImageDecoded != null) {
_cachedTargetImage = Uint8List.fromList(img.encodePng(_targetImageDecoded!));
}
setState(() {
// Trigger rebuild with the new target
});
}
Color _getBackgroundColorForTarget(String targetPath) {
// Map each target to its background color (extracted from first pixel of each image)
switch (targetPath) {
case 'assets/target.png':
return const Color(0xFFFFFFFF); // White
case 'assets/targets/Target1.jpg':
return const Color(0xFFE8ECD5); // Light beige
case 'assets/targets/Target2.jpg':
return const Color(0xFFC1DF87); // Light green
case 'assets/targets/Target3.jpg':
return const Color(0xFFFEFEFE); // Off-white
case 'assets/targets/Target4.jpg':
return const Color(0xFFD42931); // Red
case 'assets/targets/Target5.jpg':
return const Color(0xFF02050A); // Very dark blue/black
case 'assets/targets/Target6.jpg':
return const Color(0xFF000000); // Black
case 'assets/targets/Target7.jpg':
return const Color(0xFFFFD454); // Bright yellow
case 'assets/targets/Target11.jpg':
return const Color(0xFF1E5E81); // Dark teal blue
case 'assets/targets/Target14.jpg':
return const Color(0xFFFEFEFC); // Off-white
case 'assets/targets/Target15.jpg':
return const Color(0xFF98926E); // Olive green
default:
return Colors.grey[900]!; // Fallback to dark gray
}
}
Future<void> _loadBulletColor() async {
final prefs = await SharedPreferences.getInstance();
final selectedColor = prefs.getString('bulletColor') ?? 'Blue';
@@ -783,7 +856,7 @@ Color _getBulletButtonColor(int index) {
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.white,
backgroundColor: _backgroundColor,
appBar: AppBar(
backgroundColor: Colors.grey[900],
iconTheme: const IconThemeData(color: Colors.white),
@@ -799,12 +872,13 @@ Widget build(BuildContext context) {
: Icons.center_focus_strong,
),
color: Colors.white,
onPressed: () {
onPressed: () async {
setState(() {
_mode = _mode == PreviewMode.mappedTarget
? PreviewMode.cameraWithDetections
: PreviewMode.mappedTarget;
});
await _loadSelectedTarget(); // Reload target based on new mode
},
tooltip: _mode == PreviewMode.mappedTarget
? _t("Show Camera Detections")