Search in sources :

Example 11 with Animation

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

the class ComponentSelector method fadeOut.

/**
 * Fades out components in this set.
 * @param duration Duration of animation.
 * @param callback Callback to run when animation completes.
 * @return Self for chaining.
 */
public ComponentSelector fadeOut(int duration, final SuccessCallback<ComponentSelector> callback) {
    final String placeholderProperty = "com.codename1.ui.ComponentSelector#fadeOutPlaceholder";
    AnimationManager mgr = null;
    ArrayList<ComponentAnimation> animations = new ArrayList<ComponentAnimation>();
    final ArrayList<Component> animatingComponents = new ArrayList<Component>();
    for (Component c : this) {
        Container parent = c.getParent();
        if (parent != null) {
            AnimationManager cmgr = c.getAnimationManager();
            if (cmgr != null) {
                mgr = cmgr;
                Container placeholder = new Container();
                // placeholder.setShowEvenIfBlank(true);
                c.putClientProperty(placeholderProperty, placeholder);
                Component.setSameHeight(placeholder, c);
                Component.setSameWidth(placeholder, c);
                $(placeholder).setMargin(c.getStyle().getMarginTop(), c.getStyle().getMarginRight(false), c.getStyle().getMarginBottom(), c.getStyle().getMarginLeft(false)).setPadding(c.getStyle().getPaddingTop(), c.getStyle().getPaddingRight(false), c.getStyle().getPaddingBottom(), c.getStyle().getPaddingLeft(false));
                ComponentAnimation a = parent.createReplaceTransition(c, placeholder, CommonTransitions.createFade(duration));
                animations.add(a);
                animatingComponents.add(c);
            }
        // centerBackground.add(BorderLayout.CENTER, boxy);
        }
    }
    if (mgr != null) {
        mgr.addAnimation(ComponentAnimation.compoundAnimation(animations.toArray(new ComponentAnimation[animations.size()])), new Runnable() {

            public void run() {
                for (final Component c : animatingComponents) {
                    // c.setHidden(true);
                    c.setVisible(false);
                    final Container placeholder = (Container) c.getClientProperty(placeholderProperty);
                    c.putClientProperty(placeholderProperty, null);
                    if (placeholder != null) {
                        Container parent = placeholder.getParent();
                        /*
                            if (parent == null) {
                                System.out.println("Deferring replace back");
                                $(new Runnable() {
                                    public void run() {
                                        Container parent = placeholder.getParent();
                                        if (parent != null) {
                                            System.out.println("Found parent after deferral");
                                            parent.replace(placeholder, c, CommonTransitions.createEmpty());
                                        }
                                    }
                                });
                            } else {
                            */
                        if (parent != null) {
                            parent.replace(placeholder, c, CommonTransitions.createEmpty());
                        }
                    // }
                    }
                }
                if (callback != null) {
                    callback.onSucess(ComponentSelector.this);
                }
            }
        });
    }
    return this;
}
Also used : ComponentAnimation(com.codename1.ui.animations.ComponentAnimation) ArrayList(java.util.ArrayList)

Example 12 with Animation

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

the class Display method setCurrent.

/**
 * Displays the given Form on the screen.
 *
 * @param newForm the Form to Display
 */
void setCurrent(final Form newForm, boolean reverse) {
    if (edt == null) {
        throw new IllegalStateException("Initialize must be invoked before setCurrent!");
    }
    Form current = impl.getCurrentForm();
    if (autoFoldVKBOnFormSwitch && !(newForm instanceof Dialog)) {
        setShowVirtualKeyboard(false);
    }
    if (current == newForm) {
        current.revalidate();
        current.repaint();
        current.onShowCompletedImpl();
        return;
    }
    if (impl.isEditingText()) {
        switch(showDuringEdit) {
            case SHOW_DURING_EDIT_ALLOW_DISCARD:
                break;
            case SHOW_DURING_EDIT_ALLOW_SAVE:
                impl.saveTextEditingState();
                break;
            case SHOW_DURING_EDIT_EXCEPTION:
                throw new IllegalStateException("Show during edit");
            case SHOW_DURING_EDIT_IGNORE:
                return;
            case SHOW_DURING_EDIT_SET_AS_NEXT:
                impl.setCurrentForm(newForm);
                return;
        }
    }
    if (!isEdt()) {
        callSerially(new RunnableWrapper(newForm, null, reverse));
        return;
    }
    if (current != null) {
        if (current.isInitialized()) {
            current.deinitializeImpl();
        } else {
            Form fg = getCurrentUpcoming();
            if (fg != current) {
                if (fg.isInitialized()) {
                    fg.deinitializeImpl();
                }
            }
        }
    }
    if (!newForm.isInitialized()) {
        newForm.initComponentImpl();
    }
    if (newForm.getWidth() != getDisplayWidth() || newForm.getHeight() != getDisplayHeight()) {
        newForm.setSize(new Dimension(getDisplayWidth(), getDisplayHeight()));
        newForm.setShouldCalcPreferredSize(true);
        newForm.layoutContainer();
    } else {
        // if shouldLayout is true
        newForm.layoutContainer();
    }
    boolean transitionExists = false;
    if (animationQueue != null && animationQueue.size() > 0) {
        Object o = animationQueue.get(animationQueue.size() - 1);
        if (o instanceof Transition) {
            current = (Form) ((Transition) o).getDestination();
            impl.setCurrentForm(current);
        }
    }
    if (current != null) {
        // to the correct parent!
        if (current instanceof Dialog && ((Dialog) current).isMenu()) {
            Transition t = current.getTransitionOutAnimator();
            if (t != null) {
                // go back to the parent form first
                if (((Dialog) current).getPreviousForm() != null) {
                    initTransition(t.copy(false), current, ((Dialog) current).getPreviousForm());
                }
            }
            current = ((Dialog) current).getPreviousForm();
            impl.setCurrentForm(current);
        }
        // prevent the transition from occurring from a form into itself
        if (newForm != current) {
            if ((current != null && current.getTransitionOutAnimator() != null) || newForm.getTransitionInAnimator() != null) {
                if (animationQueue == null) {
                    animationQueue = new ArrayList<Animation>();
                }
                // transitions which are a bit sensitive
                if (current != null && (!(newForm instanceof Dialog))) {
                    Transition t = current.getTransitionOutAnimator();
                    if (current != null && t != null) {
                        transitionExists = initTransition(t.copy(reverse), current, newForm);
                    }
                }
                if (current != null && !(current instanceof Dialog)) {
                    Transition t = newForm.getTransitionInAnimator();
                    if (t != null) {
                        transitionExists = initTransition(t.copy(reverse), current, newForm);
                    }
                }
            }
        }
    }
    synchronized (lock) {
        lock.notify();
    }
    if (!transitionExists) {
        if (animationQueue == null || animationQueue.size() == 0) {
            setCurrentForm(newForm);
        } else {
            // we need to add an empty transition to "serialize" this
            // screen change...
            Transition t = CommonTransitions.createEmpty();
            initTransition(t, current, newForm);
        }
    }
}
Also used : Transition(com.codename1.ui.animations.Transition) Animation(com.codename1.ui.animations.Animation) Dimension(com.codename1.ui.geom.Dimension)

Example 13 with Animation

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

the class CommonTransitions method paintSlideAtPosition.

private void paintSlideAtPosition(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() && slideType == SLIDE_HORIZONTAL) {
        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 (source.getParent() != null || buffer == null) {
        source.paintBackgrounds(g);
        paint(g, source, slideX, slideY);
    } else {
        g.drawImage(buffer, slideX, slideY);
    }
    paint(g, dest, slideX + w, slideY + h);
}
Also used : Dialog(com.codename1.ui.Dialog) Component(com.codename1.ui.Component)

Example 14 with Animation

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

the class SideMenuBar method createMenu.

private Form createMenu(final String placement) {
    final Form m = new Form() {

        private boolean pressedInRightPanel;

        private boolean manualMotionLock;

        protected boolean shouldSendPointerReleaseToOtherForm() {
            return true;
        }

        void actionCommandImpl(Command cmd, ActionEvent ev) {
            if (cmd instanceof SideMenuBar.CommandWrapper) {
                cmd = ((SideMenuBar.CommandWrapper) cmd).cmd;
                ev = new ActionEvent(cmd, ActionEvent.Type.Command);
            }
            final Command c = cmd;
            final ActionEvent e = ev;
            Display.getInstance().scheduleBackgroundTask(new Runnable() {

                public void run() {
                    Display.getInstance().invokeAndBlock(new Runnable() {

                        public void run() {
                            while (Display.getInstance().getCurrent() != parent) {
                                try {
                                    Thread.sleep(40);
                                } catch (Exception ex) {
                                }
                            }
                        }
                    });
                    Display.getInstance().callSerially(new Runnable() {

                        public void run() {
                            parent.actionCommandImpl(c, e);
                        }
                    });
                }
            });
        }

        protected void sizeChanged(int w, int h) {
            Style formStyle = getStyle();
            int width = w - (formStyle.getHorizontalMargins());
            parent.sizeChangedInternal(w, h);
            // close the menu
            if (getWidth() != width) {
                closeMenu();
            }
            super.sizeChanged(w, h);
        }

        public void pointerPressed(int x, int y) {
            if (manualMotionLock) {
                return;
            }
            super.pointerPressed(x, y);
            if (rightPanel.contains(x, y)) {
                pressedInRightPanel = true;
            }
        }

        public void pointerDragged(int[] x, int[] y) {
            if (manualMotionLock) {
                return;
            }
            if (!transitionRunning && pressedInRightPanel) {
                dragActivated = true;
                pressedInRightPanel = false;
            }
            if (dragActivated) {
                setMenuGlassPane(menu, placement);
                draggedX = x[0];
                repaint();
                return;
            }
            super.pointerDragged(x, y);
        }

        public void pointerReleased(int x, int y) {
            if (manualMotionLock) {
                return;
            }
            super.pointerReleased(x, y);
            boolean isRTLValue = isRTL();
            if (placement == COMMAND_PLACEMENT_VALUE_RIGHT) {
                isRTLValue = !isRTLValue;
            }
            int displayWidth = Display.getInstance().getDisplayWidth();
            if (isRTLValue) {
                if (!transitionRunning && dragActivated && x < (displayWidth - rightPanel.getWidth()) / 2) {
                    final Motion motion = Motion.createEaseInOutMotion(draggedX, rightPanel.getWidth(), 200);
                    motion.start();
                    registerAnimated(new Animation() {

                        public boolean animate() {
                            draggedX = motion.getValue();
                            if (motion.isFinished()) {
                                dragActivated = false;
                                Display.getInstance().getCurrent().setGlassPane(null);
                                deregisterAnimated(this);
                            }
                            return true;
                        }

                        public void paint(Graphics g) {
                            repaint();
                        }
                    });
                    return;
                }
            } else {
                if (!transitionRunning && dragActivated && x > (displayWidth - rightPanel.getWidth()) / 2) {
                    final Motion motion = Motion.createEaseInOutMotion(draggedX, Display.getInstance().getDisplayWidth() - rightPanel.getWidth(), 200);
                    motion.start();
                    registerAnimated(new Animation() {

                        public boolean animate() {
                            draggedX = motion.getValue();
                            if (motion.isFinished()) {
                                dragActivated = false;
                                Display.getInstance().getCurrent().setGlassPane(null);
                                deregisterAnimated(this);
                            }
                            return true;
                        }

                        public void paint(Graphics g) {
                            repaint();
                        }
                    });
                    return;
                }
            }
            if (dragActivated || rightPanel.contains(x, y)) {
                setMenuGlassPane(menu, placement);
                draggedX = x;
                int start = x;
                int end = 0;
                if (isRTLValue) {
                    end = getWidth();
                }
                final Motion motion = Motion.createEaseInOutMotion(start, end, getUIManager().getThemeConstant("sideMenuAnimSpeedInt", 300));
                motion.start();
                manualMotionLock = true;
                sideSwipePotential = false;
                rightSideSwipePotential = false;
                topSwipePotential = false;
                registerAnimated(new Animation() {

                    public boolean animate() {
                        draggedX = motion.getValue();
                        if (motion.isFinished()) {
                            dragActivated = false;
                        }
                        return true;
                    }

                    public void paint(Graphics g) {
                        repaint();
                        if (draggedX == motion.getDestinationValue() && motion.isFinished()) {
                            parent.setTransitionInAnimator(CommonTransitions.createEmpty());
                            parent.show();
                            deregisterAnimated(this);
                            Display.getInstance().callSerially(new Runnable() {

                                public void run() {
                                    clean();
                                }
                            });
                        }
                    }
                });
            }
        }

        public void keyReleased(int keyCode) {
            if (keyCode == leftSK) {
                if (transitionRunning) {
                    return;
                }
                closeMenu();
                return;
            }
            super.keyReleased(keyCode);
        }
    };
    m.setScrollable(false);
    m.removeComponentFromForm(m.getTitleArea());
    m.putClientProperty("Menu", "true");
    m.setTransitionInAnimator(CommonTransitions.createEmpty());
    m.setTransitionOutAnimator(CommonTransitions.createEmpty());
    m.setBackCommand(new Command("") {

        public void actionPerformed(ActionEvent evt) {
            if (transitionRunning) {
                return;
            }
            closeMenu();
        }
    });
    m.setLayout(new BorderLayout());
    if (Display.getInstance().areMutableImagesFast()) {
        rightPanel = new Container(new BorderLayout());
    } else {
        rightPanel = new Container(new BorderLayout()) {

            public void paintBackground(Graphics g) {
            }

            public void paintBackgrounds(Graphics g) {
            }

            public void paint(Graphics g) {
                Component c = (Component) rightPanel.getClientProperty("$parent");
                // not sure why its happening: https://code.google.com/p/codenameone/issues/detail?id=1072
                if (c != null) {
                    boolean b = c.isVisible();
                    c.setVisible(true);
                    int x = getAbsoluteX();
                    g.translate(x, 0);
                    Container.sidemenuBarTranslation = x;
                    c.paintComponent(g, true);
                    Container.sidemenuBarTranslation = 0;
                    g.translate(-x, 0);
                    c.setVisible(b);
                }
            }
        };
    }
    if (placement == COMMAND_PLACEMENT_VALUE_TOP) {
        int v = 0;
        if (Display.getInstance().isPortrait()) {
            if (Display.getInstance().isTablet()) {
                v = getUIManager().getThemeConstant("topMenuSizeTabPortraitInt", -1);
                if (v < 0) {
                    v = m.getHeight() * 2 / 3;
                } else {
                    v = m.getHeight() / 100 * v;
                }
            } else {
                v = getUIManager().getThemeConstant("topMenuSizePortraitInt", -1);
                if (v < 0) {
                    v = openButton.getHeight();
                } else {
                    v = m.getHeight() / 100 * v;
                }
            }
        } else {
            if (Display.getInstance().isTablet()) {
                v = getUIManager().getThemeConstant("topMenuSizeTabLandscapeInt", -1);
                if (v < 0) {
                    v = m.getHeight() * 3 / 4;
                } else {
                    v = m.getWidth() / 100 * v;
                }
            } else {
                v = getUIManager().getThemeConstant("topMenuSizeLandscapeInt", -1);
                if (v < 0) {
                    v = m.getHeight() * 4 / 10;
                } else {
                    v = m.getHeight() / 100 * v;
                }
            }
        }
        rightPanel.setPreferredH(v);
    } else {
        if (Display.getInstance().isPortrait()) {
            int v = 0;
            if (Display.getInstance().isTablet()) {
                v = getUIManager().getThemeConstant("sideMenuSizeTabPortraitInt", -1);
                if (v < 0) {
                    v = m.getWidth() * 2 / 3;
                } else {
                    v = m.getWidth() / 100 * v;
                }
            } else {
                v = getUIManager().getThemeConstant("sideMenuSizePortraitInt", -1);
                if (v < 0) {
                    if (placement == COMMAND_PLACEMENT_VALUE_RIGHT) {
                        if (isRTL()) {
                            v = openButton.getWidth();
                        } else {
                            v = rightSideButton.getWidth();
                        }
                    } else {
                        v = openButton.getWidth();
                    }
                } else {
                    v = m.getWidth() / 100 * v;
                }
            }
            rightPanel.setPreferredW(v);
        } else {
            int v = 0;
            if (Display.getInstance().isTablet()) {
                v = getUIManager().getThemeConstant("sideMenuSizeTabLandscapeInt", -1);
                if (v < 0) {
                    v = m.getWidth() * 3 / 4;
                } else {
                    v = m.getWidth() / 100 * v;
                }
            } else {
                v = getUIManager().getThemeConstant("sideMenuSizeLandscapeInt", -1);
                if (v < 0) {
                    v = m.getWidth() * 4 / 10;
                } else {
                    v = m.getWidth() / 100 * v;
                }
            }
            rightPanel.setPreferredW(v);
        }
    }
    if (sidePanel != null) {
        sidePanel.removeAll();
        sidePanel = null;
    }
    sidePanel = createSideNavigationComponent(getCommands(), placement);
    if (placement == COMMAND_PLACEMENT_VALUE_RIGHT) {
        m.addComponent(BorderLayout.WEST, rightPanel);
        m.addComponent(BorderLayout.CENTER, sidePanel);
    } else {
        if (placement == COMMAND_PLACEMENT_VALUE_TOP) {
            m.addComponent(BorderLayout.NORTH, rightPanel);
            m.addComponent(BorderLayout.CENTER, sidePanel);
            Button button = new Button(" ");
            button.setUIID("Container");
            button.setPreferredH(Display.getInstance().getDisplayHeight() / 10);
            m.addComponent(BorderLayout.SOUTH, button);
            button.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent evt) {
                    closeMenu();
                }
            });
        } else {
            m.addComponent(BorderLayout.EAST, rightPanel);
            m.addComponent(BorderLayout.CENTER, sidePanel);
        }
    }
    m.putClientProperty("cn1$sideMenuParent", this);
    return m;
}
Also used : Motion(com.codename1.ui.animations.Motion) ActionEvent(com.codename1.ui.events.ActionEvent) BorderLayout(com.codename1.ui.layouts.BorderLayout) ActionListener(com.codename1.ui.events.ActionListener) Style(com.codename1.ui.plaf.Style) Animation(com.codename1.ui.animations.Animation)

Example 15 with Animation

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

the class CodenameOneImplementation method paintDirty.

/**
 * Invoked by the EDT to paint the dirty regions
 */
public void paintDirty() {
    int size = 0;
    synchronized (displayLock) {
        size = paintQueueFill;
        Animation[] array = paintQueue;
        paintQueue = paintQueueTemp;
        paintQueueTemp = array;
        paintQueueFill = 0;
    }
    if (size > 0) {
        Graphics wrapper = getCodenameOneGraphics();
        int dwidth = getDisplayWidth();
        int dheight = getDisplayHeight();
        int topX = dwidth;
        int topY = dheight;
        int bottomX = 0;
        int bottomY = 0;
        for (int iter = 0; iter < size; iter++) {
            Animation ani = paintQueueTemp[iter];
            // might happen due to paint queue removal
            if (ani == null) {
                continue;
            }
            paintQueueTemp[iter] = null;
            wrapper.translate(-wrapper.getTranslateX(), -wrapper.getTranslateY());
            wrapper.setClip(0, 0, dwidth, dheight);
            if (ani instanceof Component) {
                Component cmp = (Component) ani;
                Rectangle dirty = cmp.getDirtyRegion();
                if (dirty != null) {
                    Dimension d = dirty.getSize();
                    wrapper.setClip(dirty.getX(), dirty.getY(), d.getWidth(), d.getHeight());
                    cmp.setDirtyRegion(null);
                }
                cmp.paintComponent(wrapper);
                getPaintableBounds(cmp, paintDirtyTmpRect);
                int cmpAbsX = paintDirtyTmpRect.getX();
                topX = Math.min(cmpAbsX, topX);
                bottomX = Math.max(cmpAbsX + paintDirtyTmpRect.getWidth(), bottomX);
                int cmpAbsY = paintDirtyTmpRect.getY();
                topY = Math.min(cmpAbsY, topY);
                bottomY = Math.max(cmpAbsY + paintDirtyTmpRect.getHeight(), bottomY);
            } else {
                bottomX = dwidth;
                bottomY = dheight;
                topX = 0;
                topY = 0;
                ani.paint(wrapper);
            }
        }
        paintOverlay(wrapper);
        // Log.p("Flushing graphics : "+topX+","+topY+","+bottomX+","+bottomY);
        flushGraphics(topX, topY, bottomX - topX, bottomY - topY);
    }
}
Also used : Rectangle(com.codename1.ui.geom.Rectangle) Animation(com.codename1.ui.animations.Animation) Dimension(com.codename1.ui.geom.Dimension)

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