From c97d5ee417f6e3d7239e7bba61c07034b5a0040d Mon Sep 17 00:00:00 2001 From: Gold872 Date: Tue, 7 Nov 2023 21:29:16 -0500 Subject: [PATCH] Added minimum size to widgets --- .../draggable_nt4_widget_container.dart | 425 +++++++++++------- .../draggable_widget_container.dart | 11 +- lib/widgets/network_tree/tree_row.dart | 3 + 3 files changed, 263 insertions(+), 176 deletions(-) diff --git a/lib/widgets/draggable_containers/draggable_nt4_widget_container.dart b/lib/widgets/draggable_containers/draggable_nt4_widget_container.dart index 7a440839..68231380 100644 --- a/lib/widgets/draggable_containers/draggable_nt4_widget_container.dart +++ b/lib/widgets/draggable_containers/draggable_nt4_widget_container.dart @@ -43,6 +43,8 @@ class DraggableNT4WidgetContainer extends DraggableWidgetContainer { required super.initialPosition, required this.child, super.enabled = false, + super.minWidth, + super.minHeight, super.onUpdate, super.onDragBegin, super.onDragEnd, @@ -64,6 +66,252 @@ class DraggableNT4WidgetContainer extends DraggableWidgetContainer { super.onResizeEnd, }) : super.fromJson(); + static double getMinimumWidth(NT4Widget? widget) { + double normalSize = 128.0; + + switch (widget?.type) { + case 'Gyro': + return normalSize * 2; + case 'Camera Stream': + return normalSize * 2; + case 'Field': + return normalSize * 3; + case 'PowerDistribution': + return normalSize * 3; + case 'PIDController': + return normalSize * 2; + case 'DifferentialDrive': + return normalSize * 2; + case 'SwerveDrive': + return normalSize * 2; + case 'Subsystem': + return normalSize * 2; + case 'Command': + return normalSize * 2; + case 'Scheduler': + return normalSize * 2; + case 'FMSInfo': + return normalSize * 3; + case 'RobotPreferences': + return normalSize * 2; + case 'Alerts': + return normalSize * 2; + } + + return normalSize; + } + + static double getMinimumHeight(NT4Widget? widget) { + double normalSize = 128.0; + + switch (widget?.type) { + case 'Gyro': + return normalSize * 2; + case 'Camera Stream': + return normalSize * 2; + case 'Field': + return normalSize * 2; + case 'PowerDistribution': + return normalSize * 4; + case 'PIDController': + return normalSize * 3; + case 'DifferentialDrive': + return normalSize * 2; + case 'SwerveDrive': + return normalSize * 2; + case 'Scheduler': + return normalSize * 2; + case 'RobotPreferences': + return normalSize * 2; + case 'Alerts': + return normalSize * 2; + } + + return normalSize; + } + + @override + void init() { + super.init(); + + minWidth = DraggableNT4WidgetContainer.getMinimumWidth(child); + minHeight = DraggableNT4WidgetContainer.getMinimumHeight(child); + } + + @override + Map toJson() { + return { + ...super.toJson(), + 'type': child?.type, + 'properties': getChildJson(), + }; + } + + @override + void fromJson(Map jsonData) { + super.fromJson(jsonData); + + child = createChildFromJson(jsonData); + } + + @override + void dispose({bool deleting = false}) { + super.dispose(deleting: deleting); + + child?.dispose(deleting: deleting); + } + + @override + void unSubscribe() { + super.unSubscribe(); + + child?.unSubscribe(); + } + + NT4Widget createChildFromJson(Map jsonData) { + switch (jsonData['type']) { + case 'Boolean Box': + return BooleanBox.fromJson( + key: UniqueKey(), + jsonData: jsonData['properties'], + ); + case 'Toggle Switch': + return ToggleSwitch.fromJson( + key: UniqueKey(), + jsonData: jsonData['properties'], + ); + case 'Toggle Button': + return ToggleButton.fromJson( + key: UniqueKey(), + jsonData: jsonData['properties'], + ); + case 'Graph': + return GraphWidget.fromJson( + key: UniqueKey(), + jsonData: jsonData['properties'], + ); + case 'Match Time': + return MatchTimeWidget.fromJson( + key: UniqueKey(), + jsonData: jsonData['properties'], + ); + case 'Single Color View': + return SingleColorView.fromJson( + key: UniqueKey(), + jsonData: jsonData['properties'], + ); + case 'Multi Color View': + return MultiColorView.fromJson( + key: UniqueKey(), + jsonData: jsonData['properties'], + ); + case 'Number Bar': + return NumberBar.fromJson( + key: UniqueKey(), + jsonData: jsonData['properties'], + ); + case 'Number Slider': + return NumberSlider.fromJson( + key: UniqueKey(), + jsonData: jsonData['properties'], + ); + case 'Voltage View': + return VoltageView.fromJson( + key: UniqueKey(), + jsonData: jsonData['properties'], + ); + case 'Text Display': + return TextDisplay.fromJson( + key: UniqueKey(), + jsonData: jsonData['properties'], + ); + case 'Gyro': + return Gyro.fromJson( + key: UniqueKey(), + jsonData: jsonData['properties'], + ); + case 'Field': + return FieldWidget.fromJson( + key: UniqueKey(), + jsonData: jsonData['properties'], + ); + case 'PowerDistribution': + return PowerDistribution.fromJson( + key: UniqueKey(), + jsonData: jsonData['properties'], + ); + case 'PIDController': + return PIDControllerWidget.fromJson( + key: UniqueKey(), + jsonData: jsonData['properties'], + ); + case 'DifferentialDrive': + return DifferentialDrive.fromJson( + key: UniqueKey(), + jsonData: jsonData['properties'], + ); + case 'SwerveDrive': + return SwerveDriveWidget.fromJson( + key: UniqueKey(), + jsonData: jsonData['properties'], + ); + case 'ComboBox Chooser': + return ComboBoxChooser.fromJson( + key: UniqueKey(), + jsonData: jsonData['properties'], + ); + case 'Split Button Chooser': + return SplitButtonChooser.fromJson( + key: UniqueKey(), + jsonData: jsonData['properties'], + ); + case 'Subsystem': + return SubsystemWidget.fromJson( + key: UniqueKey(), + jsonData: jsonData['properties'], + ); + case 'Command': + return CommandWidget.fromJson( + key: UniqueKey(), + jsonData: jsonData['properties'], + ); + case 'Scheduler': + return CommandSchedulerWidget.fromJson( + key: UniqueKey(), + jsonData: jsonData['properties'], + ); + case 'FMSInfo': + return FMSInfo.fromJson( + key: UniqueKey(), + jsonData: jsonData['properties'], + ); + case 'Camera Stream': + return CameraStreamWidget.fromJson( + key: UniqueKey(), + jsonData: jsonData['properties'], + ); + case 'RobotPreferences': + return RobotPreferences.fromJson( + key: UniqueKey(), + jsonData: jsonData['properties'], + ); + case 'Alerts': + return NetworkAlerts.fromJson( + key: UniqueKey(), + jsonData: jsonData['properties'], + ); + default: + return TextDisplay.fromJson( + key: UniqueKey(), + jsonData: jsonData['properties'], + ); + } + } + + Map? getChildJson() { + return child!.toJson(); + } + void changeChildToType(String? type) { if (type == null) { return; @@ -136,21 +384,10 @@ class DraggableNT4WidgetContainer extends DraggableWidgetContainer { child!.unSubscribe(); child = newWidget; - refresh(); - } - - @override - void dispose({bool deleting = false}) { - super.dispose(deleting: deleting); + minWidth = DraggableNT4WidgetContainer.getMinimumWidth(child); + minHeight = DraggableNT4WidgetContainer.getMinimumHeight(child); - child?.dispose(deleting: deleting); - } - - @override - void unSubscribe() { - super.unSubscribe(); - - child?.unSubscribe(); + refresh(); } @override @@ -263,166 +500,6 @@ class DraggableNT4WidgetContainer extends DraggableWidgetContainer { ]; } - @override - Map toJson() { - return { - ...super.toJson(), - 'type': child?.type, - 'properties': getChildJson(), - }; - } - - @override - void fromJson(Map jsonData) { - super.fromJson(jsonData); - - child = createChildFromJson(jsonData); - } - - NT4Widget createChildFromJson(Map jsonData) { - switch (jsonData['type']) { - case 'Boolean Box': - return BooleanBox.fromJson( - key: UniqueKey(), - jsonData: jsonData['properties'], - ); - case 'Toggle Switch': - return ToggleSwitch.fromJson( - key: UniqueKey(), - jsonData: jsonData['properties'], - ); - case 'Toggle Button': - return ToggleButton.fromJson( - key: UniqueKey(), - jsonData: jsonData['properties'], - ); - case 'Graph': - return GraphWidget.fromJson( - key: UniqueKey(), - jsonData: jsonData['properties'], - ); - case 'Match Time': - return MatchTimeWidget.fromJson( - key: UniqueKey(), - jsonData: jsonData['properties'], - ); - case 'Single Color View': - return SingleColorView.fromJson( - key: UniqueKey(), - jsonData: jsonData['properties'], - ); - case 'Multi Color View': - return MultiColorView.fromJson( - key: UniqueKey(), - jsonData: jsonData['properties'], - ); - case 'Number Bar': - return NumberBar.fromJson( - key: UniqueKey(), - jsonData: jsonData['properties'], - ); - case 'Number Slider': - return NumberSlider.fromJson( - key: UniqueKey(), - jsonData: jsonData['properties'], - ); - case 'Voltage View': - return VoltageView.fromJson( - key: UniqueKey(), - jsonData: jsonData['properties'], - ); - case 'Text Display': - return TextDisplay.fromJson( - key: UniqueKey(), - jsonData: jsonData['properties'], - ); - case 'Gyro': - return Gyro.fromJson( - key: UniqueKey(), - jsonData: jsonData['properties'], - ); - case 'Field': - return FieldWidget.fromJson( - key: UniqueKey(), - jsonData: jsonData['properties'], - ); - case 'PowerDistribution': - return PowerDistribution.fromJson( - key: UniqueKey(), - jsonData: jsonData['properties'], - ); - case 'PIDController': - return PIDControllerWidget.fromJson( - key: UniqueKey(), - jsonData: jsonData['properties'], - ); - case 'DifferentialDrive': - return DifferentialDrive.fromJson( - key: UniqueKey(), - jsonData: jsonData['properties'], - ); - case 'SwerveDrive': - return SwerveDriveWidget.fromJson( - key: UniqueKey(), - jsonData: jsonData['properties'], - ); - case 'ComboBox Chooser': - return ComboBoxChooser.fromJson( - key: UniqueKey(), - jsonData: jsonData['properties'], - ); - case 'Split Button Chooser': - return SplitButtonChooser.fromJson( - key: UniqueKey(), - jsonData: jsonData['properties'], - ); - case 'Subsystem': - return SubsystemWidget.fromJson( - key: UniqueKey(), - jsonData: jsonData['properties'], - ); - case 'Command': - return CommandWidget.fromJson( - key: UniqueKey(), - jsonData: jsonData['properties'], - ); - case 'Scheduler': - return CommandSchedulerWidget.fromJson( - key: UniqueKey(), - jsonData: jsonData['properties'], - ); - case 'FMSInfo': - return FMSInfo.fromJson( - key: UniqueKey(), - jsonData: jsonData['properties'], - ); - case 'Camera Stream': - return CameraStreamWidget.fromJson( - key: UniqueKey(), - jsonData: jsonData['properties'], - ); - case 'RobotPreferences': - return RobotPreferences.fromJson( - key: UniqueKey(), - jsonData: jsonData['properties'], - ); - case 'Alerts': - return NetworkAlerts.fromJson( - key: UniqueKey(), - jsonData: jsonData['properties'], - ); - default: - return TextDisplay.fromJson( - key: UniqueKey(), - jsonData: jsonData['properties'], - ); - } - } - - Map? getChildJson() { - return child!.toJson(); - } - @override WidgetContainer getDraggingWidgetContainer(BuildContext context) { return WidgetContainer( diff --git a/lib/widgets/draggable_containers/draggable_widget_container.dart b/lib/widgets/draggable_containers/draggable_widget_container.dart index b5ee292e..98d70a5d 100644 --- a/lib/widgets/draggable_containers/draggable_widget_container.dart +++ b/lib/widgets/draggable_containers/draggable_widget_container.dart @@ -53,6 +53,9 @@ class DraggableWidgetContainer extends StatelessWidget { TransformableBoxController? controller; + double? minWidth; + double? minHeight; + bool enabled = false; bool dragging = false; bool resizing = false; @@ -76,6 +79,8 @@ class DraggableWidgetContainer extends StatelessWidget { required this.title, required Rect initialPosition, this.enabled = false, + this.minWidth = 128, + this.minHeight = 128, this.onUpdate, this.onDragBegin, this.onDragEnd, @@ -165,6 +170,7 @@ class DraggableWidgetContainer extends StatelessWidget { ]; } + @mustCallSuper Map toJson() { return { 'title': title, @@ -175,6 +181,7 @@ class DraggableWidgetContainer extends StatelessWidget { }; } + @mustCallSuper void init() { draggablePositionRect = displayRect; dragStartLocation = displayRect; @@ -233,8 +240,8 @@ class DraggableWidgetContainer extends StatelessWidget { clampingRect: const Rect.fromLTWH(0, 0, double.infinity, double.infinity), constraints: BoxConstraints( - minWidth: Globals.gridSize.toDouble(), - minHeight: Globals.gridSize.toDouble(), + minWidth: minWidth ?? Globals.gridSize.toDouble(), + minHeight: minHeight ?? Globals.gridSize.toDouble(), ), resizeModeResolver: () => ResizeMode.freeform, allowFlippingWhileResizing: false, diff --git a/lib/widgets/network_tree/tree_row.dart b/lib/widgets/network_tree/tree_row.dart index 8b5c7593..cd36e934 100644 --- a/lib/widgets/network_tree/tree_row.dart +++ b/lib/widgets/network_tree/tree_row.dart @@ -198,6 +198,9 @@ class TreeRow { if (primary is Gyro) { width = normalGridSize * 2; height = normalGridSize * 2; + } else if (primary is CameraStreamWidget) { + width = normalGridSize * 2; + height = normalGridSize * 2; } else if (primary is FieldWidget) { width = normalGridSize * 3; height = normalGridSize * 2;