Search in sources :

Example 6 with AnimationObject

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

the class PulsateEditor method updateTimeline.

private void updateTimeline() {
    AnimationObject[] anim = new AnimationObject[get(frames) * 2 - 1];
    internalImages = new EncodedImage[get(frames)];
    int small = Math.min(get(smallSize), get(largeSize));
    int large = Math.max(get(smallSize), get(largeSize));
    int dur = get(duration);
    int segment = dur / anim.length;
    Motion calculator = Motion.createSplineMotion(small, large, dur / 2);
    float ratioOfLargest = ((float) large) / 100.0f;
    int timelineWidth = (int) (ratioOfLargest * ((float) sourceImage.getWidth()));
    int timelineHeight = (int) (ratioOfLargest * ((float) sourceImage.getHeight()));
    for (int iter = 0; iter < internalImages.length; iter++) {
        calculator.setCurrentMotionTime(segment * iter);
        int percentAtTime = calculator.getValue();
        float ratioAtTime = ((float) percentAtTime) / 100.0f;
        int currentWidth = (int) (sourceImage.getWidth() * ratioAtTime);
        int currentHeight = (int) (sourceImage.getHeight() * ratioAtTime);
        internalImages[iter] = EncodedImage.create(scale(sourceImage, currentWidth, currentHeight));
        anim[iter] = AnimationObject.createAnimationImage(internalImages[iter], (timelineWidth - currentWidth) / 2, (timelineHeight - currentHeight) / 2);
        anim[iter].setStartTime(segment * iter);
        anim[iter].setEndTime(segment * iter + segment);
        // peek at the next frame to calculate the diff
        calculator.setCurrentMotionTime(segment * iter + segment);
        int percentAtNextFrame = calculator.getValue();
        float ratioAtNextFrame = ((float) percentAtNextFrame) / 100.0f;
        int nextWidth = (int) (sourceImage.getWidth() * ratioAtNextFrame);
        int nextHeight = (int) (sourceImage.getHeight() * ratioAtNextFrame);
        anim[iter].defineHeight(AnimationObject.MOTION_TYPE_SPLINE, segment * iter, segment, currentHeight, nextHeight);
        anim[iter].defineWidth(AnimationObject.MOTION_TYPE_SPLINE, segment * iter, segment, currentWidth, nextWidth);
        anim[iter].defineMotionX(AnimationObject.MOTION_TYPE_SPLINE, segment * iter, segment, (timelineWidth - currentWidth) / 2, (timelineWidth - nextWidth) / 2);
        anim[iter].defineMotionY(AnimationObject.MOTION_TYPE_SPLINE, segment * iter, segment, (timelineHeight - currentHeight) / 2, (timelineHeight - nextHeight) / 2);
        // create the "inverse" shrinking animation object
        if (iter > 0) {
            int nextOffset = anim.length - iter;
            anim[nextOffset] = AnimationObject.createAnimationImage(internalImages[iter], (timelineWidth - currentWidth) / 2, (timelineHeight - currentHeight) / 2);
            anim[nextOffset].setStartTime(segment * nextOffset);
            if (iter == 1) {
                // this resolves any rounding errors that might have occured in the creation of the frames
                anim[nextOffset].setEndTime(dur);
            } else {
                anim[nextOffset].setEndTime(segment * nextOffset + segment);
            }
            // peek at the previous frame to calculate the diff
            calculator.setCurrentMotionTime(segment * iter - segment);
            int percentAtPreviousFrame = calculator.getValue();
            float ratioAtPreviousFrame = ((float) percentAtPreviousFrame) / 100.0f;
            int previousWidth = (int) (sourceImage.getWidth() * ratioAtPreviousFrame);
            int previousHeight = (int) (sourceImage.getHeight() * ratioAtPreviousFrame);
            anim[nextOffset].defineHeight(AnimationObject.MOTION_TYPE_SPLINE, segment * nextOffset, segment, currentHeight, previousHeight);
            anim[nextOffset].defineWidth(AnimationObject.MOTION_TYPE_SPLINE, segment * nextOffset, segment, currentWidth, previousWidth);
            anim[nextOffset].defineMotionX(AnimationObject.MOTION_TYPE_SPLINE, segment * nextOffset, segment, (timelineWidth - currentWidth) / 2, (timelineWidth - previousWidth) / 2);
            anim[nextOffset].defineMotionY(AnimationObject.MOTION_TYPE_SPLINE, segment * nextOffset, segment, (timelineHeight - currentHeight) / 2, (timelineHeight - previousHeight) / 2);
        }
    }
    currentImage = Timeline.createTimeline(dur, anim, new com.codename1.ui.geom.Dimension(timelineWidth, timelineHeight));
    previewLabel.setIcon(currentImage);
    previewLabel.repaint();
    preview.repaint();
}
Also used : Motion(com.codename1.ui.animations.Motion) AnimationObject(com.codename1.ui.animations.AnimationObject)

Example 7 with AnimationObject

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

the class CodenameOneImageRenderer method mouseClicked.

public void mouseClicked(MouseEvent e) {
    Timeline t = (Timeline) image;
    AnimationObject selection = t.getAnimationAt(e.getX(), e.getY());
    // editing
    if (selection == null) {
        animationObjectList.clearSelection();
    } else {
        if (e.getClickCount() == 2) {
            selectValue(selection);
            MouseListener[] ls = animationObjectList.getListeners(MouseListener.class);
            for (MouseListener l : ls) {
                l.mouseClicked(e);
            }
        } else {
            // selection
            selectValue(selection);
        }
    }
    repaint();
}
Also used : Timeline(com.codename1.ui.animations.Timeline) MouseListener(java.awt.event.MouseListener) AnimationObject(com.codename1.ui.animations.AnimationObject)

Example 8 with AnimationObject

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

the class AnimationObjectEditor method getAnimationObject.

public AnimationObject getAnimationObject() {
    int x = ((Number) startX.getValue()).intValue();
    int y = ((Number) startY.getValue()).intValue();
    AnimationObject anim = AnimationObject.createAnimationImage(res.getImage((String) image.getSelectedItem()), x, y);
    anim.setStartTime(val(startTime));
    anim.setEndTime(val(duration) + val(startTime));
    if (xCheck.isSelected()) {
        anim.defineMotionX(motionTypeX.getSelectedIndex() + 1, val(startTime), val(duration), val(startX), val(destX));
    }
    if (yCheck.isSelected()) {
        anim.defineMotionY(motionTypeY.getSelectedIndex() + 1, val(startTime), val(duration), val(startY), val(destY));
    }
    if (widthCheck.isSelected()) {
        anim.defineWidth(motionTypeWidth.getSelectedIndex() + 1, val(startTime), val(duration), val(startWidth), val(destWidth));
    }
    if (heightCheck.isSelected()) {
        anim.defineHeight(motionTypeHeight.getSelectedIndex() + 1, val(startTime), val(duration), val(startHeight), val(destHeight));
    }
    if (opacityCheck.isSelected()) {
        anim.defineOpacity(motionTypeOpacity.getSelectedIndex() + 1, val(startTime), val(duration), val(startOpacity), val(destOpacity));
    }
    if (orientationCheck.isSelected()) {
        anim.defineOrientation(motionTypeOrientation.getSelectedIndex() + 1, val(startTime), val(duration), val(startOrientation), val(destOrientation));
    }
    if (val(frameDelay) > -1) {
        anim.defineFrames(val(frameWidth), val(frameHeight), val(frameDelay));
    }
    return anim;
}
Also used : AnimationObject(com.codename1.ui.animations.AnimationObject)

Example 9 with AnimationObject

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

the class EditableResources method writeTimeline.

private void writeTimeline(DataOutputStream output, Timeline t) throws IOException {
    output.writeInt(t.getDuration());
    output.writeInt(t.getSize().getWidth());
    output.writeInt(t.getSize().getHeight());
    AnimationObject[] animations = new AnimationObject[t.getAnimationCount()];
    output.writeShort(animations.length);
    for (int iter = 0; iter < animations.length; iter++) {
        animations[iter] = t.getAnimation(iter);
        String name = AnimationAccessor.getImageName(animations[iter]);
        if (name == null) {
            name = findId(AnimationAccessor.getImage(animations[iter]));
        }
        output.writeUTF(name);
        int startTime = animations[iter].getStartTime();
        int animDuration = animations[iter].getEndTime() - startTime;
        output.writeInt(startTime);
        output.writeInt(animDuration);
        Motion motionX = AnimationAccessor.getMotionX(animations[iter]);
        Motion motionY = AnimationAccessor.getMotionY(animations[iter]);
        output.writeInt(motionX.getSourceValue());
        output.writeInt(motionY.getSourceValue());
        int frameDelay = AnimationAccessor.getFrameDelay(animations[iter]);
        output.writeInt(frameDelay);
        if (frameDelay > -1) {
            output.writeInt(AnimationAccessor.getFrameWidth(animations[iter]));
            output.writeInt(AnimationAccessor.getFrameHeight(animations[iter]));
        }
        if (motionX.getSourceValue() != motionX.getDestinationValue()) {
            output.writeBoolean(true);
            output.writeInt(AnimationAccessor.getMotionType(motionX));
            output.writeInt(motionX.getDestinationValue());
        } else {
            output.writeBoolean(false);
        }
        if (motionY.getSourceValue() != motionY.getDestinationValue()) {
            output.writeBoolean(true);
            output.writeInt(AnimationAccessor.getMotionType(motionY));
            output.writeInt(motionY.getDestinationValue());
        } else {
            output.writeBoolean(false);
        }
        writeMotion(AnimationAccessor.getWidth(animations[iter]), output);
        writeMotion(AnimationAccessor.getHeight(animations[iter]), output);
        writeMotion(AnimationAccessor.getOpacity(animations[iter]), output);
        writeMotion(AnimationAccessor.getOrientation(animations[iter]), output);
    }
}
Also used : Motion(com.codename1.ui.animations.Motion) AnimationObject(com.codename1.ui.animations.AnimationObject)

Example 10 with AnimationObject

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

the class TimelineEditor method removeAnimationObjectActionPerformed.

// GEN-LAST:event_addAnimationObjectActionPerformed
private void removeAnimationObjectActionPerformed(java.awt.event.ActionEvent evt) {
    // GEN-FIRST:event_removeAnimationObjectActionPerformed
    Timeline t = (Timeline) res.getImage(name);
    AnimationObject[] animations = new AnimationObject[t.getAnimationCount() - 1];
    int offset = 0;
    for (int iter = 0; iter < t.getAnimationCount(); iter++) {
        if (iter != animationObjectList.getSelectedRow()) {
            animations[offset] = t.getAnimation(iter);
            offset++;
        }
    }
    ((AnimationObjectTableModel) animationObjectList.getModel()).remove(animationObjectList.getSelectedRow());
    Timeline nt = Timeline.createTimeline(getValue(duration), animations, new com.codename1.ui.geom.Dimension(getValue(width), getValue(height)));
    nt.setPause(t.isPause());
    setImage(nt);
    animationObjectList.clearSelection();
    duplicateObject.setEnabled(false);
    moveDown.setEnabled(false);
    moveUp.setEnabled(false);
    removeAnimationObject.setEnabled(false);
}
Also used : Timeline(com.codename1.ui.animations.Timeline) AnimationObject(com.codename1.ui.animations.AnimationObject) Point(java.awt.Point)

Aggregations

AnimationObject (com.codename1.ui.animations.AnimationObject)14 Timeline (com.codename1.ui.animations.Timeline)11 Point (java.awt.Point)8 EncodedImage (com.codename1.ui.EncodedImage)2 Motion (com.codename1.ui.animations.Motion)2 BufferedImage (java.awt.image.BufferedImage)2 Image (com.codename1.ui.Image)1 Dimension (com.codename1.ui.geom.Dimension)1 BasicStroke (java.awt.BasicStroke)1 Color (java.awt.Color)1 Graphics2D (java.awt.Graphics2D)1 MouseListener (java.awt.event.MouseListener)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 ImageReader (javax.imageio.ImageReader)1