Search in sources :

Example 26 with ConstraintWidget

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

the class ConstraintLayoutHandler method drawGroup.

/**
   * Paint the component and its children on the given context
   *
   * @param gc         graphics context
   * @param screenView the current screenview
   * @param component  the component to draw
   * @return true to indicate that we will need to be repainted
   */
@Override
public boolean drawGroup(@NotNull Graphics2D gc, @NotNull ScreenView screenView, @NotNull NlComponent component) {
    ConstraintModel constraintModel = ConstraintModel.getConstraintModel(screenView.getModel());
    DrawConstraintModel drawConstraintModel = ConstraintModel.getDrawConstraintModel(screenView);
    updateActions(constraintModel.getSelection());
    ConstraintWidget widget = constraintModel.getScene().getWidget(component);
    if (USE_SCENE_INTERACTION) {
        return false;
    }
    return drawConstraintModel.paint(gc, screenView, widget, Coordinates.getSwingDimension(screenView, component.w), Coordinates.getSwingDimension(screenView, component.h), myShowAllConstraints);
}
Also used : ConstraintWidget(android.support.constraint.solver.widgets.ConstraintWidget)

Example 27 with ConstraintWidget

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

the class ScoutArrange method expandVertically.

/**
     * Expands widgets vertically in an evenly spaced manner
     * @param widgetList
     * @param margin
     */
private static void expandVertically(ArrayList<ConstraintWidget> widgetList, int margin) {
    WidgetContainer base = (WidgetContainer) widgetList.get(0).getParent();
    ConstraintWidget[] pears = new ConstraintWidget[base.getChildren().size()];
    pears = base.getChildren().toArray(pears);
    Rectangle selectBounds = getBoundingBox(widgetList);
    Rectangle clip = new Rectangle();
    int gapNorth = gap(Direction.NORTH, selectBounds, pears);
    int gapSouth = gap(Direction.SOUTH, selectBounds, pears);
    clip.y = selectBounds.y - gapNorth;
    clip.height = selectBounds.height + gapSouth + gapNorth;
    ArrayList<ConstraintWidget> selectedList = new ArrayList<>(widgetList);
    while (!selectedList.isEmpty()) {
        ConstraintWidget widget = selectedList.remove(0);
        ArrayList<ConstraintWidget> col = new ArrayList<>();
        col.add(widget);
        for (Iterator<ConstraintWidget> iterator = selectedList.iterator(); iterator.hasNext(); ) {
            ConstraintWidget elem = iterator.next();
            if (isSameColumn(widget, elem)) {
                if (!col.contains(elem)) {
                    col.add(elem);
                }
                iterator.remove();
            }
        }
        ConstraintWidget[] colArray = new ConstraintWidget[col.size()];
        colArray = col.toArray(colArray);
        Arrays.sort(colArray, sSortY);
        int gaps = (colArray.length - 1) * margin;
        int totalHeight = (clip.height - gaps - 2 * margin);
        for (int i = 0; i < colArray.length; i++) {
            int y = margin * i + (i * (totalHeight)) / colArray.length;
            ConstraintWidget constraintWidget = colArray[i];
            constraintWidget.setY(y + clip.y + margin);
            int yend = margin * i + (totalHeight * (i + 1)) / colArray.length;
            constraintWidget.setVerticalDimensionBehaviour(ConstraintWidget.DimensionBehaviour.FIXED);
            constraintWidget.setHeight(yend - y);
        }
    }
}
Also used : ConstraintWidget(android.support.constraint.solver.widgets.ConstraintWidget) Rectangle(java.awt.Rectangle) ArrayList(java.util.ArrayList) WidgetContainer(android.support.constraint.solver.widgets.WidgetContainer)

Example 28 with ConstraintWidget

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

the class ScoutArrange method expandHorizontally.

/**
     * Expands widgets horizontally in an evenly spaced manner
     * @param widgetList
     * @param margin
     */
public static void expandHorizontally(ArrayList<ConstraintWidget> widgetList, int margin) {
    WidgetContainer base = (WidgetContainer) widgetList.get(0).getParent();
    ConstraintWidget[] pears = new ConstraintWidget[base.getChildren().size()];
    pears = base.getChildren().toArray(pears);
    Rectangle selectBounds = getBoundingBox(widgetList);
    Rectangle clip = new Rectangle();
    int gapWest = gap(Direction.WEST, selectBounds, pears);
    int gapEast = gap(Direction.EAST, selectBounds, pears);
    clip.x = selectBounds.x - gapWest;
    clip.width = selectBounds.width + gapEast + gapWest;
    ArrayList<ConstraintWidget> selectedList;
    selectedList = new ArrayList<ConstraintWidget>(widgetList);
    while (!selectedList.isEmpty()) {
        ConstraintWidget widget = selectedList.remove(0);
        ArrayList<ConstraintWidget> row = new ArrayList<>();
        row.add(widget);
        for (Iterator<ConstraintWidget> iterator = selectedList.iterator(); iterator.hasNext(); ) {
            ConstraintWidget elem = iterator.next();
            if (isSameRow(widget, elem)) {
                if (!row.contains(elem)) {
                    row.add(elem);
                }
                iterator.remove();
            }
        }
        ConstraintWidget[] rowArray = new ConstraintWidget[row.size()];
        rowArray = row.toArray(rowArray);
        Arrays.sort(rowArray, sSortX);
        int gaps = (rowArray.length - 1) * margin;
        int totalWidth = (clip.width - gaps - 2 * margin);
        for (int i = 0; i < rowArray.length; i++) {
            int x = margin * i + (i * (totalWidth)) / rowArray.length;
            ConstraintWidget constraintWidget = rowArray[i];
            constraintWidget.setX(x + clip.x + margin);
            int xend = margin * i + (totalWidth * (i + 1)) / rowArray.length;
            constraintWidget.setHorizontalDimensionBehaviour(ConstraintWidget.DimensionBehaviour.FIXED);
            constraintWidget.setWidth(xend - x);
        }
    }
}
Also used : ConstraintWidget(android.support.constraint.solver.widgets.ConstraintWidget) Rectangle(java.awt.Rectangle) ArrayList(java.util.ArrayList) WidgetContainer(android.support.constraint.solver.widgets.WidgetContainer)

Example 29 with ConstraintWidget

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

the class ScoutArrange method nearestHorizontal.

/**
     * find the nearest widget in a list of widgets only considering the horizontal location
     *
     * @param nextTo find nearest to this widget
     * @param list   list to search
     * @return the nearest in the list
     */
private static ConstraintWidget nearestHorizontal(ConstraintWidget nextTo, ArrayList<ConstraintWidget> list) {
    int min = Integer.MAX_VALUE;
    ConstraintWidget ret = null;
    int nextToLeft = nextTo.getX();
    int nextToRight = nextToLeft + nextTo.getWidth();
    for (ConstraintWidget widget : list) {
        if (widget == nextTo) {
            continue;
        }
        int left = widget.getX();
        int right = left + widget.getWidth();
        int dist = Math.abs(left - nextToLeft);
        dist = Math.min(dist, Math.abs(left - nextToRight));
        dist = Math.min(dist, Math.abs(right - nextToRight));
        dist = Math.min(dist, Math.abs(right - nextToLeft));
        if (dist < min) {
            min = dist;
            ret = widget;
        }
    }
    return ret;
}
Also used : ConstraintWidget(android.support.constraint.solver.widgets.ConstraintWidget)

Example 30 with ConstraintWidget

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

the class ScoutArrange method gap.

/**
     * Calculate the gap in to the nearest widget
     *
     * @param direction the direction to check
     * @param list      list of other widgets (root == list[0])
     * @return the distance on that side
     */
public static int gap(Direction direction, Rectangle region, ConstraintWidget[] list) {
    int rootWidth = list[0].getParent().getWidth();
    int rootHeight = list[0].getParent().getHeight();
    Rectangle rect = new Rectangle();
    switch(direction) {
        case NORTH:
            {
                rect.y = 0;
                rect.x = region.x + 1;
                rect.width = region.width - 2;
                rect.height = region.y;
            }
            break;
        case SOUTH:
            {
                rect.y = region.y + region.height + 1;
                rect.x = region.x + 1;
                rect.width = region.width - 2;
                rect.height = rootHeight - rect.y;
            }
            break;
        case WEST:
            {
                rect.y = region.y + 1;
                rect.x = 0;
                rect.width = region.x;
                rect.height = region.height - 2;
            }
            break;
        case EAST:
            {
                rect.y = region.y + 1;
                rect.x = region.x + region.width + 1;
                rect.width = rootWidth - rect.x;
                rect.height = region.height - 2;
            }
            break;
    }
    int min = Integer.MAX_VALUE;
    for (int i = 0; i < list.length; i++) {
        ConstraintWidget widget = list[i];
        Rectangle r = getRectangle(widget);
        if (r.intersects(rect)) {
            int dist = (int) distance(r, region);
            if (min > dist) {
                min = dist;
            }
        }
    }
    if (min > Math.max(rootHeight, rootWidth)) {
        switch(direction) {
            case NORTH:
                return region.y;
            case SOUTH:
                return rootHeight - (region.y + region.height);
            case WEST:
                return region.x;
            case EAST:
                return rootWidth - (region.x + region.width);
        }
    }
    return min;
}
Also used : ConstraintWidget(android.support.constraint.solver.widgets.ConstraintWidget) Rectangle(java.awt.Rectangle)

Aggregations

ConstraintWidget (android.support.constraint.solver.widgets.ConstraintWidget)37 ConstraintAnchor (android.support.constraint.solver.widgets.ConstraintAnchor)11 Guideline (android.support.constraint.solver.widgets.Guideline)8 Rectangle (java.awt.Rectangle)7 ArrayList (java.util.ArrayList)7 WidgetCompanion (com.android.tools.sherpa.structure.WidgetCompanion)5 Point (java.awt.Point)5 ConstraintWidgetContainer (android.support.constraint.solver.widgets.ConstraintWidgetContainer)3 WidgetContainer (android.support.constraint.solver.widgets.WidgetContainer)3 ConstraintTableLayout (android.support.constraint.solver.widgets.ConstraintTableLayout)2 WidgetDecorator (com.android.tools.sherpa.drawing.decorator.WidgetDecorator)1 WidgetInteractionTargets (com.android.tools.sherpa.interaction.WidgetInteractionTargets)1 Ellipse2D (java.awt.geom.Ellipse2D)1 Arrays (java.util.Arrays)1 Comparator (java.util.Comparator)1 Iterator (java.util.Iterator)1