Search in sources :

Example 21 with ConstraintWidget

use of android.support.constraint.solver.widgets.ConstraintWidget in project android by JetBrains.

the class SnapPlacement method checkVerticalParentMarginSnap.

/**
     * Check to snap on the vertical internal margins of a parent (used when resizing)
     *
     * @param anchor    the anchor we are trying to snap
     * @param type      the type of anchor on the parent that we want to check against
     * @param margin    the margin we'll use for the internal margin
     * @param candidate the current candidate that we can fill in
     */
private static void checkVerticalParentMarginSnap(ConstraintAnchor anchor, ConstraintAnchor.Type type, int margin, SnapCandidate candidate) {
    ConstraintWidget widget = anchor.getOwner();
    if (widget.getParent() == null) {
        return;
    }
    ConstraintAnchor targetParent = widget.getParent().getAnchor(type);
    ConstraintHandle targetParentHandle = WidgetInteractionTargets.constraintHandle(targetParent);
    ConstraintHandle anchorHandle = WidgetInteractionTargets.constraintHandle(anchor);
    ConstraintAnchor target = new ConstraintAnchor(widget.getParent(), type);
    int tx = targetParentHandle.getDrawX();
    int ty = targetParentHandle.getDrawY() + margin;
    int distance = Math.abs(anchorHandle.getDrawY() - ty);
    if (distance <= CONNECTION_SLOPE) {
        candidate.distance = distance;
        candidate.target = target;
        candidate.source = anchor;
        candidate.x = tx;
        candidate.y = ty;
    }
}
Also used : ConstraintAnchor(android.support.constraint.solver.widgets.ConstraintAnchor) ConstraintWidget(android.support.constraint.solver.widgets.ConstraintWidget) Point(java.awt.Point)

Example 22 with ConstraintWidget

use of android.support.constraint.solver.widgets.ConstraintWidget in project android by JetBrains.

the class SnapPlacement method gatherMargins.

/**
     * Given a margin and its orientation, gather all similar margins among the widgets.
     * the margins list will be filled with the margins we found.
     *
     * @param widgets    the list of known widgets
     * @param margins    the list of margins we found
     * @param margin     the value of the margin we are looking for
     * @param isVertical the orientation of the margin we are looking for
     */
public static void gatherMargins(Collection<ConstraintWidget> widgets, ArrayList<SnapCandidate> margins, int margin, boolean isVertical) {
    margin = Math.abs(margin);
    if (margin == 0) {
        return;
    }
    // TODO: we should cache the margins found
    ArrayList<SnapCandidate> foundMargins = new ArrayList<>();
    for (ConstraintWidget w1 : widgets) {
        for (ConstraintAnchor a1 : w1.getAnchors()) {
            if (!a1.isSideAnchor()) {
                continue;
            }
            if (a1.isVerticalAnchor() != isVertical) {
                continue;
            }
            for (ConstraintWidget w2 : widgets) {
                for (ConstraintAnchor a2 : w2.getAnchors()) {
                    if (!a2.isSideAnchor()) {
                        continue;
                    }
                    if (!a2.isSimilarDimensionConnection(a1)) {
                        continue;
                    }
                    ConstraintHandle h1 = WidgetInteractionTargets.constraintHandle(a1);
                    ConstraintHandle h2 = WidgetInteractionTargets.constraintHandle(a2);
                    if (h1 == null || h2 == null) {
                        continue;
                    }
                    int currentMargin = h1.getStraightDistanceFrom(h2);
                    if (Math.abs(currentMargin) == margin) {
                        SnapCandidate candidate = new SnapCandidate();
                        candidate.source = a1;
                        candidate.target = a2;
                        candidate.margin = currentMargin;
                        foundMargins.add(candidate);
                    }
                }
            }
        }
    }
    for (SnapCandidate c1 : foundMargins) {
        boolean insert = true;
        for (SnapCandidate c2 : margins) {
            // if we have the opposite margin, don't use it
            if ((Math.abs(c1.margin) == Math.abs(c2.margin)) && ((c2.source == c1.target && c2.target == c1.source) || (c2.source == c1.source && c2.target == c1.target))) {
                insert = false;
                break;
            }
            // if we have margins for the same position, don't use them
            if (c1.source.isSimilarDimensionConnection(c2.source) && c1.margin == c2.margin) {
                ConstraintHandle sourceHandle1 = WidgetInteractionTargets.constraintHandle(c1.source);
                ConstraintHandle targetHandle1 = WidgetInteractionTargets.constraintHandle(c1.target);
                ConstraintHandle sourceHandle2 = WidgetInteractionTargets.constraintHandle(c2.source);
                ConstraintHandle targetHandle2 = WidgetInteractionTargets.constraintHandle(c2.target);
                if (c1.source.isVerticalAnchor()) {
                    if (Math.min(sourceHandle1.getDrawY(), targetHandle1.getDrawY()) == Math.min(sourceHandle2.getDrawY(), targetHandle2.getDrawY())) {
                        insert = false;
                        break;
                    }
                } else if (Math.min(sourceHandle1.getDrawX(), targetHandle1.getDrawX()) == Math.min(sourceHandle2.getDrawX(), targetHandle2.getDrawX())) {
                    insert = false;
                    break;
                }
            }
        }
        if (insert) {
            margins.add(c1);
        }
    }
}
Also used : ConstraintAnchor(android.support.constraint.solver.widgets.ConstraintAnchor) ConstraintWidget(android.support.constraint.solver.widgets.ConstraintWidget) ArrayList(java.util.ArrayList) Point(java.awt.Point)

Example 23 with ConstraintWidget

use of android.support.constraint.solver.widgets.ConstraintWidget in project android by JetBrains.

the class SnapPlacement method snapAnchor.

/**
     * Try to find snapping candidates for the given anchor
     *
     * @param widgets   the list of known widgets
     * @param widget    the widget we operate on
     * @param anchor    the anchor we are trying to snap
     * @param candidate the current candidate
     */
public static void snapAnchor(Collection<ConstraintWidget> widgets, ConstraintWidget widget, ConstraintAnchor anchor, SnapCandidate candidate) {
    if (widget.getParent() != null) {
        if (!anchor.isVerticalAnchor()) {
            checkHorizontalParentMarginSnap(anchor, ConstraintAnchor.Type.RIGHT, -DEFAULT_MARGIN, candidate);
            checkHorizontalParentMarginSnap(anchor, ConstraintAnchor.Type.LEFT, DEFAULT_MARGIN, candidate);
        } else {
            checkVerticalParentMarginSnap(anchor, ConstraintAnchor.Type.BOTTOM, -DEFAULT_MARGIN, candidate);
            checkVerticalParentMarginSnap(anchor, ConstraintAnchor.Type.TOP, DEFAULT_MARGIN, candidate);
        }
    }
    for (ConstraintWidget w : widgets) {
        if (w == widget) {
            continue;
        }
        ArrayList<ConstraintAnchor> anchorsTarget = w.getAnchors();
        for (ConstraintAnchor at : anchorsTarget) {
            snapCheck(anchor, at, candidate, CONNECTION_SLOPE);
        }
    }
}
Also used : ConstraintAnchor(android.support.constraint.solver.widgets.ConstraintAnchor) ConstraintWidget(android.support.constraint.solver.widgets.ConstraintWidget)

Example 24 with ConstraintWidget

use of android.support.constraint.solver.widgets.ConstraintWidget in project android by JetBrains.

the class SnapPlacement method checkHorizontalParentMarginSnap.

/**
     * Check to snap on the horizontal internal margins of a parent (used when resizing)
     *
     * @param anchor    the anchor we are trying to snap
     * @param type      the type of anchor on the parent that we want to check against
     * @param margin    the margin we'll use for the internal margin
     * @param candidate the current candidate that we can fill in
     */
private static void checkHorizontalParentMarginSnap(ConstraintAnchor anchor, ConstraintAnchor.Type type, int margin, SnapCandidate candidate) {
    ConstraintWidget widget = anchor.getOwner();
    if (widget.getParent() == null) {
        return;
    }
    ConstraintAnchor targetParent = widget.getParent().getAnchor(type);
    ConstraintHandle targetParentHandle = WidgetInteractionTargets.constraintHandle(targetParent);
    ConstraintHandle anchorHandle = WidgetInteractionTargets.constraintHandle(anchor);
    ConstraintAnchor target = new ConstraintAnchor(widget.getParent(), type);
    int tx = targetParentHandle.getDrawX() + margin;
    int ty = targetParentHandle.getDrawY();
    int distance = Math.abs(anchorHandle.getDrawX() - tx);
    if (distance <= CONNECTION_SLOPE) {
        candidate.distance = distance;
        candidate.target = target;
        candidate.source = anchor;
        candidate.x = tx;
        candidate.y = ty;
    }
}
Also used : ConstraintAnchor(android.support.constraint.solver.widgets.ConstraintAnchor) ConstraintWidget(android.support.constraint.solver.widgets.ConstraintWidget) Point(java.awt.Point)

Example 25 with ConstraintWidget

use of android.support.constraint.solver.widgets.ConstraintWidget in project android by JetBrains.

the class WidgetDecorator method getConstraintHandle.

/**
     * Utility function to return a ConstraintHandle instance associated with the anchor
     *
     * @param anchor the given anchor
     * @return returns the associated ConstraintHandle
     */
public static ConstraintHandle getConstraintHandle(ConstraintAnchor anchor) {
    ConstraintWidget widget = anchor.getOwner();
    WidgetCompanion companion = (WidgetCompanion) widget.getCompanionWidget();
    WidgetInteractionTargets interactionTargets = companion.getWidgetInteractionTargets();
    return interactionTargets.getConstraintHandle(anchor);
}
Also used : WidgetInteractionTargets(com.android.tools.sherpa.interaction.WidgetInteractionTargets) ConstraintWidget(android.support.constraint.solver.widgets.ConstraintWidget) WidgetCompanion(com.android.tools.sherpa.structure.WidgetCompanion)

Aggregations

ConstraintWidget (android.support.constraint.solver.widgets.ConstraintWidget)37 ConstraintAnchor (android.support.constraint.solver.widgets.ConstraintAnchor)11 Guideline (android.support.constraint.solver.widgets.Guideline)8 Rectangle (java.awt.Rectangle)7 ArrayList (java.util.ArrayList)7 WidgetCompanion (com.android.tools.sherpa.structure.WidgetCompanion)5 Point (java.awt.Point)5 ConstraintWidgetContainer (android.support.constraint.solver.widgets.ConstraintWidgetContainer)3 WidgetContainer (android.support.constraint.solver.widgets.WidgetContainer)3 ConstraintTableLayout (android.support.constraint.solver.widgets.ConstraintTableLayout)2 WidgetDecorator (com.android.tools.sherpa.drawing.decorator.WidgetDecorator)1 WidgetInteractionTargets (com.android.tools.sherpa.interaction.WidgetInteractionTargets)1 Ellipse2D (java.awt.geom.Ellipse2D)1 Arrays (java.util.Arrays)1 Comparator (java.util.Comparator)1 Iterator (java.util.Iterator)1