00001 /* 00002 * The information in this file is 00003 * Copyright(c) 2007 Ball Aerospace & Technologies Corporation 00004 * and is subject to the terms and conditions of the 00005 * GNU Lesser General Public License Version 2.1 00006 * The license text is available from 00007 * http://www.gnu.org/licenses/lgpl.html 00008 */ 00009 00010 #ifndef ANIMATION_H 00011 #define ANIMATION_H 00012 00013 #include "SessionItem.h" 00014 #include "Subject.h" 00015 #include "AnimationFrame.h" 00016 #include "TypesFile.h" 00017 00018 #include <string> 00019 #include <vector> 00020 00021 /** 00022 * A collection of animation frame objects with a current frame. 00023 * 00024 * An animation object provides the means by which other objects can be animated by 00025 * providing a set of animation frames, one of which is identified as the current 00026 * frame. An animation by itself does not provide a lot of functionality but simply 00027 * notifies attached objects when its current frame is changed. The real work 00028 * of a displaying an animation is therefore in those objects that attach to the 00029 * animation object. 00030 * 00031 * To create a useful animation, first create a class with a Slot method. This class 00032 * should contain the logic needed to prepare frames for display. Next, create 00033 * a vector of animation frames for times of interest to the animation. Create the 00034 * animation by calling AnimationController::createAnimation(). Attach the class to the 00035 * animation. Then in the class's Slot method, whenever signalFrameChanged() is the 00036 * signal with a non-NULL value, this indicates that the current frame has 00037 * changed. any_cast the value to an AnimationFrame pointer and perform any 00038 * specific updates based on the new frame values. 00039 * 00040 * An animation is both created and destroyed from an animation controller. 00041 * 00042 * This subclass of Subject will notify upon the following conditions: 00043 * - The object is deleted. 00044 * - The following methods are called: setName(), setFrames(), and 00045 * setCurrentFrame(). 00046 * - Other notifications documented in the Subject class. 00047 * 00048 * @see AnimationFrame, AnimationController 00049 */ 00050 class Animation : public SessionItem, public Subject 00051 { 00052 public: 00053 /** 00054 * Emitted with boost::any<std::string> when an Animation is renamed. 00055 */ 00056 SIGNAL_METHOD(Animation, Renamed) 00057 /** 00058 * Emitted with boost::any<AnimationFrame*> when the current frame is changed. 00059 */ 00060 SIGNAL_METHOD(Animation, FrameChanged) 00061 /** 00062 * Emitted with boost::any<std::vector<AnimationFrame> > when the list of frames in the animation changes. 00063 */ 00064 SIGNAL_METHOD(Animation, FramesChanged) 00065 00066 /** 00067 * Sets the animation name. 00068 * 00069 * @param name 00070 * The new name for the animation. 00071 * 00072 * @notify This method will notify signalRenamed() with any<std::string>. 00073 */ 00074 virtual void setName(const std::string& name) = 0; 00075 00076 /** 00077 * Returns the type of frame value that the animation uses to set current frame. 00078 * 00079 * This method returns whether the animation operates on frame numbers or time 00080 * values when setting the current frame from a data value. The frame type 00081 * is specified when the animation is created in AnimationController::createAnimation(). 00082 * Once the animation is created, the frame type cannot be changed. 00083 * 00084 * @return The animation's frame type. 00085 * 00086 * @see setCurrentFrame(), getStartValue(), getStopValue() 00087 */ 00088 virtual FrameType getFrameType() const = 0; 00089 00090 /** 00091 * Sets the frame set that defines this animation. 00092 * 00093 * This method sets a new frame set for the animation. Once the frames have been 00094 * set, they are sorted in ascending order according to the frame number or 00095 * the time value. The current frame is reset to the first frame in the 00096 * internal sorted vector. 00097 * 00098 * @param frames 00099 * The new frame set for the animation. 00100 * 00101 * @notify This method will notify signalFramesChanged() with any<std::vector<AnimationFrame> >. 00102 */ 00103 virtual void setFrames(const std::vector<AnimationFrame>& frames) = 0; 00104 00105 /** 00106 * Returns the frame set that defines this animation. 00107 * 00108 * @return The sorted frame set for the animation. 00109 */ 00110 virtual const std::vector<AnimationFrame>& getFrames() const = 0; 00111 00112 /** 00113 * Returns the total number of frames in the animation. 00114 * 00115 * @return The total number of frames. 00116 */ 00117 virtual unsigned int getNumFrames() const = 0; 00118 00119 /** 00120 * Queries whether the animation contains a given frame. 00121 * 00122 * This method does not check the contents of the given frame; it only checks 00123 * if the pointer corresponds to the address of one of the internally stored 00124 * frames. To check specific contents of a frame, use AnimationFrame::operator==() 00125 * instead. 00126 * 00127 * @param pFrame 00128 * The frame to check for its existence in the animation. 00129 * 00130 * @return Returns true if the given frame is contained in the animation, 00131 * otherwise returns false. 00132 */ 00133 virtual bool hasFrame(const AnimationFrame* pFrame) const = 0; 00134 00135 /** 00136 * Sets the current frame in the animation. 00137 * 00138 * This method is called primarily as a result of a currently playing animation controller. 00139 * Calling it independently from the player will succeed and will have no effect on 00140 * the player, but if the player's animation state is AnimationState::PLAY_FORWARD or 00141 * AnimationState::PLAY_BACKWARD, this method will likely be called again soon by the 00142 * animation controller. Therefore, it is only potentially useful to call this method 00143 * when the player is paused or stopped. 00144 * 00145 * @param pFrame 00146 * The frame to set as the current frame. The given frame must exist 00147 * in the animation. 00148 * 00149 * @notify This method will notify signalFrameChanged() with any<const AnimationFrame*>. 00150 */ 00151 virtual void setCurrentFrame(const AnimationFrame* pFrame) = 0; 00152 00153 /** 00154 * Sets the current frame in the animation. 00155 * 00156 * This is a convenience method that allows an animation frame to be set as the current 00157 * frame by specifying just the data value. The method identifies the first frame 00158 * in the animation that is greater than or equal to the given frame value. The given frame 00159 * value is compared with the internal frame values according to the animation's frame 00160 * type to determine the frame to set as the current frame. Once the correct frame 00161 * has been identified, the overloaded setCurrentFrame(const AnimationFrame*) method is 00162 * called with the that frame. 00163 * 00164 * If the given frame value is greater than the animation's stop value, the overloaded 00165 * setCurrentFrame(const AnimationFrame*) method is called with a NULL value. 00166 * 00167 * This method is called primarily as a result of a currently playing animation controller. 00168 * Calling it independently from the player will succeed and will have no effect on 00169 * the player, but if the player's animation state is PLAY_FORWARD or PLAY_BACKWARD, 00170 * this method will likely be called again soon by the animation controller. Therefore, it 00171 * is only potentially useful to call this method when the player is paused or 00172 * stopped. 00173 * 00174 * @param frameValue 00175 * The frame value that will be used to set the current frame. The value 00176 * should be of the same type as specified by getFrameType(). 00177 * 00178 * @notify This method will notify signalFrameChanged() with any<const AnimationFrame*>. 00179 * 00180 * @see setCurrentFrame(const AnimationFrame*), getFrameType() 00181 */ 00182 virtual void setCurrentFrame(double frameValue) = 0; 00183 00184 /** 00185 * Returns the current frame in the animation. 00186 * 00187 * @return The current frame. 00188 */ 00189 virtual const AnimationFrame* getCurrentFrame() const = 0; 00190 00191 /** 00192 * Returns the minimum frame value of all frames in the animation. 00193 * 00194 * @return Returns the value of the first frame of the sorted frame set according 00195 * to the type specified by getFrameType(). 00196 */ 00197 virtual double getStartValue() const = 0; 00198 00199 /** 00200 * Returns the maximum frame value of all frames in the animation. 00201 * 00202 * @return Returns the value of the last frame of the sorted frame set according to 00203 * the type specified by getFrameType(). 00204 */ 00205 virtual double getStopValue() const = 0; 00206 00207 /** 00208 * Get the value of the next frame. 00209 * 00210 * This method is used to find what the value will be for some offset away from the current frame. 00211 * This method only counts unique frames, so a string of duplicate frames will count as 1 frame 00212 * relative to the offset. 00213 * 00214 * @param direction 00215 * The direction to go. 00216 * 00217 * @param offset 00218 * The number of frames to offset. If \c direction is AnimationState::STOP or 00219 * AnimationState::PAUSE_FORWARD or AnimationState::PAUSE_BACKWARD, 00220 * this value is ignored. 00221 * 00222 * @return The value of the frame asked for. This method will return a 00223 * negative number if the given offset pushes outside of the animation's 00224 * range, or there is an invalid parameter. If \c direction is 00225 * AnimationState::STOP, AnimationState::PAUSE_FORWARD or 00226 * AnimationState::PAUSE_BACKWARD, the value of the 00227 * current frame is returned. 00228 */ 00229 virtual double getNextFrameValue(AnimationState direction, 00230 size_t offset = 1) const = 0; 00231 00232 protected: 00233 /** 00234 * This should be destroyed by calling AnimationController::destroyAnimation. 00235 */ 00236 virtual ~Animation() {} 00237 }; 00238 00239 #endif