Search in sources :

Example 6 with WidgetCompanion

use of com.android.tools.sherpa.structure.WidgetCompanion in project android by JetBrains.

the class ConstraintUtilities method updateComponentFromWidget.

/**
   * Update the component attributes from its corresponding ConstraintWidget
   *
   * @param model  the model we are working on
   * @param widget the widget to update from
   * @return an AttributesTransaction
   */
@Nullable
static NlAttributesHolder updateComponentFromWidget(@NotNull ConstraintModel model, @NotNull ConstraintWidget widget, @NotNull NlAttributesHolder transaction) {
    WidgetCompanion companion = (WidgetCompanion) widget.getCompanionWidget();
    NlComponent component = (NlComponent) companion.getWidgetModel();
    boolean isDroppedWidget = (model.getDragDropWidget() == widget);
    boolean isInsideConstraintLayout = isWidgetInsideConstraintLayout(widget);
    if (!isDroppedWidget && (widget.isRoot() || widget.isRootContainer() || !isInsideConstraintLayout)) {
        return null;
    }
    if (isInsideConstraintLayout || isDroppedWidget) {
        setEditorPosition(widget, transaction, widget.getX(), widget.getY());
    } else {
        clearEditorPosition(transaction);
    }
    setDimension(transaction, widget);
    setHorizontalChainStyle(transaction, widget);
    setVerticalChainStyle(transaction, widget);
    for (ConstraintAnchor anchor : widget.getAnchors()) {
        setConnection(transaction, anchor);
    }
    setRatio(transaction, widget);
    setHorizontalBias(transaction, widget);
    setVerticalBias(transaction, widget);
    if (widget instanceof Guideline) {
        commitGuideline(component.getModel(), transaction, (Guideline) widget);
    }
    return transaction;
}
Also used : WidgetCompanion(com.android.tools.sherpa.structure.WidgetCompanion) Nullable(org.jetbrains.annotations.Nullable)

Example 7 with WidgetCompanion

use of com.android.tools.sherpa.structure.WidgetCompanion 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 8 with WidgetCompanion

use of com.android.tools.sherpa.structure.WidgetCompanion in project android by JetBrains.

the class ConstraintUtilities method getValidComponent.

/**
   * Returns a component paired to the given widget, but only if it is a valid
   * component for us to work on.
   *
   * @param model the model we are working on
   * @param widget the constraint widget paired with the component
   * @return
   */
@Nullable
static NlComponent getValidComponent(@NotNull ConstraintModel model, @NotNull ConstraintWidget widget) {
    WidgetCompanion companion = (WidgetCompanion) widget.getCompanionWidget();
    NlComponent component = (NlComponent) companion.getWidgetModel();
    boolean isDroppedWidget = (model.getDragDropWidget() == widget);
    boolean isInsideConstraintLayout = isWidgetInsideConstraintLayout(widget);
    if (!isDroppedWidget && (widget.isRoot() || widget.isRootContainer() || !isInsideConstraintLayout)) {
        return null;
    }
    return component;
}
Also used : WidgetCompanion(com.android.tools.sherpa.structure.WidgetCompanion) Nullable(org.jetbrains.annotations.Nullable)

Example 9 with WidgetCompanion

use of com.android.tools.sherpa.structure.WidgetCompanion in project android by JetBrains.

the class ConstraintUtilities method setMarginType.

/**
   * Set the type of margin (if it contains a literal value or a dimens reference)
   *
   * @param attribute
   * @param type
   * @param component
   * @param widget
   */
private static void setMarginType(ConstraintAnchor.Type type, NlComponent component, ConstraintWidget widget) {
    String margin = null;
    String marginRtl = null;
    switch(type) {
        case LEFT:
            {
                margin = component.getAttribute(SdkConstants.NS_RESOURCES, SdkConstants.ATTR_LAYOUT_MARGIN_LEFT);
                marginRtl = component.getAttribute(SdkConstants.NS_RESOURCES, SdkConstants.ATTR_LAYOUT_MARGIN_START);
            }
            break;
        case RIGHT:
            {
                margin = component.getAttribute(SdkConstants.NS_RESOURCES, SdkConstants.ATTR_LAYOUT_MARGIN_RIGHT);
                marginRtl = component.getAttribute(SdkConstants.NS_RESOURCES, SdkConstants.ATTR_LAYOUT_MARGIN_END);
            }
            break;
        case TOP:
            {
                margin = component.getAttribute(SdkConstants.NS_RESOURCES, SdkConstants.ATTR_LAYOUT_MARGIN_TOP);
            }
            break;
        case BOTTOM:
            {
                margin = component.getAttribute(SdkConstants.NS_RESOURCES, SdkConstants.ATTR_LAYOUT_MARGIN_BOTTOM);
            }
            break;
    }
    if (margin == null && marginRtl == null) {
        return;
    }
    ConstraintAnchor anchor = widget.getAnchor(type);
    WidgetCompanion companion = (WidgetCompanion) widget.getCompanionWidget();
    if (margin != null) {
        companion.getWidgetProperties().setMarginReference(anchor, margin);
    }
    if (marginRtl != null) {
        companion.getWidgetProperties().setMarginRtlReference(anchor, marginRtl);
    }
}
Also used : WidgetCompanion(com.android.tools.sherpa.structure.WidgetCompanion)

Example 10 with WidgetCompanion

use of com.android.tools.sherpa.structure.WidgetCompanion 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

WidgetCompanion (com.android.tools.sherpa.structure.WidgetCompanion)27 WidgetDecorator (com.android.tools.sherpa.drawing.decorator.WidgetDecorator)8 ConstraintWidget (android.support.constraint.solver.widgets.ConstraintWidget)5 WidgetInteractionTargets (com.android.tools.sherpa.interaction.WidgetInteractionTargets)5 ConstraintAnchor (android.support.constraint.solver.widgets.ConstraintAnchor)3 ConstraintHandle (com.android.tools.sherpa.interaction.ConstraintHandle)3 ArrayList (java.util.ArrayList)3 Selection (com.android.tools.sherpa.structure.Selection)2 Nullable (org.jetbrains.annotations.Nullable)2 Guideline (android.support.constraint.solver.widgets.Guideline)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 ResizeHandle (com.android.tools.sherpa.interaction.ResizeHandle)1 SnapCandidate (com.android.tools.sherpa.interaction.SnapCandidate)1 WidgetsScene (com.android.tools.sherpa.structure.WidgetsScene)1 Point (java.awt.Point)1 Ellipse2D (java.awt.geom.Ellipse2D)1