Search in sources :

Example 1 with WidgetInteractionTargets

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

the class WidgetsScene method updatePositions.

/**
     * Make sure the positions of the interaction targets are correctly updated
     *
     * @param viewTransform the view transform
     */
public void updatePositions(ViewTransform viewTransform) {
    for (ConstraintWidget widget : mWidgets.values()) {
        widget.updateDrawPosition();
        WidgetCompanion companion = (WidgetCompanion) widget.getCompanionWidget();
        WidgetInteractionTargets widgetInteraction = companion.getWidgetInteractionTargets();
        widgetInteraction.updatePosition(viewTransform);
    }
}
Also used : WidgetInteractionTargets(com.android.tools.sherpa.interaction.WidgetInteractionTargets)

Example 2 with WidgetInteractionTargets

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

the class WidgetCompanion method create.

public static WidgetCompanion create(ConstraintWidget widget) {
    WidgetCompanion companion = new WidgetCompanion();
    WidgetDecorator blueprintDecorator = new WidgetDecorator(widget);
    blueprintDecorator.setStyle(WidgetDecorator.BLUEPRINT_STYLE);
    WidgetDecorator androidDecorator = new WidgetDecorator(widget);
    androidDecorator.setStyle(WidgetDecorator.ANDROID_STYLE);
    companion.addDecorator(blueprintDecorator);
    companion.addDecorator(androidDecorator);
    companion.setWidgetInteractionTargets(new WidgetInteractionTargets(widget));
    return companion;
}
Also used : WidgetInteractionTargets(com.android.tools.sherpa.interaction.WidgetInteractionTargets) WidgetDecorator(com.android.tools.sherpa.drawing.decorator.WidgetDecorator)

Example 3 with WidgetInteractionTargets

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

the class ConstraintModel method setupConstraintWidget.

/**
   * Set up a ConstraintWidget from a component
   *
   * @param component
   * @param widget
   * @return true if we need to save the XML
   */
private boolean setupConstraintWidget(@NotNull NlComponent component, ConstraintWidget widget) {
    WidgetDecorator blueprintDecorator = createDecorator(component, widget);
    WidgetDecorator androidDecorator = createDecorator(component, widget);
    blueprintDecorator.setStateModel(this);
    androidDecorator.setStateModel(this);
    blueprintDecorator.setStyle(WidgetDecorator.BLUEPRINT_STYLE);
    androidDecorator.setStyle(WidgetDecorator.ANDROID_STYLE);
    WidgetCompanion companion = new WidgetCompanion();
    companion.addDecorator(blueprintDecorator);
    companion.addDecorator(androidDecorator);
    companion.setWidgetInteractionTargets(new WidgetInteractionTargets(widget));
    companion.setWidgetModel(component);
    companion.setWidgetTag(component);
    widget.setCompanionWidget(companion);
    widget.setDebugName(component.getId());
    return ConstraintUtilities.updateWidgetFromComponent(null, this, widget, component);
}
Also used : WidgetInteractionTargets(com.android.tools.sherpa.interaction.WidgetInteractionTargets) WidgetCompanion(com.android.tools.sherpa.structure.WidgetCompanion)

Example 4 with WidgetInteractionTargets

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

the class ConstraintUtilities method setGuideline.

/**
   * Update the given guideline with the attributes set in the NlComponent
   *
   * @param component the component we get the attributes from
   * @param guideline the guideline widget we want to update with the values
   */
private static void setGuideline(NlComponent component, Guideline guideline) {
    String relativeBegin = component.getAttribute(SdkConstants.SHERPA_URI, SdkConstants.LAYOUT_CONSTRAINT_GUIDE_BEGIN);
    String relativeEnd = component.getAttribute(SdkConstants.SHERPA_URI, SdkConstants.LAYOUT_CONSTRAINT_GUIDE_END);
    boolean useFloat = useGuidelineFloat(component.getModel());
    String percentAttribute = useFloat ? SdkConstants.LAYOUT_CONSTRAINT_GUIDE_PERCENT : SdkConstants.LAYOUT_CONSTRAINT_DEPRECATED_GUIDE_PERCENT;
    String relativePercent = component.getAttribute(SdkConstants.SHERPA_URI, percentAttribute);
    String oldRelativePercent = component.getAttribute(SdkConstants.SHERPA_URI, SdkConstants.LAYOUT_CONSTRAINT_DEPRECATED_GUIDE_PERCENT);
    WidgetCompanion companion = (WidgetCompanion) guideline.getCompanionWidget();
    if (useFloat && oldRelativePercent != null) {
        // we need to upgrade
        companion.getWidgetProperties().setGuidelineAttribute(relativePercent);
        float value = 0;
        try {
            value = Integer.parseInt(oldRelativePercent) / 100f;
        } catch (NumberFormatException e) {
        // ignore
        }
        guideline.setGuidePercent(value);
    } else if (relativePercent != null && relativePercent.length() > 0) {
        companion.getWidgetProperties().setGuidelineAttribute(relativePercent);
        float value = 0;
        try {
            if (useFloat) {
                value = Float.parseFloat(relativePercent);
            } else {
                value = Integer.parseInt(relativePercent) / 100f;
            }
        } catch (NumberFormatException e) {
        // ignore
        }
        guideline.setGuidePercent(value);
    } else if (relativeBegin != null && relativeBegin.length() > 0) {
        companion.getWidgetProperties().setGuidelineAttribute(relativeBegin);
        try {
            int value = getDpValue(component, relativeBegin);
            guideline.setGuideBegin(value);
        } catch (NumberFormatException e) {
        // Ignore
        }
    } else if (relativeEnd != null && relativeEnd.length() > 0) {
        companion.getWidgetProperties().setGuidelineAttribute(relativeEnd);
        try {
            int value = getDpValue(component, relativeEnd);
            guideline.setGuideEnd(value);
        } catch (NumberFormatException e) {
        // Ignore
        }
    }
    String orientation = component.getAttribute(SdkConstants.NS_RESOURCES, SdkConstants.ATTR_ORIENTATION);
    if (orientation != null) {
        int newOrientation = Guideline.HORIZONTAL;
        if (SdkConstants.ATTR_GUIDELINE_ORIENTATION_VERTICAL.equalsIgnoreCase(orientation)) {
            newOrientation = Guideline.VERTICAL;
        }
        if (newOrientation != guideline.getOrientation()) {
            guideline.setOrientation(newOrientation);
            WidgetInteractionTargets interactionTargets = companion.getWidgetInteractionTargets();
            interactionTargets.resetConstraintHandles();
        }
    }
}
Also used : WidgetInteractionTargets(com.android.tools.sherpa.interaction.WidgetInteractionTargets) WidgetCompanion(com.android.tools.sherpa.structure.WidgetCompanion)

Example 5 with WidgetInteractionTargets

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

Aggregations

WidgetInteractionTargets (com.android.tools.sherpa.interaction.WidgetInteractionTargets)10 WidgetCompanion (com.android.tools.sherpa.structure.WidgetCompanion)5 WidgetDecorator (com.android.tools.sherpa.drawing.decorator.WidgetDecorator)4 ConstraintHandle (com.android.tools.sherpa.interaction.ConstraintHandle)2 ResizeHandle (com.android.tools.sherpa.interaction.ResizeHandle)2 ConstraintAnchor (android.support.constraint.solver.widgets.ConstraintAnchor)1 ConstraintWidget (android.support.constraint.solver.widgets.ConstraintWidget)1 SnapCandidate (com.android.tools.sherpa.interaction.SnapCandidate)1 Selection (com.android.tools.sherpa.structure.Selection)1