Skip to content

Commit

Permalink
added option to modify robot & trajectory colors (#82)
Browse files Browse the repository at this point in the history
Co-authored-by: Gold87 <[email protected]>
  • Loading branch information
DanPeled and Gold872 authored Aug 10, 2024
1 parent 189d21f commit b0d1fa0
Show file tree
Hide file tree
Showing 5 changed files with 346 additions and 55 deletions.
150 changes: 108 additions & 42 deletions lib/widgets/nt_widgets/multi-topic/field_widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import 'package:elastic_dashboard/services/field_images.dart';
import 'package:elastic_dashboard/services/nt4_client.dart';
import 'package:elastic_dashboard/services/settings.dart';
import 'package:elastic_dashboard/services/text_formatter_builder.dart';
import 'package:elastic_dashboard/widgets/dialog_widgets/dialog_color_picker.dart';
import 'package:elastic_dashboard/widgets/dialog_widgets/dialog_dropdown_chooser.dart';
import 'package:elastic_dashboard/widgets/dialog_widgets/dialog_text_input.dart';
import 'package:elastic_dashboard/widgets/dialog_widgets/dialog_toggle_switch.dart';
Expand All @@ -31,11 +32,68 @@ class FieldWidgetModel extends NTWidgetModel {
bool _showOtherObjects = true;
bool _showTrajectories = true;

Color _robotColor = Colors.red;
Color _trajectoryColor = Colors.white;

final double _otherObjectSize = 0.55;
final double _trajectoryPointSize = 0.08;

Size? _widgetSize;

get robotWidthMeters => _robotWidthMeters;

set robotWidthMeters(value) {
_robotWidthMeters = value;
refresh();
}

get robotLengthMeters => _robotLengthMeters;

set robotLengthMeters(value) {
_robotLengthMeters = value;
refresh();
}

get showOtherObjects => _showOtherObjects;

set showOtherObjects(value) {
_showOtherObjects = value;
refresh();
}

get showTrajectories => _showTrajectories;

set showTrajectories(value) {
_showTrajectories = value;
refresh();
}

get robotColor => _robotColor;

set robotColor(value) {
_robotColor = value;
refresh();
}

get trajectoryColor => _trajectoryColor;

set trajectoryColor(value) {
_trajectoryColor = value;
refresh();
}

get otherObjectSize => _otherObjectSize;

get trajectoryPointSize => _trajectoryPointSize;

get widgetSize => _widgetSize;

set widgetSize(value) {
_widgetSize = value;
}

get field => _field;

late String _robotTopicName;
final List<String> _otherObjectTopics = [];

Expand All @@ -50,10 +108,18 @@ class FieldWidgetModel extends NTWidgetModel {
String? fieldName,
bool showOtherObjects = true,
bool showTrajectories = true,
double robotWidthMeters = 0.85,
double robotLengthMeters = 0.85,
Color robotColor = Colors.red,
Color trajectoryColor = Colors.white,
super.dataType,
super.period,
}) : _showTrajectories = showTrajectories,
_showOtherObjects = showOtherObjects,
_robotWidthMeters = robotWidthMeters,
_robotLengthMeters = robotLengthMeters,
_robotColor = robotColor,
_trajectoryColor = trajectoryColor,
super() {
_fieldGame = fieldName ?? _fieldGame;

Expand Down Expand Up @@ -84,6 +150,10 @@ class FieldWidgetModel extends NTWidgetModel {
}

_field = FieldImages.getFieldFromGame(_fieldGame)!;

_robotColor = Color(tryCast(jsonData['robot_color']) ?? Colors.red.value);
_trajectoryColor =
Color(tryCast(jsonData['trajectory_color']) ?? Colors.white.value);
}

@override
Expand Down Expand Up @@ -134,6 +204,8 @@ class FieldWidgetModel extends NTWidgetModel {
'robot_length': _robotLengthMeters,
'show_other_objects': _showOtherObjects,
'show_trajectories': _showTrajectories,
'robot_color': robotColor.value,
'trajectory_color': trajectoryColor.value,
};
}

Expand Down Expand Up @@ -274,6 +346,37 @@ class FieldWidgetModel extends NTWidgetModel {
),
],
),
const SizedBox(height: 10),
Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
mainAxisSize: MainAxisSize.max,
children: [
Expanded(
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 4.0),
child: DialogColorPicker(
onColorPicked: (color) {
robotColor = color;
},
label: 'Robot Color',
initialColor: robotColor,
),
),
),
Expanded(
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 4.0),
child: DialogColorPicker(
onColorPicked: (color) {
trajectoryColor = color;
},
label: 'Trajectory Color',
initialColor: trajectoryColor,
),
),
),
],
),
];
}

Expand Down Expand Up @@ -344,46 +447,6 @@ class FieldWidgetModel extends NTWidgetModel {
await Future.delayed(delayTime);
}
}

get robotWidthMeters => _robotWidthMeters;

set robotWidthMeters(value) {
_robotWidthMeters = value;
refresh();
}

get robotLengthMeters => _robotLengthMeters;

set robotLengthMeters(value) {
_robotLengthMeters = value;
refresh();
}

get showOtherObjects => _showOtherObjects;

set showOtherObjects(value) {
_showOtherObjects = value;
refresh();
}

get showTrajectories => _showTrajectories;

set showTrajectories(value) {
_showTrajectories = value;
refresh();
}

get otherObjectSize => _otherObjectSize;

get trajectoryPointSize => _trajectoryPointSize;

get widgetSize => _widgetSize;

set widgetSize(value) {
_widgetSize = value;
}

get field => _field;
}

class FieldWidget extends NTWidget {
Expand Down Expand Up @@ -448,7 +511,7 @@ class FieldWidget extends NTWidget {
decoration: BoxDecoration(
color: Colors.black.withOpacity(0.35),
border: Border.all(
color: Colors.red,
color: model.robotColor,
width: 4.0,
),
),
Expand Down Expand Up @@ -613,6 +676,7 @@ class FieldWidget extends NTWidget {
for (List<Offset> points in trajectoryPoints)
CustomPaint(
painter: TrajectoryPainter(
color: model.trajectoryColor,
points: points,
strokeWidth: model.trajectoryPointSize *
model.field.pixelsPerMeterHorizontal *
Expand Down Expand Up @@ -668,10 +732,12 @@ class TrianglePainter extends CustomPainter {
class TrajectoryPainter extends CustomPainter {
final List<Offset> points;
final double strokeWidth;
final Color color;

TrajectoryPainter({
required this.points,
required this.strokeWidth,
this.color = Colors.white,
});

@override
Expand All @@ -680,7 +746,7 @@ class TrajectoryPainter extends CustomPainter {
return;
}
Paint trajectoryPaint = Paint()
..color = Colors.white
..color = color
..strokeWidth = strokeWidth
..style = PaintingStyle.stroke
..strokeCap = StrokeCap.round;
Expand Down
24 changes: 12 additions & 12 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -564,18 +564,18 @@ packages:
dependency: transitive
description:
name: leak_tracker
sha256: "7f0df31977cb2c0b88585095d168e689669a2cc9b97c309665e3386f3e9d341a"
sha256: "3f87a60e8c63aecc975dda1ceedbc8f24de75f09e4856ea27daf8958f2f0ce05"
url: "https://pub.dev"
source: hosted
version: "10.0.4"
version: "10.0.5"
leak_tracker_flutter_testing:
dependency: transitive
description:
name: leak_tracker_flutter_testing
sha256: "06e98f569d004c1315b991ded39924b21af84cf14cc94791b8aea337d25b57f8"
sha256: "932549fb305594d82d7183ecd9fa93463e9914e1b67cacc34bc40906594a1806"
url: "https://pub.dev"
source: hosted
version: "3.0.3"
version: "3.0.5"
leak_tracker_testing:
dependency: transitive
description:
Expand Down Expand Up @@ -620,10 +620,10 @@ packages:
dependency: transitive
description:
name: material_color_utilities
sha256: "0e0a020085b65b6083975e499759762399b4475f766c21668c4ecca34ea74e5a"
sha256: f7142bb1154231d7ea5f96bc7bde4bda2a0945d2806bb11670e30b850d56bdec
url: "https://pub.dev"
source: hosted
version: "0.8.0"
version: "0.11.1"
messagepack:
dependency: "direct main"
description:
Expand All @@ -636,10 +636,10 @@ packages:
dependency: transitive
description:
name: meta
sha256: "7687075e408b093f36e6bbf6c91878cc0d4cd10f409506f7bc996f68220b9136"
sha256: bdb68674043280c3428e9ec998512fb681678676b3c54e773629ffe74419f8c7
url: "https://pub.dev"
source: hosted
version: "1.12.0"
version: "1.15.0"
mime:
dependency: transitive
description:
Expand Down Expand Up @@ -1033,10 +1033,10 @@ packages:
dependency: transitive
description:
name: test_api
sha256: "9955ae474176f7ac8ee4e989dadfb411a58c30415bcfb648fa04b2b8a03afa7f"
sha256: "5b8a98dafc4d5c4c9c72d8b31ab2b23fc13422348d2997120294d3bac86b4ddb"
url: "https://pub.dev"
source: hosted
version: "0.7.0"
version: "0.7.2"
timing:
dependency: transitive
description:
Expand Down Expand Up @@ -1177,10 +1177,10 @@ packages:
dependency: transitive
description:
name: vm_service
sha256: "3923c89304b715fb1eb6423f017651664a03bf5f4b29983627c4da791f74a4ec"
sha256: f652077d0bdf60abe4c1f6377448e8655008eef28f128bc023f7b5e8dfeb48fc
url: "https://pub.dev"
source: hosted
version: "14.2.1"
version: "14.2.4"
watcher:
dependency: transitive
description:
Expand Down
10 changes: 10 additions & 0 deletions test/test_util.dart
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,16 @@ MockNTConnection createMockOnlineNT4({

when(mockNT4Connection.announcedTopics()).thenReturn(virtualTopicsMap);

when(mockNT4Connection.addTopicAnnounceListener(any))
.thenAnswer((realInvocation) {
if (virtualTopics == null) {
return;
}
for (NT4Topic topic in virtualTopics) {
realInvocation.positionalArguments[0].call(topic);
}
});

when(mockSubscription.periodicStream()).thenAnswer((_) => Stream.value(null));

when(mockSubscription.listen(any)).thenAnswer((realInvocation) {});
Expand Down
Loading

0 comments on commit b0d1fa0

Please sign in to comment.