diff --git a/include/EffectBase.h b/include/EffectBase.h index edba03d8..55f65aad 100644 --- a/include/EffectBase.h +++ b/include/EffectBase.h @@ -60,9 +60,12 @@ namespace openshot * The only requirements for an 'effect', is to derive from this base class, implement the Apply() * method, and call the InitEffectInfo() method. */ - class EffectBase : ClipBase + class EffectBase : public ClipBase { + private: + int order; ///< The order to evaluate this effect. Effects are processed in this order (when more than one overlap). public: + /// Information about the current effect EffectInfo info; @@ -83,6 +86,12 @@ namespace openshot /// Initialize the values of the EffectInfo struct. It is important for derived classes to call /// this method, or the EffectInfo struct values will not be initialized. void InitEffectInfo(); + + /// Get the order that this effect should be executed. + int Order() { return order; } + + /// Set the order that this effect should be executed. + void Order(int new_order) { order = new_order; } }; } diff --git a/include/Timeline.h b/include/Timeline.h index c3741d8e..1010ec15 100644 --- a/include/Timeline.h +++ b/include/Timeline.h @@ -57,8 +57,14 @@ namespace openshot { /// Comparison method for sorting clip pointers (by Position and Layer) struct CompareClips{ - bool operator()( ClipBase* lhs, ClipBase* rhs){ - return lhs->Position() <= rhs->Position() && lhs->Layer() < rhs->Layer(); + bool operator()( Clip* lhs, Clip* rhs){ + return lhs->Position() < rhs->Position() && lhs->Layer() < rhs->Layer(); + }}; + + /// Comparison method for sorting effect pointers (by Position, Layer, and Order) + struct CompareEffects{ + bool operator()( EffectBase* lhs, EffectBase* rhs){ + return lhs->Position() < rhs->Position() && lhs->Layer() < rhs->Layer() && lhs->Order() < rhs->Order(); }}; /** @@ -171,7 +177,7 @@ namespace openshot { /// @brief Add an openshot::Clip to the timeline /// @param clip Add an openshot::Clip to the timeline. A clip can contain any type of Reader. - void AddClip(Clip* clip); + void AddClip(Clip* clip) throw(ReaderClosed); /// @brief Add an effect to the timeline /// @param effect Add an effect to the timeline. An effect can modify the audio or video of an openshot::Frame. @@ -228,6 +234,9 @@ namespace openshot { /// Sort clips by position on the timeline void SortClips(); + /// Sort effects by position on the timeline + void SortEffects(); + /// Get the width of canvas and viewport int Width() { return width; } diff --git a/src/EffectBase.cpp b/src/EffectBase.cpp index 7906a88f..ac728fe9 100644 --- a/src/EffectBase.cpp +++ b/src/EffectBase.cpp @@ -37,6 +37,7 @@ void EffectBase::InitEffectInfo() Layer(0); Start(0.0); End(0.0); + Order(0); info.has_video = false; info.has_audio = false; diff --git a/src/Timeline.cpp b/src/Timeline.cpp index a538c8a6..d5506876 100644 --- a/src/Timeline.cpp +++ b/src/Timeline.cpp @@ -58,7 +58,7 @@ Timeline::Timeline(int width, int height, Framerate fps, int sample_rate, int ch } // Add an openshot::Clip to the timeline -void Timeline::AddClip(Clip* clip) +void Timeline::AddClip(Clip* clip) throw(ReaderClosed) { // All clips must be converted to the frame rate of this timeline, // so assign the same frame rate to each clip. @@ -76,6 +76,9 @@ void Timeline::AddEffect(EffectBase* effect) { // Add effect to list effects.push_back(effect); + + // Sort effects + SortEffects(); } // Remove an effect from the timeline @@ -343,6 +346,13 @@ void Timeline::SortClips() clips.sort(CompareClips()); } +// Sort effects by position on the timeline +void Timeline::SortEffects() +{ + // sort clips + effects.sort(CompareEffects()); +} + // Close the reader (and any resources it was consuming) void Timeline::Close() {