Ignore:
Timestamp:
03/17/20 10:43:15 (4 years ago)
Author:
bnemhaus
Message:

Reimplementing snapping of SampledTracks and LinkedTracks so that it does not interfere with StandardGestureActions.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/apollo/ApolloGestureActions.java

    r1517 r1523  
    1616import org.expeditee.core.Dimension;
    1717import org.expeditee.core.Point;
     18import org.expeditee.core.bounds.AxisAlignedBoxBounds;
    1819import org.expeditee.gio.EcosystemManager;
    1920import org.expeditee.gio.gesture.Gesture;
     
    203204                                        @Override
    204205                                        public void exec(Gesture gesture) {
    205                                                 mouseMoved(gesture.getData().getPosition());
     206                                                //mouseMoved(gesture.getData().getPosition());
     207                                                if (isSnapOn()) {
     208                                                        snapFreeTracks(gesture.getData().getPosition());
     209                                                }
    206210                                        }
    207211                                });
     
    481485        private static final int COARSE_Y_PLACEMENT_TOLERANCE = 20;
    482486
    483         public void mouseMoved(Point to) {
    484                 boolean forwareToExpeditee = true;
    485 
    486                 if (isYAxisRestictionOn() && !FreeItems.getInstance().isEmpty()) {
    487 
    488                         // Restrict movement of free items to Y-Axis only
    489                         forwareToExpeditee = false;
    490 
    491                         int smallestY = FreeItems.getInstance().get(0).getY();
    492 
    493                         for (Item i : FreeItems.getInstance()) {
    494                                 if (i.getY() < smallestY) {
    495                                         smallestY = i.getY();
    496                                 }
    497                         }
    498 
    499                         for (Item i : FreeItems.getInstance()) {
    500                                 i.setY(to.getY() + (i.getY() - smallestY));
    501                         }
    502 
    503                 } else if (isSnapOn() && !FreeItems.getInstance().isEmpty()) {
    504 
    505                         // Couse movement of free items: Restraining left-most pixel in items
    506                         // to the closest anchored track widgets x position.
    507 
    508                         Frame currentFrame = DisplayController.getCurrentFrame();
    509                         if (currentFrame != null) {
    510 
    511                                 // Search all anchored track x positions for the current frame
    512                                 List<Widget> widgets = currentFrame.getInteractiveWidgets();
    513 
    514                                 int closestXPosition = -1;
    515                                 int closestYPosition = -1;
    516                                 int closestXDistance = -1;
    517                                 int closestYDistance = -1;
    518 
    519                                 for (Widget iw : widgets) {
    520 
    521                                         if (iw instanceof SampledTrack || iw instanceof LinkedTrack) {
    522 
    523                                                 // Determine TOP-LEFT Snapping
    524                                                 int xDistance = Math.abs(to.getX() - iw.getX());
    525                                                 if (closestXDistance < 0 || xDistance < closestXDistance) {
    526                                                         closestXDistance = xDistance;
    527                                                         closestXPosition = iw.getX();
    528                                                 }
    529 
    530                                                 int yDistance = Math.abs(to.getY() - iw.getY());
    531                                                 if (closestYDistance < 0 || yDistance < closestYDistance) {
    532                                                         closestYDistance = yDistance;
    533                                                         closestYPosition = iw.getY();
    534                                                 }
    535 
    536                                                 // Determine BOTTOM-RIGHT Snapping
    537                                                 xDistance = Math.abs(to.getX() - (iw.getX() + iw.getWidth()));
    538                                                 if (closestXDistance < 0 || xDistance < closestXDistance) {
    539                                                         closestXDistance = xDistance;
    540                                                         closestXPosition = iw.getX() + iw.getWidth();
    541                                                 }
    542 
    543                                                 yDistance = Math.abs(to.getY() - (iw.getY() + iw.getHeight()));
    544                                                 if (closestYDistance < 0 || yDistance < closestYDistance) {
    545                                                         closestYDistance = yDistance;
    546                                                         closestYPosition = iw.getY() + iw.getHeight();
    547                                                 }
    548                                         }
    549 
    550                                 }
    551 
    552                                 // Determine top-left position of free items
    553                                 int smallestX = FreeItems.getInstance().get(0).getX();
    554                                 int smallestY = FreeItems.getInstance().get(0).getY();
    555                                 for (Item i : FreeItems.getInstance()) {
    556                                         if (i.getX() < smallestX) {
    557                                                 smallestX = i.getX();
    558                                         }
    559                                         if (i.getY() < smallestY) {
    560                                                 smallestY = i.getY();
    561                                         }
    562                                 }
    563 
    564                                 for (Item i : FreeItems.getInstance()) {
    565 
    566                                         int x;
    567                                         int y;
    568 
    569                                         if (closestXDistance > 0 && closestXDistance < COARSE_X_PLACEMENT_TOLERANCE) {
    570                                                 x = closestXPosition + (i.getX() - smallestX);
    571                                         } else {
    572                                                 x = to.getX() + (i.getX() - smallestX);
    573                                         }
    574 
    575                                         if (closestYDistance > 0 && closestYDistance < COARSE_Y_PLACEMENT_TOLERANCE) {
    576                                                 y = closestYPosition + (i.getY() - smallestY);
    577                                         } else {
    578                                                 y = to.getY() + (i.getY() - smallestY);
    579                                         }
    580 
    581                                         i.setPosition(x, y);
    582 
    583                                 }
    584 
    585                                 forwareToExpeditee = false;
    586 
    587                         }
    588 
     487        private void snapFreeTracks(Point position) {
     488                // Get Apollo widgets that are attached to cursor
     489                FreeItems freeItems = FreeItems.getInstance();
     490                List<Widget> freeWidgets = ItemUtils.extractWidgets(freeItems);
     491                List<Widget> freeApolloWidgets = new ArrayList<Widget>();
     492                for (Widget w: freeWidgets) {
     493                        if (w instanceof SampledTrack || w instanceof LinkedTrack) { freeApolloWidgets.add(w); }
     494                }
     495                if (freeApolloWidgets.isEmpty()) { return; }
     496               
     497                // Get Apollo widgets that are on frame
     498                Frame currentFrame = DisplayController.getCurrentFrame();
     499                List<Widget> interactiveWidgets = currentFrame.getInteractiveWidgets();
     500                List<Widget> anchoredApolloWidgets = new ArrayList<Widget>();
     501                for (Widget w: interactiveWidgets) {
     502                        if (w instanceof SampledTrack || w instanceof LinkedTrack) { anchoredApolloWidgets.add(w); }
    589503                }
    590 
    591                 // Expeditees frame mouse actions uses an offset and fights over free-item
    592                 // movement if it listens to the mouse event router... therefore add an extra
    593                 // layer to avoid this... otherwise auto-aligned items jitter like crazy while
    594                 // moving the cursus
    595                 // if (forwareToExpeditee) {
    596                 // FrameMouseActions.getInstance().mouseMoved(e);
    597                 // } else {
    598                 // FrameGraphics.refresh(true);
    599                 // }
    600         }
    601 
     504                if (anchoredApolloWidgets.isEmpty()) { return; }
     505               
     506                // Snap the first freeApolloWidget.
     507                // Snap the freeApolloWidgets, using the first as a guidepost.
     508                Widget toSnap = freeApolloWidgets.remove(0);
     509                Point origin = new Point(0,0);
     510                Point canditateDistance = new Point(Integer.MAX_VALUE, Integer.MAX_VALUE);
     511                Widget anchoredWidgetToSnapTo = null;
     512                AxisAlignedBoxBounds freeWidgetBounds = toSnap.getBounds();
     513                for (Widget anchoredWidget: anchoredApolloWidgets) {
     514                        AxisAlignedBoxBounds anchoredWidgetBounds = anchoredWidget.getBounds();
     515                        Point distance = freeWidgetBounds.distanceFrom(anchoredWidgetBounds);
     516                        if (distance.getDistanceTo(origin) < canditateDistance.getDistanceTo(origin)) {
     517                                canditateDistance = distance;
     518                                anchoredWidgetToSnapTo = anchoredWidget;
     519                        }
     520                }
     521                AxisAlignedBoxBounds anchoredWidgetToSnapToBounds = anchoredWidgetToSnapTo.getBounds();
     522               
     523                int xDist = anchoredWidgetToSnapToBounds.getCentreX() < freeWidgetBounds.getCentreX() ? canditateDistance.getX() : -canditateDistance.getX();
     524                int yDist = anchoredWidgetToSnapToBounds.getCentreY() < freeWidgetBounds.getCentreY() ? canditateDistance.getY() : -canditateDistance.getY();
     525
     526                int direction = -1;
     527                int tlX = toSnap.getX();
     528                int tlY = toSnap.getY();
     529                if ((xDist > 0 && yDist == 0) || (xDist == 0 && yDist == 0)) {
     530                        tlX = anchoredWidgetToSnapTo.getBounds().getMaxX();
     531                        tlY = anchoredWidgetToSnapTo.getBounds().getMinY();
     532                        direction = 0;
     533                } else if (xDist < 0 && yDist == 0) {
     534                        tlX = anchoredWidgetToSnapTo.getBounds().getMinX() - toSnap.getWidth();
     535                        tlY = anchoredWidgetToSnapTo.getBounds().getMinY();
     536                        direction = 1;
     537                } else if (yDist > 0 && xDist == 0) {
     538                        tlX = anchoredWidgetToSnapTo.getBounds().getMinX();
     539                        tlY = anchoredWidgetToSnapTo.getBounds().getMaxY();
     540                        direction = 2;
     541                } else if (yDist < 0 && xDist == 0) {
     542                        tlX = anchoredWidgetToSnapTo.getBounds().getMinX();
     543                        tlY = anchoredWidgetToSnapTo.getBounds().getMinY() - toSnap.getHeight();
     544                        direction = 3;
     545                }
     546                toSnap.setPosition(tlX, tlY);
     547               
     548                for (Widget toSnapNext: freeApolloWidgets) {
     549                        switch (direction) {
     550                        case 0:
     551                                tlX = toSnap.getBounds().getMaxX();
     552                                tlY = toSnap.getBounds().getMinY();
     553                                toSnapNext.setPosition(tlX, tlY);
     554                                break;
     555                        case 1:
     556                                tlX = toSnap.getBounds().getMinX() - toSnapNext.getWidth();
     557                                tlY = toSnap.getBounds().getMinY();
     558                                toSnapNext.setPosition(tlX, tlY);
     559                                break;
     560                        case 2:
     561                                tlX = toSnap.getBounds().getMinX();
     562                                tlY = toSnap.getBounds().getMaxX();
     563                                toSnapNext.setPosition(tlX, tlY);
     564                                break;
     565                        case 3:
     566                                tlX = toSnap.getBounds().getMinX();
     567                                tlY = toSnap.getBounds().getMinY() - toSnapNext.getHeight();
     568                                toSnapNext.setPosition(tlX, tlY);
     569                                break;
     570                        default:
     571                                break;
     572                        }
     573                        toSnap = toSnapNext;
     574                }
     575        }
     576       
    602577        /**
    603578         * @return True if the user is restricting movement on the y-axis only
Note: See TracChangeset for help on using the changeset viewer.