Search in sources :

Example 21 with WidgetCompanion

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

the class WidgetDecorator method getConstraintHandle.

/**
     * Utility function to return a ConstraintHandle instance associated with the anchor
     *
     * @param anchor the given anchor
     * @return returns the associated ConstraintHandle
     */
public static ConstraintHandle getConstraintHandle(ConstraintAnchor anchor) {
    ConstraintWidget widget = anchor.getOwner();
    WidgetCompanion companion = (WidgetCompanion) widget.getCompanionWidget();
    WidgetInteractionTargets interactionTargets = companion.getWidgetInteractionTargets();
    return interactionTargets.getConstraintHandle(anchor);
}
Also used : WidgetInteractionTargets(com.android.tools.sherpa.interaction.WidgetInteractionTargets) ConstraintWidget(android.support.constraint.solver.widgets.ConstraintWidget) WidgetCompanion(com.android.tools.sherpa.structure.WidgetCompanion)

Example 22 with WidgetCompanion

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

the class SceneDraw method getDecorator.

/**
     * Utility function to get the decorator of the widget and set the correct
     * anchor and resize indicators
     *
     * @param widget               the widget to draw
     * @param selectedWidget       the selected widget if any
     * @param selectedAnchor       the selected anchor if any
     * @param selectedResizeHandle the selected resize handle if any
     * @return the widget decorator
     */
private WidgetDecorator getDecorator(ConstraintWidget widget, ConstraintWidget selectedWidget, ConstraintAnchor selectedAnchor, ResizeHandle selectedResizeHandle) {
    WidgetCompanion companion = (WidgetCompanion) widget.getCompanionWidget();
    WidgetDecorator decorator = companion.getWidgetDecorator(getCurrentStyle());
    if (!decorator.isSelected()) {
        decorator.updateShowAnchorsPolicy(selectedWidget, selectedAnchor);
    } else {
        decorator.setShowResizeHandles(mWidgetMotion.needToShowDecorations());
        decorator.setShowSizeIndicator(selectedResizeHandle != null);
    }
    if (mMoveOnlyMode) {
        decorator.setShowResizeHandles(false);
    }
    return decorator;
}
Also used : WidgetCompanion(com.android.tools.sherpa.structure.WidgetCompanion) WidgetDecorator(com.android.tools.sherpa.drawing.decorator.WidgetDecorator)

Example 23 with WidgetCompanion

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

the class ConstraintModel method onSelectionChanged.

/**
   * Something has changed in our selection
   *
   * @param selection our internal selection object
   */
@Override
public void onSelectionChanged(Selection selection) {
    SelectionModel selectionModel = myNlModel.getSelectionModel();
    if (selection.isEmpty()) {
        selectionModel.clear();
        return;
    }
    List<NlComponent> components = new ArrayList<>();
    for (Selection.Element selectedElement : mySelection.getElements()) {
        if (selectedElement.widget == myDragDropWidget) {
            continue;
        }
        WidgetCompanion companion = (WidgetCompanion) selectedElement.widget.getCompanionWidget();
        NlComponent component = (NlComponent) companion.getWidgetModel();
        components.add(component);
    }
    if (!components.isEmpty()) {
        selectionModel.setSelection(components);
    } else {
        selectionModel.clear();
    }
}
Also used : Selection(com.android.tools.sherpa.structure.Selection) WidgetCompanion(com.android.tools.sherpa.structure.WidgetCompanion) ArrayList(java.util.ArrayList)

Example 24 with WidgetCompanion

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

the class ConstraintModel method createSolverWidgetFromComponent.

/**
   * Create the widget associated to a component if necessary.
   *
   * @param component the component we want to represent
   * @return true if we need to save the XML
   */
private boolean createSolverWidgetFromComponent(@NotNull NlComponent component) {
    ConstraintWidget widget = myWidgetsScene.getWidget(component);
    boolean saveXML = false;
    if (widget != null && isConstraintLayout(component)) {
        if (!(widget instanceof ConstraintWidgetContainer)) {
            if (widget instanceof WidgetContainer) {
                ConstraintWidgetContainer container = new ConstraintWidgetContainer();
                myWidgetsScene.transformContainerToContainer((WidgetContainer) widget, container);
                saveXML = setupConstraintWidget(component, container);
                widget = container;
            } else {
                myWidgetsScene.removeWidget(widget);
                widget = null;
            }
        }
    }
    if (widget == null) {
        boolean dropWidget = false;
        if (USE_GUIDELINES_DURING_DND) {
            if (myDragDropWidget != null) {
                WidgetCompanion companion = (WidgetCompanion) myDragDropWidget.getCompanionWidget();
                if (companion.getWidgetModel() == component) {
                    widget = myDragDropWidget;
                    if (component.isOrHasSuperclass(CLASS_VIEWGROUP) || isDataBindingLayout(component)) {
                        widget = new WidgetContainer();
                    }
                    widget.setCompanionWidget(null);
                    dropWidget = true;
                }
            }
        }
        if (widget == null) {
            if (isConstraintLayout(component)) {
                widget = new ConstraintWidgetContainer();
            } else if (component.getTagName().equalsIgnoreCase(SdkConstants.CONSTRAINT_LAYOUT_GUIDELINE)) {
                widget = new Guideline();
                String orientation = component.getAttribute(SdkConstants.NS_RESOURCES, SdkConstants.ATTR_ORIENTATION);
                if (orientation != null) {
                    if (SdkConstants.ATTR_GUIDELINE_ORIENTATION_VERTICAL.equalsIgnoreCase(orientation)) {
                        Guideline guideline = (Guideline) widget;
                        guideline.setOrientation(Guideline.VERTICAL);
                    }
                }
            } else {
                if (component.isOrHasSuperclass(CLASS_VIEWGROUP) || isDataBindingLayout(component) || component.getChildCount() > 0) {
                    widget = new WidgetContainer();
                } else {
                    widget = new ConstraintWidget();
                }
            }
        }
        saveXML |= setupConstraintWidget(component, widget);
        myWidgetsScene.setWidget(widget);
        if (USE_GUIDELINES_DURING_DND) {
            if (dropWidget) {
                connectDroppedWidget();
                myDragDropWidget = null;
                mySelection.add(widget);
            }
        }
    }
    for (NlComponent child : component.getChildren()) {
        saveXML |= createSolverWidgetFromComponent(child);
    }
    return saveXML;
}
Also used : WidgetCompanion(com.android.tools.sherpa.structure.WidgetCompanion)

Example 25 with WidgetCompanion

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

the class ConstraintUtilities method setConnection.

/**
   * Update the attributes given a new anchor
   *
   * @param attributes the attributes we work on
   * @param anchor     the anchor we want to update from
   */
static void setConnection(@NotNull NlAttributesHolder attributes, @NotNull ConstraintAnchor anchor) {
    resetAnchor(attributes, anchor.getType());
    String attribute = getConnectionAttribute(anchor, anchor.getTarget());
    String marginAttribute = getConnectionAttributeMargin(anchor);
    String marginAttributeRtl = getConnectionRtlAttributeMargin(anchor);
    if (anchor.isConnected() && attribute != null) {
        ConstraintWidget owner = anchor.getOwner();
        WidgetCompanion ownerCompanion = (WidgetCompanion) owner.getCompanionWidget();
        ConstraintWidget target = anchor.getTarget().getOwner();
        WidgetCompanion companion = (WidgetCompanion) target.getCompanionWidget();
        NlComponent targetComponent = (NlComponent) companion.getWidgetModel();
        String targetId;
        boolean use_parent_ref = useParentReference(targetComponent.getModel());
        if (owner.getParent() == target && use_parent_ref) {
            targetId = SdkConstants.ATTR_PARENT;
        } else {
            targetId = SdkConstants.NEW_ID_PREFIX + targetComponent.ensureId();
        }
        attributes.setAttribute(SdkConstants.SHERPA_URI, attribute, targetId);
        String margin = ownerCompanion.getWidgetProperties().getMarginValue(anchor);
        String marginRtl = ownerCompanion.getWidgetProperties().getMarginRtlValue(anchor);
        String marginValue = null;
        if (anchor.getMargin() > 0) {
            marginValue = String.format(SdkConstants.VALUE_N_DP, anchor.getMargin());
        }
        if (margin != null && !ownerCompanion.getWidgetProperties().isMarginReference(anchor)) {
            margin = marginValue;
        }
        if (marginRtl != null && !ownerCompanion.getWidgetProperties().isMarginRtlReference(anchor)) {
            marginRtl = marginValue;
        }
        NlModel model = targetComponent.getModel();
        if (supportsStartEnd(anchor, model)) {
            // If we need to set RTL attributes
            if (marginRtl == null) {
                if (margin != null) {
                    marginRtl = margin;
                } else {
                    marginRtl = marginValue;
                }
            }
            if (requiresRightLeft(model)) {
                // If in addition to RTL attribute we need right/left...
                if (margin == null) {
                    margin = marginRtl;
                }
            }
        } else if (margin == null) {
            margin = marginValue;
        }
        if (marginAttribute != null && margin != null) {
            attributes.setAttribute(SdkConstants.NS_RESOURCES, marginAttribute, margin);
        }
        if (marginAttributeRtl != null && marginRtl != null) {
            attributes.setAttribute(SdkConstants.NS_RESOURCES, marginAttributeRtl, marginRtl);
        }
        String attributeCreator = getConnectionAttributeCreator(anchor);
        if (anchor.getConnectionCreator() != 0) {
            attributes.setAttribute(SdkConstants.TOOLS_URI, attributeCreator, String.valueOf(anchor.getConnectionCreator()));
        } else {
            attributes.setAttribute(SdkConstants.TOOLS_URI, attributeCreator, null);
        }
    }
}
Also used : 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