Search in sources :

Example 1 with ConstraintHandle

use of com.android.tools.sherpa.interaction.ConstraintHandle 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)

Example 2 with ConstraintHandle

use of com.android.tools.sherpa.interaction.ConstraintHandle 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 3 with ConstraintHandle

use of com.android.tools.sherpa.interaction.ConstraintHandle in project android by JetBrains.

the class SceneDraw method animateInCandidateAnchors.

/**
     * Utility function. Given the currently selected anchor, gather all the
     * potential anchors that we could connect to and start an AnimatedCircle animation
     * where they are, to indicate them to the user.
     */
private void animateInCandidateAnchors(ConstraintAnchor selectedAnchor) {
    if (selectedAnchor == null) {
        return;
    }
    mAnimationCandidateAnchors.clear();
    for (ConstraintWidget widget : mWidgetsScene.getWidgets()) {
        boolean highlighted = false;
        for (ConstraintAnchor a : widget.getAnchors()) {
            if (selectedAnchor.isValidConnection(a) && selectedAnchor.isConnectionAllowed(a.getOwner(), a)) {
                ConstraintHandle constraintHandle = WidgetInteractionTargets.constraintHandle(a);
                if (constraintHandle == null) {
                    continue;
                }
                mAnimationCandidateAnchors.add(new AnimatedLine(constraintHandle));
                highlighted = true;
            }
        }
        WidgetCompanion companion = (WidgetCompanion) widget.getCompanionWidget();
        WidgetDecorator decorator = companion.getWidgetDecorator(getCurrentStyle());
        if (decorator.getLook() == ColorTheme.Look.NORMAL) {
            if (highlighted) {
                decorator.setLook(ColorTheme.Look.HIGHLIGHTED);
            } else {
                decorator.setLook(ColorTheme.Look.SUBDUED);
            }
        }
    }
    mChoreographer.addAnimation(mAnimationCandidateAnchors);
}
Also used : AnimatedLine(com.android.tools.sherpa.animation.AnimatedLine) ConstraintHandle(com.android.tools.sherpa.interaction.ConstraintHandle) WidgetCompanion(com.android.tools.sherpa.structure.WidgetCompanion) WidgetDecorator(com.android.tools.sherpa.drawing.decorator.WidgetDecorator)

Example 4 with ConstraintHandle

use of com.android.tools.sherpa.interaction.ConstraintHandle in project android by JetBrains.

the class AnimatedConnection method onPaint.

@Override
public void onPaint(ViewTransform transform, Graphics2D g) {
    double progress = getProgress();
    int alpha = getPulsatingAlpha(progress);
    Color highlight = new Color(mColor.getRed(), mColor.getGreen(), mColor.getBlue(), alpha);
    g.setColor(highlight);
    ConstraintHandle sourceHandle = WidgetInteractionTargets.constraintHandle(mAnchor);
    ConstraintHandle targetHandle = WidgetInteractionTargets.constraintHandle(mAnchor.getTarget());
    if (sourceHandle != null && targetHandle != null && progress >= 0.05) {
        float p = (float) (progress * 2);
        if (p > 1) {
            p = 1;
        }
        sourceHandle.drawConnection(transform, g, mColorSet, true, false, mOriginalCreator, p);
    }
}
Also used : ConstraintHandle(com.android.tools.sherpa.interaction.ConstraintHandle) Color(java.awt.Color)

Example 5 with ConstraintHandle

use of com.android.tools.sherpa.interaction.ConstraintHandle in project android by JetBrains.

the class SnapDraw method drawSnapVerticalIndicator.

/**
     * Draw a snap indicator for vertical anchors
     *
     * @param transform view transform
     * @param g         Graphics context
     * @param candidate the snap candidate
     */
private static void drawSnapVerticalIndicator(ViewTransform transform, Graphics2D g, SnapCandidate candidate) {
    ConstraintAnchor source = candidate.source;
    ConstraintAnchor target = candidate.target;
    ConstraintHandle sourceHandle = WidgetInteractionTargets.constraintHandle(source);
    ConstraintHandle targetHandle = WidgetInteractionTargets.constraintHandle(target);
    int y = transform.getSwingY(candidate.y);
    if (targetHandle != null) {
        y = transform.getSwingY(targetHandle.getDrawY());
    }
    int x1 = transform.getSwingX(source.getOwner().getDrawX());
    int x2 = transform.getSwingX(source.getOwner().getDrawX() + source.getOwner().getDrawWidth());
    int x3 = transform.getSwingX(target.getOwner().getDrawX());
    int x4 = transform.getSwingX(target.getOwner().getDrawX() + target.getOwner().getDrawWidth());
    int minX = Math.min(x1, x3);
    int maxX = Math.max(x2, x4);
    if (candidate.margin != 0) {
        int y2 = transform.getSwingY(sourceHandle.getDrawY());
        String textMargin = "" + Math.abs(candidate.margin);
        int xS = x2;
        int xT = x4 + OVER_MARGIN / 2;
        int mX = xS + OVER_MARGIN;
        if (x1 < x3) {
            xS = x1;
            xT = x3 - OVER_MARGIN / 2;
            mX = xS - OVER_MARGIN;
        }
        drawSnapVerticalMargin(transform, g, mX, y, y2, textMargin);
        Graphics2D g2 = (Graphics2D) g.create();
        g2.setStroke(sDashedStroke);
        g2.drawLine(xS, y, xT, y);
        g2.dispose();
    } else {
        Graphics2D g2 = (Graphics2D) g.create();
        boolean insideIndicator = (source.getOwner().getParent() == candidate.target.getOwner()) || (candidate.target.getOwner() instanceof Guideline);
        if (insideIndicator) {
            g2.setStroke(sLongDashedStroke);
            g2.drawLine(minX, y, maxX, y);
        } else {
            g2.setStroke(sDashedStroke);
            g2.drawLine(minX - OVER, y, maxX + OVER, y);
        }
        g2.dispose();
    }
}
Also used : ConstraintAnchor(android.support.constraint.solver.widgets.ConstraintAnchor) ConstraintHandle(com.android.tools.sherpa.interaction.ConstraintHandle) Guideline(android.support.constraint.solver.widgets.Guideline) Graphics2D(java.awt.Graphics2D)

Aggregations

ConstraintHandle (com.android.tools.sherpa.interaction.ConstraintHandle)8 ConstraintAnchor (android.support.constraint.solver.widgets.ConstraintAnchor)4 WidgetCompanion (com.android.tools.sherpa.structure.WidgetCompanion)3 Guideline (android.support.constraint.solver.widgets.Guideline)2 WidgetDecorator (com.android.tools.sherpa.drawing.decorator.WidgetDecorator)2 WidgetInteractionTargets (com.android.tools.sherpa.interaction.WidgetInteractionTargets)2 Graphics2D (java.awt.Graphics2D)2 AnimatedHoverAnchor (com.android.tools.sherpa.animation.AnimatedHoverAnchor)1 AnimatedLine (com.android.tools.sherpa.animation.AnimatedLine)1 ResizeHandle (com.android.tools.sherpa.interaction.ResizeHandle)1 SnapCandidate (com.android.tools.sherpa.interaction.SnapCandidate)1 Selection (com.android.tools.sherpa.structure.Selection)1 Color (java.awt.Color)1 ArrayList (java.util.ArrayList)1