Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 5cb2c646 authored by Sebastian Franco's avatar Sebastian Franco
Browse files

Simplify attemptPushInDirection

Flag: NA
Bug: 229292911
Test: ReorderAlgorithmUnitTest
Change-Id: I0d8cda2534592a8efce25d254579c4580d645e42
parent 7ce51874
Loading
Loading
Loading
Loading
+30 −80
Original line number Diff line number Diff line
@@ -285,6 +285,11 @@ public class ReorderAlgorithm {
        return foundSolution;
    }

    private void revertDir(int[] direction) {
        direction[0] *= -1;
        direction[1] *= -1;
    }

    // This method tries to find a reordering solution which satisfies the push mechanic by trying
    // to push items in each of the cardinal directions, in an order based on the direction vector
    // passed.
@@ -293,92 +298,37 @@ public class ReorderAlgorithm {
        if ((Math.abs(direction[0]) + Math.abs(direction[1])) > 1) {
            // If the direction vector has two non-zero components, we try pushing
            // separately in each of the components.
            int temp = direction[1];
            direction[1] = 0;

            int temp;
            for (int j = 0; j < 2; j++) {
                for (int i = 1; i >= 0; i--) {
                    temp = direction[i];
                    direction[i] = 0;
                    if (pushViewsToTempLocation(intersectingViews, occupied, direction, ignoreView,
                            solution)) {
                        return true;
                    }
            direction[1] = temp;
            temp = direction[0];
            direction[0] = 0;

            if (pushViewsToTempLocation(intersectingViews, occupied, direction, ignoreView,
                    solution)) {
                return true;
            }
            // Revert the direction
            direction[0] = temp;

            // Now we try pushing in each component of the opposite direction
            direction[0] *= -1;
            direction[1] *= -1;
            temp = direction[1];
            direction[1] = 0;
            if (pushViewsToTempLocation(intersectingViews, occupied, direction, ignoreView,
                    solution)) {
                return true;
                    direction[i] = temp;
                }

            direction[1] = temp;
            temp = direction[0];
            direction[0] = 0;
            if (pushViewsToTempLocation(intersectingViews, occupied, direction, ignoreView,
                    solution)) {
                return true;
                revertDir(direction);
            }
            // revert the direction
            direction[0] = temp;
            direction[0] *= -1;
            direction[1] *= -1;

        } else {
            // If the direction vector has a single non-zero component, we push first in the
            // direction of the vector
            int temp;
            for (int j = 0; j < 2; j++) {
                for (int i = 0; i < 2; i++) {
                    if (pushViewsToTempLocation(intersectingViews, occupied, direction, ignoreView,
                            solution)) {
                        return true;
                    }
            // Then we try the opposite direction
            direction[0] *= -1;
            direction[1] *= -1;
            if (pushViewsToTempLocation(intersectingViews, occupied, direction, ignoreView,
                    solution)) {
                return true;
                    revertDir(direction);
                }
            // Switch the direction back
            direction[0] *= -1;
            direction[1] *= -1;

            // If we have failed to find a push solution with the above, then we try
            // to find a solution by pushing along the perpendicular axis.

                // Swap the components
            int temp = direction[1];
            direction[1] = direction[0];
            direction[0] = temp;
            if (pushViewsToTempLocation(intersectingViews, occupied, direction, ignoreView,
                    solution)) {
                return true;
            }

            // Then we try the opposite direction
            direction[0] *= -1;
            direction[1] *= -1;
            if (pushViewsToTempLocation(intersectingViews, occupied, direction, ignoreView,
                    solution)) {
                return true;
            }
            // Switch the direction back
            direction[0] *= -1;
            direction[1] *= -1;

            // Swap the components back
                temp = direction[1];
                direction[1] = direction[0];
                direction[0] = temp;
            }
        }
        return false;
    }