Search in sources :

Example 1 with AndroidCoordinate

use of com.android.tools.idea.uibuilder.model.AndroidCoordinate in project android by JetBrains.

the class AbsoluteLayoutHandler method createDragHandler.

@Override
@Nullable
public DragHandler createDragHandler(@NotNull ViewEditor editor, @NotNull NlComponent layout, @NotNull List<NlComponent> components, @NotNull DragType type) {
    return new DragHandler(editor, this, layout, components, type) {

        @Override
        public void paint(@NotNull NlGraphics graphics) {
            int deltaX = lastX - startX;
            int deltaY = lastY - startY;
            for (NlComponent component : components) {
                int x = component.x + deltaX;
                int y = component.y + deltaY;
                int w = component.w;
                int h = component.h;
                graphics.useStyle(NlDrawingStyle.DROP_PREVIEW);
                graphics.drawRect(x, y, w, h);
            }
        }

        @Override
        public void commit(@AndroidCoordinate int x, @AndroidCoordinate int y, int modifiers, @NotNull InsertType insertType) {
            // TODO: Remove all existing layout parameters; if you're dragging from one layout type to another, you don't
            // want stale layout parameters (e.g. layout_alignLeft from a previous RelativeLayout in a new GridLayout, and so on.)
            int deltaX = x - startX;
            int deltaY = y - startY;
            for (NlComponent component : components) {
                component.setAttribute(ANDROID_URI, ATTR_LAYOUT_X, editor.pxToDpWithUnits(component.x - layout.x + deltaX));
                component.setAttribute(ANDROID_URI, ATTR_LAYOUT_Y, editor.pxToDpWithUnits(component.y - layout.y + deltaY));
            }
            insertComponents(-1, insertType);
        }
    };
}
Also used : AndroidCoordinate(com.android.tools.idea.uibuilder.model.AndroidCoordinate) NlGraphics(com.android.tools.idea.uibuilder.graphics.NlGraphics) NlComponent(com.android.tools.idea.uibuilder.model.NlComponent) NotNull(org.jetbrains.annotations.NotNull) Nullable(org.jetbrains.annotations.Nullable)

Example 2 with AndroidCoordinate

use of com.android.tools.idea.uibuilder.model.AndroidCoordinate in project android by JetBrains.

the class RelativeLayoutHandler method createDragHandler.

@Override
@Nullable
public DragHandler createDragHandler(@NotNull ViewEditor editor, @NotNull NlComponent layout, @NotNull List<NlComponent> components, @NotNull DragType type) {
    final RelativeDragHandler moveHandler = new RelativeDragHandler(editor, layout, components);
    return new DragHandler(editor, this, layout, components, type) {

        @Nullable
        @Override
        public String update(@AndroidCoordinate int x, @AndroidCoordinate int y, int modifiers) {
            super.update(x, y, modifiers);
            NlComponent primary = components.get(0);
            int deltaX = lastX - startX;
            int deltaY = lastY - startY;
            moveHandler.updateMove(primary, deltaX, deltaY, modifiers);
            return null;
        }

        @Override
        public void paint(@NotNull NlGraphics graphics) {
            GuidelinePainter.paint(graphics, moveHandler);
        }

        @Override
        public void commit(@AndroidCoordinate int x, @AndroidCoordinate int y, int modifiers, @NotNull InsertType insertType) {
            moveHandler.removeCycles();
            NlComponent previous = null;
            for (NlComponent component : components) {
                if (previous == null) {
                    moveHandler.applyConstraints(component);
                } else {
                    // Arrange the nodes next to each other, depending on which
                    // edge we are attaching to. For example, if attaching to the
                    // top edge, arrange the subsequent nodes in a column below it.
                    //
                    // TODO: Try to do something smarter here where we detect
                    // constraints between the dragged edges, and we preserve these.
                    // We have to do this carefully though because if the
                    // constraints go through some other nodes not part of the
                    // selection, this doesn't work right, and you might be
                    // dragging several connected components, which we'd then
                    // need to stitch together such that they are all visible.
                    moveHandler.attachPrevious(previous, component);
                }
                previous = component;
            }
            insertAddedComponents(insertType);
        }

        private void insertAddedComponents(@NotNull InsertType insertType) {
            List<NlComponent> added = components.stream().filter(component -> component.getParent() != layout).collect(Collectors.toList());
            editor.getModel().addComponents(added, layout, null, insertType);
        }
    };
}
Also used : AndroidCoordinate(com.android.tools.idea.uibuilder.model.AndroidCoordinate) AndroidCoordinate(com.android.tools.idea.uibuilder.model.AndroidCoordinate) com.android.tools.idea.uibuilder.api(com.android.tools.idea.uibuilder.api) TextDirection(com.android.tools.idea.uibuilder.model.TextDirection) NlGraphics(com.android.tools.idea.uibuilder.graphics.NlGraphics) SegmentType(com.android.tools.idea.uibuilder.model.SegmentType) Collectors(java.util.stream.Collectors) java.awt(java.awt) Nullable(org.jetbrains.annotations.Nullable) List(java.util.List) Lists(com.google.common.collect.Lists) NlComponent(com.android.tools.idea.uibuilder.model.NlComponent) ScreenView(com.android.tools.idea.uibuilder.surface.ScreenView) NotNull(org.jetbrains.annotations.NotNull) NlGraphics(com.android.tools.idea.uibuilder.graphics.NlGraphics) NlComponent(com.android.tools.idea.uibuilder.model.NlComponent) NotNull(org.jetbrains.annotations.NotNull) Nullable(org.jetbrains.annotations.Nullable)

Example 3 with AndroidCoordinate

use of com.android.tools.idea.uibuilder.model.AndroidCoordinate in project android by JetBrains.

the class GridInfo method getSize.

@AndroidCoordinate
private Dimension getSize() {
    Dimension size = new Dimension();
    if (layout.children != null) {
        Insets padding = layout.getPadding();
        for (NlComponent child : layout.children) {
            size.width = Math.max(child.x - layout.x - padding.left + child.w, size.width);
            size.height = Math.max(child.y - layout.y - padding.top + child.h, size.height);
        }
    }
    return size;
}
Also used : Insets(com.android.tools.idea.uibuilder.model.Insets) NlComponent(com.android.tools.idea.uibuilder.model.NlComponent) AndroidCoordinate(com.android.tools.idea.uibuilder.model.AndroidCoordinate)

Example 4 with AndroidCoordinate

use of com.android.tools.idea.uibuilder.model.AndroidCoordinate in project android by JetBrains.

the class RelativeLayoutHandler method createResizeHandler.

@Override
@Nullable
public ResizeHandler createResizeHandler(@NotNull ViewEditor editor, @NotNull NlComponent component, @Nullable SegmentType horizontalEdgeType, @Nullable SegmentType verticalEdgeType) {
    NlComponent parent = component.getParent();
    if (parent == null) {
        return null;
    }
    final RelativeResizeHandler resizeHandler = new RelativeResizeHandler(editor, parent, component, horizontalEdgeType, verticalEdgeType);
    return new ResizeHandler(editor, this, component, horizontalEdgeType, verticalEdgeType) {

        @Nullable
        @Override
        public String update(@AndroidCoordinate int x, @AndroidCoordinate int y, int modifiers, @NotNull @AndroidCoordinate Rectangle newBounds) {
            super.update(x, y, modifiers, newBounds);
            resizeHandler.updateResize(component, newBounds, modifiers);
            return null;
        }

        @Override
        public void commit(@AndroidCoordinate int px, @AndroidCoordinate int py, int modifiers, @NotNull @AndroidCoordinate Rectangle newBounds) {
            resizeHandler.removeCycles();
            resizeHandler.applyConstraints(component);
        }

        @Override
        public void paint(@NotNull NlGraphics graphics) {
            GuidelinePainter.paint(graphics, resizeHandler);
        }
    };
}
Also used : AndroidCoordinate(com.android.tools.idea.uibuilder.model.AndroidCoordinate) NlGraphics(com.android.tools.idea.uibuilder.graphics.NlGraphics) NlComponent(com.android.tools.idea.uibuilder.model.NlComponent) NotNull(org.jetbrains.annotations.NotNull) Nullable(org.jetbrains.annotations.Nullable)

Example 5 with AndroidCoordinate

use of com.android.tools.idea.uibuilder.model.AndroidCoordinate in project android by JetBrains.

the class GridInfo method getAxisLocations.

@AndroidCoordinate
private int[] getAxisLocations(String name1, String name2) throws NoSuchFieldException, IllegalAccessException {
    assert layout.viewInfo != null;
    Object view = layout.viewInfo.getViewObject();
    Field axisField = getDeclaredField(view.getClass(), name1, name2);
    axisField.setAccessible(true);
    Object axis = axisField.get(view);
    Field locationsField = axis.getClass().getDeclaredField("locations");
    locationsField.setAccessible(true);
    return (int[]) locationsField.get(axis);
}
Also used : Field(java.lang.reflect.Field) AndroidCoordinate(com.android.tools.idea.uibuilder.model.AndroidCoordinate)

Aggregations

AndroidCoordinate (com.android.tools.idea.uibuilder.model.AndroidCoordinate)6 NlComponent (com.android.tools.idea.uibuilder.model.NlComponent)4 Nullable (org.jetbrains.annotations.Nullable)4 NlGraphics (com.android.tools.idea.uibuilder.graphics.NlGraphics)3 NotNull (org.jetbrains.annotations.NotNull)3 ScreenView (com.android.tools.idea.uibuilder.surface.ScreenView)2 com.android.tools.idea.uibuilder.api (com.android.tools.idea.uibuilder.api)1 DnDTransferComponent (com.android.tools.idea.uibuilder.model.DnDTransferComponent)1 DnDTransferItem (com.android.tools.idea.uibuilder.model.DnDTransferItem)1 Insets (com.android.tools.idea.uibuilder.model.Insets)1 ItemTransferable (com.android.tools.idea.uibuilder.model.ItemTransferable)1 SegmentType (com.android.tools.idea.uibuilder.model.SegmentType)1 TextDirection (com.android.tools.idea.uibuilder.model.TextDirection)1 Lists (com.google.common.collect.Lists)1 java.awt (java.awt)1 BufferedImage (java.awt.image.BufferedImage)1 Field (java.lang.reflect.Field)1 List (java.util.List)1 Collectors (java.util.stream.Collectors)1