File indexing completed on 2024-05-12 16:23:25

0001 /***************************************************************************
0002  *   Copyright (C) 2005-2014 by Linuxstopmotion contributors;              *
0003  *   see the AUTHORS file for details.                                     *
0004  *                                                                         *
0005  *   This program is free software; you can redistribute it and/or modify  *
0006  *   it under the terms of the GNU General Public License as published by  *
0007  *   the Free Software Foundation; either version 2 of the License, or     *
0008  *   (at your option) any later version.                                   *
0009  *                                                                         *
0010  *   This program is distributed in the hope that it will be useful,       *
0011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
0012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
0013  *   GNU General Public License for more details.                          *
0014  *                                                                         *
0015  *   You should have received a copy of the GNU General Public License     *
0016  *   along with this program; if not, write to the                         *
0017  *   Free Software Foundation, Inc.,                                       *
0018  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
0019  ***************************************************************************/
0020 #ifndef ANIMATION_H
0021 #define ANIMATION_H
0022 
0023 #include "src/config.h"
0024 
0025 #include <stdint.h>
0026 #include <stdio.h>
0027 #include <exception>
0028 #include <vector>
0029 
0030 
0031 using namespace std;
0032 
0033 class Executor;
0034 class FileNameVisitor;
0035 class Frontend;
0036 class Observer;
0037 class ObserverNotifier;
0038 class VideoEncoder;
0039 class ProjectSerializer;
0040 class AudioDriver;
0041 class FileCommandLogger;
0042 class Scene;
0043 class StringIterator;
0044 class UiException;
0045 class UndoRedoObserver;
0046 
0047 class FailedToInitializeCommandLogger : public std::exception {
0048 public:
0049     FailedToInitializeCommandLogger();
0050     const char* what() const throw();
0051 };
0052 
0053 /**
0054  * Represents the animation. Is responsible for the undo system and a bunch of
0055  * other minor stuff it really shouldn't be doing.
0056  */
0057 class Animation {
0058 public:
0059     /**
0060      * Initializes the variables of the animation to starting values.
0061      */
0062     Animation();
0063 
0064     /**
0065      * Cleans up the animation.
0066      */
0067     ~Animation();
0068 
0069     /**
0070      * Attaches a new observer to the model. The observer will be notified when
0071      * something is changed in the model.
0072      * @param o the observer to be attached to the model.
0073      */
0074     void attach(Observer *o);
0075 
0076     /**
0077      * Detaches an observer from the model. The observer will no longer be notified
0078      * when something is changed in the model.
0079      * @param o the observer to be detached from the model.
0080      */
0081     void detach(Observer *o);
0082 
0083     /**
0084      * Registers the GUI frontend which is used to displaying and updating
0085      * progress when running time-consuming operations.
0086      * @param frontend the GUI frontend
0087      */
0088     void registerFrontend(Frontend *frontend);
0089 
0090     /**
0091      * Retrieves the registered frontend.
0092      * @return the frontend if it is a valid frontend pointer, NULL otherwise.
0093      */
0094     Frontend* getFrontend();
0095 
0096     /**
0097      * Attempts to re-synch the UI after an error.
0098      */
0099     void resync(std::exception& e);
0100     /**
0101      * Attempts to re-synch the UI after a non-critical error.
0102      */
0103     void resync(UiException& e);
0104 
0105     /**
0106      * Inserts new frames into the animation model.
0107      * @param scene The scene to add the frames to.
0108      * @param frame The frame index to add the frames to in the animation.
0109      * @param frameNames a vector containing the names to be added in the model
0110      */
0111     void addFrames(int scene, int frame, StringIterator& frameNames);
0112 
0113     /**
0114      * Removes frames from a scene in the animation.
0115      * @param scene The scene from which to remove the frames.
0116      * @param frame The first frame to remove.
0117      * @param count The number of frames to remove.
0118      */
0119     void removeFrames(int32_t scene, int32_t frame, int32_t count);
0120 
0121     /**
0122      * Moves frames from one position in the animation to another.
0123      * @param fromScene The scene from which to move the frames.
0124      * @param fromFrame The first frame within that scene to move.
0125      * @param count The number of frames to move; all frames moved must be
0126      * within the same scene.
0127      * @param fromScene The scene to move the frames to.
0128      * @param toFrame The position within the scene {@a toScene} to which
0129      * the frames should be moved.
0130      */
0131     void moveFrames(int32_t fromScene, int32_t fromFrame,
0132             int32_t count, int32_t toScene, int32_t toFrame);
0133 
0134     /**
0135      * Returns the path to the image file for the frame specified.
0136      * @param scene The scene to which the frame belongs.
0137      * @param frame The frame within scene {@a scene} to query.
0138      * @return The image path of frame number {@a frame} of scene {@a scene}.
0139      * Ownership is not returned.
0140      */
0141     const char* getImagePath(int scene, int frame) const;
0142 
0143     /**
0144      * Replaces the image for the specified frame.
0145      * @param scene The scene to which the frame belongs.
0146      * @param frame The frame number of the frame within the scene.
0147      * @param newImagePath The full path to the new image for this frame.
0148      */
0149     void setImagePath(int32_t sceneNumber, int32_t frameNumber,
0150             const char* newImagePath);
0151 
0152     /**
0153      * Duplicates the image file for the specified frame, setting the frames
0154      * image to the duplicate.
0155      * @param sceneNumber The scene to which the frame belongs.
0156      * @param frameNumber The frame within the scene.
0157      */
0158     void duplicateImage(int32_t sceneNumber, int32_t frameNumber);
0159 
0160     /**
0161      * Adds the sound from the file "sound" to frame
0162      * at position frameNumber.
0163      * @param scene The scene containing the frame to add the sound to.
0164      * @param frameNumber The number of the frame to add the sound to.
0165      * @param filename The path to the file with the sound
0166      */
0167     void addSound(int32_t scene, int32_t frameNumber, const char *filename);
0168 
0169     /**
0170      * Removes the sound with index soundNumber from the frame with index
0171      * frameNumber.
0172      * @param sceneNumber The index of the scene containing the frame
0173      * from which the sound is to be removed.
0174      * @param frameNumber the index of the frame to remove a sound from.
0175      * @param soundNumber the index of the sound to remove from the frame.
0176      */
0177     void removeSound(int32_t sceneNumber, int32_t frameNumber,
0178             int32_t soundNumber);
0179 
0180     /**
0181      * Returns the human-readable name of the sound specified.
0182      * @param scene The scene to which the frame belongs.
0183      * @param frame The frame to which the sound belongs.
0184      * @param soundNumber The index of the sound.
0185      * @return The name of sound indexed {@a soundNumber} of frame {@a frame}
0186      * of scene {@a scene}. Ownership is not returned.
0187      */
0188     const char* getSoundName(int scene, int frame, int soundNumber) const;
0189 
0190     /**
0191      * Sets the name of the sound with index {@a soundNumber} in the frame with
0192      * index {@a frameNumber} to {@a soundName}.
0193      * @param sceneNumber The index of the scene containing the frame
0194      * whose sound will be renamed.
0195      * @param frameNumber the index of the frame the sound is in.
0196      * @param soundNumber the index to the sound to change the name of.
0197      * @param soundName the new name of the sound.
0198      */
0199     void setSoundName(int32_t sceneNumber, int32_t frameNumber,
0200             int32_t soundNumber, const char* soundName);
0201 
0202     /**
0203      * Returns the filename of the sound number {@a sound} within frame number
0204      * {@a frame} within scene number {@a scene}.
0205      * @param scene The index of the scene containing the frame.
0206      * @param frame The index of the frame within scene {@a scene}
0207      * @param sound The index of the sound within frame {@frame}.
0208      * @return The path of the file containing the sound. Ownership is not
0209      * returned.
0210      */
0211     const char* getSoundPath(int scene, int frame, int sound) const;
0212 
0213     /**
0214      * Play the sounds in the specified frame.
0215      * @param scene The scene that the frame belongs to.
0216      * @param frame The frame within that scene.
0217      */
0218     void playSounds(int scene, int frame) const;
0219 
0220     /**
0221      * Returns the total number of frames in the model.
0222      * @return The total number of frames in all scenes of the model.
0223      */
0224     int frameCount() const;
0225 
0226     /**
0227      * Retrieves the size of the scene at index sceneNumber.
0228      * @param sceneNumber the index of the scene to retrieve the size of.
0229      * @return the size of the scene.
0230      */
0231     int frameCount(int sceneNumber) const;
0232 
0233     /**
0234      * Returns the number of sounds in the frame specified.
0235      * @param scene The number of the scene.
0236      * @param frame The number of the frame within the scene.
0237      * @return The number of sounds attached to the specified frame.
0238      */
0239     int soundCount(int scene, int frame) const;
0240 
0241     /**
0242      * Returns the total number of sounds in the project.
0243      * @return The total number of sounds in all frames in all scenes.
0244      * Duplicates are counted the number of times they appear.
0245      */
0246     int soundCount() const;
0247 
0248     /**
0249      * Retrieves the number of scenes in the animation.
0250      * @return the number of scenes in the animation.
0251      */
0252     int sceneCount() const;
0253 
0254     void clearHistory();
0255 
0256     void clear();
0257 
0258     void undo();
0259 
0260     void redo();
0261 
0262     bool canUndo();
0263     bool canRedo();
0264 
0265     /**
0266      * Sets the observer to receive notification when the can undo/can redo
0267      * state changes.
0268      */
0269     void setUndoRedoObserver(UndoRedoObserver* observer);
0270 
0271     /**
0272      * Retrieves the project file.
0273      * @return the project file if it has been set, NULL otherwise.
0274      */
0275     const char *getProjectFile();
0276 
0277     /**
0278      * Clears the animation and loads it from the dat file specified.
0279      * If unsuccessful, the animation is untouched.
0280      * @param filename The XML file to load.
0281      * @param projectFilename The name of the project file this XML file came
0282      * from or was last saved to, if known.
0283      * @return {@c true} if successful.
0284      */
0285     bool loadFromDat(const char* filename, const char* projectFilename);
0286 
0287     /**
0288      * Opens a project.
0289      * @param filename the project file to open (ends with .sto)
0290      */
0291     void openProject(const char *filename);
0292 
0293     /**
0294      * Saves the active project to a XML-file which is written to disk.
0295      * @param filename the filename to store the project files within.
0296      */
0297     void saveProject(const char *filename);
0298 
0299     /**
0300      * Creates a new project.
0301      * @throw FailedToInitializeCommandLogger if either the current project
0302      * file could not be deleted or the command log file could not be
0303      * reinitialized. The project may or may not be cleared in this case.
0304      */
0305     void newProject();
0306 
0307     /**
0308      * Checks if there are unsaved changes in the model.
0309      * @return true if there are unsaved changes, false otherwise.
0310      */
0311     bool isUnsavedChanges();
0312 
0313     /**
0314      * Create and adds a new scene to the animation at position ``index''.
0315      * @param index the position to add the new scene.
0316      */
0317     void newScene(int32_t index);
0318 
0319     /**
0320      * Removes the scene at the location sceneNumber from the animation.
0321      * @param sceneNumber the scene to be removed from the animation.
0322      */
0323     void removeScene(int32_t sceneNumber);
0324 
0325     /**
0326      * Moves the scene at position sceneNumber to the position movePosition.
0327      * @param sceneNumber the number of the scene to move.
0328      * @param movePosition the position to move the scene to.
0329      */
0330     void moveScene(int32_t sceneNumber, int32_t movePosition);
0331 
0332     /**
0333      * Initializes the audio device so it is ready to play sounds.
0334      * @return true on success, false otherwise
0335      */
0336     bool initAudioDevice();
0337 
0338     /**
0339      * Shutdowns the the audio device so that other programs can use it.
0340      */
0341     void shutdownAudioDevice();
0342 
0343     /**
0344      * Exports the current project to a video file as specified by the video encoder.
0345      * @param encoder The encoder to be used for video export
0346      * @param playbackSpeed The frame rate (in frames per second) at which
0347      * the video should be encoded, assuming the video encoder supports
0348      * this adjustment.
0349      * @return true on success, false otherwise
0350      */
0351     bool exportToVideo(VideoEncoder *encoder, int playbackSpeed);
0352 
0353     /**
0354      * Exports the current project to a Cinerella project.
0355      * @param file the Cinerella project file
0356      * @return true on success, false otherwise
0357      */
0358     bool exportToCinerella(const char *file);
0359 
0360     /**
0361      * Sets the file to be used as the command log.
0362      * @param file The file, which must be opened for write. Ownership is
0363      * passed.
0364      */
0365     void setCommandLoggerFile(FILE* file);
0366 
0367     /**
0368      * Replay the commands in the file.
0369      * @param file The command log file, opened for reading and seeked to the
0370      * beginning of the file (or wherever is appropriate).
0371      * @throws FileException if the file could not be read in its entirety, or
0372      * another exception if decoding the log failed. Some commands may have
0373      * been applied.
0374      */
0375     void replayCommandLog(FILE* file);
0376 
0377     /**
0378      * Have v visit all the files referenced (images and sounds)
0379      */
0380     void accept(FileNameVisitor& v) const;
0381 
0382 private:
0383     void setScenes(const std::vector<Scene*>& sv);
0384     FILE* initializeCommandLog();
0385 
0386     /** All of the scenes in the animation. */
0387     ObserverNotifier* scenes;
0388 
0389     /** Undo and disaster recovery. */
0390     Executor* executor;
0391     FileCommandLogger* logger;
0392 
0393     /** Serializer to be used on saving and loading of the project. */
0394     ProjectSerializer *serializer;
0395 
0396     /** Audio driver that can play sound. */
0397     AudioDriver *audioDriver;
0398 
0399     /** Variable for checking if the audio device is successfully initialized. */
0400     bool isAudioDriverInitialized;
0401 
0402     /** For the reporting of errors and warnings */
0403     Frontend* frontend;
0404 };
0405 
0406 #endif