Search in sources :

Example 51 with Container

use of com.codename1.ui.Container in project CodenameOne by codenameone.

the class Form method pointerPressed.

/**
 * {@inheritDoc}
 */
public void pointerPressed(int x, int y) {
    pressedCmp = null;
    stickyDrag = null;
    dragStopFlag = false;
    dragged = null;
    boolean isScrollWheeling = Display.INSTANCE.impl.isScrollWheeling();
    if (pointerPressedListeners != null && pointerPressedListeners.hasListeners()) {
        pointerPressedListeners.fireActionEvent(new ActionEvent(this, ActionEvent.Type.PointerPressed, x, y));
    }
    // check if the click is relevant to the menu bar.
    /*
        if (menuBar.contains(x, y)) {
            Component cmp = menuBar.getComponentAt(x, y);
            while (cmp != null && cmp.isIgnorePointerEvents()) {
                cmp = cmp.getParent();
            }
            if (cmp != null && cmp.isEnabled()) {
                cmp.pointerPressed(x, y);
                tactileTouchVibe(x, y, cmp);
                initRippleEffect(x, y, cmp);
            }
            return;
        }
        */
    Container actual = getActualPane(formLayeredPane, x, y);
    if (y >= actual.getY() && x >= actual.getX()) {
        Component cmp = actual.getComponentAt(x, y);
        while (cmp != null && cmp.isIgnorePointerEvents()) {
            cmp = cmp.getParent();
        }
        if (cmp != null) {
            cmp.initDragAndDrop(x, y);
            if (cmp.hasLead) {
                if (isCurrentlyScrolling(cmp)) {
                    dragStopFlag = true;
                    cmp.clearDrag();
                    return;
                }
                Container leadParent;
                if (cmp instanceof Container) {
                    leadParent = ((Container) cmp).getLeadParent();
                } else {
                    leadParent = cmp.getParent().getLeadParent();
                }
                leadParent.repaint();
                if (!isScrollWheeling) {
                    setFocused(leadParent);
                }
                pressedCmp = cmp.getLeadComponent();
                cmp.getLeadComponent().pointerPressed(x, y);
            } else {
                if (isCurrentlyScrolling(cmp)) {
                    dragStopFlag = true;
                    cmp.clearDrag();
                    return;
                }
                if (cmp.isEnabled()) {
                    if (!isScrollWheeling && cmp.isFocusable()) {
                        setFocused(cmp);
                    }
                    pressedCmp = cmp;
                    cmp.pointerPressed(x, y);
                    tactileTouchVibe(x, y, cmp);
                    initRippleEffect(x, y, cmp);
                }
            }
        }
    } else {
        if (y < actual.getY()) {
            Component cmp = getTitleArea().getComponentAt(x, y);
            while (cmp != null && cmp.isIgnorePointerEvents()) {
                cmp = cmp.getParent();
            }
            if (cmp != null && cmp.isEnabled() && cmp.isFocusable()) {
                pressedCmp = cmp;
                cmp.pointerPressed(x, y);
                tactileTouchVibe(x, y, cmp);
                initRippleEffect(x, y, cmp);
            }
        } else {
            Component cmp = ((BorderLayout) super.getLayout()).getWest();
            if (cmp != null) {
                cmp = ((Container) cmp).getComponentAt(x, y);
                while (cmp != null && cmp.isIgnorePointerEvents()) {
                    cmp = cmp.getParent();
                }
                if (cmp != null && cmp.isEnabled() && cmp.isFocusable()) {
                    if (cmp.hasLead) {
                        Container leadParent;
                        if (cmp instanceof Container) {
                            leadParent = ((Container) cmp).getLeadParent();
                        } else {
                            leadParent = cmp.getParent().getLeadParent();
                        }
                        if (!isScrollWheeling) {
                            setFocused(leadParent);
                        }
                        cmp = cmp.getLeadComponent();
                    }
                    cmp.initDragAndDrop(x, y);
                    pressedCmp = cmp;
                    cmp.pointerPressed(x, y);
                    tactileTouchVibe(x, y, cmp);
                    initRippleEffect(x, y, cmp);
                }
            }
        }
    }
    initialPressX = x;
    initialPressY = y;
}
Also used : BorderLayout(com.codename1.ui.layouts.BorderLayout) ActionEvent(com.codename1.ui.events.ActionEvent)

Example 52 with Container

use of com.codename1.ui.Container in project CodenameOne by codenameone.

the class Form method pointerDragged.

@Override
public void pointerDragged(int[] x, int[] y) {
    // disable the drag stop flag if we are dragging again
    boolean isScrollWheeling = Display.INSTANCE.impl.isScrollWheeling();
    if (dragStopFlag) {
        pointerPressed(x, y);
    }
    autoRelease(x[0], y[0]);
    if (pointerDraggedListeners != null && pointerDraggedListeners.hasListeners()) {
        ActionEvent av = new ActionEvent(this, ActionEvent.Type.PointerDrag, x[0], y[0]);
        pointerDraggedListeners.fireActionEvent(av);
        if (av.isConsumed()) {
            return;
        }
    }
    rippleMotion = null;
    if (dragged != null) {
        dragged.pointerDragged(x, y);
        return;
    }
    if (pressedCmp != null && pressedCmp.isStickyDrag()) {
        stickyDrag = pressedCmp;
    }
    if (stickyDrag != null) {
        stickyDrag.pointerDragged(x, y);
        repaint();
        return;
    }
    Container actual = getActualPane(formLayeredPane, x[0], y[0]);
    if (x[0] < actual.getX()) {
        // special case for sidemenu
        Component cmp = ((BorderLayout) super.getLayout()).getWest();
        if (cmp != null) {
            cmp = ((Container) cmp).getComponentAt(x[0], y[0]);
            while (cmp != null && cmp.isIgnorePointerEvents()) {
                cmp = cmp.getParent();
            }
            if (cmp != null && cmp.isEnabled()) {
                cmp.pointerDragged(x, y);
                cmp.repaint();
                if (cmp == pressedCmp && cmp.isStickyDrag()) {
                    stickyDrag = cmp;
                }
            }
        }
        return;
    }
    Component cmp = actual.getComponentAt(x[0], y[0]);
    while (cmp != null && cmp.isIgnorePointerEvents()) {
        cmp = cmp.getParent();
    }
    if (cmp != null) {
        if (!isScrollWheeling && cmp.isFocusable() && cmp.isEnabled()) {
            setFocused(cmp);
        }
        cmp.pointerDragged(x, y);
        cmp.repaint();
        if (cmp == pressedCmp && cmp.isStickyDrag()) {
            stickyDrag = cmp;
        }
    }
}
Also used : BorderLayout(com.codename1.ui.layouts.BorderLayout) ActionEvent(com.codename1.ui.events.ActionEvent)

Example 53 with Container

use of com.codename1.ui.Container in project CodenameOne by codenameone.

the class Form method autoRelease.

private void autoRelease(int x, int y) {
    if (buttonsAwatingRelease != null && buttonsAwatingRelease.size() == 1) {
        // special case allowing drag within a button
        Component atXY = getComponentAt(x, y);
        if (atXY instanceof Container) {
            atXY = atXY.getLeadComponent();
        }
        Component pendingButton = buttonsAwatingRelease.get(0);
        if (atXY != pendingButton) {
            if (pendingButton instanceof Button) {
                Button b = (Button) pendingButton;
                int relRadius = b.getReleaseRadius();
                if (relRadius > 0) {
                    Rectangle r = new Rectangle(b.getAbsoluteX() - relRadius, b.getAbsoluteY() - relRadius, b.getWidth() + relRadius * 2, b.getHeight() + relRadius * 2);
                    if (!r.contains(x, y)) {
                        buttonsAwatingRelease = null;
                        b.dragInitiated();
                    }
                    return;
                }
                buttonsAwatingRelease = null;
                b.dragInitiated();
            }
        } else if (pendingButton instanceof Button && ((Button) pendingButton).isAutoRelease()) {
            buttonsAwatingRelease = null;
            ((Button) pendingButton).dragInitiated();
        }
    }
}
Also used : Rectangle(com.codename1.ui.geom.Rectangle)

Example 54 with Container

use of com.codename1.ui.Container in project CodenameOne by codenameone.

the class Form method pointerReleased.

/**
 * {@inheritDoc}
 */
public void pointerReleased(int x, int y) {
    rippleMotion = null;
    pressedCmp = null;
    boolean isScrollWheeling = Display.INSTANCE.impl.isScrollWheeling();
    Container actual = getActualPane(formLayeredPane, x, y);
    if (buttonsAwatingRelease != null && buttonsAwatingRelease.size() == 1) {
        // special case allowing drag within a button
        Component atXY = actual.getComponentAt(x, y);
        Component pendingButton = (Component) buttonsAwatingRelease.get(0);
        if (atXY == pendingButton) {
            buttonsAwatingRelease = null;
            if (dragged == pendingButton) {
                if (pendingButton.isDragAndDropInitialized()) {
                    pendingButton.dragFinishedImpl(x, y);
                } else {
                    pendingButton.pointerReleased(x, y);
                }
                dragged = null;
            } else {
                pendingButton.pointerReleased(x, y);
                if (dragged != null) {
                    if (dragged.isDragAndDropInitialized()) {
                        dragged.dragFinishedImpl(x, y);
                        dragged = null;
                    } else {
                        dragged.pointerReleased(x, y);
                        dragged = null;
                    }
                }
            }
            return;
        }
        if (pendingButton instanceof Button) {
            Button b = (Button) pendingButton;
            int relRadius = b.getReleaseRadius();
            if (relRadius > 0 || b.contains(x, y)) {
                Rectangle r = new Rectangle(b.getAbsoluteX() - relRadius, b.getAbsoluteY() - relRadius, b.getWidth() + relRadius * 2, b.getHeight() + relRadius * 2);
                if (r.contains(x, y)) {
                    buttonsAwatingRelease = null;
                    pointerReleased(b.getAbsoluteX() + 1, b.getAbsoluteY() + 1);
                    return;
                }
            }
        }
    }
    if (pointerReleasedListeners != null && pointerReleasedListeners.hasListeners()) {
        ActionEvent ev = new ActionEvent(this, ActionEvent.Type.PointerReleased, x, y);
        pointerReleasedListeners.fireActionEvent(ev);
        if (ev.isConsumed()) {
            if (dragged != null) {
                if (dragged.isDragAndDropInitialized()) {
                    dragged.dragFinishedImpl(x, y);
                }
                dragged = null;
            }
            return;
        }
    }
    if (dragStopFlag) {
        if (dragged != null) {
            if (dragged.isDragAndDropInitialized()) {
                dragged.dragFinishedImpl(x, y);
            }
            dragged = null;
        }
        dragStopFlag = false;
        return;
    }
    if (dragged == null) {
        // soft button.
        if (menuBar.contains(x, y)) {
            Component cmp = menuBar.getComponentAt(x, y);
            if (cmp != null && cmp.isEnabled()) {
                cmp.pointerReleased(x, y);
            }
            return;
        }
        if (stickyDrag != null) {
            stickyDrag.pointerReleased(x, y);
            repaint();
        } else {
            // Container actual = getActualPane();
            if (y >= actual.getY() && x >= actual.getX()) {
                Component cmp = actual.getComponentAt(x, y);
                while (cmp != null && cmp.isIgnorePointerEvents()) {
                    cmp = cmp.getParent();
                }
                if (cmp != null && cmp.isEnabled()) {
                    if (cmp.hasLead) {
                        Container leadParent;
                        if (cmp instanceof Container) {
                            leadParent = ((Container) cmp).getLeadParent();
                        } else {
                            leadParent = cmp.getParent().getLeadParent();
                        }
                        leadParent.repaint();
                        if (!isScrollWheeling) {
                            setFocused(leadParent);
                        }
                        cmp.getLeadComponent().pointerReleased(x, y);
                    } else {
                        if (cmp.isEnabled()) {
                            if (!isScrollWheeling && cmp.isFocusable()) {
                                setFocused(cmp);
                            }
                            cmp.pointerReleased(x, y);
                        }
                    }
                }
            } else {
                if (y < actual.getY()) {
                    Component cmp = getTitleArea().getComponentAt(x, y);
                    while (cmp != null && cmp.isIgnorePointerEvents()) {
                        cmp = cmp.getParent();
                    }
                    if (cmp != null && cmp.isEnabled()) {
                        cmp.pointerReleased(x, y);
                    }
                } else {
                    Component cmp = ((BorderLayout) super.getLayout()).getWest();
                    if (cmp != null) {
                        cmp = ((Container) cmp).getComponentAt(x, y);
                        while (cmp != null && cmp.isIgnorePointerEvents()) {
                            cmp = cmp.getParent();
                        }
                        if (cmp != null && cmp.isEnabled()) {
                            if (cmp.hasLead) {
                                Container leadParent;
                                if (cmp instanceof Container) {
                                    leadParent = ((Container) cmp).getLeadParent();
                                } else {
                                    leadParent = cmp.getParent().getLeadParent();
                                }
                                leadParent.repaint();
                                if (!isScrollWheeling) {
                                    setFocused(leadParent);
                                }
                                cmp = cmp.getLeadComponent();
                                cmp.pointerReleased(x, y);
                            } else {
                                cmp.pointerReleased(x, y);
                            }
                        }
                    }
                }
            }
        }
    } else {
        if (dragged.isDragAndDropInitialized()) {
            dragged.dragFinishedImpl(x, y);
            dragged = null;
        } else {
            dragged.pointerReleased(x, y);
            dragged = null;
        }
    }
    stickyDrag = null;
    if (buttonsAwatingRelease != null && !Display.getInstance().isRecursivePointerRelease()) {
        for (int iter = 0; iter < buttonsAwatingRelease.size(); iter++) {
            Button b = (Button) buttonsAwatingRelease.get(iter);
            b.setState(Button.STATE_DEFAULT);
            b.repaint();
        }
        buttonsAwatingRelease = null;
    }
}
Also used : BorderLayout(com.codename1.ui.layouts.BorderLayout) ActionEvent(com.codename1.ui.events.ActionEvent) Rectangle(com.codename1.ui.geom.Rectangle)

Example 55 with Container

use of com.codename1.ui.Container in project CodenameOne by codenameone.

the class List method calculateElementSize.

/**
 * Calculates the size of an element based on a forumla or on rendering prototype
 */
private Dimension calculateElementSize(boolean selected, boolean addMargin) {
    if (renderingPrototype != null) {
        Component unselected = renderer.getListCellRendererComponent(this, renderingPrototype, 0, selected);
        if (addMargin) {
            return unselected.getPreferredSizeWithMargin();
        } else {
            return unselected.getPreferredSize();
        }
    }
    int width = 0;
    int height = 0;
    int elements = Math.min(listSizeCalculationSampleCount, model.getSize());
    int marginY = 0;
    int marginX = 0;
    for (int iter = 0; iter < elements; iter++) {
        Component cmp = renderer.getListCellRendererComponent(this, model.getItemAt(iter), iter, selected);
        if (cmp instanceof Container) {
            cmp.setShouldCalcPreferredSize(true);
        }
        Dimension d = cmp.getPreferredSize();
        width = Math.max(width, d.getWidth());
        height = Math.max(height, d.getHeight());
        if (iter == 0) {
            Style s = cmp.getStyle();
            marginY = s.getVerticalMargins();
            marginX = s.getHorizontalMargins();
        }
    }
    return new Dimension(width + marginX, height + marginY);
}
Also used : Style(com.codename1.ui.plaf.Style) Dimension(com.codename1.ui.geom.Dimension)

Aggregations

Container (com.codename1.ui.Container)85 Component (com.codename1.ui.Component)65 BorderLayout (com.codename1.ui.layouts.BorderLayout)46 Style (com.codename1.ui.plaf.Style)38 ActionEvent (com.codename1.ui.events.ActionEvent)29 Form (com.codename1.ui.Form)26 Label (com.codename1.ui.Label)21 BoxLayout (com.codename1.ui.layouts.BoxLayout)21 Dimension (com.codename1.ui.geom.Dimension)20 ActionListener (com.codename1.ui.events.ActionListener)19 Button (com.codename1.ui.Button)15 FlowLayout (com.codename1.ui.layouts.FlowLayout)15 ArrayList (java.util.ArrayList)14 LayeredLayout (com.codename1.ui.layouts.LayeredLayout)12 TextArea (com.codename1.ui.TextArea)11 Point (com.codename1.ui.geom.Point)11 Rectangle (com.codename1.ui.geom.Rectangle)11 Dialog (com.codename1.ui.Dialog)10 RadioButton (com.codename1.ui.RadioButton)10 Vector (java.util.Vector)9