Search in sources :

Example 11 with ConstraintWidget

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

the class SnapPlacement method findSnapMargin.

/**
     * Utility function iterating through the widgets, comparing the specified anchors,
     * and finding snapping candidate on the indicated margin.
     * <p/>
     * TODO: iterate only on the closest widget (right now we iterate on all of them
     * and *then* check the distance)
     *
     * @param widgets      the list of known widgets
     * @param widget       the widget we operate on
     * @param candidate    the SnapCandidate instance we need to fill
     * @param isVertical   flag set indicating if we are looking at horizontal or vertical anchors
     * @param sourceAnchor the anchor type we are looking at on the widget
     * @param targetAnchor the anchor type we will compare to
     * @param margin       the margin value we are trying to snap on
     * @param slope        the allowed slope
     */
private static void findSnapMargin(Collection<ConstraintWidget> widgets, ConstraintWidget widget, SnapCandidate candidate, boolean isVertical, ConstraintAnchor.Type sourceAnchor, ConstraintAnchor.Type targetAnchor, int margin, int slope) {
    if (widget instanceof Guideline) {
        return;
    }
    ConstraintAnchor source = widget.getAnchor(sourceAnchor);
    for (ConstraintWidget w : widgets) {
        if (w == widget) {
            continue;
        }
        ConstraintAnchor target = w.getAnchor(targetAnchor);
        if (target == null) {
            // for e.g. guidelines
            continue;
        }
        ConstraintHandle sourceHandle = WidgetInteractionTargets.constraintHandle(source);
        ConstraintHandle targetHandle = WidgetInteractionTargets.constraintHandle(target);
        if (sourceHandle == null || targetHandle == null) {
            continue;
        }
        int anchorDistance = sourceHandle.getDrawX() - targetHandle.getDrawX() - margin;
        if (isVertical) {
            anchorDistance = sourceHandle.getDrawY() - targetHandle.getDrawY() - margin;
        }
        if (anchorDistance < 0) {
            continue;
        }
        int minDistance = sourceHandle.getDistanceFrom(target.getOwner());
        double distance = Math.sqrt(anchorDistance * anchorDistance + minDistance * minDistance);
        if (target.getOwner() instanceof Guideline) {
            distance = Math.sqrt(anchorDistance * anchorDistance);
        }
        if (anchorDistance < slope && distance <= candidate.distance && (ALLOWS_ALL_SNAP_MARGIN || distance < MAX_SNAP_MARGIN_DISTANCE)) {
            if (candidate.target == null || (candidate.margin > margin || (candidate.target.getType() == ConstraintAnchor.Type.CENTER_X || candidate.target.getType() == ConstraintAnchor.Type.CENTER || candidate.target.getType() == ConstraintAnchor.Type.CENTER_Y))) {
                candidate.distance = distance;
                candidate.target = target;
                candidate.source = source;
                candidate.margin = margin;
            }
        }
    }
}
Also used : ConstraintAnchor(android.support.constraint.solver.widgets.ConstraintAnchor) ConstraintWidget(android.support.constraint.solver.widgets.ConstraintWidget) Guideline(android.support.constraint.solver.widgets.Guideline) Point(java.awt.Point)

Example 12 with ConstraintWidget

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

the class SnapPlacement method createSnapCandidate.

/**
     * Internal utility function to create a snap candidate on the fly for a margin.
     *
     * @param widget the widget we operate on
     * @param type   the type of ConstraintAnchor we want to use for the target
     * @param x      the horizontal position for the ConstraintAnchor target
     * @param y      the vertical position for the ConstraintAnchor target
     * @return a new instance of SnapCandidate representing the margin
     */
private static SnapCandidate createSnapCandidate(ConstraintWidget widget, ConstraintAnchor.Type type, int x, int y) {
    SnapCandidate candidate = new SnapCandidate();
    candidate.source = widget.getAnchor(type);
    ConstraintWidget owner = widget.getParent();
    ConstraintAnchor anchor = new ConstraintAnchor(owner, type);
    candidate.x = x;
    candidate.y = y;
    candidate.target = anchor;
    return candidate;
}
Also used : ConstraintAnchor(android.support.constraint.solver.widgets.ConstraintAnchor) ConstraintWidget(android.support.constraint.solver.widgets.ConstraintWidget)

Example 13 with ConstraintWidget

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

the class Scout method inferTableList.

/**
     * Recursive decent of widget tree inferring constraints on ConstraintWidgetContainer
     *
     * @param base
     */
private static ConstraintWidget[] inferTableList(WidgetContainer base) {
    if (base instanceof ConstraintWidgetContainer && ((ConstraintWidgetContainer) base).handlesInternalConstraints()) {
        return null;
    }
    for (ConstraintWidget constraintWidget : base.getChildren()) {
        if (constraintWidget instanceof ConstraintWidgetContainer) {
            inferConstraints((ConstraintWidgetContainer) constraintWidget);
        }
    }
    ArrayList<ConstraintWidget> list = new ArrayList<>(base.getChildren());
    list.add(0, base);
    ConstraintWidget[] widgets = list.toArray(new ConstraintWidget[list.size()]);
    ConstraintWidget[] iw = ScoutGroupInference.computeGroups(ScoutWidget.create(widgets));
    if (iw != null && iw.length > 0) {
        return iw;
    }
    return null;
}
Also used : ConstraintWidget(android.support.constraint.solver.widgets.ConstraintWidget) ConstraintWidgetContainer(android.support.constraint.solver.widgets.ConstraintWidgetContainer) ArrayList(java.util.ArrayList)

Example 14 with ConstraintWidget

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

the class Scout method inferConstraints.

/**
     * Recursive decent of widget tree inferring constraints on ConstraintWidgetContainer
     *
     * @param base
     */
private static void inferConstraints(WidgetContainer base) {
    if (base == null) {
        return;
    }
    if (base instanceof ConstraintWidgetContainer && ((ConstraintWidgetContainer) base).handlesInternalConstraints()) {
        return;
    }
    int preX = base.getX();
    int preY = base.getY();
    base.setX(0);
    base.setY(0);
    for (ConstraintWidget constraintWidget : base.getChildren()) {
        if (constraintWidget instanceof ConstraintWidgetContainer) {
            ConstraintWidgetContainer container = (ConstraintWidgetContainer) constraintWidget;
            if (!container.getChildren().isEmpty()) {
                inferConstraints(container);
            }
        }
    }
    ArrayList<ConstraintWidget> list = new ArrayList<>(base.getChildren());
    list.add(0, base);
    ConstraintWidget[] widgets = list.toArray(new ConstraintWidget[list.size()]);
    ScoutWidget.computeConstraints(ScoutWidget.create(widgets));
    base.setX(preX);
    base.setY(preY);
}
Also used : ConstraintWidget(android.support.constraint.solver.widgets.ConstraintWidget) ConstraintWidgetContainer(android.support.constraint.solver.widgets.ConstraintWidgetContainer) ArrayList(java.util.ArrayList)

Example 15 with ConstraintWidget

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

the class ScoutArrange method reverse.

/**
     * in place Reverses the order of the widgets
     *
     * @param widgets to reverse
     */
private static void reverse(ConstraintWidget[] widgets) {
    for (int i = 0; i < widgets.length / 2; i++) {
        ConstraintWidget widget = widgets[i];
        widgets[i] = widgets[widgets.length - 1 - i];
        widgets[widgets.length - 1 - i] = widget;
    }
}
Also used : ConstraintWidget(android.support.constraint.solver.widgets.ConstraintWidget)

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