Search in sources :

Example 11 with WidgetDecorator

use of com.android.tools.sherpa.drawing.decorator.WidgetDecorator in project android by JetBrains.

the class WidgetsScene method findWidgets.

/**
     * Gather all the widgets contained in the area specified and return them as an array,
     * taking the decorator visibility in account
     *
     * @param x      x position of the selection area
     * @param y      y position of the selection area
     * @param width  width of the selection area
     * @param height height of the selection area
     * @return an array containing the widgets inside the selection area
     */
public ArrayList<ConstraintWidget> findWidgets(WidgetContainer container, int x, int y, int width, int height) {
    ArrayList<ConstraintWidget> found = new ArrayList<>();
    Rectangle area = new Rectangle(x, y, width, height);
    for (ConstraintWidget widget : container.getChildren()) {
        WidgetCompanion companion = (WidgetCompanion) widget.getCompanionWidget();
        WidgetDecorator decorator = companion.getWidgetDecorator(WidgetDecorator.BLUEPRINT_STYLE);
        if (!decorator.isVisible()) {
            continue;
        }
        Rectangle bounds = new Rectangle(widget.getDrawX(), widget.getDrawY(), widget.getWidth(), widget.getHeight());
        if (area.intersects(bounds)) {
            found.add(widget);
        }
    }
    return found;
}
Also used : ArrayList(java.util.ArrayList) Rectangle(java.awt.Rectangle) WidgetDecorator(com.android.tools.sherpa.drawing.decorator.WidgetDecorator)

Example 12 with WidgetDecorator

use of com.android.tools.sherpa.drawing.decorator.WidgetDecorator in project android by JetBrains.

the class WidgetsScene method findResizeHandleInWidget.

/**
     * Find which ResizeHandle is close to the (x, y) coordinates
     *
     * @param widget the widget we are checking
     * @param x      x coordinate
     * @param y      y coordinate
     * @return the ResizeHandle close to (x, y), or null if none are close enough
     */
private static ResizeHandle findResizeHandleInWidget(ConstraintWidget widget, float x, float y, ViewTransform transform) {
    WidgetCompanion companion = (WidgetCompanion) widget.getCompanionWidget();
    WidgetDecorator decorator = companion.getWidgetDecorator(WidgetDecorator.BLUEPRINT_STYLE);
    if (!decorator.isVisible()) {
        return null;
    }
    WidgetInteractionTargets widgetInteraction = companion.getWidgetInteractionTargets();
    widgetInteraction.updatePosition(transform);
    ResizeHandle handle = widgetInteraction.findResizeHandle(x, y);
    if (handle != null) {
        return handle;
    }
    return null;
}
Also used : ResizeHandle(com.android.tools.sherpa.interaction.ResizeHandle) WidgetInteractionTargets(com.android.tools.sherpa.interaction.WidgetInteractionTargets) WidgetDecorator(com.android.tools.sherpa.drawing.decorator.WidgetDecorator)

Example 13 with WidgetDecorator

use of com.android.tools.sherpa.drawing.decorator.WidgetDecorator in project android by JetBrains.

the class WidgetsScene method findAnchor.

/**
     * Find which ConstraintAnchor is close to the (x, y) coordinates
     *
     * @param x               x coordinate
     * @param y               y coordinate
     * @param checkGuidelines if true, we will check for guidelines to connect to
     * @param mousePress      pass true on mouse press
     * @param viewTransform   the view transform
     * @return the ConstraintAnchor close to (x, y), or null if none are close enough
     */
public ConstraintAnchor findAnchor(float x, float y, boolean checkGuidelines, boolean mousePress, ViewTransform viewTransform) {
    ConnectionCandidate candidate = new ConnectionCandidate();
    float dist = (ConnectionDraw.CONNECTION_ANCHOR_SIZE + ConnectionDraw.CONNECTION_ANCHOR_SIZE) / viewTransform.getScale();
    candidate.distance = ConnectionDraw.CONNECTION_ANCHOR_SIZE * ConnectionDraw.CONNECTION_ANCHOR_SIZE;
    // We first try to find an anchor in the current selection
    for (Selection.Element element : mSelection.getElements()) {
        ConstraintWidget widget = element.widget;
        if (!checkGuidelines && (widget instanceof Guideline)) {
            continue;
        }
        WidgetCompanion companion = (WidgetCompanion) widget.getCompanionWidget();
        WidgetInteractionTargets widgetInteraction = companion.getWidgetInteractionTargets();
        widgetInteraction.updatePosition(viewTransform);
        widgetInteraction.findClosestConnection(viewTransform, x, y, candidate, mousePress);
    }
    float slope = (dist * dist);
    if (candidate.anchorTarget != null && candidate.distance < slope) {
        // allow some slope if we picked an anchor from the selection
        candidate.distance = 0;
    } else {
        candidate.anchorTarget = null;
    }
    for (ConstraintWidget widget : mWidgets.values()) {
        if (!checkGuidelines && (widget instanceof Guideline)) {
            continue;
        }
        WidgetCompanion companion = (WidgetCompanion) widget.getCompanionWidget();
        WidgetDecorator decorator = companion.getWidgetDecorator(WidgetDecorator.BLUEPRINT_STYLE);
        if (!decorator.isVisible()) {
            continue;
        }
        WidgetInteractionTargets widgetInteraction = companion.getWidgetInteractionTargets();
        widgetInteraction.updatePosition(viewTransform);
        widgetInteraction.findClosestConnection(viewTransform, x, y, candidate, mousePress);
    }
    return candidate.anchorTarget;
}
Also used : WidgetInteractionTargets(com.android.tools.sherpa.interaction.WidgetInteractionTargets) WidgetDecorator(com.android.tools.sherpa.drawing.decorator.WidgetDecorator)

Example 14 with WidgetDecorator

use of com.android.tools.sherpa.drawing.decorator.WidgetDecorator in project android by JetBrains.

the class WidgetsScene method findWidget.

/**
     * Find a widget at the coordinate (x, y), taking the decorator visibility in account
     *
     * @param x x position
     * @param y y position
     * @return a widget if found, null otherwise
     */
public ConstraintWidget findWidget(ConstraintWidgetContainer container, float x, float y) {
    WidgetCompanion companion = (WidgetCompanion) container.getCompanionWidget();
    WidgetDecorator containerDecorator = companion.getWidgetDecorator(WidgetDecorator.BLUEPRINT_STYLE);
    if (!containerDecorator.isVisible()) {
        return null;
    }
    ConstraintWidget found = null;
    if (container == getRoot()) {
        // First, check the current selection
        found = findWidgetInSelection(x, y);
        if (found != null) {
            return found;
        }
    }
    int l = container.getDrawX();
    int t = container.getDrawY();
    int r = l + container.getWidth();
    int b = t + container.getHeight();
    if (x >= l && x <= r && y >= t && y <= b) {
        found = container;
    }
    for (ConstraintWidget widget : container.getChildren()) {
        WidgetCompanion widgetCompanion = (WidgetCompanion) widget.getCompanionWidget();
        WidgetDecorator widgetDecorator = widgetCompanion.getWidgetDecorator(WidgetDecorator.BLUEPRINT_STYLE);
        if (!widgetDecorator.isVisible()) {
            continue;
        }
        if (widget instanceof ConstraintWidgetContainer) {
            ConstraintWidget f = findWidget((ConstraintWidgetContainer) widget, x, y);
            if (f != null) {
                found = f;
            }
        } else {
            l = widget.getDrawX();
            t = widget.getDrawY();
            r = l + widget.getWidth();
            b = t + widget.getHeight();
            if (x >= l && x <= r && y >= t && y <= b) {
                found = widget;
            }
        }
    }
    return found;
}
Also used : WidgetDecorator(com.android.tools.sherpa.drawing.decorator.WidgetDecorator)

Example 15 with WidgetDecorator

use of com.android.tools.sherpa.drawing.decorator.WidgetDecorator in project android by JetBrains.

the class SceneDraw method paintWidgets.

/**
     * Main painting function
     *
     *
     * @param rootDrawComponent  the component we want to draw (with its children)
     * @param width              width of the canvas we paint on
     * @param height             height of the canvas we paint on
     * @param transform
     * @param g
     * @param showAllConstraints
     * @param mouseInteraction
     * @return true if need to be called again (animation...)
     */
public boolean paintWidgets(ConstraintWidget rootDrawComponent, int width, int height, ViewTransform transform, Graphics2D g, boolean showAllConstraints, MouseInteraction mouseInteraction) {
    g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
    WidgetContainer root = mWidgetsScene.getRoot();
    if (root == null) {
        return false;
    }
    mViewWidth = width;
    mViewHeight = height;
    if (mApplyConstraints) {
        root.layout();
    }
    // Adapt the anchor size
    ConnectionDraw.CONNECTION_ANCHOR_SIZE = (int) getAnchorSize(transform.getScale());
    ConstraintAnchor selectedAnchor = mSelection.getSelectedAnchor();
    ResizeHandle selectedResizeHandle = mSelection.getSelectedResizeHandle();
    // Let's draw the widgets and their constraints.
    boolean needsRepaint = false;
    WidgetDecorator.setShowAllConstraints(showAllConstraints);
    // First, mark which widgets is selected.
    for (ConstraintWidget widget : mWidgetsScene.getWidgets()) {
        WidgetCompanion widgetCompanion = (WidgetCompanion) widget.getCompanionWidget();
        WidgetDecorator decorator = widgetCompanion.getWidgetDecorator(getCurrentStyle());
        WidgetInteractionTargets widgetInteraction = widgetCompanion.getWidgetInteractionTargets();
        widgetInteraction.updatePosition(transform);
        decorator.setColorSet(mColorSet);
        if (mSelection.contains(widget)) {
            decorator.setIsSelected(true);
        } else {
            decorator.setIsSelected(false);
        }
    }
    // Then, mark highlighted widgets
    animateInCandidateAnchors(selectedAnchor);
    for (ConstraintWidget widget : mWidgetsScene.getWidgets()) {
        WidgetCompanion widgetCompanion = (WidgetCompanion) widget.getCompanionWidget();
        WidgetDecorator decorator = widgetCompanion.getWidgetDecorator(getCurrentStyle());
        decorator.applyLook();
    }
    // Draw the constraints
    for (ConstraintWidget widget : mWidgetsScene.getWidgets()) {
        WidgetCompanion widgetCompanion = (WidgetCompanion) widget.getCompanionWidget();
        WidgetDecorator decorator = widgetCompanion.getWidgetDecorator(getCurrentStyle());
        if (widget.getVisibility() == ConstraintWidget.GONE || (widget.getParent() != null && widget.getParent().getVisibility() == ConstraintWidget.GONE)) {
            continue;
        }
        if (DRAW_ENTIRE_TREE || widget == rootDrawComponent || widget.getParent() == rootDrawComponent) {
            if (decorator.isVisible() && !decorator.isSelected() && decorator.getLook() != ColorTheme.Look.HIGHLIGHTED) {
                decorator.onPaintConstraints(transform, g);
            }
        }
    }
    // Draw all the widgets
    ConstraintWidget selectedWidget = null;
    if (mSelection.hasSingleElement()) {
        selectedWidget = mSelection.getFirstElement().widget;
    }
    needsRepaint |= paintWidgets(transform, g, rootDrawComponent, mWidgetsScene.getRoot(), selectedWidget, selectedAnchor, selectedResizeHandle);
    // Draw the selected constraints
    for (ConstraintWidget widget : mWidgetsScene.getWidgets()) {
        WidgetCompanion widgetCompanion = (WidgetCompanion) widget.getCompanionWidget();
        WidgetDecorator decorator = widgetCompanion.getWidgetDecorator(getCurrentStyle());
        ConstraintWidget parent = widget.getParent();
        if (DRAW_ENTIRE_TREE || widget == rootDrawComponent || parent == rootDrawComponent) {
            if (decorator.isVisible() && (decorator.isSelected() || decorator.getLook() == ColorTheme.Look.HIGHLIGHTED)) {
                decorator.onPaintConstraints(transform, g);
                decorator.onPaintAnchors(transform, g);
                decorator.onPaintActions(transform, g);
            }
        }
    }
    // Draw snap candidates
    g.setColor(mColorSet.getHighlightedSnapGuides());
    for (SnapCandidate candidate : mWidgetMotion.getSimilarMargins()) {
        SnapDraw.drawSnapIndicator(transform, g, candidate);
    }
    g.setColor(mColorSet.getSnapGuides());
    for (SnapCandidate candidate : mWidgetMotion.getSnapCandidates()) {
        SnapDraw.drawSnapIndicator(transform, g, candidate);
    }
    for (SnapCandidate candidate : mWidgetResize.getSnapCandidates()) {
        SnapDraw.drawSnapIndicator(transform, g, candidate);
    }
    if (mSelection.hasSingleElement() && selectedAnchor != null) {
        ConstraintAnchor anchor = mSelection.getConnectionCandidateAnchor();
        ConstraintHandle selectedHandle = WidgetInteractionTargets.constraintHandle(selectedAnchor);
        g.setColor(mColorSet.getHighlightedConstraints());
        if (!selectedHandle.getAnchor().isConnected() || selectedHandle.getAnchor().getTarget() != anchor) {
            Point lastPoint = mouseInteraction.getLastPoint();
            if (lastPoint.x != 0 && lastPoint.y != 0) {
                selectedHandle.drawConnection(transform, g, mColorSet, true, mouseInteraction.getLastPoint());
            }
        }
    }
    if (selectedResizeHandle != null) {
        g.setColor(mColorSet.getSelectionColor());
        WidgetDraw.drawResizeHandleSelection(transform, g, selectedResizeHandle);
    }
    if (mSelection.isEmpty() && mouseInteraction.isMouseDown()) {
        Point startPoint = mouseInteraction.getStartPoint();
        Point lastMousePosition = mouseInteraction.getLastPoint();
        // draw a selection rect
        int x1 = Math.min(startPoint.x, lastMousePosition.x);
        int x2 = Math.max(startPoint.x, lastMousePosition.x);
        int y1 = Math.min(startPoint.y, lastMousePosition.y);
        int y2 = Math.max(startPoint.y, lastMousePosition.y);
        int ax1 = transform.getSwingX(x1);
        int ax2 = transform.getSwingX(x2);
        int ay1 = transform.getSwingY(y1);
        int ay2 = transform.getSwingY(y2);
        int w = x2 - x1;
        int h = y2 - y1;
        if (w > 0 || h > 0) {
            g.setColor(mColorSet.getSelectionColor());
            g.setStroke(SnapDraw.sDashedStroke);
            if (w >= 8 && h >= 8) {
                g.drawRect(ax1, ay1, ax2 - ax1, ay2 - ay1);
            } else if (w >= 8 && h < 8) {
                g.drawLine(ax1, ay1, ax2, ay1);
            } else {
                g.drawLine(ax1, ay1, ax1, ay2);
            }
            g.setStroke(SnapDraw.sNormalStroke);
            if (w >= 8) {
                ConnectionDraw.drawHorizontalMarginIndicator(g, String.valueOf(w), ax1, ax2, ay1 - 20);
            }
            if (h >= 8) {
                ConnectionDraw.drawVerticalMarginIndicator(g, String.valueOf(h), ax1 - 20, ay1, ay2);
            }
        }
    }
    if (mSelection.getSelectionBounds() != null) {
        Selection.Element bounds = mSelection.getSelectionBounds();
        g.setColor(mColorSet.getSelectionColor());
        g.setStroke(SnapDraw.sDashedStroke);
        int x = transform.getSwingX(bounds.widget.getDrawX());
        int y = transform.getSwingY(bounds.widget.getDrawY());
        int w = transform.getSwingDimension(bounds.widget.getDrawWidth());
        int h = transform.getSwingDimension(bounds.widget.getDrawHeight());
        g.drawRect(x, y, w, h);
    }
    needsRepaint |= mChoreographer.onPaint(transform, g);
    if (!needsRepaint && !mSelection.isEmpty()) {
        for (Selection.Element element : mSelection.getElements()) {
            needsRepaint |= element.widget.isAnimating();
        }
    }
    if (!needsRepaint) {
        for (ConstraintWidget widget : mWidgetsScene.getWidgets()) {
            needsRepaint |= widget.isAnimating();
        }
    }
    if (needsRepaint) {
        repaint();
    }
    return needsRepaint;
}
Also used : ResizeHandle(com.android.tools.sherpa.interaction.ResizeHandle) SnapCandidate(com.android.tools.sherpa.interaction.SnapCandidate) WidgetCompanion(com.android.tools.sherpa.structure.WidgetCompanion) Selection(com.android.tools.sherpa.structure.Selection) WidgetDecorator(com.android.tools.sherpa.drawing.decorator.WidgetDecorator) WidgetInteractionTargets(com.android.tools.sherpa.interaction.WidgetInteractionTargets) ConstraintHandle(com.android.tools.sherpa.interaction.ConstraintHandle)

Aggregations

WidgetDecorator (com.android.tools.sherpa.drawing.decorator.WidgetDecorator)15 WidgetCompanion (com.android.tools.sherpa.structure.WidgetCompanion)8 WidgetInteractionTargets (com.android.tools.sherpa.interaction.WidgetInteractionTargets)4 ConstraintHandle (com.android.tools.sherpa.interaction.ConstraintHandle)2 ResizeHandle (com.android.tools.sherpa.interaction.ResizeHandle)2 ArrayList (java.util.ArrayList)2 ConstraintAnchor (android.support.constraint.solver.widgets.ConstraintAnchor)1 ConstraintWidget (android.support.constraint.solver.widgets.ConstraintWidget)1 ResourceResolver (com.android.ide.common.resources.ResourceResolver)1 Configuration (com.android.tools.idea.configurations.Configuration)1 SceneComponent (com.android.tools.idea.uibuilder.scene.SceneComponent)1 AnimatedLine (com.android.tools.sherpa.animation.AnimatedLine)1 TextWidget (com.android.tools.sherpa.drawing.decorator.TextWidget)1 SnapCandidate (com.android.tools.sherpa.interaction.SnapCandidate)1 Selection (com.android.tools.sherpa.structure.Selection)1 WidgetsScene (com.android.tools.sherpa.structure.WidgetsScene)1 Rectangle (java.awt.Rectangle)1 Ellipse2D (java.awt.geom.Ellipse2D)1