Search in sources :

Example 46 with Rectangle

use of com.codename1.ui.geom.Rectangle in project CodenameOne by codenameone.

the class List method pointerSelect.

private int pointerSelect(int x, int y) {
    int selectedIndex = -1;
    int numOfcomponents = getModel().getSize();
    Style style = getStyle();
    Dimension rendererSize = getElementSize(false, true);
    Dimension selectedSize = getElementSize(true, true);
    Rectangle pos = new Rectangle();
    int width = getWidth() - style.getHorizontalPadding() - getSideGap();
    if (isScrollableX()) {
        width = Math.max(width, getScrollDimension().getWidth() - style.getHorizontalPadding() - getSideGap());
    }
    y = y - getAbsoluteY();
    x = x - getAbsoluteX();
    if (fixedSelection < FIXED_NONE_BOUNDRY) {
        calculateComponentPosition(getSelectedIndex(), width, pos, rendererSize, getElementSize(true, true), true);
        if (orientation != HORIZONTAL) {
            if (y < pos.getY()) {
                selectedIndex = (y - style.getPaddingTop()) / (rendererSize.getHeight() + itemGap);
            } else {
                int current = getSelectedIndex();
                if (y < pos.getY() + selectedSize.getHeight()) {
                    selectedIndex = current;
                } else {
                    selectedIndex = (current + 1) + (y - (pos.getY() + selectedSize.getHeight())) / (rendererSize.getHeight() + itemGap);
                }
            }
        } else {
            if (isRTL()) {
                if (x > pos.getX() + selectedSize.getWidth()) {
                    int delta = x - (pos.getX() + selectedSize.getWidth());
                    delta /= (rendererSize.getWidth() + itemGap);
                    // should have been -1-delta, but works better like this.
                    selectedIndex = getSelectedIndex() - 1 - delta;
                } else {
                    if (x >= pos.getX()) {
                        selectedIndex = getSelectedIndex();
                    } else {
                        int delta = pos.getX() - x;
                        delta /= (rendererSize.getWidth() + itemGap);
                        selectedIndex = getSelectedIndex() + 1 + delta;
                    }
                }
            } else {
                if (x < pos.getX()) {
                    selectedIndex = (x - style.getPaddingLeftNoRTL()) / (rendererSize.getWidth() + itemGap);
                } else {
                    int current = getSelectedIndex();
                    if (x < pos.getX() + selectedSize.getWidth()) {
                        selectedIndex = current;
                    } else {
                        selectedIndex = (current + 1) + (x - (pos.getX() + selectedSize.getWidth())) / (rendererSize.getWidth() + itemGap);
                    }
                }
            }
        }
    } else {
        for (int i = 0; i < numOfcomponents; i++) {
            calculateComponentPosition(i, width, pos, rendererSize, selectedSize, true);
            if (pos.contains(x, y)) {
                selectedIndex = i;
                break;
            }
        }
    }
    if (selectedIndex < 0 || selectedIndex >= size()) {
        return -1;
    }
    return selectedIndex;
}
Also used : Rectangle(com.codename1.ui.geom.Rectangle) Style(com.codename1.ui.plaf.Style) Dimension(com.codename1.ui.geom.Dimension)

Example 47 with Rectangle

use of com.codename1.ui.geom.Rectangle in project CodenameOne by codenameone.

the class List method getVisibleBounds.

/**
 * {@inheritDoc}
 */
protected Rectangle getVisibleBounds() {
    Rectangle pos = new Rectangle();
    Dimension rendererSize = getElementSize(false, true);
    Style style = getStyle();
    int width = getWidth() - style.getHorizontalPadding() - getSideGap();
    calculateComponentPosition(getCurrentSelected(), width, pos, rendererSize, getElementSize(true, true), true);
    pos.setX(pos.getX() + getX());
    pos.setY(pos.getY() + getY());
    return pos;
}
Also used : Rectangle(com.codename1.ui.geom.Rectangle) Style(com.codename1.ui.plaf.Style) Dimension(com.codename1.ui.geom.Dimension)

Example 48 with Rectangle

use of com.codename1.ui.geom.Rectangle in project CodenameOne by codenameone.

the class List method scrollRectToVisible.

/**
 * Makes sure the selected index is visible if it is not in the current view
 * rect the list will scroll so it fits within
 *
 * @param rect the rectangle area to scroll to
 */
public void scrollRectToVisible(Rectangle rect) {
    if (fixedSelection < FIXED_NONE_BOUNDRY) {
        // Dimension elemSize = getElementSize();
        Rectangle toScroll;
        if (orientation != HORIZONTAL) {
            toScroll = new Rectangle(getScrollX(), rect.getY(), rect.getSize().getWidth(), rect.getSize().getHeight() + itemGap);
        } else {
            toScroll = new Rectangle(rect.getX(), getScrollY(), rect.getSize().getWidth() + itemGap, rect.getSize().getHeight());
        }
        super.scrollRectToVisible(toScroll, this);
    }
}
Also used : Rectangle(com.codename1.ui.geom.Rectangle)

Example 49 with Rectangle

use of com.codename1.ui.geom.Rectangle in project CodenameOne by codenameone.

the class List method selectElement.

void selectElement(int selectedIndex) {
    Dimension size = getElementSize(false, true);
    Rectangle rect;
    if (getOrientation() != HORIZONTAL) {
        rect = new Rectangle(getX(), (size.getHeight() + itemGap) * selectedIndex, getElementSize(true, true));
    } else {
        int x = (size.getWidth() + itemGap) * selectedIndex;
        if (isRTL() && isScrollableX()) {
            x = getScrollDimension().getWidth() - x - (size.getWidth() + itemGap);
        }
        rect = new Rectangle(x, getY(), getElementSize(true, true));
    }
    if (hasScrollableParent(getParent())) {
        if (hasFocus()) {
            scrollRectToVisible(rect);
        }
    } else {
        scrollRectToVisible(rect);
    }
}
Also used : Rectangle(com.codename1.ui.geom.Rectangle) Dimension(com.codename1.ui.geom.Dimension)

Example 50 with Rectangle

use of com.codename1.ui.geom.Rectangle in project CodenameOne by codenameone.

the class List method pointerReleasedImpl.

private void pointerReleasedImpl(int x, int y, boolean isHover, boolean longPress) {
    if (!isEnabled()) {
        return;
    }
    if (isDragActivated()) {
        if (fixedSelection < FIXED_NONE_BOUNDRY) {
            super.pointerReleased(x, y);
        } else {
            boolean vertical = getOrientation() == VERTICAL;
            float speed = getDragSpeed(vertical);
            if (vertical) {
                fixedDraggedMotion = Motion.createFrictionMotion(-fixedDraggedAnimationPosition, Integer.MAX_VALUE, speed, 0.0007f);
            } else {
                fixedDraggedMotion = Motion.createFrictionMotion(-fixedDraggedAnimationPosition, Integer.MAX_VALUE, speed, 0.0007f);
            }
            fixedDraggedPosition = fixedDraggedAnimationPosition;
            Form p = getComponentForm();
            if (p != null) {
                p.registerAnimatedInternal(this);
            }
            fixedDraggedMotion.start();
        }
        return;
    }
    if (!isHover && pointerSelect(x, y) > -1) {
        if (fixedSelection > FIXED_NONE_BOUNDRY) {
            int index = pointerSelect(x, y);
            updateAnimationPosition(index - getSelectedIndex());
            setSelectedIndex(index);
            fireActionEvent(new ActionEvent(eventSource, ActionEvent.Type.Other));
            return;
        }
        if ((fireOnClick && fixedSelection < FIXED_NONE_BOUNDRY) || fireOnRelease) {
            // fire the action event into the selected component
            Component selectionCmp = renderer.getListCellRendererComponent(this, getSelectedItem(), getSelectedIndex(), true);
            Style style = getStyle();
            int width = getWidth() - style.getHorizontalPadding() - getSideGap();
            Rectangle pos = new Rectangle();
            Dimension rendererSize = getElementSize(false, true);
            calculateComponentPosition(getSelectedIndex(), width, pos, rendererSize, getElementSize(true, true), true);
            int absX = getAbsoluteX();
            int posX = pos.getX();
            int absY = getAbsoluteY();
            int posY = pos.getY();
            int newX = x - absX - posX;
            int newY = y - absY - posY;
            selectionCmp.setX(0);
            selectionCmp.setY(0);
            if (selectionCmp instanceof Container) {
                Component tmp = ((Container) selectionCmp).getComponentAt(newX, newY);
                if (tmp != null) {
                    selectionCmp = tmp;
                }
            }
            if (longPress) {
                selectionCmp.longPointerPress(newX, newY);
                fireActionEvent(new ActionEvent(eventSource, newX, newY, true));
            } else {
                selectionCmp.pointerPressed(newX, newY);
                selectionCmp.pointerReleased(newX, newY);
                fireActionEvent(new ActionEvent(eventSource, newX, newY, false));
            }
        }
    }
}
Also used : ActionEvent(com.codename1.ui.events.ActionEvent) Rectangle(com.codename1.ui.geom.Rectangle) Style(com.codename1.ui.plaf.Style) Dimension(com.codename1.ui.geom.Dimension)

Aggregations

Rectangle (com.codename1.ui.geom.Rectangle)41 Dimension (com.codename1.ui.geom.Dimension)16 Style (com.codename1.ui.plaf.Style)13 Image (com.codename1.ui.Image)10 ActionEvent (com.codename1.ui.events.ActionEvent)5 Border (com.codename1.ui.plaf.Border)5 Component (com.codename1.ui.Component)4 Graphics (com.codename1.ui.Graphics)4 UIManager (com.codename1.ui.plaf.UIManager)4 Form (com.codename1.ui.Form)3 Label (com.codename1.ui.Label)3 GeneralPath (com.codename1.ui.geom.GeneralPath)3 Point (com.codename1.ui.geom.Point)3 BorderLayout (com.codename1.ui.layouts.BorderLayout)3 Paint (com.codename1.charts.compat.Paint)2 Button (com.codename1.ui.Button)2 EncodedImage (com.codename1.ui.EncodedImage)2 Painter (com.codename1.ui.Painter)2 ActionListener (com.codename1.ui.events.ActionListener)2 RoundBorder (com.codename1.ui.plaf.RoundBorder)2