Search in sources :

Example 6 with Guideline

use of android.support.constraint.solver.widgets.Guideline 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)

Example 7 with Guideline

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

the class SnapDraw method drawSnapHorizontalIndicator.

/**
     * Draw a snap indicator for horizontal anchors
     *
     * @param transform view transform
     * @param g         Graphics context
     * @param candidate the snap candidate
     */
private static void drawSnapHorizontalIndicator(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 x = transform.getSwingX(candidate.x);
    if (targetHandle != null) {
        x = transform.getSwingX(targetHandle.getDrawX());
    }
    int y1 = transform.getSwingY(source.getOwner().getDrawY());
    int y2 = transform.getSwingY(source.getOwner().getDrawY() + source.getOwner().getHeight());
    int y3 = transform.getSwingY(target.getOwner().getDrawY());
    int y4 = transform.getSwingY(target.getOwner().getDrawY() + target.getOwner().getHeight());
    int minY = Math.min(y1, y3);
    int maxY = Math.max(y2, y4);
    if (candidate.margin != 0) {
        int x2 = transform.getSwingX(sourceHandle.getDrawX());
        String textMargin = String.valueOf(Math.abs(candidate.margin));
        int yS = y2;
        int yT = y4 + OVER_MARGIN / 2;
        int mY = yS + OVER_MARGIN;
        boolean textOver = false;
        if (y1 < y3) {
            yS = y1;
            yT = y3 - OVER_MARGIN / 2;
            mY = yS - OVER_MARGIN;
            textOver = true;
        }
        drawSnapHorizontalMargin(transform, g, x, x2, mY, textMargin, textOver);
        Graphics2D g2 = (Graphics2D) g.create();
        g2.setStroke(sDashedStroke);
        g2.drawLine(x, yS, x, yT);
        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(x, minY, x, maxY);
        } else {
            g2.setStroke(sDashedStroke);
            g2.drawLine(x, minY - OVER, x, maxY + OVER);
        }
        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)

Example 8 with Guideline

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

the class WidgetDraw method drawWidgetFrame.

/**
     * Draw the widget frame (resizable area...) as well as
     * the constraints anchors and their state.
     * The color and style used for the drawing will be the current ones in the graphics context.
     *
     * @param transform         view transform
     * @param g                 Graphics context
     * @param widget            the widget we are drawing
     * @param showAnchors       determinate how to display the Constraints anchors points
     * @param showResizeHandles pass true to show Resize handles
     * @param isSelected        if the widget is currently selected
     */
public static void drawWidgetFrame(ViewTransform transform, Graphics2D g, ConstraintWidget widget, EnumSet<ANCHORS_DISPLAY> showAnchors, boolean showResizeHandles, boolean showSizeIndicator, boolean isSelected) {
    g.setStroke(SnapDraw.sNormalStroke);
    int l = transform.getSwingX(widget.getDrawX());
    int t = transform.getSwingY(widget.getDrawY());
    int w = transform.getSwingDimension(widget.getDrawWidth());
    int h = transform.getSwingDimension(widget.getDrawHeight());
    int r = transform.getSwingX(widget.getDrawX() + widget.getDrawWidth());
    int b = transform.getSwingY(widget.getDrawY() + widget.getDrawHeight());
    int radius = ConnectionDraw.CONNECTION_ANCHOR_SIZE;
    int radiusRect = ConnectionDraw.CONNECTION_RESIZE_SIZE;
    int rectDimension = radiusRect * 2;
    int midX = transform.getSwingX((int) (widget.getDrawX() + widget.getDrawWidth() / 2f));
    int midY = transform.getSwingY((int) (widget.getDrawY() + widget.getDrawHeight() / 2f));
    if (widget.getParent() instanceof ConstraintWidgetContainer) {
        ConstraintWidgetContainer parent = (ConstraintWidgetContainer) widget.getParent();
        if (widget instanceof Guideline) {
            if (parent.isRootContainer()) {
                drawRootGuideline(transform, g, parent, (Guideline) widget, isSelected);
            }
            return;
        }
    }
    if (widget.getVisibility() == ConstraintWidget.INVISIBLE) {
        g.setStroke(SnapDraw.sDashedStroke);
    }
    ConstraintAnchor leftAnchor = widget.getAnchor(ConstraintAnchor.Type.LEFT);
    ConstraintAnchor rightAnchor = widget.getAnchor(ConstraintAnchor.Type.RIGHT);
    ConstraintAnchor topAnchor = widget.getAnchor(ConstraintAnchor.Type.TOP);
    ConstraintAnchor bottomAnchor = widget.getAnchor(ConstraintAnchor.Type.BOTTOM);
    boolean leftAnchorIsConnected = leftAnchor.isConnected();
    boolean rightAnchorIsConnected = rightAnchor.isConnected();
    boolean topAnchorIsConnected = topAnchor.isConnected();
    boolean bottomAnchorIsConnected = bottomAnchor.isConnected();
    boolean baselineAnchorIsConnected = widget.getAnchor(ConstraintAnchor.Type.BASELINE).isConnected();
    boolean centerAnchorIsConnected = (leftAnchorIsConnected && rightAnchorIsConnected && leftAnchor.getTarget() == rightAnchor.getTarget()) || (topAnchorIsConnected && bottomAnchorIsConnected && topAnchor.getTarget() == bottomAnchor.getTarget());
    // First, resize handles...
    if (showResizeHandles) {
        g.fillRect(l - radiusRect, t - radiusRect, rectDimension, rectDimension);
        g.fillRect(r - radiusRect, t - radiusRect, rectDimension, rectDimension);
        g.fillRect(l - radiusRect, b - radiusRect, rectDimension, rectDimension);
        g.fillRect(r - radiusRect, b - radiusRect, rectDimension, rectDimension);
        if (showSizeIndicator) {
            ConnectionDraw.drawHorizontalMarginIndicator(g, String.valueOf(widget.getWidth()), l, r, t - 20);
            ConnectionDraw.drawVerticalMarginIndicator(g, String.valueOf(widget.getHeight()), l - 20, t, b);
        }
    }
    // Then, let's draw the constraints anchors
    boolean displayAllAnchors = showAnchors.contains(ANCHORS_DISPLAY.ALL);
    boolean showLeftAnchor = displayAllAnchors || showAnchors.contains(ANCHORS_DISPLAY.LEFT) || showAnchors.contains(ANCHORS_DISPLAY.HORIZONTAL);
    boolean showRightAnchor = displayAllAnchors || showAnchors.contains(ANCHORS_DISPLAY.RIGHT) || showAnchors.contains(ANCHORS_DISPLAY.HORIZONTAL);
    boolean showTopAnchor = displayAllAnchors || showAnchors.contains(ANCHORS_DISPLAY.TOP) || showAnchors.contains(ANCHORS_DISPLAY.VERTICAL);
    boolean showBottomAnchor = displayAllAnchors || showAnchors.contains(ANCHORS_DISPLAY.BOTTOM) || showAnchors.contains(ANCHORS_DISPLAY.VERTICAL);
    boolean showCenterAnchor = displayAllAnchors || showAnchors.contains(ANCHORS_DISPLAY.CENTER);
    boolean showBaselineAnchor = displayAllAnchors || showAnchors.contains(ANCHORS_DISPLAY.BASELINE);
    if (!showAnchors.contains(ANCHORS_DISPLAY.NONE) && showAnchors.contains(ANCHORS_DISPLAY.CONNECTED)) {
        showLeftAnchor |= leftAnchorIsConnected;
        showRightAnchor |= rightAnchorIsConnected;
        showTopAnchor |= topAnchorIsConnected;
        showBottomAnchor |= bottomAnchorIsConnected;
        showCenterAnchor |= centerAnchorIsConnected;
        showBaselineAnchor |= baselineAnchorIsConnected;
    }
    if (showBaselineAnchor && !(widget instanceof ConstraintWidgetContainer) && widget.getBaselineDistance() > 0) {
        int baselineY = transform.getSwingY(WidgetInteractionTargets.constraintHandle(widget.getAnchor(ConstraintAnchor.Type.BASELINE)).getDrawY());
        g.drawLine(l, baselineY, r, baselineY);
    }
    // Now, let's draw the widget's frame
    boolean horizontalSpring = widget.getHorizontalDimensionBehaviour() == ConstraintWidget.DimensionBehaviour.MATCH_CONSTRAINT;
    boolean verticalSpring = widget.getVerticalDimensionBehaviour() == ConstraintWidget.DimensionBehaviour.MATCH_CONSTRAINT;
    Graphics2D g2 = (Graphics2D) g.create();
    if (widget instanceof ConstraintWidgetContainer) {
        g2.setStroke(SnapDraw.sLongDashedStroke);
        if (widget instanceof ConstraintTableLayout) {
            drawTableLayoutGuidelines(transform, g2, (ConstraintTableLayout) widget);
        }
    }
    if (!widget.isRootContainer() && (horizontalSpring || verticalSpring)) {
        int x = l;
        int y = t;
        Stroke previousStroke = g.getStroke();
        if (baselineAnchorIsConnected) {
            g2.setStroke(ConnectionDraw.sSpreadDashedStroke);
        }
        if (horizontalSpring) {
            if (showTopAnchor) {
                drawHorizontalZigZagLine(g2, l, midX - radius, t, ZIGZAG, 0);
                drawHorizontalZigZagLine(g2, midX + radius, r, t, ZIGZAG, 0);
            } else {
                drawHorizontalZigZagLine(g2, l, r, t, ZIGZAG, 0);
            }
            if (showBottomAnchor) {
                drawHorizontalZigZagLine(g2, l, midX - radius, b, -ZIGZAG, 0);
                drawHorizontalZigZagLine(g2, midX + radius, r, b, -ZIGZAG, 0);
            } else {
                drawHorizontalZigZagLine(g2, l, r, b, -ZIGZAG, 0);
            }
        } else {
            g2.drawLine(x, y, x + w, y);
            g2.drawLine(x, y + h, x + w, y + h);
        }
        g2.setStroke(previousStroke);
        if (verticalSpring) {
            if (showLeftAnchor) {
                drawVerticalZigZagLine(g2, l, t, midY - radius, ZIGZAG, 0);
                drawVerticalZigZagLine(g2, l, midY + radius, b, ZIGZAG, 0);
            } else {
                drawVerticalZigZagLine(g2, l, t, b, ZIGZAG, 0);
            }
            if (showRightAnchor) {
                drawVerticalZigZagLine(g2, r, t, midY - radius, -ZIGZAG, 0);
                drawVerticalZigZagLine(g2, r, midY + radius, b, -ZIGZAG, 0);
            } else {
                drawVerticalZigZagLine(g2, r, t, b, -ZIGZAG, 0);
            }
        } else {
            g2.drawLine(x, y, x, y + h);
            g2.drawLine(x + w, y, x + w, y + h);
        }
    } else {
        Stroke previousStroke = g.getStroke();
        if (baselineAnchorIsConnected) {
            g2.setStroke(ConnectionDraw.sSpreadDashedStroke);
        }
        if (showTopAnchor) {
            g2.drawLine(l, t, midX - radius, t);
            g2.drawLine(midX + radius, t, r, t);
        } else {
            g2.drawLine(l, t, r, t);
        }
        if (showBottomAnchor) {
            g2.drawLine(l, b, midX - radius, b);
            g2.drawLine(midX + radius, b, r, b);
        } else {
            g2.drawLine(l, b, r, b);
        }
        g2.setStroke(previousStroke);
        if (showLeftAnchor) {
            g2.drawLine(l, t, l, midY - radius);
            g2.drawLine(l, midY + radius, l, b);
        } else {
            g2.drawLine(l, t, l, b);
        }
        if (showRightAnchor) {
            g2.drawLine(r, t, r, midY - radius);
            g2.drawLine(r, midY + radius, r, b);
        } else {
            g2.drawLine(r, t, r, b);
        }
    }
    g2.dispose();
    if (DEBUG) {
        // Draw diagonals
        g.drawLine(l, t, r, b);
        g.drawLine(l, b, r, t);
    }
    g.setStroke(SnapDraw.sNormalStroke);
}
Also used : ConstraintAnchor(android.support.constraint.solver.widgets.ConstraintAnchor) ConstraintWidgetContainer(android.support.constraint.solver.widgets.ConstraintWidgetContainer) ConstraintTableLayout(android.support.constraint.solver.widgets.ConstraintTableLayout) Guideline(android.support.constraint.solver.widgets.Guideline)

Example 9 with Guideline

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

the class WidgetDraw method drawTableControls.

/**
     * Draw table controls when selected
     *
     * @param transform view transform
     * @param g         graphics context
     * @param table     the ConstraintTableLayout we are drawing
     */
public static void drawTableControls(ViewTransform transform, Graphics2D g, ConstraintTableLayout table) {
    ArrayList<Guideline> vertical = table.getVerticalGuidelines();
    Graphics2D g2 = (Graphics2D) g.create();
    g2.setStroke(new BasicStroke(1));
    int l = transform.getSwingX(table.getDrawX());
    int t = transform.getSwingY(table.getDrawY());
    g2.setFont(sFont);
    g2.drawRect(l, t - 20 - 4, 20, 20);
    int column = 0;
    String align = table.getColumnAlignmentRepresentation(column++);
    g2.drawString(align, l + 5, t - 4 - 5);
    for (ConstraintWidget v : vertical) {
        int x = transform.getSwingX(v.getX()) + l;
        g2.drawRect(x, t - 20 - 4, 20, 20);
        align = table.getColumnAlignmentRepresentation(column++);
        g2.drawString(align, x + 5, t - 4 - 5);
    }
    g2.dispose();
}
Also used : ConstraintWidget(android.support.constraint.solver.widgets.ConstraintWidget) Guideline(android.support.constraint.solver.widgets.Guideline)

Example 10 with Guideline

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

the class WidgetDraw method drawTableLayoutGuidelines.

/**
     * Draw the internal guidelines of a ConstraintTableLayout
     *
     * @param transform view transform
     * @param g         graphics context
     * @param table     the ConstraintTableLayout we are drawing
     */
private static void drawTableLayoutGuidelines(ViewTransform transform, Graphics2D g, ConstraintTableLayout table) {
    Graphics2D g2 = (Graphics2D) g.create();
    ArrayList<Guideline> vertical = table.getVerticalGuidelines();
    ArrayList<Guideline> horizontal = table.getHorizontalGuidelines();
    g2.setStroke(SnapDraw.sThinDashedStroke);
    int l = transform.getSwingX(table.getDrawX());
    int t = transform.getSwingY(table.getDrawY());
    int r = transform.getSwingX(table.getDrawX() + table.getDrawWidth());
    int b = transform.getSwingY(table.getDrawY() + table.getDrawHeight());
    for (ConstraintWidget v : vertical) {
        int x = transform.getSwingX(v.getX()) + l;
        g2.drawLine(x, t, x, b);
    }
    for (ConstraintWidget h : horizontal) {
        int y = transform.getSwingY(h.getY()) + t;
        g2.drawLine(l, y, r, y);
    }
    g2.dispose();
}
Also used : 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