Search in sources :

Example 11 with Guideline

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

the class SnapPlacement method snapCheck.

/**
     * Check the possibility of snapping between a source anchor and a target anchor. They
     * will snap if they are close enough (based on the slope value), and if their distance is
     * less than the current SnapCandidate.
     *
     * @param source    the source anchor we are checking
     * @param target    the target anchor we are looking at
     * @param candidate the current snap candidate information
     * @param slope     a value allowing some slope to consider a target to be close enough to the source
     */
private static void snapCheck(ConstraintAnchor source, ConstraintAnchor target, SnapCandidate candidate, int slope) {
    if (!target.isSnapCompatibleWith(source) || target.getOwner() == source.getOwner()) {
        return;
    }
    ConstraintHandle handleSource = WidgetInteractionTargets.constraintHandle(source);
    ConstraintHandle handleTarget = WidgetInteractionTargets.constraintHandle(target);
    int anchorDistance = handleSource.getStraightDistanceFrom(handleTarget);
    int minDistance = handleSource.getDistanceFrom(target.getOwner());
    if (target.getOwner() instanceof Guideline) {
        minDistance = 0;
    }
    double distance = Math.sqrt(anchorDistance * anchorDistance + minDistance * minDistance);
    boolean targetBelongsToParent = source.getOwner().getParent() == target.getOwner();
    if (anchorDistance < slope && distance <= candidate.distance && (targetBelongsToParent || (distance < MAX_CONNECTION_DISTANCE))) {
        // of the window...)
        if (candidate.target != null) {
            boolean currentTargetBelongsToParent = source.getOwner().getParent() == candidate.target.getOwner();
            if (currentTargetBelongsToParent) {
                // we'll prefer to keep the current candidate if it belongs to the parent...
                return;
            }
            if (candidate.distance == distance) {
                // as abiding by the anchor's priority level
                if (!targetBelongsToParent && candidate.target.getSnapPriorityLevel() > target.getSnapPriorityLevel()) {
                    return;
                }
            }
        }
        candidate.distance = distance;
        candidate.target = target;
        candidate.source = source;
    }
}
Also used : Guideline(android.support.constraint.solver.widgets.Guideline) Point(java.awt.Point)

Example 12 with Guideline

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

the class ScoutArrange method getBoundingBox.

/**
     * Get the bounding box around a list of widgets
     *
     * @param widgets
     * @return
     */
static Rectangle getBoundingBox(ArrayList<ConstraintWidget> widgets) {
    Rectangle all = null;
    Rectangle tmp = new Rectangle();
    for (ConstraintWidget widget : widgets) {
        if (widget instanceof Guideline) {
            continue;
        }
        tmp.x = widget.getX();
        tmp.y = widget.getY();
        tmp.width = widget.getWidth();
        tmp.height = widget.getHeight();
        if (all == null) {
            all = new Rectangle(tmp);
        } else {
            all = all.union(tmp);
        }
    }
    return all;
}
Also used : ConstraintWidget(android.support.constraint.solver.widgets.ConstraintWidget) Rectangle(java.awt.Rectangle) Guideline(android.support.constraint.solver.widgets.Guideline)

Example 13 with Guideline

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

the class WidgetResize method resizeWidget.

/**
     * Resize the widget given the current mouse position
     *
     * @param widgets the list of known widgets
     * @param widget the widget we are resizing
     * @param posX   in android coordinate
     * @param posY   in android coordinate
     */
public void resizeWidget(Collection<ConstraintWidget> widgets, ConstraintWidget widget, ResizeHandle handle, Rectangle originalBounds, int posX, int posY) {
    if (widget == null) {
        return;
    }
    Animator.setAnimationEnabled(false);
    mSnapCandidates.clear();
    int x = widget.getDrawX();
    int y = widget.getDrawY();
    int w = widget.getDrawWidth();
    int h = widget.getDrawHeight();
    ResizeHandle.Type resize = handle.getType();
    ConstraintAnchor left = widget.getAnchor(ConstraintAnchor.Type.LEFT);
    ConstraintAnchor top = widget.getAnchor(ConstraintAnchor.Type.TOP);
    ConstraintAnchor right = widget.getAnchor(ConstraintAnchor.Type.RIGHT);
    ConstraintAnchor bottom = widget.getAnchor(ConstraintAnchor.Type.BOTTOM);
    ConstraintAnchor baseline = widget.getAnchor(ConstraintAnchor.Type.BASELINE);
    boolean leftIsConnected = left != null && left.isConnected();
    boolean rightIsConnected = right != null && right.isConnected();
    boolean topIsConnected = top != null && top.isConnected();
    boolean bottomIsConnected = bottom != null && bottom.isConnected();
    boolean baselineIsConnected = baseline != null && baseline.isConnected();
    // Limits range of motion depending on which anchor is connected
    if (leftIsConnected && !rightIsConnected) {
        switch(resize) {
            case LEFT_TOP:
                {
                    resize = ResizeHandle.Type.TOP_SIDE;
                }
                break;
            case LEFT_BOTTOM:
                {
                    resize = ResizeHandle.Type.BOTTOM_SIDE;
                }
                break;
        }
    }
    if (rightIsConnected && !leftIsConnected) {
        switch(resize) {
            case RIGHT_TOP:
                {
                    resize = ResizeHandle.Type.TOP_SIDE;
                }
                break;
            case RIGHT_BOTTOM:
                {
                    resize = ResizeHandle.Type.BOTTOM_SIDE;
                }
                break;
        }
    }
    if ((topIsConnected || baselineIsConnected) && !bottomIsConnected) {
        switch(resize) {
            case LEFT_TOP:
                {
                    resize = ResizeHandle.Type.LEFT_SIDE;
                }
                break;
            case RIGHT_TOP:
                {
                    resize = ResizeHandle.Type.RIGHT_SIDE;
                }
                break;
        }
    }
    if (bottomIsConnected && !topIsConnected) {
        switch(resize) {
            case LEFT_BOTTOM:
                {
                    resize = ResizeHandle.Type.LEFT_SIDE;
                }
                break;
            case RIGHT_BOTTOM:
                {
                    resize = ResizeHandle.Type.RIGHT_SIDE;
                }
                break;
        }
    }
    ConstraintWidget base = widget.getParent();
    switch(resize) {
        case LEFT_TOP:
            {
                int newX = Math.min(originalBounds.x + originalBounds.width - widget.getMinWidth(), posX);
                newX = clampX(newX, base);
                newX = snapLeft(widgets, widget, newX, mSnapCandidates);
                int newY = Math.min(originalBounds.y + originalBounds.height - widget.getMinHeight(), posY);
                newY = clampY(newY, base);
                newY = snapTop(widgets, widget, newY, mSnapCandidates);
                int newWidth = originalBounds.x + originalBounds.width - newX;
                int newHeight = originalBounds.y + originalBounds.height - newY;
                setNewFrame(widget, newX, newY, newWidth, newHeight);
            }
            break;
        case LEFT_BOTTOM:
            {
                int newX = Math.min(originalBounds.x + originalBounds.width - widget.getMinWidth(), posX);
                newX = clampX(newX, base);
                newX = snapLeft(widgets, widget, newX, mSnapCandidates);
                int newWidth = originalBounds.x + originalBounds.width - newX;
                int newHeight = posY - originalBounds.y;
                newHeight = snapHeight(widgets, widget, newHeight, mSnapCandidates);
                setNewFrame(widget, newX, y, newWidth, newHeight);
            }
            break;
        case RIGHT_TOP:
            {
                int newY = Math.min(originalBounds.y + originalBounds.height - widget.getMinHeight(), posY);
                newY = clampY(newY, base);
                newY = snapTop(widgets, widget, newY, mSnapCandidates);
                int newWidth = posX - originalBounds.x;
                newWidth = snapWidth(widgets, widget, newWidth, mSnapCandidates);
                int newHeight = originalBounds.y + originalBounds.height - newY;
                setNewFrame(widget, x, newY, newWidth, newHeight);
            }
            break;
        case RIGHT_BOTTOM:
            {
                int newWidth = posX - originalBounds.x;
                int newHeight = posY - originalBounds.y;
                newWidth = snapWidth(widgets, widget, newWidth, mSnapCandidates);
                newHeight = snapHeight(widgets, widget, newHeight, mSnapCandidates);
                setNewFrame(widget, x, y, newWidth, newHeight);
            }
            break;
        case LEFT_SIDE:
            {
                int newX = Math.min(originalBounds.x + originalBounds.width - widget.getMinWidth(), posX);
                if (widget instanceof Guideline) {
                    newX = posX;
                }
                newX = clampX(newX, base);
                newX = snapLeft(widgets, widget, newX, mSnapCandidates);
                int newWidth = originalBounds.x + originalBounds.width - newX;
                setNewFrame(widget, newX, y, newWidth, h);
            }
            break;
        case RIGHT_SIDE:
            {
                int newWidth = posX - originalBounds.x;
                newWidth = snapWidth(widgets, widget, newWidth, mSnapCandidates);
                setNewFrame(widget, x, y, newWidth, h);
            }
            break;
        case TOP_SIDE:
            {
                int newY = Math.min(originalBounds.y + originalBounds.height - widget.getMinHeight(), posY);
                if (widget instanceof Guideline) {
                    newY = posY;
                }
                newY = clampY(newY, base);
                newY = snapTop(widgets, widget, newY, mSnapCandidates);
                int newHeight = originalBounds.y + originalBounds.height - newY;
                setNewFrame(widget, x, newY, w, newHeight);
            }
            break;
        case BOTTOM_SIDE:
            {
                int newHeight = posY - originalBounds.y;
                newHeight = snapHeight(widgets, widget, newHeight, mSnapCandidates);
                setNewFrame(widget, x, y, w, newHeight);
            }
            break;
    }
}
Also used : ConstraintAnchor(android.support.constraint.solver.widgets.ConstraintAnchor) ConstraintWidget(android.support.constraint.solver.widgets.ConstraintWidget) Guideline(android.support.constraint.solver.widgets.Guideline)

Aggregations

Guideline (android.support.constraint.solver.widgets.Guideline)13 ConstraintAnchor (android.support.constraint.solver.widgets.ConstraintAnchor)8 ConstraintWidget (android.support.constraint.solver.widgets.ConstraintWidget)7 Point (java.awt.Point)3 ConstraintTableLayout (android.support.constraint.solver.widgets.ConstraintTableLayout)2 ConstraintWidgetContainer (android.support.constraint.solver.widgets.ConstraintWidgetContainer)2 ConstraintHandle (com.android.tools.sherpa.interaction.ConstraintHandle)2 Graphics2D (java.awt.Graphics2D)2 WidgetCompanion (com.android.tools.sherpa.structure.WidgetCompanion)1 Rectangle (java.awt.Rectangle)1