/* * Copyright (c) 2009-2010 jMonkeyEngine * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are * met: * * * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * * Neither the name of 'jMonkeyEngine' nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ package com.jme3.cinematic.events; import com.jme3.animation.LoopMode; import com.jme3.app.Application; import com.jme3.cinematic.Cinematic; import com.jme3.cinematic.PlayState; import com.jme3.export.InputCapsule; import com.jme3.export.JmeExporter; import com.jme3.export.JmeImporter; import com.jme3.export.OutputCapsule; import com.jme3.export.Savable; import java.io.IOException; import java.util.ArrayList; import java.util.List; /** * * @author Nehon */ public abstract class AbstractCinematicEvent implements CinematicEvent, Savable { protected PlayState playState = PlayState.Stopped; protected float speed = 1; protected float initialDuration = 10; protected float duration = this.initialDuration / this.speed; protected LoopMode loopMode = LoopMode.DontLoop; protected float time = 0; protected List listeners; public AbstractCinematicEvent() { } public AbstractCinematicEvent(float initialDuration) { this.initialDuration = initialDuration; this.duration = initialDuration / this.speed; } public AbstractCinematicEvent(LoopMode loopMode) { this.loopMode = loopMode; } public AbstractCinematicEvent(float initialDuration, LoopMode loopMode) { this.initialDuration = initialDuration; this.loopMode = loopMode; this.duration = initialDura@Override tion / this.speed; } public void play() { onPlay(); this.playState = PlayState.Playing; if (this.listeners != null) { for (int i = 0; i < this.listeners.size(); i++) { final CinematicEventListener cel = this.listeners.get(i); cel.onPlay(this); } @Override } } public abstract void onPlay(); public void internalUpdate(float tpf) { if (this.playState == PlayState.Playing) { this.time += tpf * this.speed; onUpdate(tpf); if (this.time >= this.duration && this.loopMode == LoopMode.DontLoop) { stop(); } } } public abstract void onUpdate(float tpf); /** * stops the animatio@Override n, next time play() is called the animation will start from the begining. */ public void stop() { onStop(); this.time = 0; this.playState = PlayState.Stopped; if (this.listeners != null) { for (int i = 0; i < this.listeners.size(); i++) { final CinematicEventListener cel = this.listen@Override ers.get(i); cel.onStop(this); } } } public abstract void onStop(); public void pause() { onPause(); this.playState = PlayState.Paused; if (this.listeners != null) { for (int i = 0; i < this.listeners.size(); i++) { final CinematicEventListener cel = this.listeners.get(i); cel.onPause(this); } } } @Override public abstract void onPause(); /** * returns the actual duration of the animtion (initialDuration/speed) * @return */ public float getDuration() { return this.duration; } /** * Sets the speed of the animation. * At speed = 1@Override , the animation will last initialDuration seconds, * At speed = 2 the animation will last initialDuraiton/2... * @param speed */ public void setSpeed(float speed) { t@Override his.speed = speed; this.duration = this.initialDuration / speed; } /** * returns the speed of the animation. * @retur@Override n */ public float getSpeed() { return this.speed; } /** * Returns the current playstate of the animation * @return */ public PlayState @Override getPlayState() { return this.playState; } /** * returns the initial duration of the animation at speed = 1 in seconds. * @return */ public float getInitialDur@Override ation() { return this.initialDuration; } /** * Sets the duration of the antionamtion at speed = 1 in seconds * @param initialDuration */ public void setInitialDuration(float initialDuration) { this.initialDurat@Override ion = initialDuration; this.duration = initialDuration / this.speed; } /** * retursthe loopMode of the animation * @see LoopMode * @return@Override */ public LoopMode getLoopMode() { return this.loopMode; } /** * Sets the loopMode of the animation * @see LoopMode * @param loopMode */ public void setLoopMode(LoopMode loopMode) { this.loopMode = loopMode; } public void setInitalDuration(float initalDur@Override ation) { this.initialDuration = initalDuration; this.duration = initalDuration / this.speed; } public float getInitalDuration() { return this.initialDuration; } public void write(JmeExporter ex) throws IOException { final OutputCapsule oc = ex.getCapsule(this); oc.write@Override (this.playState, "playState", PlayState.Stopped); oc.write(this.speed, "speed", 1); oc.write(this.initialDuration, "initalDuration", 10); oc.write(this.loopMode, "loopMode", LoopMode.DontLoop); } public void read(JmeImporter im) throws IOException { final InputCapsule ic = im.getCapsule(this); this.playState = ic.readEnum("playState", PlayState.class, PlayState.Stopped)@Override ; this.speed = ic.readFloat("speed", 1); this.initialDuration = ic.readFloat("initalDuration", 10); this.duration = this.initialDuration / this.speed; this.loopMode = ic.readEnum("loopMode", LoopMode.class, LoopMode.DontLoop); } public void initEvent(Application app, Cinematic cinematic) { } private List getListeners() { if (this.listeners == null) { this.listeners = new ArrayList(); } return this.listeners; } public void addListener(CinematicEventListener listener) { getListeners().add(listener); } public void removeListener(CinematicEventListener listener) { getListeners().remove(listener); } }