Search in sources :

Example 21 with Animation

use of com.codename1.ui.animations.Animation in project CodenameOne by codenameone.

the class Display method paintTransitionAnimation.

private void paintTransitionAnimation() {
    Animation ani = (Animation) animationQueue.get(0);
    if (!ani.animate()) {
        animationQueue.remove(0);
        if (ani instanceof Transition) {
            Form source = (Form) ((Transition) ani).getSource();
            restoreMenu(source);
            if (animationQueue.size() > 0) {
                ani = (Animation) animationQueue.get(0);
                if (ani instanceof Transition) {
                    ((Transition) ani).initTransition();
                }
            } else {
                Form f = (Form) ((Transition) ani).getDestination();
                restoreMenu(f);
                if (source == null || source == impl.getCurrentForm() || source == getCurrent()) {
                    setCurrentForm(f);
                }
                ((Transition) ani).cleanup();
            }
            return;
        }
    }
    ani.paint(codenameOneGraphics);
    impl.flushGraphics();
    if (transitionDelay > 0) {
        // flush and so require the painting thread to get CPU too.
        try {
            synchronized (lock) {
                lock.wait(transitionDelay);
            }
        } catch (InterruptedException ex) {
            ex.printStackTrace();
        }
    }
}
Also used : Transition(com.codename1.ui.animations.Transition) Animation(com.codename1.ui.animations.Animation)

Example 22 with Animation

use of com.codename1.ui.animations.Animation in project CodenameOne by codenameone.

the class List method paintFocus.

private void paintFocus(Graphics g, int width, Rectangle pos, Dimension rendererSize) {
    if (ignoreFocusComponentWhenUnfocused && !hasFocus()) {
        return;
    }
    if (!shouldRenderSelection()) {
        return;
    }
    calculateComponentPosition(getCurrentSelected(), width, pos, rendererSize, getElementSize(true, true), true);
    Dimension size = pos.getSize();
    Component cmp = renderer.getListFocusComponent(this);
    if (cmp != null) {
        cmp.setCellRenderer(true);
        int x = pos.getX();
        int y = pos.getY();
        // prevent focus animation from working during a drag operation
        if (orientation != HORIZONTAL) {
            y -= (animationPosition + fixedDraggedAnimationPosition);
        } else {
            x -= (animationPosition + fixedDraggedAnimationPosition);
        }
        renderComponentBackground(g, cmp, x, y, size.getWidth(), size.getHeight());
        renderComponent(g, cmp, x, y, size.getWidth(), size.getHeight());
    }
}
Also used : Dimension(com.codename1.ui.geom.Dimension)

Example 23 with Animation

use of com.codename1.ui.animations.Animation in project CodenameOne by codenameone.

the class List method animate.

/**
 * {@inheritDoc}
 */
public boolean animate() {
    // parent is performing the animation we shouldn't do anything in this case
    // this is the scrolling animation which we don't want to interfear with
    boolean parentFinished = super.animate();
    if ((animationPosition != 0) && listMotion != null && !isDragActivated()) {
        if (animationPosition < 0) {
            animationPosition = Math.min(listMotion.getValue() - destination, 0);
        } else {
            animationPosition = Math.max(destination - listMotion.getValue(), 0);
        }
        if (animationPosition == 0) {
            listMotion = null;
            deregisterAnimatedInternal();
        }
        return true;
    }
    if (fixedDraggedMotion != null) {
        int val = -fixedDraggedMotion.getValue();
        fixedDraggedAnimationPosition = fixedDraggedAnimationPosition - (fixedDraggedPosition - val);
        fixedDraggedPosition = val;
        Dimension size = getElementSize(false, true);
        int s;
        if (orientation == VERTICAL) {
            s = size.getHeight();
        } else {
            s = size.getWidth();
        }
        if (fixedDraggedMotion.isFinished()) {
            deregisterAnimatedInternal();
            // is the closest and animate to it.
            if (fixedDraggedAnimationPosition <= -s / 2) {
                fixedDraggedSelection++;
                if (fixedDraggedSelection >= model.getSize()) {
                    fixedDraggedSelection = 0;
                }
            } else if (fixedDraggedAnimationPosition >= s / 2) {
                fixedDraggedSelection--;
                if (fixedDraggedSelection < 0) {
                    fixedDraggedSelection = model.getSize() - 1;
                }
            }
            if (fixedDraggedAnimationPosition != 0) {
                if (fixedDraggedAnimationPosition < 0) {
                    if (fixedDraggedAnimationPosition < -s / 2) {
                        destination = s + fixedDraggedAnimationPosition;
                        animationPosition = destination;
                    } else {
                        destination = -fixedDraggedAnimationPosition;
                        animationPosition = fixedDraggedAnimationPosition;
                    }
                } else {
                    if (fixedDraggedAnimationPosition > s / 2) {
                        destination = (s - fixedDraggedAnimationPosition);
                        animationPosition = -destination;
                    } else {
                        destination = fixedDraggedAnimationPosition;
                        animationPosition = fixedDraggedAnimationPosition;
                    }
                }
                initListMotion();
                fixedDraggedAnimationPosition = 0;
            }
            // this happens when dragging an empty list causing an exception on a negative selection
            if (fixedDraggedSelection >= 0 && fixedDraggedSelection < getModel().getSize()) {
                setSelectedIndex(fixedDraggedSelection, false);
            }
            setDragActivated(false);
            fixedDraggedMotion = null;
            return false;
        } else {
            if (fixedDraggedAnimationPosition <= -s) {
                fixedDraggedSelection++;
                if (fixedDraggedSelection >= model.getSize()) {
                    fixedDraggedSelection = 0;
                }
                fixedDraggedPosition = val;
            } else if (fixedDraggedAnimationPosition >= s) {
                fixedDraggedSelection--;
                if (fixedDraggedSelection < 0) {
                    fixedDraggedSelection = model.getSize() - 1;
                }
                fixedDraggedPosition = val;
            }
            fixedDraggedAnimationPosition = fixedDraggedAnimationPosition % s;
        }
        return true;
    }
    return parentFinished;
}
Also used : Dimension(com.codename1.ui.geom.Dimension)

Example 24 with Animation

use of com.codename1.ui.animations.Animation 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 25 with Animation

use of com.codename1.ui.animations.Animation in project CodenameOne by codenameone.

the class CommonTransitions method paintCoverAtPosition.

private void paintCoverAtPosition(Graphics g, int slideX, int slideY) {
    Component source = getSource();
    // if this is the first form we can't do a slide transition since we have no source form
    if (source == null) {
        return;
    }
    Component dest = getDestination();
    int w = source.getWidth();
    int h = source.getHeight();
    if (slideType == SLIDE_HORIZONTAL) {
        h = 0;
    } else {
        w = 0;
    }
    boolean dir = forward;
    if (dest != null && dest.getUIManager().getLookAndFeel().isRTL()) {
        dir = !dir;
    }
    if (dir) {
        w = -w;
        h = -h;
    } else {
        slideX = -slideX;
        slideY = -slideY;
    }
    g.setClip(source.getAbsoluteX() + source.getScrollX(), source.getAbsoluteY() + source.getScrollY(), source.getWidth(), source.getHeight());
    // dialog animation is slightly different...
    if (source instanceof Dialog) {
        if (buffer != null) {
            g.drawImage(buffer, 0, 0);
        } else {
            paint(g, dest, 0, 0);
        }
        paint(g, source, -slideX, -slideY);
        return;
    }
    if (dest instanceof Dialog) {
        if (buffer != null) {
            g.drawImage(buffer, 0, 0);
        } else {
            paint(g, source, 0, 0);
        }
        paint(g, dest, -slideX - w, -slideY - h);
        return;
    }
    if (transitionType == TYPE_UNCOVER) {
        paint(g, dest, 0, 0);
        if (source.getParent() != null || buffer == null) {
            source.paintBackgrounds(g);
            paint(g, source, slideX + w, slideY + h);
        } else {
            g.drawImage(buffer, slideX + w, slideY + h);
        }
    } else {
        if (source.getParent() != null || buffer == null) {
            source.paintBackgrounds(g);
            paint(g, source, 0, 0);
        } else {
            g.drawImage(buffer, 0, 0);
        }
        paint(g, dest, slideX + w, slideY + h);
    }
}
Also used : Dialog(com.codename1.ui.Dialog) Component(com.codename1.ui.Component)

Aggregations

Animation (com.codename1.ui.animations.Animation)13 Motion (com.codename1.ui.animations.Motion)8 ComponentAnimation (com.codename1.ui.animations.ComponentAnimation)7 Dimension (com.codename1.ui.geom.Dimension)7 Style (com.codename1.ui.plaf.Style)7 Form (com.codename1.ui.Form)6 Component (com.codename1.ui.Component)5 Graphics (com.codename1.ui.Graphics)5 ActionEvent (com.codename1.ui.events.ActionEvent)5 ArrayList (java.util.ArrayList)5 ActionListener (com.codename1.ui.events.ActionListener)4 BorderLayout (com.codename1.ui.layouts.BorderLayout)4 FontImage (com.codename1.ui.FontImage)3 Transition (com.codename1.ui.animations.Transition)3 IOException (java.io.IOException)3 BufferedOutputStream (com.codename1.io.BufferedOutputStream)2 Dialog (com.codename1.ui.Dialog)2 Image (com.codename1.ui.Image)2 PeerComponent (com.codename1.ui.PeerComponent)2 AnimationObject (com.codename1.ui.animations.AnimationObject)2