vid_us_2d_visual.dart 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import 'dart:typed_data';
  2. import 'package:vid/us/vid_us_data_reader.dart';
  3. import 'package:vid/us/vid_us_data_writer.dart';
  4. import 'package:vid/us/vid_us_display_mode.dart';
  5. import 'package:vid/us/vid_us_logical_coordinate.dart';
  6. import 'package:vid/us/vid_us_mode.dart';
  7. import 'package:vid/us/vid_us_physical_coordinate.dart';
  8. import 'package:vid/us/vid_us_visual.dart';
  9. import 'package:vid/us/vid_us_visual_area_type.dart';
  10. import 'package:vid/us/vid_us_visual_indicator.dart';
  11. import 'package:vid/us/vid_us_visual_type.dart';
  12. class VidUs2DVisual extends VidUsVisual {
  13. late Map<VidUsVisualAreaType, VidUsLogicalCoordinate> _logicalCoordinates;
  14. late Map<VidUsVisualAreaType, VidUsPhysicalCoordinate> _physicalCoordinates;
  15. /// Get all LogicalCoordinates of this visual.
  16. Map<VidUsVisualAreaType, VidUsLogicalCoordinate> get logicalCoordinates =>
  17. _logicalCoordinates;
  18. ///Get all PhysicalCoordinates of this visual.
  19. Map<VidUsVisualAreaType, VidUsPhysicalCoordinate> get physicalCoordinates =>
  20. _physicalCoordinates;
  21. VidUs2DVisual() {
  22. setVisualType(VidUsVisualType.V2D);
  23. _logicalCoordinates =
  24. new Map<VidUsVisualAreaType, VidUsLogicalCoordinate>();
  25. _physicalCoordinates =
  26. new Map<VidUsVisualAreaType, VidUsPhysicalCoordinate>();
  27. }
  28. ///Convert VidUs2DVisual to binary data.
  29. @override
  30. Uint8List toBytes() {
  31. var writer = new VidUsDataWriter();
  32. var baseData = super.toBytes();
  33. writer.writeBytes(baseData);
  34. writer.writeByte(_physicalCoordinates.length);
  35. _physicalCoordinates.forEach((key, value) {
  36. writer.writeByte(key.index);
  37. writer.writeBytes(value.toBytes());
  38. });
  39. writer.writeByte(_logicalCoordinates.length);
  40. _logicalCoordinates.forEach((key, value) {
  41. writer.writeByte(key.index);
  42. writer.writeBytes(value.toBytes());
  43. });
  44. return writer.data;
  45. }
  46. ///Convert binary data to VidUs2DVisual.
  47. static VidUsVisual fromBytes(Uint8List bytes) {
  48. var reader = new VidUsDataReader(bytes);
  49. var visualType = VidUsVisualType.values[reader.readByte()];
  50. var displayMode = VidUsDisplayMode.values[reader.readByte()];
  51. if (visualType == VidUsVisualType.V2D) {
  52. var indicator = VidUsVisualIndicator.values[reader.readByte()];
  53. var activeModeType = VidUsModeType.values[reader.readByte()];
  54. var modeCount = reader.readByte();
  55. List<VidUsMode> modes = [];
  56. for (var i = 0; i < modeCount; i++) {
  57. var mode = VidUsMode.fromBytes(reader.readBytes());
  58. modes.add(mode);
  59. }
  60. var visual = new VidUs2DVisual();
  61. visual.setIndicator(indicator);
  62. modes.forEach((mode) {
  63. visual.modes.add(mode);
  64. });
  65. visual..setActiveModeType(activeModeType);
  66. var physicalCoordinateCount = reader.readByte();
  67. for (var i = 0; i < physicalCoordinateCount; i++) {
  68. var visualAreaType = VidUsVisualAreaType.values[reader.readByte()];
  69. var physicalCoordinate =
  70. VidUsPhysicalCoordinate.fromBytes(reader.readBytes());
  71. visual._physicalCoordinates[visualAreaType] = physicalCoordinate;
  72. }
  73. var logicalCoordinateCount = reader.readByte();
  74. for (var i = 0; i < logicalCoordinateCount; i++) {
  75. var visualAreaType = VidUsVisualAreaType.values[reader.readByte()];
  76. var logicalCoordinate =
  77. VidUsLogicalCoordinate.fromBytes(reader.readBytes());
  78. visual._logicalCoordinates[visualAreaType] = logicalCoordinate;
  79. }
  80. visual.setDisplayMode(displayMode);
  81. return visual;
  82. }
  83. throw new Exception(
  84. "Can not restore visual data from $visualType to VidUs2DVisual.");
  85. }
  86. }