11package edu .wpi .grip .ui .pipeline ;
22
33import edu .wpi .grip .core .events .BenchmarkEvent ;
4+ import edu .wpi .grip .core .events .SocketChangedEvent ;
45import edu .wpi .grip .core .events .SocketPreviewChangedEvent ;
56import edu .wpi .grip .core .sockets .OutputSocket ;
67import edu .wpi .grip .core .sockets .Socket ;
78import edu .wpi .grip .core .sockets .SocketHint ;
89import edu .wpi .grip .ui .Controller ;
910import edu .wpi .grip .ui .annotations .ParametrizedController ;
11+ import edu .wpi .grip .ui .preview .ImageBasedPreviewView ;
1012
1113import com .google .common .annotations .VisibleForTesting ;
1214import com .google .common .eventbus .Subscribe ;
2325import javafx .scene .layout .StackPane ;
2426
2527import static com .google .common .base .Preconditions .checkNotNull ;
28+ import static org .bytedeco .javacpp .opencv_core .Mat ;
2629
2730/**
2831 * A JavaFX control that renders an {@link OutputSocket} that is the output of a step. It shows a
@@ -34,7 +37,7 @@ public class OutputSocketController implements Controller {
3437
3538 private final SocketHandleView .Factory socketHandleFactory ;
3639 private final InvalidationListener previewListener ;
37- private final OutputSocket socket ;
40+ private final OutputSocket <?> socket ;
3841 @ FXML
3942 private HBox root ;
4043 @ FXML
@@ -79,14 +82,43 @@ protected void initialize() {
7982 this .type .setText (this .socket .getSocketHint ().getTypeLabel ());
8083 }
8184
82- public Socket getSocket () {
85+ public Socket <?> getSocket () {
8386 return this .socket ;
8487 }
8588
8689 public SocketHandleView getHandle () {
8790 return this .handle ;
8891 }
8992
93+ @ Subscribe
94+ public void onSocketChanged (SocketChangedEvent event ) {
95+ if (event .isRegarding (this .socket )) {
96+ if (!this .socket .getValue ().isPresent ()) {
97+ // No value
98+ handlePreview (false );
99+ } else if (!(this .socket .getValue ().get () instanceof Mat )) {
100+ // There is a non-image value, which can always be previewed
101+ handlePreview (true );
102+ } else {
103+ // Only allow the image to be previewed if it's previewable
104+ boolean previewable = this .socket .getValue ()
105+ .map (Mat .class ::cast )
106+ .map (ImageBasedPreviewView ::isPreviewable )
107+ .get ();
108+ handlePreview (previewable );
109+ }
110+ }
111+ }
112+
113+ /**
114+ * Disables the preview button and hides the preview if the socket value isn't able to be
115+ * previewed.
116+ */
117+ private void handlePreview (boolean previewable ) {
118+ this .preview .setDisable (!previewable );
119+ this .socket .setPreviewed (this .socket .isPreviewed () && previewable );
120+ }
121+
90122 @ Subscribe
91123 public void onSocketPreviewChangedEvent (SocketPreviewChangedEvent event ) {
92124 // Only try to update the button if the two aren't the same
0 commit comments