Search in sources :

Example 6 with ComponentAnimation

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

the class Container method animateHierarchy.

/**
 * Animates a pending layout into place, this effectively replaces revalidate with a more visual form of animation
 *
 * @param duration the duration in milliseconds for the animation
 */
private ComponentAnimation animateHierarchy(final int duration, boolean wait, int opacity, boolean add) {
    setShouldCalcPreferredSize(true);
    enableLayoutOnPaint = false;
    dontRecurseContainer = true;
    Vector comps = new Vector();
    findComponentsInHierachy(comps);
    final int componentCount = comps.size();
    int[] beforeX = new int[componentCount];
    int[] beforeY = new int[componentCount];
    int[] beforeW = new int[componentCount];
    int[] beforeH = new int[componentCount];
    final Motion[] xMotions = new Motion[componentCount];
    final Motion[] yMotions = new Motion[componentCount];
    final Motion[] wMotions = new Motion[componentCount];
    final Motion[] hMotions = new Motion[componentCount];
    for (int iter = 0; iter < componentCount; iter++) {
        Component current = (Component) comps.elementAt(iter);
        beforeX[iter] = current.getX();
        beforeY[iter] = current.getY();
        beforeW[iter] = current.getWidth();
        beforeH[iter] = current.getHeight();
    }
    layoutContainer();
    for (int iter = 0; iter < componentCount; iter++) {
        Component current = (Component) comps.elementAt(iter);
        xMotions[iter] = createAnimateMotion(beforeX[iter], current.getX(), duration);
        yMotions[iter] = createAnimateMotion(beforeY[iter], current.getY(), duration);
        wMotions[iter] = createAnimateMotion(beforeW[iter], current.getWidth(), duration);
        hMotions[iter] = createAnimateMotion(beforeH[iter], current.getHeight(), duration);
        xMotions[iter].start();
        yMotions[iter].start();
        wMotions[iter].start();
        hMotions[iter].start();
        current.setX(beforeX[iter]);
        current.setY(beforeY[iter]);
        current.setWidth(beforeW[iter]);
        current.setHeight(beforeH[iter]);
    }
    MorphAnimation a = new MorphAnimation(this, duration, new Motion[][] { xMotions, yMotions, wMotions, hMotions });
    setAnimOpacity(opacity, 255, a, componentCount, duration);
    a.animatedComponents = comps;
    if (add) {
        if (wait) {
            getAnimationManager().addAnimationAndBlock(a);
        } else {
            getAnimationManager().addAnimation(a);
        }
    }
    return a;
}
Also used : Motion(com.codename1.ui.animations.Motion) Vector(java.util.Vector)

Example 7 with ComponentAnimation

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

the class Container method removeComponentImpl.

void removeComponentImpl(final Component cmp) {
    AnimationManager a = getAnimationManager();
    if (a != null && a.isAnimating()) {
        // pretend like the component was already removed
        layout.removeLayoutComponent(cmp);
        cmp.setParent(null);
        a.addAnimation(new ComponentAnimation() {

            private boolean alreadyRemoved;

            @Override
            public boolean isInProgress() {
                return false;
            }

            @Override
            protected void updateState() {
                if (!alreadyRemoved) {
                    alreadyRemoved = true;
                    removeComponentImplNoAnimationSafety(cmp);
                    revalidate();
                }
            }

            @Override
            public void flush() {
                updateAnimationState();
            }
        });
    } else {
        removeComponentImplNoAnimationSafety(cmp);
    }
}
Also used : ComponentAnimation(com.codename1.ui.animations.ComponentAnimation)

Example 8 with ComponentAnimation

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

the class ComponentSelector method fadeIn.

/**
 * Fade in this set of components.  Prior to calling this, the component visibility should
 * be set to "false".
 * @param duration The duration of the fade in.
 * @param callback Callback to run when animation completes.
 * @return
 */
public ComponentSelector fadeIn(final int duration, final SuccessCallback<ComponentSelector> callback) {
    final String placeholderProperty = "com.codename1.ui.ComponentSelector#fadeOutPlaceholder";
    AnimationManager mgr = null;
    ArrayList<ComponentAnimation> animations1 = new ArrayList<ComponentAnimation>();
    final ArrayList<ComponentAnimation> animations2 = 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.getStyle().setBgColor(0xff0000);
                // placeholder.getStyle().setBgTransparency(255);
                // 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));
                // System.out.println("Placeholder height "+c.getHeight());
                // parent.replace(c, placeholder, false);
                // c.setHidden(false);
                ComponentAnimation a = parent.createReplaceTransition(c, placeholder, CommonTransitions.createEmpty());
                animations1.add(a);
                animatingComponents.add(c);
            }
        // centerBackground.add(BorderLayout.CENTER, boxy);
        }
    }
    if (mgr != null) {
        mgr.addAnimation(ComponentAnimation.compoundAnimation(animations1.toArray(new ComponentAnimation[animations1.size()])), new Runnable() {

            public void run() {
                AnimationManager mgr = null;
                for (final Component c : animatingComponents) {
                    Container placeholder = (Container) c.getClientProperty(placeholderProperty);
                    if (placeholder != null) {
                        // System.out.println("Placeholder height after replace "+(c.getHeight() + c.getStyle().getMarginBottom() + c.getStyle().getMarginTop()));
                        // System.out.println("Placeholder not null");
                        c.putClientProperty(placeholderProperty, null);
                        AnimationManager cmgr = placeholder.getAnimationManager();
                        if (cmgr != null) {
                            // System.out.println("Animation manager not null");
                            mgr = cmgr;
                            c.setVisible(true);
                            Container parent = placeholder.getParent();
                            if (parent != null) {
                                // System.out.println("Parent not null");
                                ComponentAnimation a = parent.createReplaceTransition(placeholder, c, CommonTransitions.createFade(duration));
                                animations2.add(a);
                            }
                        }
                    }
                }
                if (mgr != null) {
                    final AnimationManager fmgr = mgr;
                    $(new Runnable() {

                        public void run() {
                            fmgr.addAnimation(ComponentAnimation.compoundAnimation(animations2.toArray(new ComponentAnimation[animations2.size()])), new Runnable() {

                                public void run() {
                                    if (callback != null) {
                                        callback.onSucess(ComponentSelector.this);
                                    }
                                }
                            });
                        }
                    });
                }
            }
        });
    }
    return this;
}
Also used : ComponentAnimation(com.codename1.ui.animations.ComponentAnimation) ArrayList(java.util.ArrayList)

Example 9 with ComponentAnimation

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

the class ComponentSelector method fadeInAndWait.

/**
 * Fades in this component and blocks until animation is complete.
 * @param duration The duration of the animation.
 * @return Self for chaining.
 */
public ComponentSelector fadeInAndWait(final int duration) {
    final String placeholderProperty = "com.codename1.ui.ComponentSelector#fadeOutPlaceholder";
    AnimationManager mgr = null;
    ArrayList<ComponentAnimation> animations1 = new ArrayList<ComponentAnimation>();
    final ArrayList<ComponentAnimation> animations2 = 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.getStyle().setBgColor(0xff0000);
                // placeholder.getStyle().setBgTransparency(255);
                // 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));
                // System.out.println("Placeholder height "+c.getHeight());
                // parent.replace(c, placeholder, false);
                // c.setHidden(false);
                ComponentAnimation a = parent.createReplaceTransition(c, placeholder, CommonTransitions.createEmpty());
                animations1.add(a);
                animatingComponents.add(c);
            }
        // centerBackground.add(BorderLayout.CENTER, boxy);
        }
    }
    if (mgr != null) {
        mgr.addAnimationAndBlock(ComponentAnimation.compoundAnimation(animations1.toArray(new ComponentAnimation[animations1.size()])));
        for (final Component c : animatingComponents) {
            Container placeholder = (Container) c.getClientProperty(placeholderProperty);
            if (placeholder != null) {
                // System.out.println("Placeholder height after replace "+(c.getHeight() + c.getStyle().getMarginBottom() + c.getStyle().getMarginTop()));
                // System.out.println("Placeholder not null");
                c.putClientProperty(placeholderProperty, null);
                AnimationManager cmgr = placeholder.getAnimationManager();
                if (cmgr != null) {
                    // System.out.println("Animation manager not null");
                    mgr = cmgr;
                    c.setVisible(true);
                    Container parent = placeholder.getParent();
                    if (parent != null) {
                        // System.out.println("Parent not null");
                        ComponentAnimation a = parent.createReplaceTransition(placeholder, c, CommonTransitions.createFade(duration));
                        animations2.add(a);
                    }
                }
            }
        }
        if (mgr != null) {
            mgr.addAnimationAndBlock(ComponentAnimation.compoundAnimation(animations2.toArray(new ComponentAnimation[animations2.size()])));
        }
    }
    return this;
}
Also used : ComponentAnimation(com.codename1.ui.animations.ComponentAnimation) ArrayList(java.util.ArrayList)

Example 10 with ComponentAnimation

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

the class ComponentSelector method animateStyle.

/**
 * Animates this set of components, replacing any modified style properties of the
 * destination style to the components.
 * @param destStyle The style to apply to the components via animation.
 * @param duration The duration of the animation (ms)
 * @param callback Callback to call after animation is complete.
 * @return Self for chaining
 * @see Component#createStyleAnimation(java.lang.String, int)
 */
public ComponentSelector animateStyle(Style destStyle, int duration, final SuccessCallback<ComponentSelector> callback) {
    ArrayList<ComponentAnimation> animations = new ArrayList<ComponentAnimation>();
    AnimationManager mgr = null;
    for (Component c : this) {
        AnimationManager cmgr = c.getAnimationManager();
        if (cmgr != null) {
            mgr = cmgr;
            Style sourceStyle = c.getUnselectedStyle();
            destStyle.merge(sourceStyle);
            animations.add(c.createStyleAnimation(sourceStyle, destStyle, duration, null));
        }
    }
    if (mgr != null) {
        ComponentAnimation anim = ComponentAnimation.compoundAnimation(animations.toArray(new ComponentAnimation[animations.size()]));
        mgr.addAnimation(anim, new Runnable() {

            public void run() {
                if (callback != null) {
                    callback.onSucess(ComponentSelector.this);
                }
            }
        });
    }
    return this;
}
Also used : ComponentAnimation(com.codename1.ui.animations.ComponentAnimation) ArrayList(java.util.ArrayList) Style(com.codename1.ui.plaf.Style)

Aggregations

ComponentAnimation (com.codename1.ui.animations.ComponentAnimation)10 ArrayList (java.util.ArrayList)5 Motion (com.codename1.ui.animations.Motion)2 Style (com.codename1.ui.plaf.Style)2 Point (com.codename1.ui.geom.Point)1 BorderLayout (com.codename1.ui.layouts.BorderLayout)1 BoxLayout (com.codename1.ui.layouts.BoxLayout)1 FlowLayout (com.codename1.ui.layouts.FlowLayout)1 LayeredLayout (com.codename1.ui.layouts.LayeredLayout)1 Layout (com.codename1.ui.layouts.Layout)1 Vector (java.util.Vector)1