Search in sources :

Example 81 with Dimension

use of com.codename1.ui.geom.Dimension 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)

Example 82 with Dimension

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

the class List method getSelectedRect.

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

Example 83 with Dimension

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

the class List method paint.

/**
 * {@inheritDoc}
 */
public void paint(Graphics g) {
    getUIManager().getLookAndFeel().drawList(g, this);
    Style style = getStyle();
    int width = getWidth() - style.getHorizontalPadding() - getSideGap();
    if (isScrollableX()) {
        width = Math.max(width, getScrollDimension().getWidth() - style.getHorizontalPadding() - getSideGap());
    }
    int numOfcomponents = model.getSize();
    if (numOfcomponents == 0) {
        paintHint(g);
        return;
    }
    int xTranslate = getX();
    int yTranslate = getY();
    g.translate(xTranslate, yTranslate);
    Rectangle pos = new Rectangle();
    Dimension rendererSize = getElementSize(false, true);
    if (fixedSelection > FIXED_NONE_BOUNDRY) {
        if (animationPosition != 0 || isDragActivated()) {
            if (orientation != HORIZONTAL) {
                yTranslate += (animationPosition + fixedDraggedAnimationPosition);
                g.translate(0, animationPosition + fixedDraggedAnimationPosition);
            } else {
                xTranslate += (animationPosition + fixedDraggedAnimationPosition);
                g.translate(animationPosition + fixedDraggedAnimationPosition, 0);
            }
        }
    }
    int clipX = g.getClipX();
    int clipY = g.getClipY();
    int clipWidth = g.getClipWidth();
    int clipHeight = g.getClipHeight();
    // this flag is for preformance improvements
    // if we figured out that the list items are not visible anymore
    // we should break from the List loop
    boolean shouldBreak = false;
    // improve performance for browsing the end of a very large list
    int startingPoint = 0;
    if (fixedSelection < FIXED_NONE_BOUNDRY) {
        int startX = clipX + getAbsoluteX();
        if (isRTL()) {
            // In RTL the start of the list is not in the left side of the viewport, but rather the right side
            startX += getWidth();
        }
        startingPoint = Math.max(0, pointerSelect(startX, clipY + getAbsoluteY()) - 1);
    }
    int startOffset = 0;
    int endOffset = numOfcomponents;
    if (mutableRendererBackgrounds) {
        for (int i = startingPoint; i < numOfcomponents; i++) {
            // skip on the selected
            if (i == getCurrentSelected() && animationPosition == 0 && fixedDraggedAnimationPosition == 0) {
                if (!shouldBreak) {
                    startOffset = i;
                }
                endOffset = i;
                shouldBreak = true;
                continue;
            }
            calculateComponentPosition(i, width, pos, rendererSize, getElementSize(true, true), i <= getCurrentSelected());
            // if the renderer is in the clipping region
            if (pos.intersects(clipX, clipY, clipWidth, clipHeight)) {
                if (!shouldBreak) {
                    startOffset = i;
                }
                endOffset = i;
                Dimension size = pos.getSize();
                Component selectionCmp = renderer.getListCellRendererComponent(this, getModel().getItemAt(i), i, i == getCurrentSelected());
                renderComponentBackground(g, selectionCmp, pos.getX(), pos.getY(), size.getWidth(), size.getHeight());
                shouldBreak = true;
            } else {
                // this is relevant only if the List is not fixed.
                if (shouldBreak && (fixedSelection < FIXED_NONE_BOUNDRY)) {
                    break;
                }
            }
        }
    } else {
        T valueAt0 = getModel().getItemAt(0);
        Component selectionCmp;
        int selectedIndex = getSelectedIndex();
        if (selectedIndex > -1 && selectedIndex < numOfcomponents) {
            // this is essential otherwise we constantly ticker based on the value of the first entry
            selectionCmp = renderer.getListCellRendererComponent(this, getModel().getItemAt(selectedIndex), 0, true);
        } else {
            selectionCmp = renderer.getListCellRendererComponent(this, valueAt0, 0, true);
        }
        Component unselectedCmp = renderer.getListCellRendererComponent(this, valueAt0, 0, false);
        for (int i = startingPoint; i < numOfcomponents; i++) {
            // skip on the selected
            if (i == getCurrentSelected() && animationPosition == 0) {
                if (!shouldBreak) {
                    startOffset = i;
                }
                endOffset = i;
                shouldBreak = true;
                continue;
            }
            calculateComponentPosition(i, width, pos, rendererSize, getElementSize(true, true), i <= getCurrentSelected());
            // if the renderer is in the clipping region
            if (pos.intersects(clipX, clipY, clipWidth, clipHeight)) {
                if (!shouldBreak) {
                    startOffset = i;
                }
                endOffset = i;
                if (i == getCurrentSelected()) {
                    Dimension size = pos.getSize();
                    renderComponentBackground(g, selectionCmp, pos.getX(), pos.getY(), size.getWidth(), size.getHeight());
                } else {
                    Dimension size = pos.getSize();
                    renderComponentBackground(g, unselectedCmp, pos.getX(), pos.getY(), size.getWidth(), size.getHeight());
                }
                shouldBreak = true;
            } else {
                // this is relevant only if the List is not fixed.
                if (shouldBreak && (fixedSelection < FIXED_NONE_BOUNDRY)) {
                    break;
                }
            }
        }
    }
    boolean shouldRendererSelectedEntry = (renderer.getListFocusComponent(this) == null && (fixedSelection < FIXED_NONE_BOUNDRY)) || animationPosition == 0 && model.getSize() > 0;
    Rectangle selectedPos = new Rectangle();
    calculateComponentPosition(getCurrentSelected(), width, selectedPos, rendererSize, getElementSize(true, true), true);
    Dimension size = selectedPos.getSize();
    int curSel = getCurrentSelected();
    if (shouldRendererSelectedEntry && curSel > -1 && curSel < model.getSize()) {
        Component selected = renderer.getListCellRendererComponent(this, model.getItemAt(getCurrentSelected()), getCurrentSelected(), true);
        renderComponentBackground(g, selected, selectedPos.getX(), selectedPos.getY(), size.getWidth(), size.getHeight());
    }
    if (paintFocusBehindList) {
        paintFocus(g, width, pos, rendererSize);
    }
    for (int i = startOffset; i <= endOffset; i++) {
        // skip on the selected
        if (i == getCurrentSelected() && animationPosition == 0) {
            continue;
        }
        calculateComponentPosition(i, width, pos, rendererSize, getElementSize(true, true), i <= getCurrentSelected());
        if (pos.intersects(clipX, clipY, clipWidth, clipHeight)) {
            T value = model.getItemAt(i);
            Component cmp = renderer.getListCellRendererComponent(this, value, i, false);
            cmp.setCellRenderer(true);
            Dimension sizeC = pos.getSize();
            renderComponent(g, cmp, pos.getX(), pos.getY(), sizeC.getWidth(), sizeC.getHeight());
        }
    }
    // if the animation has finished draw the selected element
    if (shouldRendererSelectedEntry) {
        Component selected = renderer.getListCellRendererComponent(this, model.getItemAt(getCurrentSelected()), getCurrentSelected(), true);
        renderComponent(g, selected, selectedPos.getX(), selectedPos.getY(), size.getWidth(), size.getHeight());
    }
    if (!paintFocusBehindList) {
        paintFocus(g, width, pos, rendererSize);
    }
    g.translate(-xTranslate, -yTranslate);
    if (spinnerOverlay != null) {
        if (spinnerOverlay.getBorder() != null) {
            spinnerOverlay.getBorder().paintBorderBackground(g, this);
            spinnerOverlay.getBorder().paint(g, this);
        } else {
            spinnerOverlay.getBgPainter().paint(g, getBounds());
        }
    }
}
Also used : Rectangle(com.codename1.ui.geom.Rectangle) Style(com.codename1.ui.plaf.Style) Dimension(com.codename1.ui.geom.Dimension)

Example 84 with Dimension

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

the class Form method sizeChangedInternal.

/**
 * This method is only invoked when the underlying canvas for the form gets
 * a size changed event.
 * This method will trigger a relayout of the Form.
 * This method will get the callback only if this Form is the Current Form
 * @param w the new width of the Form
 * @param h the new height of the Form
 */
void sizeChangedInternal(int w, int h) {
    int oldWidth = getWidth();
    int oldHeight = getHeight();
    sizeChanged(w, h);
    Style formStyle = getStyle();
    w = w - (formStyle.getHorizontalMargins());
    h = h - (formStyle.getVerticalMargins());
    setSize(new Dimension(w, h));
    setShouldCalcPreferredSize(true);
    doLayout();
    focused = getFocused();
    if (focused != null) {
        Component.setDisableSmoothScrolling(true);
        scrollComponentToVisible(focused);
        Component.setDisableSmoothScrolling(false);
    }
    if (oldWidth != w && oldHeight != h) {
        if (orientationListener != null) {
            orientationListener.fireActionEvent(new ActionEvent(this, ActionEvent.Type.OrientationChange));
        }
    }
    if (sizeChangedListener != null) {
        sizeChangedListener.fireActionEvent(new ActionEvent(this, ActionEvent.Type.SizeChange, w, h));
    }
    repaint();
}
Also used : ActionEvent(com.codename1.ui.events.ActionEvent) Style(com.codename1.ui.plaf.Style) Dimension(com.codename1.ui.geom.Dimension)

Example 85 with Dimension

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

the class Form method initAdPadding.

void initAdPadding(Display d) {
    // this is injected automatically by the implementation in case of ads
    String adPaddingBottom = d.getProperty("adPaddingBottom", null);
    if (adPaddingBottom != null && adPaddingBottom.length() > 0) {
        Container pad = new Container();
        int dim = Integer.parseInt(adPaddingBottom);
        dim = d.convertToPixels(dim, true);
        if (Display.getInstance().isTablet()) {
            dim *= 2;
        }
        pad.setPreferredSize(new Dimension(dim, dim));
        addComponentToForm(BorderLayout.SOUTH, pad);
    }
}
Also used : Dimension(com.codename1.ui.geom.Dimension)

Aggregations

Dimension (com.codename1.ui.geom.Dimension)74 Style (com.codename1.ui.plaf.Style)27 Component (com.codename1.ui.Component)16 Image (com.codename1.ui.Image)16 Rectangle (com.codename1.ui.geom.Rectangle)9 EncodedImage (com.codename1.ui.EncodedImage)8 Label (com.codename1.ui.Label)7 Container (com.codename1.ui.Container)6 FileEncodedImage (com.codename1.components.FileEncodedImage)5 StorageImage (com.codename1.components.StorageImage)5 ActionEvent (com.codename1.ui.events.ActionEvent)5 Form (com.codename1.ui.Form)4 Dimension (java.awt.Dimension)4 Font (com.codename1.ui.Font)3 FontImage (com.codename1.ui.FontImage)3 TextArea (com.codename1.ui.TextArea)3 ActionListener (com.codename1.ui.events.ActionListener)3 Point (com.codename1.ui.geom.Point)3 BorderLayout (com.codename1.ui.layouts.BorderLayout)3 Border (com.codename1.ui.plaf.Border)3