Search in sources :

Example 1 with ComponentAnimation

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

the class Component method createStyleAnimation.

/**
 * Creates an animation that will transform the current component to the styling of the destination UIID when
 * completed. Notice that fonts will only animate within the truetype and native familiy and we recommend that you
 * don't shift weight/typeface/style as this might diminish the effect.<br>
 * <b>Important: </b> Only unselected styles are animated but once the animation completes all styles are applied.
 * @param destUIID the UIID to which this component will gradually shift
 * @param duration the duration of the animation or the number of steps
 * @return an animation component that can either be stepped or played
 */
public ComponentAnimation createStyleAnimation(final String destUIID, final int duration) {
    final Style sourceStyle = getUnselectedStyle();
    final Style destStyle = hasInlineUnselectedStyle() ? getUIManager().parseComponentStyle(getInlineStylesTheme(), destUIID, getInlineStylesUIID(destUIID), getInlineUnselectedStyleStrings()) : getUIManager().getComponentStyle(destUIID);
    return createStyleAnimation(sourceStyle, destStyle, duration, destUIID);
}
Also used : Style(com.codename1.ui.plaf.Style)

Example 2 with ComponentAnimation

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

the class Component method createStyleAnimation.

ComponentAnimation createStyleAnimation(final Style sourceStyle, final Style destStyle, final int duration, final String destUIID) {
    int d = duration;
    Motion m = null;
    if (sourceStyle.getFgColor() != destStyle.getFgColor()) {
        m = Motion.createLinearColorMotion(sourceStyle.getFgColor(), destStyle.getFgColor(), d);
    }
    final Motion fgColorMotion = m;
    m = null;
    if (sourceStyle.getOpacity() != destStyle.getOpacity()) {
        m = Motion.createLinearColorMotion(sourceStyle.getOpacity(), destStyle.getOpacity(), d);
    }
    final Motion opacityMotion = m;
    m = null;
    if (sourceStyle.getFont().getHeight() != destStyle.getFont().getHeight() && sourceStyle.getFont().isTTFNativeFont()) {
        // allows for fractional font sizes
        m = Motion.createLinearMotion(sourceStyle.getFont().getHeight() * 100, destStyle.getFont().getHeight() * 100, d);
    }
    final Motion fontMotion = m;
    m = null;
    if (sourceStyle.getPaddingTop() != destStyle.getPaddingTop()) {
        m = Motion.createLinearMotion(sourceStyle.getPaddingTop(), destStyle.getPaddingTop(), d);
    }
    final Motion paddingTop = m;
    m = null;
    if (sourceStyle.getPaddingBottom() != destStyle.getPaddingBottom()) {
        m = Motion.createLinearMotion(sourceStyle.getPaddingBottom(), destStyle.getPaddingBottom(), d);
    }
    final Motion paddingBottom = m;
    m = null;
    if (sourceStyle.getPaddingLeftNoRTL() != destStyle.getPaddingLeftNoRTL()) {
        m = Motion.createLinearMotion(sourceStyle.getPaddingLeftNoRTL(), destStyle.getPaddingLeftNoRTL(), d);
    }
    final Motion paddingLeft = m;
    m = null;
    if (sourceStyle.getPaddingRightNoRTL() != destStyle.getPaddingRightNoRTL()) {
        m = Motion.createLinearMotion(sourceStyle.getPaddingRightNoRTL(), destStyle.getPaddingRightNoRTL(), d);
    }
    final Motion paddingRight = m;
    m = null;
    if (sourceStyle.getMarginTop() != destStyle.getMarginTop()) {
        m = Motion.createLinearMotion(sourceStyle.getMarginTop(), destStyle.getMarginTop(), d);
    }
    final Motion marginTop = m;
    m = null;
    if (sourceStyle.getMarginBottom() != destStyle.getMarginBottom()) {
        m = Motion.createLinearMotion(sourceStyle.getMarginBottom(), destStyle.getMarginBottom(), d);
    }
    final Motion marginBottom = m;
    m = null;
    if (sourceStyle.getMarginLeftNoRTL() != destStyle.getMarginLeftNoRTL()) {
        m = Motion.createLinearMotion(sourceStyle.getMarginLeftNoRTL(), destStyle.getMarginLeftNoRTL(), d);
    }
    final Motion marginLeft = m;
    m = null;
    if (sourceStyle.getMarginRightNoRTL() != destStyle.getMarginRightNoRTL()) {
        m = Motion.createLinearMotion(sourceStyle.getMarginRightNoRTL(), destStyle.getMarginRightNoRTL(), d);
    }
    final Motion marginRight = m;
    m = null;
    if (paddingLeft != null || paddingRight != null || paddingTop != null || paddingBottom != null) {
        // convert the padding to pixels for smooth animation
        int left = sourceStyle.getPaddingLeftNoRTL();
        int right = sourceStyle.getPaddingRightNoRTL();
        int top = sourceStyle.getPaddingTop();
        int bottom = sourceStyle.getPaddingBottom();
        sourceStyle.setPaddingUnit(Style.UNIT_TYPE_PIXELS, Style.UNIT_TYPE_PIXELS, Style.UNIT_TYPE_PIXELS, Style.UNIT_TYPE_PIXELS);
        sourceStyle.setPadding(top, bottom, left, right);
    }
    if (marginLeft != null || marginRight != null || marginTop != null || marginBottom != null) {
        // convert the margin to pixels for smooth animation
        int left = sourceStyle.getMarginLeftNoRTL();
        int right = sourceStyle.getMarginRightNoRTL();
        int top = sourceStyle.getMarginTop();
        int bottom = sourceStyle.getMarginBottom();
        sourceStyle.setMarginUnit(Style.UNIT_TYPE_PIXELS, Style.UNIT_TYPE_PIXELS, Style.UNIT_TYPE_PIXELS, Style.UNIT_TYPE_PIXELS);
        sourceStyle.setMargin(top, bottom, left, right);
    }
    final AnimationTransitionPainter ap = new AnimationTransitionPainter();
    if (sourceStyle.getBgTransparency() != 0 || destStyle.getBgTransparency() != 0 || (sourceStyle.getBorder() != null && sourceStyle.getBorder().isEmptyBorder()) || (destStyle.getBorder() != null && destStyle.getBorder().isEmptyBorder()) || sourceStyle.getBgImage() != null || destStyle.getBgImage() != null) {
        ap.original = sourceStyle.getBgPainter();
        ap.dest = destStyle.getBgPainter();
        ap.originalStyle = sourceStyle;
        ap.destStyle = destStyle;
        if (ap.dest == null) {
            ap.dest = new BGPainter();
        }
        sourceStyle.setBgPainter(ap);
    }
    final Motion bgMotion = Motion.createLinearMotion(0, 255, d);
    return new ComponentAnimation() {

        private boolean finished;

        private boolean stepMode;

        private boolean started;

        @Override
        public boolean isStepModeSupported() {
            return true;
        }

        @Override
        public int getMaxSteps() {
            return duration;
        }

        @Override
        public void setStep(int step) {
            stepMode = true;
            if (!finished) {
                if (bgMotion != null) {
                    bgMotion.setCurrentMotionTime(step);
                }
                if (fgColorMotion != null) {
                    fgColorMotion.setCurrentMotionTime(step);
                }
                if (opacityMotion != null) {
                    opacityMotion.setCurrentMotionTime(step);
                }
                if (fontMotion != null) {
                    fontMotion.setCurrentMotionTime(step);
                }
                if (paddingTop != null) {
                    paddingTop.setCurrentMotionTime(step);
                }
                if (paddingBottom != null) {
                    paddingBottom.setCurrentMotionTime(step);
                }
                if (paddingLeft != null) {
                    paddingLeft.setCurrentMotionTime(step);
                }
                if (paddingRight != null) {
                    paddingRight.setCurrentMotionTime(step);
                }
                if (marginTop != null) {
                    marginTop.setCurrentMotionTime(step);
                }
                if (marginBottom != null) {
                    marginBottom.setCurrentMotionTime(step);
                }
                if (marginLeft != null) {
                    marginLeft.setCurrentMotionTime(step);
                }
                if (marginRight != null) {
                    marginRight.setCurrentMotionTime(step);
                }
            }
            super.setStep(step);
        }

        @Override
        public boolean isInProgress() {
            if (!stepMode && !started) {
                return true;
            }
            return stepMode || !((bgMotion == null || bgMotion.isFinished()) && (opacityMotion == null || opacityMotion.isFinished()) && (fgColorMotion == null || fgColorMotion.isFinished()) && (paddingLeft == null || paddingLeft.isFinished()) && (paddingRight == null || paddingRight.isFinished()) && (paddingTop == null || paddingTop.isFinished()) && (paddingBottom == null || paddingBottom.isFinished()) && (marginLeft == null || marginLeft.isFinished()) && (marginRight == null || marginRight.isFinished()) && (marginTop == null || marginTop.isFinished()) && (marginBottom == null || marginBottom.isFinished()) && (fontMotion == null || fontMotion.isFinished()));
        }

        @Override
        protected void updateState() {
            if (finished) {
                return;
            }
            if (!started && !stepMode) {
                started = true;
                if (bgMotion != null) {
                    bgMotion.start();
                }
                if (opacityMotion != null) {
                    opacityMotion.start();
                }
                if (fgColorMotion != null) {
                    fgColorMotion.start();
                }
                if (fontMotion != null) {
                    fontMotion.start();
                }
                if (paddingTop != null) {
                    paddingTop.start();
                }
                if (paddingBottom != null) {
                    paddingBottom.start();
                }
                if (paddingLeft != null) {
                    paddingLeft.start();
                }
                if (paddingRight != null) {
                    paddingRight.start();
                }
                if (marginTop != null) {
                    marginTop.start();
                }
                if (marginBottom != null) {
                    marginBottom.start();
                }
                if (marginLeft != null) {
                    marginLeft.start();
                }
                if (marginRight != null) {
                    marginRight.start();
                }
            }
            if (!isInProgress()) {
                finished = true;
                if (destUIID != null) {
                    setUIID(destUIID);
                }
            } else {
                if (opacityMotion != null) {
                    sourceStyle.setOpacity(opacityMotion.getValue());
                }
                if (fgColorMotion != null) {
                    sourceStyle.setFgColor(fgColorMotion.getValue());
                }
                if (bgMotion != null) {
                    ap.alpha = bgMotion.getValue();
                }
                if (fontMotion != null) {
                    Font fnt = sourceStyle.getFont();
                    fnt = fnt.derive(((float) fontMotion.getValue()) / 100.0f, fnt.getStyle());
                    sourceStyle.setFont(fnt);
                }
                if (paddingTop != null) {
                    sourceStyle.setPadding(TOP, paddingTop.getValue());
                }
                if (paddingBottom != null) {
                    sourceStyle.setPadding(BOTTOM, paddingBottom.getValue());
                }
                if (paddingLeft != null) {
                    sourceStyle.setPadding(LEFT, paddingLeft.getValue());
                }
                if (paddingRight != null) {
                    sourceStyle.setPadding(RIGHT, paddingRight.getValue());
                }
                if (marginTop != null) {
                    sourceStyle.setMargin(TOP, marginTop.getValue());
                }
                if (marginBottom != null) {
                    sourceStyle.setMargin(BOTTOM, marginBottom.getValue());
                }
                if (marginLeft != null) {
                    sourceStyle.setMargin(LEFT, marginLeft.getValue());
                }
                if (marginRight != null) {
                    sourceStyle.setMargin(RIGHT, marginRight.getValue());
                }
            }
        }

        @Override
        public void flush() {
            if (bgMotion != null) {
                bgMotion.finish();
            }
            if (opacityMotion != null) {
                opacityMotion.finish();
            }
            if (fgColorMotion != null) {
                fgColorMotion.finish();
            }
            if (fontMotion != null) {
                fontMotion.finish();
            }
            if (paddingTop != null) {
                paddingTop.finish();
            }
            if (paddingBottom != null) {
                paddingBottom.finish();
            }
            if (paddingLeft != null) {
                paddingLeft.finish();
            }
            if (paddingRight != null) {
                paddingRight.finish();
            }
            if (marginTop != null) {
                marginTop.finish();
            }
            if (marginBottom != null) {
                marginBottom.finish();
            }
            if (marginLeft != null) {
                marginLeft.finish();
            }
            if (marginRight != null) {
                marginRight.finish();
            }
            updateState();
        }
    };
}
Also used : Motion(com.codename1.ui.animations.Motion) ComponentAnimation(com.codename1.ui.animations.ComponentAnimation) Point(com.codename1.ui.geom.Point)

Example 3 with ComponentAnimation

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

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

the class Container method wrapInLayeredPane.

/**
 * An atomic operation that wraps the current component in a Container with
 * a layered layout.  This prevents us from having to initialize and deinitialize
 * all of the components in a sub-tree because we want to re-root it.  In particular
 * Form.getLayeredPane() re-roots the entire content pane the first time it is
 * called on a form.  If the form contains native peers there is a flicker which
 * is quite annoying.  Providing a way to do this atomically results in a better
 * user experience.
 * @return The Container that is the new parent of this component.
 */
Container wrapInLayeredPane() {
    final Container oldParent = getParent();
    final Container newParent = new Container(new LayeredLayout());
    final Layout parentLayout = oldParent != null && oldParent.layout != null ? oldParent.layout : null;
    final Object constraint = parentLayout != null ? parentLayout.getComponentConstraint(this) : null;
    newParent.setParent(oldParent);
    newParent.components.add(this);
    final Runnable r = new Runnable() {

        public void run() {
            if (parentLayout != null) {
                parentLayout.removeLayoutComponent(Container.this);
                parentLayout.addLayoutComponent(constraint, newParent, oldParent);
            }
            newParent.initComponentImpl();
            if (oldParent != null) {
                int cmpIndex = -1;
                for (int i = 0; i < oldParent.getComponentCount(); i++) {
                    Component c = oldParent.getComponentAt(i);
                    if (c.equals(Container.this)) {
                        cmpIndex = i;
                        break;
                    }
                }
                // int cmpIndex = oldParent.getComponentIndex(Container.this);  <---  WTF... this always returns -1!!
                if (cmpIndex == -1) {
                    throw new RuntimeException("WTF we have parent but no index!!!!");
                }
                oldParent.components.set(cmpIndex, newParent);
            }
            Container.this.setParent(newParent);
            newParent.revalidate();
        }
    };
    AnimationManager a = getAnimationManager();
    if (a != null && a.isAnimating()) {
        a.addAnimation(new ComponentAnimation() {

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

            @Override
            protected void updateState() {
                r.run();
            }
        });
        return newParent;
    } else {
        r.run();
        return newParent;
    }
}
Also used : ComponentAnimation(com.codename1.ui.animations.ComponentAnimation) Layout(com.codename1.ui.layouts.Layout) BoxLayout(com.codename1.ui.layouts.BoxLayout) FlowLayout(com.codename1.ui.layouts.FlowLayout) LayeredLayout(com.codename1.ui.layouts.LayeredLayout) BorderLayout(com.codename1.ui.layouts.BorderLayout) LayeredLayout(com.codename1.ui.layouts.LayeredLayout)

Example 5 with ComponentAnimation

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

the class Container method insertComponentAt.

void insertComponentAt(final int index, final Object constraint, final Component cmp) {
    AnimationManager a = getAnimationManager();
    if (a != null && a.isAnimating()) {
        // pretend like the component was already added
        if (cmp.getParent() != null) {
            throw new IllegalArgumentException("Component is already contained in Container: " + cmp.getParent());
        }
        cmp.setParent(this);
        a.addAnimation(new ComponentAnimation() {

            private boolean alreadyAdded;

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

            @Override
            protected void updateState() {
                if (!alreadyAdded) {
                    alreadyAdded = true;
                    cmp.setParent(null);
                    if (constraint != null) {
                        layout.addLayoutComponent(constraint, cmp, Container.this);
                    }
                    insertComponentAtImpl(index, cmp);
                    revalidate();
                }
            }

            @Override
            public void flush() {
                updateState();
            }
        });
    } else {
        if (constraint != null) {
            layout.addLayoutComponent(constraint, cmp, this);
        }
        insertComponentAtImpl(index, cmp);
    }
}
Also used : ComponentAnimation(com.codename1.ui.animations.ComponentAnimation)

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