Search in sources :

Example 1 with NlGraphics

use of com.android.tools.idea.uibuilder.graphics.NlGraphics 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 NlGraphics

use of com.android.tools.idea.uibuilder.graphics.NlGraphics in project android by JetBrains.

the class PreferenceScreenDragHandlerTest method drawDropPreviewLine.

@Test
public void drawDropPreviewLine() {
    PreferenceGroupDragHandler handler = newPreferenceScreenDragHandler(newPreferenceScreen());
    NlGraphics graphics = Mockito.mock(NlGraphics.class);
    handler.update(360, 690, 0);
    handler.drawDropPreviewLine(graphics);
    handler.update(360, 740, 0);
    handler.drawDropPreviewLine(graphics);
    Mockito.verify(graphics).drawBottom(new Rectangle(0, 607, 768, 104));
    Mockito.verify(graphics).drawTop(new Rectangle(0, 711, 768, 104));
}
Also used : NlGraphics(com.android.tools.idea.uibuilder.graphics.NlGraphics) Test(org.junit.Test)

Example 3 with NlGraphics

use of com.android.tools.idea.uibuilder.graphics.NlGraphics 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 4 with NlGraphics

use of com.android.tools.idea.uibuilder.graphics.NlGraphics in project android by JetBrains.

the class RelativeLayoutHandler method paintConstraints.

@Override
public boolean paintConstraints(@NotNull ScreenView screenView, @NotNull Graphics2D graphics, @NotNull NlComponent component) {
    NlGraphics g = new NlGraphics(graphics, screenView);
    Iterable<NlComponent> iterable = component.getChildren();
    List<NlComponent> children = Lists.newArrayList(iterable);
    ConstraintPainter.paintSelectionFeedback(g, component, children, true, TextDirection.LEFT_TO_RIGHT);
    return false;
}
Also used : NlGraphics(com.android.tools.idea.uibuilder.graphics.NlGraphics) NlComponent(com.android.tools.idea.uibuilder.model.NlComponent)

Example 5 with NlGraphics

use of com.android.tools.idea.uibuilder.graphics.NlGraphics 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)

Aggregations

NlGraphics (com.android.tools.idea.uibuilder.graphics.NlGraphics)8 NlComponent (com.android.tools.idea.uibuilder.model.NlComponent)6 Test (org.junit.Test)4 AndroidCoordinate (com.android.tools.idea.uibuilder.model.AndroidCoordinate)3 NotNull (org.jetbrains.annotations.NotNull)3 Nullable (org.jetbrains.annotations.Nullable)3 com.android.tools.idea.uibuilder.api (com.android.tools.idea.uibuilder.api)1 SegmentType (com.android.tools.idea.uibuilder.model.SegmentType)1 TextDirection (com.android.tools.idea.uibuilder.model.TextDirection)1 ScreenView (com.android.tools.idea.uibuilder.surface.ScreenView)1 Lists (com.google.common.collect.Lists)1 java.awt (java.awt)1 List (java.util.List)1 Collectors (java.util.stream.Collectors)1