Skip to content

Commit

Permalink
implemented snake sort
Browse files Browse the repository at this point in the history
  • Loading branch information
teddywilson committed Jul 11, 2017
1 parent cf47317 commit d6519b7
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
import com.willowtreeapps.spruce.sort.LinearSort;
import com.willowtreeapps.spruce.sort.RadialSort;
import com.willowtreeapps.spruce.sort.RandomSort;
import com.willowtreeapps.spruce.sort.SnakeSort;
import com.willowtreeapps.spruce.sort.SortFunction;
import com.willowtreeapps.spurceexampleapp.R;
import com.willowtreeapps.spurceexampleapp.SpruceActivity;
Expand All @@ -69,6 +70,7 @@ public class ControlsFragment extends Fragment implements RadioGroupGridLayout.O
private static final int LINEAR_SORT = 5;
private static final int RADIAL_SORT = 6;
private static final int RANDOM_SORT = 7;
private static final int SNAKE_SORT = 8;

private Animator spruceAnimator;
private SeekBar seekBar;
Expand Down Expand Up @@ -139,6 +141,7 @@ public void onClick(View v) {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
switch (position) {
case SNAKE_SORT:
case CORNERED_SORT:
case INLINE_SORT:
linearRadioGroup.setVisibility(View.GONE);
Expand Down Expand Up @@ -389,6 +392,13 @@ private void setupSort() {
sortFunction = new RandomSort(seekBar.getProgress());
codeSample.setText(String.format(getResources().getString(R.string.random_sort_code), seekBar.getProgress()));
break;
case SNAKE_SORT:
sortFunction = new SnakeSort(seekBar.getProgress(), linearReversed.isChecked(), corner);
codeSample.setText(String.format(getResources().getString(R.string.snake_sort_code),
seekBar.getProgress(),
String.valueOf(linearReversed.isChecked()),
corner));
break;
default:
sortFunction = new DefaultSort(seekBar.getProgress());
codeSample.setText(String.format(getResources().getString(R.string.default_sort_code), seekBar.getProgress()));
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/sort_functions_array.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,6 @@
<item>@string/linear_sort</item>
<item>@string/radial_sort</item>
<item>@string/random_sort</item>
<item>@string/snake_sort</item>
</string-array>
</resources>
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
<string name="linear_sort">Linear Sort</string>
<string name="radial_sort">Radial Sort</string>
<string name="random_sort">Random Sort</string>
<string name="snake_sort">Snake Sort</string>
<!-- Linear -->
<string name="bottom_to_top">Bottom to Top</string>
<string name="top_to_bottom">Top to Bottom</string>
Expand Down Expand Up @@ -82,5 +83,6 @@
<string name="linear_sort_code">new LinearSort(%1$d, %2$s, LinearSort.Direction.%3$s);</string>
<string name="radial_sort_code">new RadialSort(%1$d, %2$s, RadialSort.Position.%3$s);</string>
<string name="random_sort_code">new RandomSort(%1$d);</string>
<string name="snake_sort_code">new SnakeSort(%1$d, %2$s, CorneredSort.Corner.%3$s);</string>
<string name="sort_controls">Sort Controls</string>
</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,4 @@ public int compare(View left, View right) {
return timedViews;
}

}
}
108 changes: 108 additions & 0 deletions lib/src/main/java/com/willowtreeapps/spruce/sort/SnakeSort.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/*
* Spruce
*
* Copyright (c) 2017 WillowTree, Inc.
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
*/

package com.willowtreeapps.spruce.sort;

import android.graphics.PointF;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class SnakeSort extends CorneredSort {

private final long interObjectDelay;
private final boolean reversed;

/**
* Animate child views from side to side (based on the provided corner parameter), alternating left to right and right to left on each row.
*
* @param interObjectDelay long delay between objects
* @param reversed boolean indicating if the selection is reversed
* @param corner {@link com.willowtreeapps.spruce.sort.CorneredSort.Corner Corner} value to start from
*/
public SnakeSort(long interObjectDelay, boolean reversed, Corner corner) {
super(interObjectDelay, reversed, corner);
this.interObjectDelay = interObjectDelay;
this.reversed = reversed;
}

@Override
public List<SpruceTimedView> getViewListWithTimeOffsets(ViewGroup parent, List<View> children) {
final PointF comparisonPoint = getDistancePoint(parent, children);
List<SpruceTimedView> timedViews = new ArrayList<>();
long currentTimeOffset = 0;

// Calculate all possible vertical distances from the point of comparison.
final List<Float> verticalDistances = new ArrayList<>();
for (View child: children) {
float d = Utils.verticalDistance(comparisonPoint, Utils.viewToPoint(child));
if (!verticalDistances.contains(d)) {
verticalDistances.add(d);
}
}

// Sort these so we can find the row index by the vertical distance.
Collections.sort(verticalDistances);

Collections.sort(children, new Comparator<View>() {
@Override
public int compare(View left, View right) {
double leftHorizontalDistance = Utils.horizontalDistance(comparisonPoint, Utils.viewToPoint(left));
double leftVerticalDistance = Utils.verticalDistance(comparisonPoint, Utils.viewToPoint(left));
double rightHorizontalDistance = Utils.horizontalDistance(comparisonPoint, Utils.viewToPoint(right));
double rightVerticalDistance = Utils.verticalDistance(comparisonPoint, Utils.viewToPoint(right));

// Difference in vertical distance takes priority.
if (leftVerticalDistance < rightVerticalDistance) {
return -1;
} else if (leftVerticalDistance > rightVerticalDistance) {
return 1;
}

// If the are in the same row, find the row index.
int row = verticalDistances.indexOf((float) leftVerticalDistance);
if (leftHorizontalDistance < rightHorizontalDistance) {
return row % 2 == 0 ? -1: 1;
} else {
return row % 2 == 0 ? 1: -1;
}
}
});

if (reversed) {
Collections.reverse(children);
}

for (View view : children) {
timedViews.add(new SpruceTimedView(view, currentTimeOffset));
currentTimeOffset += interObjectDelay;
}

return timedViews;
}


}

0 comments on commit d6519b7

Please sign in to comment.