Skip to content

Commit 01b2a7e

Browse files
committed
Re-updated the code from a rollback to undo accidentally adding generated files. Functionality is complete, comments have been updated (including headers and javadocs) and the code has been reformatted.
1 parent 6e21789 commit 01b2a7e

File tree

1 file changed

+35
-30
lines changed

1 file changed

+35
-30
lines changed

ui/src/main/java/edu/wpi/grip/ui/preview/PreviewsView.java

Lines changed: 35 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
import edu.wpi.grip.core.events.SocketPreviewChangedEvent;
99
import edu.wpi.grip.core.events.StepMovedEvent;
1010
import edu.wpi.grip.ui.pipeline.PipelineView;
11-
import edu.wpi.grip.ui.pipeline.SourceView;
1211
import edu.wpi.grip.ui.pipeline.StepView;
12+
import edu.wpi.grip.ui.pipeline.source.SourceView;
1313
import javafx.application.Platform;
1414
import javafx.fxml.FXML;
1515
import javafx.fxml.FXMLLoader;
@@ -56,9 +56,11 @@ public PreviewsView(EventBus eventBus, PipelineView pipeline) {
5656

5757
this.eventBus.register(this);
5858
}
59+
5960
@Subscribe
6061
/**
61-
* This function is called when a step moves in the pipeline.
62+
* This function is called when a step moves in the pipeline to adjust the positions of any open previews it has
63+
* to reflect the new order of the pipeline.
6264
*/
6365
public synchronized void onPreviewOrderChanged(StepMovedEvent event) {
6466
Platform.runLater(() -> {//Run this function on the main gui thread
@@ -75,7 +77,7 @@ public synchronized void onPreviewOrderChanged(StepMovedEvent event) {
7577
Stack<OutputSocket<?>> previewedMovedSockets = new Stack<OutputSocket<?>>();//This will hold the sockets of the step that was moved that are open for preview
7678

7779
for (OutputSocket<?> i : socketsMovedArray) {
78-
if (this.previewedSockets.indexOf(i)!= -1){//If this socket is previewed
80+
if (this.previewedSockets.indexOf(i) != -1) {//If this socket is previewed
7981
previewedMovedSockets.push(i);
8082
if (rightmostIndex < this.previewedSockets.indexOf(i))
8183
rightmostIndex = this.previewedSockets.indexOf(i);
@@ -85,56 +87,58 @@ public synchronized void onPreviewOrderChanged(StepMovedEvent event) {
8587
}
8688

8789
//Deal with each previewed socket from the step that was moved in turn
88-
while (previewedMovedSockets.size() != 0){ //While there are still sockets to deal with on the stack
90+
while (previewedMovedSockets.size() != 0) { //While there are still sockets to deal with on the stack
8991
OutputSocket<?> current = previewedMovedSockets.pop();//Grab the top socket on the stack
9092
int oldIndex = this.previewedSockets.indexOf(current);//Get the index of this preview so we can remove the correct entry
9193

9294
int newLocation = 0;//This will hold the new index in the list of previewed sockets for this socket
9395

94-
if(distanceMoved<0) //If the step moved left....
96+
if (distanceMoved < 0) //If the step moved left....
9597
newLocation = leftmostIndex + distanceMoved; //Calculate the new index from the leftmost previewed socket of this step
9698
else //The step must have moved right....
9799
newLocation = rightmostIndex + distanceMoved;//So calculate the new index from the rightmost previewed socket of this step
98100

99-
if (newLocation <numberOfSourcePreviews){//If the new calculated index would put it in the midst of source previews
101+
if (newLocation < numberOfSourcePreviews) {//If the new calculated index would put it in the midst of source previews
100102
newLocation = numberOfSourcePreviews;//Make the index the location of the first non-source preview
101-
}else{ //The new index is the current location of another step (NOT a source)
102-
103-
//So we need to make sure that we jump over groups of previews associated with the same step as a unit
103+
} else { //The new index is the current location of another step (NOT a source)
104104

105-
int count = 0;
105+
//So we need to make sure that we jump over GROUPS of previews associated with the SAME step as a unit
106+
int count = 0;//This will hold the number of previews open from the same step in sequence, starting from the new location and going in the direction we are moving
106107

107-
if(distanceMoved<0) {//If the step moved left....
108-
OutputSocket<?> nextSocketInDirection = this.previewedSockets.get(newLocation);
109-
while ((nextSocketInDirection.getStep().isPresent())
110-
&& (nextSocketInDirection.getStep().get() == this.previewedSockets.get(newLocation).getStep().get())){
111-
count++;
112-
nextSocketInDirection = this.previewedSockets.get(newLocation-count);
108+
if (distanceMoved < 0) {//If the step moved left....
109+
OutputSocket<?> nextSocketInDirection = this.previewedSockets.get(newLocation);//Grab the socket whose preview is open at the new location
110+
boolean zeroReached = false;//We will set this to true if we reach the beginning of the list of previews (there are no source previews open)
111+
while ((!zeroReached) &&
112+
((nextSocketInDirection.getStep().isPresent())
113+
&& (nextSocketInDirection.getStep().get() == this.previewedSockets.get(newLocation).getStep().get()))) { //While we haven't reached the beginning of the list of previews, the socket at this location is a socket from a step, and it is the SAME step as the step of the socket at the new location...
114+
count++;
115+
if ((newLocation - count) > 0)//If we haven't reached the beginning of the list of open previews...
116+
nextSocketInDirection = this.previewedSockets.get(newLocation - count);//Grab the next previewed socket to examine in the direction we are moving
117+
else
118+
zeroReached = true;//Mark that we've reached the beginning of the list of previews so we know to stop looking for more
113119
}
114-
newLocation = newLocation - (count-1);
120+
newLocation = newLocation - (count - 1);//Since the first compare of the while loop will always be true, we subract one from the count when we use it to adjust newLocation
115121

116-
}else {//The step must have moved right....
117-
while ((newLocation+count < this.previewedSockets.size())
118-
&& (this.previewedSockets.get(newLocation+count).getStep().get() == this.previewedSockets.get(newLocation).getStep().get())) {
122+
} else {//The step must have moved right....
123+
while ((newLocation + count < this.previewedSockets.size())
124+
&& (this.previewedSockets.get(newLocation + count).getStep().get() == this.previewedSockets.get(newLocation).getStep().get())) { //While there are still previewed sockets to examine, and the socket being examined is one from the SAME step of the socket at the new location....
119125
count++;
120126
}
121-
newLocation = newLocation + (count - 1);
127+
newLocation = newLocation + (count - 1);//Since the first compare of the while loop will always be true, we subract one from the count when we use it to adjust newLocation
122128
}
123-
124129
}
125130

126-
//Remove this socket from the previews
131+
//Remove this socket from the old point in the previews
127132
this.previewedSockets.remove(oldIndex);
128133
this.eventBus.unregister(this.previewBox.getChildren().remove(oldIndex));
129134

130-
if (newLocation > this.previewedSockets.size())//If the new index is too big for the list of previews
135+
if (newLocation > this.previewedSockets.size())//If the new index is now too big for the list of previews
131136
newLocation = this.previewedSockets.size();//Make it so it will be added to the end of the list of previews
132137

133-
this.previewedSockets.add(newLocation, current);//...use this index to add it to the correct location in the list of previews open
138+
this.previewedSockets.add(newLocation, current);//...add it to the correct location in the list of previews open
134139
this.previewBox.getChildren().add(newLocation, SocketPreviewViewFactory.createPreviewView(this.eventBus, current));//...and display it in the correct location in the list of previews open
135140
}
136141
});
137-
138142
}
139143

140144
@Subscribe
@@ -181,7 +185,7 @@ public synchronized void onSocketPreviewChanged(SocketPreviewChangedEvent event)
181185
/**
182186
* Find the correct index in the displayed previews for a socket associated with a source (NOT a step socket)
183187
* by comparing the indices in the pipeline.
184-
* Called in {@link PreviewsView#onSocketPreviewChanged}
188+
* Made to be called in {@link PreviewsView#onSocketPreviewChanged}
185189
*
186190
* @param socket An output socket associated with a source (NOT a step)
187191
* @return The correct index (an int) in the list of displayed previews for the given <code>socket</code>
@@ -206,7 +210,7 @@ private int getIndexInPreviewsOfASourceSocket(OutputSocket<?> socket) {
206210
/**
207211
* Find the correct index in the displayed previews for a socket associated with a step (NOT a source socket)
208212
* by comparing the indices in the pipeline, starting with the first non-source preview displayed.
209-
* Called in {@link PreviewsView#onSocketPreviewChanged}
213+
* Made to be called in {@link PreviewsView#onSocketPreviewChanged}
210214
*
211215
* @param socket An output socket associated with a step (NOT a source)
212216
* @return The correct index in the list of displayed previews for the given <code>socket</code>
@@ -231,10 +235,11 @@ private int getIndexInPreviewsOfAStepSocket(OutputSocket<?> socket) {
231235

232236
/**
233237
* Counts how many source previews (NOT step previews) are currently displayed.
234-
* Called in {@link PreviewsView#getIndexInPreviewsOfAStepSocket}
238+
* Called in {@link PreviewsView#getIndexInPreviewsOfAStepSocket} and {@link PreviewsView#onPreviewOrderChanged(StepMovedEvent)}
235239
*
236240
* @return The number of source (NOT step) previews that are currently displayed
237241
* @see PreviewsView#getIndexInPreviewsOfAStepSocket(OutputSocket)
242+
* @see PreviewsView#onPreviewOrderChanged(StepMovedEvent)
238243
*/
239244
private int getNumbOfSourcePreviews() {
240245
//Start at the beginning of the list.
@@ -245,4 +250,4 @@ private int getNumbOfSourcePreviews() {
245250
}
246251
return numbOfSourcePreviews;
247252
}
248-
}
253+
}

0 commit comments

Comments
 (0)