Search in sources :

Example 1 with ConstraintAnchor

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

the class ScoutWidget method setConstraint.

/**
     * set a constraint if possible return true if it did
     *
     * @param dir  the direction of the connection
     * @param to   the widget to connect to
     * @param cDir the direction of
     * @param gap
     * @return false if unable to apply
     */
boolean setConstraint(int dir, ScoutWidget to, int cDir, float gap) {
    ConstraintAnchor.Type anchorType = lookupType(dir);
    if (to.isGuideline()) {
        cDir &= 0x2;
    }
    ConstraintAnchor anchor = mConstraintWidget.getAnchor(anchorType);
    if (mKeepExistingConnections) {
        if (anchor.isConnected()) {
            if (anchor.getTarget().getOwner() != to.mConstraintWidget) {
                return false;
            }
            return true;
        }
        if (dir == Direction.BASE.getDirection()) {
            if (mConstraintWidget.getAnchor(ConstraintAnchor.Type.BOTTOM).isConnected()) {
                return false;
            }
            if (mConstraintWidget.getAnchor(ConstraintAnchor.Type.TOP).isConnected()) {
                return false;
            }
        } else if (dir == Direction.NORTH.getDirection()) {
            if (mConstraintWidget.getAnchor(ConstraintAnchor.Type.BOTTOM).isConnected()) {
                return false;
            }
            if (mConstraintWidget.getAnchor(ConstraintAnchor.Type.BASELINE).isConnected()) {
                return false;
            }
        } else if (dir == Direction.SOUTH.getDirection()) {
            if (mConstraintWidget.getAnchor(ConstraintAnchor.Type.TOP).isConnected()) {
                return false;
            }
            if (mConstraintWidget.getAnchor(ConstraintAnchor.Type.BASELINE).isConnected()) {
                return false;
            }
        } else if (dir == Direction.WEST.getDirection()) {
            if (mConstraintWidget.getAnchor(ConstraintAnchor.Type.RIGHT).isConnected()) {
                return false;
            }
        } else if (dir == Direction.EAST.getDirection()) {
            if (mConstraintWidget.getAnchor(ConstraintAnchor.Type.LEFT).isConnected()) {
                return false;
            }
        }
    }
    if (anchor.isConnectionAllowed(to.mConstraintWidget)) {
        connect(mConstraintWidget, lookupType(dir), to.mConstraintWidget, lookupType(cDir), (int) gap);
        return true;
    } else {
        return false;
    }
}
Also used : ConstraintAnchor(android.support.constraint.solver.widgets.ConstraintAnchor)

Example 2 with ConstraintAnchor

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

the class ScoutWidget method recursiveConnectedDistanceToRoot.

/**
     * Walk the widget connections to get the distance to the container in a direction
     *
     * @param list      list of widgets (container is list[0]
     * @param direction direction to check in
     * @return distance root or NaN if no connection available
     */
private float recursiveConnectedDistanceToRoot(ScoutWidget[] list, Direction direction) {
    if (isDistanceToRootCache(direction)) {
        return mDistToRootCache[direction.getDirection()];
    }
    ConstraintAnchor.Type anchorType = lookupType(direction);
    ConstraintAnchor anchor = mConstraintWidget.getAnchor(anchorType);
    if (anchor == null || !anchor.isConnected()) {
        return Float.NaN;
    }
    float margin = anchor.getMargin();
    ConstraintAnchor toAnchor = anchor.getTarget();
    ConstraintWidget toWidget = toAnchor.getOwner();
    if (list[0].mConstraintWidget == toWidget) {
        // found the base return;
        return margin;
    }
    // if atached to the same side
    if (toAnchor.getType() == anchorType) {
        for (ScoutWidget scoutWidget : list) {
            if (scoutWidget.mConstraintWidget == toWidget) {
                float dist = scoutWidget.recursiveConnectedDistanceToRoot(list, direction);
                scoutWidget.cacheRootDistance(direction, dist);
                return margin + dist;
            }
        }
    }
    // if atached to the other side (you will need to add the length of the widget
    if (toAnchor.getType() == lookupType(direction.getOpposite())) {
        for (ScoutWidget scoutWidget : list) {
            if (scoutWidget.mConstraintWidget == toWidget) {
                margin += scoutWidget.getLength(direction);
                float dist = scoutWidget.recursiveConnectedDistanceToRoot(list, direction);
                scoutWidget.cacheRootDistance(direction, dist);
                return margin + dist;
            }
        }
    }
    return Float.NaN;
}
Also used : ConstraintAnchor(android.support.constraint.solver.widgets.ConstraintAnchor) ConstraintWidget(android.support.constraint.solver.widgets.ConstraintWidget)

Example 3 with ConstraintAnchor

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

the class ScoutWidget method setWeakConstraint.

/**
     * set a Weak constraint if possible return true if it did
     *
     * @param dir  the direction of the connection
     * @param to   the widget to connect to
     * @param cDir the direction of
     * @return false if unable to apply
     */
boolean setWeakConstraint(int dir, ScoutWidget to, int cDir) {
    ConstraintAnchor anchor = mConstraintWidget.getAnchor(lookupType(dir));
    float gap = 8f;
    if (mKeepExistingConnections && anchor.isConnected()) {
        if (anchor.getTarget().getOwner() != to.mConstraintWidget) {
            return false;
        }
        return true;
    }
    if (anchor.isConnectionAllowed(to.mConstraintWidget)) {
        if (DEBUG) {
            System.out.println("WEAK CONSTRAINT " + mConstraintWidget + " to " + to.mConstraintWidget);
        }
        connectWeak(mConstraintWidget, lookupType(dir), to.mConstraintWidget, lookupType(cDir), (int) gap);
        return true;
    } else {
        return false;
    }
}
Also used : ConstraintAnchor(android.support.constraint.solver.widgets.ConstraintAnchor)

Example 4 with ConstraintAnchor

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

the class ScoutWidget method setCentered.

/**
     * set a centered constraint if possible return true if it did
     *
     * @param dir   direction 0 = vertical
     * @param to1   first widget  to connect to
     * @param to2   second widget to connect to
     * @param cDir1 the side of first widget to connect to
     * @param cDir2 the sed of the second widget to connect to
     * @param gap   the gap
     * @return true if it was able to connect
     */
boolean setCentered(int dir, ScoutWidget to1, ScoutWidget to2, Direction cDir1, Direction cDir2, float gap) {
    Direction ori = (dir == 0) ? Direction.NORTH : Direction.WEST;
    ConstraintAnchor anchor1 = mConstraintWidget.getAnchor(lookupType(ori));
    ConstraintAnchor anchor2 = mConstraintWidget.getAnchor(lookupType(ori.getOpposite()));
    if (mKeepExistingConnections && (anchor1.isConnected() || anchor2.isConnected())) {
        if (anchor1.isConnected() ^ anchor2.isConnected()) {
            return false;
        }
        if (anchor1.isConnected() && (anchor1.getTarget().getOwner() != to1.mConstraintWidget)) {
            return false;
        }
        if (anchor2.isConnected() && (anchor2.getTarget().getOwner() != to2.mConstraintWidget)) {
            return false;
        }
    }
    if (anchor1.isConnectionAllowed(to1.mConstraintWidget) && anchor2.isConnectionAllowed(to2.mConstraintWidget)) {
        // Resize
        if (!isResizable(dir)) {
            if (dir == 0) {
                int height = mConstraintWidget.getHeight();
                float stretchRatio = (gap * 2) / (float) height;
                if (isCandidateResizable(dir) && stretchRatio < MAXIMUM_STRETCH_GAP) {
                    mConstraintWidget.setVerticalDimensionBehaviour(ConstraintWidget.DimensionBehaviour.MATCH_CONSTRAINT);
                } else {
                    gap = 0;
                }
            } else {
                int width = mConstraintWidget.getWidth();
                float stretchRatio = (gap * 2) / (float) width;
                if (isCandidateResizable(dir) && stretchRatio < MAXIMUM_STRETCH_GAP) {
                    mConstraintWidget.setHorizontalDimensionBehaviour(ConstraintWidget.DimensionBehaviour.MATCH_CONSTRAINT);
                } else {
                    gap = 0;
                }
            }
        }
        if (to1.equals(to2)) {
            connect(mConstraintWidget, lookupType(cDir1), to1.mConstraintWidget, lookupType(cDir1), (int) gap);
            connect(mConstraintWidget, lookupType(cDir2), to2.mConstraintWidget, lookupType(cDir2), (int) gap);
        } else {
            float pos1 = to1.getLocation(cDir1);
            float pos2 = to2.getLocation(cDir2);
            Direction c1 = (pos1 < pos2) ? (ori) : (ori.getOpposite());
            Direction c2 = (pos1 > pos2) ? (ori) : (ori.getOpposite());
            int gap1 = gap(mConstraintWidget, c1, to1.mConstraintWidget, cDir1);
            int gap2 = gap(mConstraintWidget, c2, to2.mConstraintWidget, cDir2);
            connect(mConstraintWidget, lookupType(c1), to1.mConstraintWidget, lookupType(cDir1), Math.max(0, gap1));
            connect(mConstraintWidget, lookupType(c2), to2.mConstraintWidget, lookupType(cDir2), Math.max(0, gap2));
        }
        return true;
    } else {
        return false;
    }
}
Also used : ConstraintAnchor(android.support.constraint.solver.widgets.ConstraintAnchor)

Example 5 with ConstraintAnchor

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

the class WidgetDecorator method onPaintConstraints.

/**
     * Paint the constraints of this widget
     *
     * @param transform the view transform
     * @param g         the graphics context
     */
public void onPaintConstraints(ViewTransform transform, Graphics2D g) {
    if (mColorSet == null) {
        return;
    }
    if (mWidget.getVisibility() == ConstraintWidget.GONE) {
        return;
    }
    g.setColor(mConstraintsColor.getColor());
    if (mIsSelected || isShowAllConstraints() || getLook() == ColorTheme.Look.HIGHLIGHTED) {
        if (mWidget.getVisibility() == ConstraintWidget.INVISIBLE) {
            g.setStroke(SnapDraw.sDashedStroke);
        }
        ArrayList<ConstraintAnchor.Type> anchors = new ArrayList<>();
        if (mWidget.hasBaseline() && (mShowBaseline.isDone() || mWidget.getAnchor(ConstraintAnchor.Type.BASELINE).isConnected())) {
            anchors.add(ConstraintAnchor.Type.BASELINE);
        }
        anchors.add(ConstraintAnchor.Type.LEFT);
        anchors.add(ConstraintAnchor.Type.TOP);
        anchors.add(ConstraintAnchor.Type.RIGHT);
        anchors.add(ConstraintAnchor.Type.BOTTOM);
        Color currentColor = g.getColor();
        Stroke currentStroke = g.getStroke();
        for (ConstraintAnchor.Type type : anchors) {
            ConstraintAnchor anchor = mWidget.getAnchor(type);
            if (anchor == null) {
                continue;
            }
            if (anchor.isConnected()) {
                ConstraintAnchor target = anchor.getTarget();
                if (target.getOwner().getVisibility() == ConstraintWidget.GONE) {
                    continue;
                }
                ConstraintHandle startHandle = WidgetInteractionTargets.constraintHandle(anchor);
                if (startHandle.getAnchor().isConnected() && startHandle.getAnchor().getConnectionCreator() == ConstraintAnchor.AUTO_CONSTRAINT_CREATOR) {
                    g.setColor(mColorSet.getSoftConstraintColor());
                    g.setStroke(mColorSet.getSoftConstraintStroke());
                } else {
                    g.setColor(currentColor);
                    g.setStroke(currentStroke);
                }
                boolean painted = false;
                if (startHandle.isLocking()) {
                    float progress = startHandle.getLockingProgress();
                    if (progress > 0) {
                        startHandle.drawConnection(transform, g, mColorSet, mIsSelected, true, startHandle.getAnchor().getConnectionCreator(), progress);
                        painted = true;
                        repaint();
                    }
                }
                if (!painted) {
                    startHandle.drawConnection(transform, g, mColorSet, mIsSelected);
                }
            }
        }
        g.setStroke(SnapDraw.sNormalStroke);
        paintBias(transform, g);
    }
}
Also used : ConstraintAnchor(android.support.constraint.solver.widgets.ConstraintAnchor) ConstraintHandle(com.android.tools.sherpa.interaction.ConstraintHandle) ArrayList(java.util.ArrayList)

Aggregations

ConstraintAnchor (android.support.constraint.solver.widgets.ConstraintAnchor)26 ConstraintWidget (android.support.constraint.solver.widgets.ConstraintWidget)10 Guideline (android.support.constraint.solver.widgets.Guideline)8 Point (java.awt.Point)7 ConstraintHandle (com.android.tools.sherpa.interaction.ConstraintHandle)4 ConstraintWidgetContainer (android.support.constraint.solver.widgets.ConstraintWidgetContainer)3 WidgetCompanion (com.android.tools.sherpa.structure.WidgetCompanion)3 Graphics2D (java.awt.Graphics2D)2 ArrayList (java.util.ArrayList)2 ConstraintTableLayout (android.support.constraint.solver.widgets.ConstraintTableLayout)1 WidgetDecorator (com.android.tools.sherpa.drawing.decorator.WidgetDecorator)1 WidgetInteractionTargets (com.android.tools.sherpa.interaction.WidgetInteractionTargets)1