|
13 | 13 | import edu.wpi.grip.core.sockets.OutputSocket; |
14 | 14 | import edu.wpi.grip.ui.annotations.ParametrizedController; |
15 | 15 | import edu.wpi.grip.ui.dragging.OperationDragService; |
| 16 | +import edu.wpi.grip.ui.dragging.StepDragService; |
16 | 17 | import edu.wpi.grip.ui.pipeline.input.InputSocketController; |
17 | 18 | import edu.wpi.grip.ui.pipeline.source.SourceController; |
18 | 19 | import edu.wpi.grip.ui.pipeline.source.SourceControllerFactory; |
|
31 | 32 | import javafx.scene.layout.Pane; |
32 | 33 | import javafx.scene.layout.VBox; |
33 | 34 |
|
| 35 | +import javax.annotation.Nullable; |
34 | 36 | import javax.inject.Inject; |
35 | 37 | import java.util.Collection; |
36 | 38 | import java.util.Map; |
@@ -70,6 +72,8 @@ public final class PipelineController { |
70 | 72 | private AddSourceView addSourceView; |
71 | 73 | @Inject |
72 | 74 | private OperationDragService operationDragService; |
| 75 | + @Inject |
| 76 | + private StepDragService stepDragService; |
73 | 77 |
|
74 | 78 | private ControllerMap<StepController, Node> stepsMapManager; |
75 | 79 | private ControllerMap<SourceController, Node> sourceMapManager; |
@@ -98,55 +102,90 @@ public void initialize() throws Exception { |
98 | 102 | dragEvent.acceptTransferModes(TransferMode.ANY); |
99 | 103 | }); |
100 | 104 |
|
| 105 | + stepDragService.getValue().ifPresent(step -> { |
| 106 | + dragEvent.acceptTransferModes(TransferMode.ANY); |
| 107 | + }); |
| 108 | + |
101 | 109 | }); |
102 | 110 |
|
103 | 111 | stepBox.setOnDragDropped(mouseEvent -> { |
104 | 112 | // If this is an operation being dropped |
105 | 113 | operationDragService.getValue().ifPresent(operation -> { |
106 | | - // Then we need to figure out where to put it in the pipeline |
107 | | - // First create a map of every node in the steps list to its x position |
108 | | - final Map<Double, Node> positionMapping = stepsMapManager |
109 | | - .entrySet() |
110 | | - .stream() |
111 | | - .collect( |
112 | | - Collectors |
113 | | - .toMap(e -> calculateMiddleXPosOfNodeInParent(e.getValue()), |
114 | | - Map.Entry::getValue)); |
115 | | - |
116 | | - // A tree map is an easy way to sort the values |
117 | | - final NavigableMap<Double, Node> sortedPositionMapping |
118 | | - = new TreeMap<>(positionMapping); |
119 | | - |
120 | | - // Now we find the sockets that are to the immediate left and |
121 | | - // immediate right of the drop point |
122 | | - |
123 | | - // These can be null |
124 | | - final Map.Entry<Double, Node> |
125 | | - lowerEntry = sortedPositionMapping.floorEntry(mouseEvent.getX()), |
126 | | - higherEntry = sortedPositionMapping.ceilingEntry(mouseEvent.getX()); |
127 | | - // These can be null |
128 | | - final StepController |
129 | | - lowerStepController = |
130 | | - lowerEntry == null ? |
131 | | - null : stepsMapManager.getWithNode(lowerEntry.getValue()); |
132 | | - final StepController |
133 | | - higherStepController = |
134 | | - higherEntry == null ? |
135 | | - null : stepsMapManager.getWithNode(higherEntry.getValue()); |
136 | | - final Step |
137 | | - lowerStep = lowerStepController == null ? null : lowerStepController.getStep(), |
138 | | - higherStep = higherStepController == null ? null : higherStepController.getStep(); |
139 | | - |
140 | | - |
141 | 114 | operationDragService.completeDrag(); |
| 115 | + final StepPair pair = lowerAndHigherStep(mouseEvent.getX()); |
142 | 116 | // Add the new step to the pipeline between these two steps |
143 | | - pipeline.addStepBetween(stepFactory.create(operation), lowerStep, higherStep); |
| 117 | + pipeline.addStepBetween(stepFactory.create(operation), pair.lower, pair.higher); |
| 118 | + }); |
| 119 | + |
| 120 | + // If this is a step being dropped |
| 121 | + stepDragService.getValue().ifPresent(step -> { |
| 122 | + stepDragService.completeDrag(); |
| 123 | + final StepPair pair = lowerAndHigherStep(mouseEvent.getX()); |
| 124 | + // Move the new step to the pipeline between these two steps |
| 125 | + pipeline.moveStepBetween(step, pair.lower, pair.higher); |
144 | 126 | }); |
145 | 127 | }); |
146 | 128 |
|
147 | 129 | addSourcePane.getChildren().add(addSourceView); |
148 | 130 | } |
149 | 131 |
|
| 132 | + /** |
| 133 | + * Simple class for returning two steps |
| 134 | + */ |
| 135 | + private static final class StepPair { |
| 136 | + final Step lower; |
| 137 | + final Step higher; |
| 138 | + |
| 139 | + StepPair(@Nullable Step lower, @Nullable Step higher) { |
| 140 | + this.lower = lower; |
| 141 | + this.higher = higher; |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + /** |
| 146 | + * Determines the steps (via the {@link StepController} that are above and below the given |
| 147 | + * {@code x} value in the list of steps. |
| 148 | + * |
| 149 | + * @param x The x value to find what steps this is between |
| 150 | + * @return The pair of steps that are above and below this x position. |
| 151 | + */ |
| 152 | + private StepPair lowerAndHigherStep(double x) { |
| 153 | + // Then we need to figure out where to put it in the pipeline |
| 154 | + // First create a map of every node in the steps list to its x position |
| 155 | + final Map<Double, Node> positionMapping = stepsMapManager |
| 156 | + .entrySet() |
| 157 | + .stream() |
| 158 | + .collect( |
| 159 | + Collectors |
| 160 | + .toMap(e -> calculateMiddleXPosOfNodeInParent(e.getValue()), |
| 161 | + Map.Entry::getValue)); |
| 162 | + |
| 163 | + // A tree map is an easy way to sort the values |
| 164 | + final NavigableMap<Double, Node> sortedPositionMapping |
| 165 | + = new TreeMap<>(positionMapping); |
| 166 | + |
| 167 | + // Now we find the sockets that are to the immediate left and |
| 168 | + // immediate right of the drop point |
| 169 | + |
| 170 | + // These can be null |
| 171 | + final Map.Entry<Double, Node> |
| 172 | + lowerEntry = sortedPositionMapping.floorEntry(x), |
| 173 | + higherEntry = sortedPositionMapping.ceilingEntry(x); |
| 174 | + // These can be null |
| 175 | + final StepController |
| 176 | + lowerStepController = |
| 177 | + lowerEntry == null ? |
| 178 | + null : stepsMapManager.getWithNode(lowerEntry.getValue()); |
| 179 | + final StepController |
| 180 | + higherStepController = |
| 181 | + higherEntry == null ? |
| 182 | + null : stepsMapManager.getWithNode(higherEntry.getValue()); |
| 183 | + return new StepPair( |
| 184 | + lowerStepController == null ? null : lowerStepController.getStep(), |
| 185 | + higherStepController == null ? null : higherStepController.getStep() |
| 186 | + ); |
| 187 | + } |
| 188 | + |
150 | 189 | private double calculateMiddleXPosOfNodeInParent(Node node) { |
151 | 190 | return node.getLayoutX() + (node.getBoundsInParent().getWidth() / 2.); |
152 | 191 | } |
|
0 commit comments