Search in sources :

Example 6 with ConstraintAnchor

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

the class WidgetDecorator method onPaintAnchors.

/**
     * Paint the anchors of this object
     *
     * @param transform the view transform
     * @param g         the graphics context
     */
public void onPaintAnchors(ViewTransform transform, Graphics2D g) {
    if (mColorSet == null) {
        return;
    }
    if (mWidget.getVisibility() == ConstraintWidget.GONE) {
        return;
    }
    ConstraintAnchor leftAnchor = mWidget.getAnchor(ConstraintAnchor.Type.LEFT);
    ConstraintAnchor rightAnchor = mWidget.getAnchor(ConstraintAnchor.Type.RIGHT);
    ConstraintAnchor topAnchor = mWidget.getAnchor(ConstraintAnchor.Type.TOP);
    ConstraintAnchor bottomAnchor = mWidget.getAnchor(ConstraintAnchor.Type.BOTTOM);
    boolean leftAnchorIsConnected = leftAnchor != null ? leftAnchor.isConnected() : false;
    boolean rightAnchorIsConnected = rightAnchor != null ? rightAnchor.isConnected() : false;
    boolean topAnchorIsConnected = topAnchor != null ? topAnchor.isConnected() : false;
    boolean bottomAnchorIsConnected = bottomAnchor != null ? bottomAnchor.isConnected() : false;
    boolean displayAllAnchors = mDisplayAnchorsPolicy.contains(WidgetDraw.ANCHORS_DISPLAY.ALL);
    boolean showLeftAnchor = displayAllAnchors || mDisplayAnchorsPolicy.contains(WidgetDraw.ANCHORS_DISPLAY.LEFT) || mDisplayAnchorsPolicy.contains(WidgetDraw.ANCHORS_DISPLAY.HORIZONTAL);
    boolean showRightAnchor = displayAllAnchors || mDisplayAnchorsPolicy.contains(WidgetDraw.ANCHORS_DISPLAY.RIGHT) || mDisplayAnchorsPolicy.contains(WidgetDraw.ANCHORS_DISPLAY.HORIZONTAL);
    boolean showTopAnchor = displayAllAnchors || mDisplayAnchorsPolicy.contains(WidgetDraw.ANCHORS_DISPLAY.TOP) || mDisplayAnchorsPolicy.contains(WidgetDraw.ANCHORS_DISPLAY.VERTICAL);
    boolean showBottomAnchor = displayAllAnchors || mDisplayAnchorsPolicy.contains(WidgetDraw.ANCHORS_DISPLAY.BOTTOM) || mDisplayAnchorsPolicy.contains(WidgetDraw.ANCHORS_DISPLAY.VERTICAL);
    if (!mDisplayAnchorsPolicy.contains(WidgetDraw.ANCHORS_DISPLAY.NONE)) {
        showLeftAnchor |= leftAnchorIsConnected;
        showRightAnchor |= rightAnchorIsConnected;
        showTopAnchor |= topAnchorIsConnected;
        showBottomAnchor |= bottomAnchorIsConnected;
    }
    WidgetCompanion widgetCompanion = (WidgetCompanion) mWidget.getCompanionWidget();
    WidgetInteractionTargets interactionTargets = widgetCompanion.getWidgetInteractionTargets();
    // Let's draw all the anchors
    g.setColor(mConstraintsColor.getColor());
    // Draw the baseline first, if needed
    if (mWidget.hasBaseline()) {
        ConstraintAnchor baseline = mWidget.getAnchor(ConstraintAnchor.Type.BASELINE);
        if (baseline.isConnected() || (mIsSelected && mShowResizeHandles)) {
            Color c = g.getColor();
            float progress = 1;
            if (!baseline.isConnected()) {
                progress = mShowBaseline.getProgress();
                if (progress > 0) {
                    int alpha = (int) (255 * progress);
                    g.setColor(new Color(c.getRed(), c.getGreen(), c.getBlue(), alpha));
                }
            }
            if (progress > 0) {
                ConstraintHandle handle = interactionTargets.getConstraintHandle(baseline);
                handle.draw(transform, g, mColorSet, mIsSelected);
            }
            g.setColor(c);
        }
    }
    if (mIsSelected) {
        g.setColor(mColorSet.getSelectedConstraints());
    }
    if (showLeftAnchor) {
        ConstraintHandle handle = interactionTargets.getConstraintHandle(leftAnchor);
        if (handle != null) {
            handle.draw(transform, g, mColorSet, mIsSelected);
        }
    }
    if (showRightAnchor) {
        ConstraintHandle handle = interactionTargets.getConstraintHandle(rightAnchor);
        if (handle != null) {
            handle.draw(transform, g, mColorSet, mIsSelected);
        }
    }
    if (showTopAnchor) {
        ConstraintHandle handle = interactionTargets.getConstraintHandle(topAnchor);
        if (handle != null) {
            handle.draw(transform, g, mColorSet, mIsSelected);
        }
    }
    if (showBottomAnchor) {
        ConstraintHandle handle = interactionTargets.getConstraintHandle(bottomAnchor);
        if (handle != null) {
            handle.draw(transform, g, mColorSet, mIsSelected);
        }
    }
}
Also used : ConstraintAnchor(android.support.constraint.solver.widgets.ConstraintAnchor) WidgetInteractionTargets(com.android.tools.sherpa.interaction.WidgetInteractionTargets) ConstraintHandle(com.android.tools.sherpa.interaction.ConstraintHandle) WidgetCompanion(com.android.tools.sherpa.structure.WidgetCompanion)

Example 7 with ConstraintAnchor

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

the class WidgetDecorator method updateShowAnchorsPolicy.

/**
     * Update the show anchors policy. Used for unselected widgets.
     *
     * @param selectedWidget the current selected widget (if any)
     * @param selectedAnchor the current selected anchor (if any)
     */
public void updateShowAnchorsPolicy(ConstraintWidget selectedWidget, ConstraintAnchor selectedAnchor) {
    mDisplayAnchorsPolicy.clear();
    mDisplayAnchorsPolicy.add(WidgetDraw.ANCHORS_DISPLAY.NONE);
    if (getLook() != ColorTheme.Look.HIGHLIGHTED) {
        return;
    }
    if (isShowAllConstraints()) {
        if (mWidget.getParent() != null) {
            // the constraints already
            if (mWidget.getParent() instanceof ConstraintWidgetContainer) {
                ConstraintWidgetContainer container = (ConstraintWidgetContainer) mWidget.getParent();
                if (!container.handlesInternalConstraints()) {
                    mDisplayAnchorsPolicy.add(WidgetDraw.ANCHORS_DISPLAY.CONNECTED);
                }
            }
        } else {
            mDisplayAnchorsPolicy.add(WidgetDraw.ANCHORS_DISPLAY.CONNECTED);
        }
    }
    if (selectedWidget != null) {
        if (!isShowAllConstraints()) {
            mDisplayAnchorsPolicy.clear();
            mDisplayAnchorsPolicy.add(WidgetDraw.ANCHORS_DISPLAY.NONE);
        }
        ConstraintAnchor left = isConnectedAnchor(selectedWidget, ConstraintAnchor.Type.LEFT, mWidget);
        ConstraintAnchor right = isConnectedAnchor(selectedWidget, ConstraintAnchor.Type.RIGHT, mWidget);
        ConstraintAnchor top = isConnectedAnchor(selectedWidget, ConstraintAnchor.Type.TOP, mWidget);
        ConstraintAnchor bottom = isConnectedAnchor(selectedWidget, ConstraintAnchor.Type.BOTTOM, mWidget);
        ConstraintAnchor baseline = isConnectedAnchor(selectedWidget, ConstraintAnchor.Type.BASELINE, mWidget);
        updateDisplayAnchorSet(mDisplayAnchorsPolicy, left);
        updateDisplayAnchorSet(mDisplayAnchorsPolicy, top);
        updateDisplayAnchorSet(mDisplayAnchorsPolicy, right);
        updateDisplayAnchorSet(mDisplayAnchorsPolicy, bottom);
        updateDisplayAnchorSet(mDisplayAnchorsPolicy, baseline);
    }
    if (selectedAnchor != null) {
        if (selectedAnchor.isConnectionAllowed(mWidget)) {
            if (selectedAnchor.getType() == ConstraintAnchor.Type.BASELINE) {
                mDisplayAnchorsPolicy.add(WidgetDraw.ANCHORS_DISPLAY.BASELINE);
            } else if (selectedAnchor.getType() == ConstraintAnchor.Type.CENTER) {
                mDisplayAnchorsPolicy.add(WidgetDraw.ANCHORS_DISPLAY.VERTICAL);
                mDisplayAnchorsPolicy.add(WidgetDraw.ANCHORS_DISPLAY.HORIZONTAL);
                if (mWidget == selectedAnchor.getOwner().getParent()) {
                    // only display the center anchor for the parent of the selected widget
                    mDisplayAnchorsPolicy.add(WidgetDraw.ANCHORS_DISPLAY.CENTER);
                }
            } else if (selectedAnchor.isVerticalAnchor()) {
                mDisplayAnchorsPolicy.add(WidgetDraw.ANCHORS_DISPLAY.VERTICAL);
            } else {
                mDisplayAnchorsPolicy.add(WidgetDraw.ANCHORS_DISPLAY.HORIZONTAL);
            }
        }
    }
    if (getLook() == ColorTheme.Look.HIGHLIGHTED) {
        mDisplayAnchorsPolicy.clear();
        mDisplayAnchorsPolicy.add(WidgetDraw.ANCHORS_DISPLAY.CONNECTED);
    }
}
Also used : ConstraintAnchor(android.support.constraint.solver.widgets.ConstraintAnchor) ConstraintWidgetContainer(android.support.constraint.solver.widgets.ConstraintWidgetContainer)

Example 8 with ConstraintAnchor

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

the class WidgetInteractionTargets method findClosestConnection.

/**
     * Fill in the ConnectionCandidate structure with the closest anchor found at (x, y).
     * This function will not consider CENTER_X/CENTER_Y anchors.
     *
     * @param viewTransform
     * @param x         x coordinate we are looking at
     * @param y         y coordinate we are looking at
     * @param candidate a structure containing the current best anchor candidate.
     * @param mousePress true if we are in mouse press
     */
public void findClosestConnection(ViewTransform viewTransform, float x, float y, ConnectionCandidate candidate, boolean mousePress) {
    // FIXME: should use subclasses this
    if (mWidget instanceof Guideline) {
        float distance;
        Guideline guideline = (Guideline) mWidget;
        ConstraintAnchor anchor = guideline.getAnchor();
        ConstraintHandle handle = constraintHandle(anchor);
        if (handle == null) {
            return;
        }
        if (guideline.getOrientation() == Guideline.VERTICAL) {
            distance = (handle.getDrawX() - x) * (handle.getDrawX() - x);
        } else {
            distance = (handle.getDrawY() - y) * (handle.getDrawY() - y);
        }
        distance = viewTransform.getSwingDimensionF(distance);
        if (distance < candidate.distance) {
            candidate.anchorTarget = anchor;
            candidate.distance = distance;
        }
    } else if (mWidget instanceof ConstraintWidgetContainer) {
        for (ConstraintHandle handle : mConstraintHandles) {
            ConstraintAnchor anchor = handle.getAnchor();
            if (anchor.getType() == ConstraintAnchor.Type.CENTER_X || anchor.getType() == ConstraintAnchor.Type.CENTER_Y) {
                continue;
            }
            float distance = 0;
            boolean computed = false;
            if (!mousePress && anchor.isSideAnchor()) {
                if (!anchor.isVerticalAnchor()) {
                    if (y >= mWidget.getDrawY() && y <= mWidget.getDrawBottom()) {
                        distance = (handle.getDrawX() - x) * (handle.getDrawX() - x);
                        computed = true;
                    }
                } else {
                    if (x >= mWidget.getDrawX() && x <= mWidget.getDrawRight()) {
                        distance = (handle.getDrawY() - y) * (handle.getDrawY() - y);
                        computed = true;
                    }
                }
            }
            if (!computed) {
                distance = (handle.getDrawX() - x) * (handle.getDrawX() - x) + (handle.getDrawY() - y) * (handle.getDrawY() - y);
            }
            distance = viewTransform.getSwingDimensionF(distance);
            if (distance < candidate.distance) {
                candidate.anchorTarget = anchor;
                candidate.distance = distance;
            }
        }
    } else {
        for (ConstraintHandle handle : mConstraintHandles) {
            ConstraintAnchor anchor = handle.getAnchor();
            float distance = (handle.getDrawX() - x) * (handle.getDrawX() - x) + (handle.getDrawY() - y) * (handle.getDrawY() - y);
            if (anchor.getType() == ConstraintAnchor.Type.CENTER_X || anchor.getType() == ConstraintAnchor.Type.CENTER_Y) {
                continue;
            }
            if (anchor.getType() == ConstraintAnchor.Type.BASELINE) {
                if (!anchor.getOwner().hasBaseline()) {
                    continue;
                }
                ConstraintWidget widget = anchor.getOwner();
                int minX = widget.getDrawX();
                int maxX = widget.getDrawRight();
                float d = Math.abs(handle.getDrawY() - y);
                if (x >= minX && x <= maxX && d < 3) {
                    distance = d * d;
                }
            }
            distance = viewTransform.getSwingDimensionF(distance);
            if (distance <= candidate.distance) {
                if (candidate.anchorTarget == null || candidate.anchorTarget.getPriorityLevel() < anchor.getPriorityLevel()) {
                    candidate.anchorTarget = anchor;
                    candidate.distance = distance;
                }
            }
        }
    }
}
Also used : ConstraintAnchor(android.support.constraint.solver.widgets.ConstraintAnchor) ConstraintWidget(android.support.constraint.solver.widgets.ConstraintWidget) ConstraintWidgetContainer(android.support.constraint.solver.widgets.ConstraintWidgetContainer) Guideline(android.support.constraint.solver.widgets.Guideline)

Example 9 with ConstraintAnchor

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

the class WidgetMotion method snapBias.

/**
     * Snap the widget's horizontal or vertical bias if we have horizontal/vertical
     * centered connections
     *
     * @param widget         the current widget
     * @param candidatePoint the candidate point containing the current location
     */
private void snapBias(ConstraintWidget widget, Point candidatePoint) {
    WidgetCompanion widgetCompanion = (WidgetCompanion) widget.getCompanionWidget();
    int currentStyle = WidgetDecorator.BLUEPRINT_STYLE;
    if (mSceneDraw != null) {
        currentStyle = mSceneDraw.getCurrentStyle();
    }
    WidgetDecorator decorator = widgetCompanion.getWidgetDecorator(currentStyle);
    ConstraintAnchor leftAnchor = widget.getAnchor(ConstraintAnchor.Type.LEFT);
    ConstraintAnchor rightAnchor = widget.getAnchor(ConstraintAnchor.Type.RIGHT);
    if (leftAnchor != null && rightAnchor != null && leftAnchor.isConnected() && rightAnchor.isConnected() && leftAnchor.getTarget() != rightAnchor.getTarget()) {
        int begin = WidgetInteractionTargets.constraintHandle(leftAnchor.getTarget()).getDrawX();
        int end = WidgetInteractionTargets.constraintHandle(rightAnchor.getTarget()).getDrawX();
        int width = widget.getDrawWidth();
        int delta = candidatePoint.x - begin;
        float percent = delta / (float) (end - begin - width);
        percent = Math.max(0, Math.min(1, percent));
        percent = snapPercent(percent);
        widget.setHorizontalBiasPercent(percent);
        decorator.updateBias();
    }
    ConstraintAnchor topAnchor = widget.getAnchor(ConstraintAnchor.Type.TOP);
    ConstraintAnchor bottomAnchor = widget.getAnchor(ConstraintAnchor.Type.BOTTOM);
    if (topAnchor != null && bottomAnchor != null && topAnchor.isConnected() && bottomAnchor.isConnected() && topAnchor.getTarget() != bottomAnchor.getTarget()) {
        int begin = WidgetInteractionTargets.constraintHandle(topAnchor.getTarget()).getDrawY();
        int end = WidgetInteractionTargets.constraintHandle(bottomAnchor.getTarget()).getDrawY();
        int height = widget.getDrawHeight();
        int delta = candidatePoint.y - begin;
        float percent = delta / (float) (end - begin - height);
        percent = Math.max(0, Math.min(1, percent));
        percent = snapPercent(percent);
        widget.setVerticalBiasPercent(percent);
        decorator.updateBias();
    }
}
Also used : ConstraintAnchor(android.support.constraint.solver.widgets.ConstraintAnchor) WidgetCompanion(com.android.tools.sherpa.structure.WidgetCompanion) WidgetDecorator(com.android.tools.sherpa.drawing.decorator.WidgetDecorator)

Example 10 with ConstraintAnchor

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

the class WidgetDecorator method paintBias.

/**
     * Paint the horizontal and vertical informations of this widget, if appropriate
     *
     * @param transform the view transform
     * @param g         the graphics context
     */
private void paintBias(ViewTransform transform, Graphics2D g) {
    ConstraintAnchor left = mWidget.getAnchor(ConstraintAnchor.Type.LEFT);
    ConstraintAnchor right = mWidget.getAnchor(ConstraintAnchor.Type.RIGHT);
    if (left != null && right != null && left.isConnected() && right.isConnected() && left.getConnectionCreator() != ConstraintAnchor.AUTO_CONSTRAINT_CREATOR) {
        if (mShowBias.isRunning()) {
            float progress = 1 - mShowBias.getProgress();
            int percent = (int) (mWidget.getHorizontalBiasPercent() * 100);
            int x = mWidget.getDrawX();
            int y = (int) (mWidget.getDrawY() + mWidget.getDrawHeight() / 2f);
            Color c = mColorSet.getTooltipBackground();
            Color pre = g.getColor();
            int alpha = (int) (progress * 255);
            int tx, ty;
            // Draw left
            x = transform.getSwingFX(x);
            y = transform.getSwingFY(y);
            tx = transform.getSwingFX(getTargetHandle(left).getDrawX());
            g.setColor(new Color(c.getRed(), c.getGreen(), c.getBlue(), alpha));
            Color textColor = new Color(mColorSet.getTooltipText().getRed(), mColorSet.getTooltipText().getGreen(), mColorSet.getTooltipText().getBlue(), alpha);
            ConnectionDraw.drawRoundRectText(g, sInfoFont, textColor, formatPercent(percent, true), (x + tx) / 2, y);
            // Draw right
            tx = transform.getSwingFX(getTargetHandle(right).getDrawX());
            x = mWidget.getDrawRight();
            x = transform.getSwingFX(x);
            ConnectionDraw.drawRoundRectText(g, sInfoFont, textColor, formatPercent(percent, false), (x + tx) / 2, y);
            g.setColor(pre);
        }
    }
    ConstraintAnchor top = mWidget.getAnchor(ConstraintAnchor.Type.TOP);
    ConstraintAnchor bottom = mWidget.getAnchor(ConstraintAnchor.Type.BOTTOM);
    if (top != null && bottom != null && top.isConnected() && bottom.isConnected() && mShowBias.isRunning()) {
        float progress = 1 - mShowBias.getProgress();
        int percent = (int) (mWidget.getVerticalBiasPercent() * 100);
        int y = mWidget.getDrawY();
        int x = (int) (mWidget.getDrawX() + mWidget.getDrawWidth() / 2f);
        Color c = mColorSet.getTooltipBackground();
        Color pre = g.getColor();
        int alpha = (int) (progress * 255);
        int tx, ty;
        // draw top
        ty = transform.getSwingFY(getTargetHandle(top).getDrawY());
        x = transform.getSwingFX(x);
        y = transform.getSwingFY(y);
        g.setColor(new Color(c.getRed(), c.getGreen(), c.getBlue(), alpha));
        Color textColor = new Color(mColorSet.getTooltipText().getRed(), mColorSet.getTooltipText().getGreen(), mColorSet.getTooltipText().getBlue(), alpha);
        ConnectionDraw.drawRoundRectText(g, sInfoFont, textColor, formatPercent(percent, true), x, (y + ty) / 2);
        // draw bottom
        ty = transform.getSwingFY(getTargetHandle(bottom).getDrawY());
        y = mWidget.getDrawBottom();
        y = transform.getSwingFY(y);
        ConnectionDraw.drawRoundRectText(g, sInfoFont, textColor, formatPercent(percent, false), x, (y + ty) / 2);
        g.setColor(pre);
    }
}
Also used : ConstraintAnchor(android.support.constraint.solver.widgets.ConstraintAnchor)

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