Search in sources :

Example 1 with SceneComponent

use of com.android.tools.idea.uibuilder.scene.SceneComponent in project android by JetBrains.

the class ConstraintUtilities method updateWidgetFromComponent.

/**
   * Update the constraint widget with the component information (coming from XML)
   *
   * @param scene           the scene of components. Can be null.
   * @param constraintModel the constraint model we are working with
   * @param widget          constraint widget
   * @param component       the model component
   * @return true if need to save the xml
   */
static boolean updateWidgetFromComponent(@Nullable Scene scene, @NotNull ConstraintModel constraintModel, @Nullable ConstraintWidget widget, @Nullable NlComponent component) {
    if (component == null || widget == null) {
        return false;
    }
    if (!(widget instanceof Guideline)) {
        widget.setVisibility(component.getAndroidViewVisibility());
    }
    widget.setDebugName(component.getId());
    Insets padding = component.getPadding(true);
    WidgetsScene widgetsScene = constraintModel.getScene();
    if (scene != null) {
        // If the scene exists, use the bounds from it instead of from the NlComponent.
        // This gives us animation on layout changes.
        // Note: this is temporary, once the Scene interaction / painting is fully done we'll switch to it.
        long time = System.currentTimeMillis();
        SceneComponent sceneComponent = scene.getSceneComponent(component);
        if (sceneComponent != null) {
            widget.setDrawOrigin(sceneComponent.getDrawX(time), sceneComponent.getDrawY(time));
            int w = sceneComponent.getDrawWidth(time);
            int h = sceneComponent.getDrawHeight(time);
            widget.setDimension(w, h);
            return false;
        }
    }
    if (widget instanceof ConstraintWidgetContainer) {
        int paddingLeft = constraintModel.pxToDp(padding.left);
        int paddingTop = constraintModel.pxToDp(padding.top);
        int paddingRight = constraintModel.pxToDp(padding.right);
        int paddingBottom = constraintModel.pxToDp(padding.bottom);
        ((ConstraintWidgetContainer) widget).setPadding(paddingLeft, paddingTop, paddingRight, paddingBottom);
        widget.setDimension(constraintModel.pxToDp(component.w) - paddingLeft - paddingRight, constraintModel.pxToDp(component.h) - paddingTop - paddingBottom);
    } else {
        widget.setDimension(constraintModel.pxToDp(component.w), constraintModel.pxToDp(component.h));
    }
    String absoluteWidth = component.getAttribute(SdkConstants.TOOLS_URI, ConvertToConstraintLayoutAction.ATTR_LAYOUT_CONVERSION_ABSOLUTE_WIDTH);
    if (absoluteWidth != null) {
        Configuration configuration = component.getModel().getConfiguration();
        ResourceResolver resourceResolver = configuration.getResourceResolver();
        int size = ViewEditor.resolveDimensionPixelSize(resourceResolver, absoluteWidth, configuration);
        size = constraintModel.pxToDp(size);
        widget.setWidth(size);
    }
    String absoluteHeight = component.getAttribute(SdkConstants.TOOLS_URI, ConvertToConstraintLayoutAction.ATTR_LAYOUT_CONVERSION_ABSOLUTE_HEIGHT);
    if (absoluteHeight != null) {
        Configuration configuration = component.getModel().getConfiguration();
        ResourceResolver resourceResolver = configuration.getResourceResolver();
        int size = ViewEditor.resolveDimensionPixelSize(resourceResolver, absoluteHeight, configuration);
        size = constraintModel.pxToDp(size);
        widget.setHeight(size);
    }
    widget.setMinWidth(constraintModel.pxToDp(component.getMinimumWidth()));
    widget.setMinHeight(constraintModel.pxToDp(component.getMinimumHeight()));
    NlComponent parent = component.getParent();
    NlModel model = component.getModel();
    if (parent != null) {
        ConstraintWidget parentWidget = widgetsScene.getWidget(parent);
        if (parentWidget instanceof WidgetContainer) {
            WidgetContainer parentContainerWidget = (WidgetContainer) parentWidget;
            if (widget.getParent() != parentContainerWidget) {
                parentContainerWidget.add(widget);
            }
        }
    }
    // First set the origin of the widget
    int x = constraintModel.pxToDp(component.x);
    int y = constraintModel.pxToDp(component.y);
    if (widget instanceof ConstraintWidgetContainer) {
        x += constraintModel.pxToDp(padding.left);
        y += constraintModel.pxToDp(padding.top);
    }
    WidgetContainer parentContainer = (WidgetContainer) widget.getParent();
    if (parentContainer != null) {
        if (!(parentContainer instanceof ConstraintWidgetContainer)) {
            x = constraintModel.pxToDp(component.x - component.getParent().x);
            y = constraintModel.pxToDp(component.y - component.getParent().y);
        } else {
            x -= parentContainer.getDrawX();
            y -= parentContainer.getDrawY();
        }
    }
    String absoluteX = component.getAttribute(SdkConstants.TOOLS_URI, ConvertToConstraintLayoutAction.ATTR_LAYOUT_CONVERSION_ABSOLUTE_X);
    if (absoluteX != null) {
        Configuration configuration = component.getModel().getConfiguration();
        ResourceResolver resourceResolver = configuration.getResourceResolver();
        int position = ViewEditor.resolveDimensionPixelSize(resourceResolver, absoluteX, configuration);
        x = constraintModel.pxToDp(position);
    }
    String absoluteY = component.getAttribute(SdkConstants.TOOLS_URI, ConvertToConstraintLayoutAction.ATTR_LAYOUT_CONVERSION_ABSOLUTE_Y);
    if (absoluteY != null) {
        Configuration configuration = component.getModel().getConfiguration();
        ResourceResolver resourceResolver = configuration.getResourceResolver();
        int position = ViewEditor.resolveDimensionPixelSize(resourceResolver, absoluteY, configuration);
        y = constraintModel.pxToDp(position);
    }
    if (scene == null) {
        if (widget.getX() != x || widget.getY() != y) {
            widget.setOrigin(x, y);
            widget.forceUpdateDrawPosition();
        }
    }
    boolean overrideDimension = false;
    // FIXME: need to agree on the correct magic value for this rather than simply using zero.
    String layout_width = component.getAttribute(SdkConstants.ANDROID_URI, SdkConstants.ATTR_LAYOUT_WIDTH);
    if (component.w == 0 || getLayoutDimensionDpValue(component, layout_width) == 0) {
        widget.setHorizontalDimensionBehaviour(ConstraintWidget.DimensionBehaviour.MATCH_CONSTRAINT);
    } else if (layout_width != null && layout_width.equalsIgnoreCase(SdkConstants.VALUE_WRAP_CONTENT)) {
        if (APPLY_MINIMUM_SIZE && widget.getWidth() < MINIMUM_SIZE && widget instanceof WidgetContainer && ((WidgetContainer) widget).getChildren().size() == 0) {
            widget.setWidth(MINIMUM_SIZE);
            widget.setHorizontalDimensionBehaviour(ConstraintWidget.DimensionBehaviour.FIXED);
            overrideDimension = true;
        } else {
            widget.setWrapWidth(widget.getWidth());
            widget.setHorizontalDimensionBehaviour(ConstraintWidget.DimensionBehaviour.WRAP_CONTENT);
        }
    } else if (layout_width != null && layout_width.equalsIgnoreCase(SdkConstants.VALUE_MATCH_PARENT)) {
        if (isWidgetInsideConstraintLayout(widget)) {
            if (widget.getAnchor(ConstraintAnchor.Type.LEFT).isConnected() && widget.getAnchor(ConstraintAnchor.Type.RIGHT).isConnected()) {
                widget.setHorizontalDimensionBehaviour(ConstraintWidget.DimensionBehaviour.MATCH_CONSTRAINT);
            } else {
                widget.setHorizontalDimensionBehaviour(ConstraintWidget.DimensionBehaviour.FIXED);
                widget.setWidth(MINIMUM_SIZE_EXPAND);
                int height = widget.getHeight();
                ConstraintWidget.DimensionBehaviour verticalBehaviour = widget.getVerticalDimensionBehaviour();
                if (height <= 1 && widget instanceof WidgetContainer) {
                    widget.setHeight(MINIMUM_SIZE_EXPAND);
                }
                ArrayList<ConstraintWidget> widgets = new ArrayList<>();
                widgets.add(widget);
                Scout.arrangeWidgets(Scout.Arrange.ExpandHorizontally, widgets, true);
                widget.setHeight(height);
                widget.setVerticalDimensionBehaviour(verticalBehaviour);
                overrideDimension = true;
            }
        }
    } else {
        widget.setHorizontalDimensionBehaviour(ConstraintWidget.DimensionBehaviour.FIXED);
    }
    String layout_height = component.getAttribute(SdkConstants.ANDROID_URI, SdkConstants.ATTR_LAYOUT_HEIGHT);
    if (component.h == 0 || getLayoutDimensionDpValue(component, layout_height) == 0) {
        widget.setVerticalDimensionBehaviour(ConstraintWidget.DimensionBehaviour.MATCH_CONSTRAINT);
    } else if (layout_height != null && layout_height.equalsIgnoreCase(SdkConstants.VALUE_WRAP_CONTENT)) {
        if (APPLY_MINIMUM_SIZE && widget.getHeight() < MINIMUM_SIZE && widget instanceof WidgetContainer && ((WidgetContainer) widget).getChildren().size() == 0) {
            widget.setHeight(MINIMUM_SIZE);
            widget.setVerticalDimensionBehaviour(ConstraintWidget.DimensionBehaviour.FIXED);
            overrideDimension = true;
        } else {
            widget.setWrapHeight(widget.getHeight());
            widget.setVerticalDimensionBehaviour(ConstraintWidget.DimensionBehaviour.WRAP_CONTENT);
        }
    } else if (layout_height != null && layout_height.equalsIgnoreCase(SdkConstants.VALUE_MATCH_PARENT)) {
        if (isWidgetInsideConstraintLayout(widget)) {
            if ((widget.getAnchor(ConstraintAnchor.Type.TOP).isConnected() && widget.getAnchor(ConstraintAnchor.Type.BOTTOM).isConnected())) {
                widget.setVerticalDimensionBehaviour(ConstraintWidget.DimensionBehaviour.MATCH_CONSTRAINT);
            } else {
                widget.setVerticalDimensionBehaviour(ConstraintWidget.DimensionBehaviour.FIXED);
                widget.setHeight(MINIMUM_SIZE_EXPAND);
                int width = widget.getWidth();
                ConstraintWidget.DimensionBehaviour horizontalBehaviour = widget.getHorizontalDimensionBehaviour();
                if (width <= 1 && widget instanceof WidgetContainer) {
                    widget.setWidth(MINIMUM_SIZE_EXPAND);
                }
                ArrayList<ConstraintWidget> widgets = new ArrayList<>();
                widgets.add(widget);
                Scout.arrangeWidgets(Scout.Arrange.ExpandVertically, widgets, true);
                widget.setWidth(width);
                widget.setHorizontalDimensionBehaviour(horizontalBehaviour);
                overrideDimension = true;
            }
        }
    } else {
        widget.setVerticalDimensionBehaviour(ConstraintWidget.DimensionBehaviour.FIXED);
    }
    widget.setBaselineDistance(constraintModel.pxToDp(component.getBaseline()));
    widget.resetAnchors();
    String left1 = component.getAttribute(SdkConstants.SHERPA_URI, SdkConstants.ATTR_LAYOUT_LEFT_TO_LEFT_OF);
    String left2 = component.getAttribute(SdkConstants.SHERPA_URI, SdkConstants.ATTR_LAYOUT_LEFT_TO_RIGHT_OF);
    String right1 = component.getAttribute(SdkConstants.SHERPA_URI, SdkConstants.ATTR_LAYOUT_RIGHT_TO_LEFT_OF);
    String right2 = component.getAttribute(SdkConstants.SHERPA_URI, SdkConstants.ATTR_LAYOUT_RIGHT_TO_RIGHT_OF);
    String top1 = component.getAttribute(SdkConstants.SHERPA_URI, SdkConstants.ATTR_LAYOUT_TOP_TO_TOP_OF);
    String top2 = component.getAttribute(SdkConstants.SHERPA_URI, SdkConstants.ATTR_LAYOUT_TOP_TO_BOTTOM_OF);
    String bottom1 = component.getAttribute(SdkConstants.SHERPA_URI, SdkConstants.ATTR_LAYOUT_BOTTOM_TO_TOP_OF);
    String bottom2 = component.getAttribute(SdkConstants.SHERPA_URI, SdkConstants.ATTR_LAYOUT_BOTTOM_TO_BOTTOM_OF);
    String baseline = component.getAttribute(SdkConstants.SHERPA_URI, SdkConstants.ATTR_LAYOUT_BASELINE_TO_BASELINE_OF);
    String ratio = component.getAttribute(SdkConstants.SHERPA_URI, SdkConstants.ATTR_LAYOUT_DIMENSION_RATIO);
    WidgetCompanion companion = (WidgetCompanion) widget.getCompanionWidget();
    companion.getWidgetProperties().clear();
    companion.getWidgetProperties().setDimensionRatio(ratio);
    setMarginType(ConstraintAnchor.Type.LEFT, component, widget);
    setMarginType(ConstraintAnchor.Type.RIGHT, component, widget);
    setMarginType(ConstraintAnchor.Type.TOP, component, widget);
    setMarginType(ConstraintAnchor.Type.BOTTOM, component, widget);
    setTarget(model, widgetsScene, left1, widget, ConstraintAnchor.Type.LEFT, ConstraintAnchor.Type.LEFT);
    setStartMargin(left1, component, widget);
    setTarget(model, widgetsScene, left2, widget, ConstraintAnchor.Type.LEFT, ConstraintAnchor.Type.RIGHT);
    setStartMargin(left2, component, widget);
    setTarget(model, widgetsScene, right1, widget, ConstraintAnchor.Type.RIGHT, ConstraintAnchor.Type.LEFT);
    setEndMargin(right1, component, widget);
    setTarget(model, widgetsScene, right2, widget, ConstraintAnchor.Type.RIGHT, ConstraintAnchor.Type.RIGHT);
    setEndMargin(right2, component, widget);
    setTarget(model, widgetsScene, top1, widget, ConstraintAnchor.Type.TOP, ConstraintAnchor.Type.TOP);
    setTopMargin(top1, component, widget);
    setTarget(model, widgetsScene, top2, widget, ConstraintAnchor.Type.TOP, ConstraintAnchor.Type.BOTTOM);
    setTopMargin(top2, component, widget);
    setTarget(model, widgetsScene, bottom1, widget, ConstraintAnchor.Type.BOTTOM, ConstraintAnchor.Type.TOP);
    setBottomMargin(bottom1, component, widget);
    setTarget(model, widgetsScene, bottom2, widget, ConstraintAnchor.Type.BOTTOM, ConstraintAnchor.Type.BOTTOM);
    setBottomMargin(bottom2, component, widget);
    setTarget(model, widgetsScene, baseline, widget, ConstraintAnchor.Type.BASELINE, ConstraintAnchor.Type.BASELINE);
    setBias(SdkConstants.ATTR_LAYOUT_HORIZONTAL_BIAS, component, widget);
    setBias(SdkConstants.ATTR_LAYOUT_VERTICAL_BIAS, component, widget);
    setDimensionRatio(SdkConstants.ATTR_LAYOUT_DIMENSION_RATIO, component, widget);
    setChainStyle(SdkConstants.ATTR_LAYOUT_HORIZONTAL_CHAIN_STYLE, component, widget);
    setChainStyle(SdkConstants.ATTR_LAYOUT_VERTICAL_CHAIN_STYLE, component, widget);
    setChainWeight(SdkConstants.ATTR_LAYOUT_HORIZONTAL_WEIGHT, component, widget);
    setChainWeight(SdkConstants.ATTR_LAYOUT_VERTICAL_WEIGHT, component, widget);
    if (widget instanceof Guideline) {
        Guideline guideline = (Guideline) widget;
        setGuideline(component, guideline);
    }
    // Update text decorator
    WidgetDecorator decorator = companion.getWidgetDecorator(WidgetDecorator.BLUEPRINT_STYLE);
    if (decorator != null && decorator instanceof TextWidget) {
        TextWidget textWidget = (TextWidget) decorator;
        textWidget.setText(getResolvedText(component));
        Configuration configuration = component.getModel().getConfiguration();
        ResourceResolver resourceResolver = configuration.getResourceResolver();
        Integer size = null;
        if (resourceResolver != null) {
            String textSize = component.getAttribute(SdkConstants.ANDROID_URI, SdkConstants.ATTR_TEXT_SIZE);
            if (textSize != null) {
                size = ViewEditor.resolveDimensionPixelSize(resourceResolver, textSize, configuration);
            }
        }
        if (size == null) {
            // With the specified string, this method cannot return null
            //noinspection ConstantConditions
            size = ViewEditor.resolveDimensionPixelSize(resourceResolver, "15sp", configuration);
        }
        String alignment = component.getAttribute(SdkConstants.ANDROID_URI, SdkConstants.ATTR_TEXT_ALIGNMENT);
        textWidget.setTextAlignment((alignment == null) ? TextWidget.TEXT_ALIGNMENT_VIEW_START : alignmentMap.get(alignment));
        String single = component.getAttribute(SdkConstants.ANDROID_URI, SdkConstants.ATTR_SINGLE_LINE);
        textWidget.setSingleLine(Boolean.parseBoolean(single));
        // Cannot be null, see previous condition
        //noinspection ConstantConditions
        textWidget.setTextSize(constraintModel.pxToDp(size));
    }
    // if true, need to update the XML
    return overrideDimension;
}
Also used : Configuration(com.android.tools.idea.configurations.Configuration) WidgetCompanion(com.android.tools.sherpa.structure.WidgetCompanion) ArrayList(java.util.ArrayList) WidgetDecorator(com.android.tools.sherpa.drawing.decorator.WidgetDecorator) WidgetsScene(com.android.tools.sherpa.structure.WidgetsScene) TextWidget(com.android.tools.sherpa.drawing.decorator.TextWidget) SceneComponent(com.android.tools.idea.uibuilder.scene.SceneComponent) ResourceResolver(com.android.ide.common.resources.ResourceResolver)

Example 2 with SceneComponent

use of com.android.tools.idea.uibuilder.scene.SceneComponent in project android by JetBrains.

the class SceneDecorator method buildListChildren.

/**
   * This is responsible for setting the clip and building the list for this component's children
   *
   * @param list
   * @param time
   * @param sceneContext
   * @param component
   */
protected void buildListChildren(@NotNull DisplayList list, long time, @NotNull SceneContext sceneContext, @NotNull SceneComponent component) {
    ArrayList<SceneComponent> children = component.getChildren();
    if (children.size() > 0) {
        Rectangle rect = new Rectangle();
        component.fillRect(rect);
        DisplayList.UNClip unClip = list.addClip(sceneContext, rect);
        for (SceneComponent child : children) {
            child.buildDisplayList(time, list, sceneContext);
        }
        list.add(unClip);
    }
}
Also used : DisplayList(com.android.tools.idea.uibuilder.scene.draw.DisplayList) SceneComponent(com.android.tools.idea.uibuilder.scene.SceneComponent)

Example 3 with SceneComponent

use of com.android.tools.idea.uibuilder.scene.SceneComponent in project android by JetBrains.

the class GuidelineAnchorTarget method layout.

@Override
public boolean layout(@NotNull SceneContext sceneTransform, int l, int t, int r, int b) {
    int dist = 4;
    SceneComponent parent = myComponent.getParent();
    if (myIsHorizontal) {
        myLeft = parent.getDrawX();
        myTop = t - dist;
        myRight = myLeft + parent.getDrawWidth();
        myBottom = t + dist;
    } else {
        myLeft = l - dist;
        myTop = parent.getDrawY();
        myRight = l + dist;
        myBottom = myTop + parent.getDrawHeight();
    }
    return false;
}
Also used : SceneComponent(com.android.tools.idea.uibuilder.scene.SceneComponent)

Example 4 with SceneComponent

use of com.android.tools.idea.uibuilder.scene.SceneComponent in project android by JetBrains.

the class GuidelineCycleTarget method mouseRelease.

@Override
public void mouseRelease(int x, int y, @Nullable Target closestTarget) {
    AttributesTransaction attributes = myComponent.getNlComponent().startAttributeTransaction();
    String begin = attributes.getAttribute(SdkConstants.SHERPA_URI, SdkConstants.LAYOUT_CONSTRAINT_GUIDE_BEGIN);
    String end = attributes.getAttribute(SdkConstants.SHERPA_URI, SdkConstants.LAYOUT_CONSTRAINT_GUIDE_END);
    String percent = attributes.getAttribute(SdkConstants.SHERPA_URI, SdkConstants.LAYOUT_CONSTRAINT_GUIDE_PERCENT);
    SceneComponent parent = myComponent.getParent();
    int value = myComponent.getDrawY() - parent.getDrawY();
    int dimension = parent.getDrawHeight();
    if (!myIsHorizontal) {
        value = myComponent.getDrawX() - parent.getDrawX();
        dimension = parent.getDrawWidth();
    }
    if (begin != null) {
        setEnd(attributes, dimension - value);
    } else if (end != null) {
        setPercent(attributes, value / (float) dimension);
    } else if (percent != null) {
        setBegin(attributes, value);
    }
    attributes.apply();
    NlModel nlModel = myComponent.getNlComponent().getModel();
    Project project = nlModel.getProject();
    XmlFile file = nlModel.getFile();
    String label = "Cycle Guideline";
    WriteCommandAction action = new WriteCommandAction(project, label, file) {

        @Override
        protected void run(@NotNull Result result) throws Throwable {
            attributes.commit();
        }
    };
    action.execute();
}
Also used : WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) Project(com.intellij.openapi.project.Project) XmlFile(com.intellij.psi.xml.XmlFile) SceneComponent(com.android.tools.idea.uibuilder.scene.SceneComponent) NlModel(com.android.tools.idea.uibuilder.model.NlModel) NotNull(org.jetbrains.annotations.NotNull) AttributesTransaction(com.android.tools.idea.uibuilder.model.AttributesTransaction) Result(com.intellij.openapi.application.Result)

Example 5 with SceneComponent

use of com.android.tools.idea.uibuilder.scene.SceneComponent in project android by JetBrains.

the class GuidelineTarget method render.

@Override
public void render(@NotNull DisplayList list, @NotNull SceneContext sceneContext) {
    if (myIsHorizontal) {
        int y = (int) (myTop + (myBottom - myTop) / 2);
        SceneComponent parent = myComponent.getParent();
        DrawHorizontalGuideline.add(list, sceneContext, myLeft, y, myRight, parent.getDrawX(), parent.getDrawY(), parent.getDrawHeight(), myBegin, myEnd, myPercent, myComponent.isSelected());
    } else {
        int x = (int) (myLeft + (myRight - myLeft) / 2);
        SceneComponent parent = myComponent.getParent();
        DrawVerticalGuideline.add(list, sceneContext, x, myTop, myBottom, parent.getDrawX(), parent.getDrawY(), parent.getDrawWidth(), myBegin, myEnd, myPercent, myComponent.isSelected());
    }
}
Also used : SceneComponent(com.android.tools.idea.uibuilder.scene.SceneComponent)

Aggregations

SceneComponent (com.android.tools.idea.uibuilder.scene.SceneComponent)11 NlComponent (com.android.tools.idea.uibuilder.model.NlComponent)2 ResourceResolver (com.android.ide.common.resources.ResourceResolver)1 Configuration (com.android.tools.idea.configurations.Configuration)1 AttributesTransaction (com.android.tools.idea.uibuilder.model.AttributesTransaction)1 NlModel (com.android.tools.idea.uibuilder.model.NlModel)1 DisplayList (com.android.tools.idea.uibuilder.scene.draw.DisplayList)1 TextWidget (com.android.tools.sherpa.drawing.decorator.TextWidget)1 WidgetDecorator (com.android.tools.sherpa.drawing.decorator.WidgetDecorator)1 WidgetCompanion (com.android.tools.sherpa.structure.WidgetCompanion)1 WidgetsScene (com.android.tools.sherpa.structure.WidgetsScene)1 Result (com.intellij.openapi.application.Result)1 WriteCommandAction (com.intellij.openapi.command.WriteCommandAction)1 Project (com.intellij.openapi.project.Project)1 XmlFile (com.intellij.psi.xml.XmlFile)1 ArrayList (java.util.ArrayList)1 NotNull (org.jetbrains.annotations.NotNull)1