Skip to content

Commit

Permalink
Added support to set an effect's properties by it's parent effect
Browse files Browse the repository at this point in the history
  • Loading branch information
BrennoCaldato committed Feb 4, 2021
1 parent c60dd40 commit 67895f7
Show file tree
Hide file tree
Showing 20 changed files with 157 additions and 8 deletions.
8 changes: 6 additions & 2 deletions src/Clip.cpp
Expand Up @@ -1210,10 +1210,14 @@ void Clip::AddEffect(EffectBase* effect)
// Sort effects
sort_effects();

// Get the parent timeline of this clip
Timeline* parentTimeline = (Timeline *) ParentTimeline();

if (parentTimeline)
effect->ParentTimeline(parentTimeline);

// Add Tracked Object to Timeline
if (effect->info.has_tracked_object){

Timeline* parentTimeline = (Timeline *) ParentTimeline();

// Check if this clip has a parent timeline
if (parentTimeline){
Expand Down
76 changes: 72 additions & 4 deletions src/EffectBase.cpp
Expand Up @@ -30,6 +30,7 @@

#include "EffectBase.h"
#include "Exceptions.h"
#include "Timeline.h"

using namespace openshot;

Expand All @@ -44,11 +45,14 @@ void EffectBase::InitEffectInfo()
Order(0);
ParentClip(NULL);

parentEffect = NULL;

info.has_video = false;
info.has_audio = false;
info.has_tracked_object = false;
info.name = "";
info.description = "";
info.has_tracked_object = false;
info.parent_effect_id = "None";
}

// Display file information
Expand Down Expand Up @@ -91,6 +95,7 @@ Json::Value EffectBase::JsonValue() const {
root["name"] = info.name;
root["class_name"] = info.class_name;
root["description"] = info.description;
root["parent_effect_id"] = info.parent_effect_id;
root["has_video"] = info.has_video;
root["has_audio"] = info.has_audio;
root["has_tracked_object"] = info.has_tracked_object;
Expand All @@ -101,12 +106,12 @@ Json::Value EffectBase::JsonValue() const {
}

// Load JSON string into this object
void EffectBase::SetJson(const std::string value) {
void EffectBase::SetJson(std::string value) {

// Parse JSON string into JSON objects
try
{
const Json::Value root = openshot::stringToJson(value);
Json::Value root = openshot::stringToJson(value);
// Set all values that match
SetJsonValue(root);
}
Expand All @@ -118,14 +123,44 @@ void EffectBase::SetJson(const std::string value) {
}

// Load Json::Value into this object
void EffectBase::SetJsonValue(const Json::Value root) {
void EffectBase::SetJsonValue(Json::Value root) {

if (ParentTimeline()){
// Get parent timeline
Timeline* parentTimeline = (Timeline *) ParentTimeline();

// Get the list of effects on the timeline
std::list<EffectBase*> effects = parentTimeline->ClipEffects();

// Loop through the effects
for (auto const& effect : effects){
// Set the properties of all effects which parentEffect points to this
if (effect->info.parent_effect_id == this->Id())
effect->SetJsonValue(root);
}
}

// Set this effect properties with the parent effect properties (except the id and parent_effect_id)
if (parentEffect){
root = parentEffect->JsonValue();
root["id"] = this->Id();
root["parent_effect_id"] = this->info.parent_effect_id;
}

// Set parent data
ClipBase::SetJsonValue(root);

// Set data from Json (if key is found)
if (!root["order"].isNull())
Order(root["order"].asInt());

if (!root["parent_effect_id"].isNull()){
info.parent_effect_id = root["parent_effect_id"].asString();
if (info.parent_effect_id.size() > 0 && info.parent_effect_id != "None" && parentEffect == NULL)
SetParentEffect(info.parent_effect_id);
else
parentEffect = NULL;
}
}

// Generate Json::Value for this object
Expand All @@ -152,3 +187,36 @@ openshot::ClipBase* EffectBase::ParentClip() {
void EffectBase::ParentClip(openshot::ClipBase* new_clip) {
clip = new_clip;
}

// Set the parent effect from which this properties will be set to
void EffectBase::SetParentEffect(std::string parentEffect_id) {

// Get parent Timeline
Timeline* parentTimeline = (Timeline *) ParentTimeline();

if (parentTimeline){

// Get a pointer to the parentEffect
EffectBase* parentEffectPtr = parentTimeline->GetClipEffect(parentEffect_id);

if (parentEffectPtr){
// Set the parent Effect
parentEffect = parentEffectPtr;

// Set the properties of this effect with the parent effect's properties
Json::Value EffectJSON = parentEffect->JsonValue();
EffectJSON["id"] = this->Id();
EffectJSON["parent_effect_id"] = this->info.parent_effect_id;
this->SetJsonValue(EffectJSON);
}
}
return;
}

// Return the ID of this effect's parent clip
std::string EffectBase::ParentClipId() const{
if(clip)
return clip->Id();
else
return "None";
}
14 changes: 12 additions & 2 deletions src/EffectBase.h
Expand Up @@ -53,6 +53,7 @@ namespace openshot
std::string class_name; ///< The class name of the effect
std::string name; ///< The name of the effect
std::string description; ///< The description of this effect and what it does
std::string parent_effect_id; ///< Id of the parent effect (if there is one)
bool has_video; ///< Determines if this effect manipulates the image of a frame
bool has_audio; ///< Determines if this effect manipulates the audio of a frame
bool has_tracked_object; ///< Determines if this effect track objects through the clip
Expand All @@ -75,6 +76,9 @@ namespace openshot

public:

/// Parent effect (which properties will set this effect properties)
EffectBase* parentEffect;

/// Map of Tracked Object's by their indices (used by Effects that track objects on clips)
std::map<int, std::shared_ptr<TrackedObjectBase> > trackedObjects;

Expand All @@ -96,15 +100,21 @@ namespace openshot

/// Set parent clip object of this effect
void ParentClip(openshot::ClipBase* new_clip);

/// Set the parent effect from which this properties will be set to
void SetParentEffect(std::string parentEffect_id);

/// Return the ID of this effect's parent clip
std::string ParentClipId() const;

/// Get the indexes and IDs of all visible objects in the given frame
virtual std::string GetVisibleObjects(int64_t frame_number) const {return {}; };

/// Get and Set JSON methods
virtual std::string Json() const = 0; ///< Generate JSON string of this object
virtual void SetJson(const std::string value) = 0; ///< Load JSON string into this object
virtual void SetJson(std::string value) = 0; ///< Load JSON string into this object
virtual Json::Value JsonValue() const = 0; ///< Generate Json::Value for this object
virtual void SetJsonValue(const Json::Value root) = 0; ///< Load Json::Value into this object
virtual void SetJsonValue(Json::Value root) = 0; ///< Load Json::Value into this object

virtual std::string Json(int64_t requested_frame) const{
return {};
Expand Down
19 changes: 19 additions & 0 deletions src/Timeline.cpp
Expand Up @@ -432,6 +432,25 @@ openshot::EffectBase* Timeline::GetClipEffect(const std::string& id)
return nullptr;
}

// Return the list of effects on all clips
std::list<openshot::EffectBase*> Timeline::ClipEffects() const {

// Initialize the list
std::list<EffectBase*> ClipEffects;

// Loop through all clips
for (const auto& clip : clips) {

// Get the clip's list of effects
std::list<EffectBase*> clipEffects = clip->Effects();

// Append the clip's effects to the list
ClipEffects.insert(ClipEffects.end(), clipEffects.begin(), clipEffects.end());
}

return ClipEffects;
}

// Compute the end time of the latest timeline element
double Timeline::GetMaxTime() {
double last_clip = 0.0;
Expand Down
3 changes: 3 additions & 0 deletions src/Timeline.h
Expand Up @@ -296,6 +296,9 @@ namespace openshot {
/// Return the list of effects on the timeline
std::list<openshot::EffectBase*> Effects() { return effects; };

/// Return the list of effects on all clips
std::list<openshot::EffectBase*> ClipEffects() const;

/// Get the cache object used by this reader
openshot::CacheBase* GetCache() override { return final_cache; };

Expand Down
3 changes: 3 additions & 0 deletions src/effects/Bars.cpp
Expand Up @@ -196,6 +196,9 @@ std::string Bars::PropertiesJSON(int64_t requested_frame) const {
root["right"] = add_property_json("Right Size", right.GetValue(requested_frame), "float", "", &right, 0.0, 0.5, false, requested_frame);
root["bottom"] = add_property_json("Bottom Size", bottom.GetValue(requested_frame), "float", "", &bottom, 0.0, 0.5, false, requested_frame);

// Set the parent effect which properties this effect will inherit
root["parent_effect_id"] = add_property_json("Parent Effect ID", 0.0, "string", info.parent_effect_id, NULL, -1, -1, false, requested_frame);

// Return formatted string
return root.toStyledString();
}
3 changes: 3 additions & 0 deletions src/effects/Blur.cpp
Expand Up @@ -243,6 +243,9 @@ std::string Blur::PropertiesJSON(int64_t requested_frame) const {
root["sigma"] = add_property_json("Sigma", sigma.GetValue(requested_frame), "float", "", &sigma, 0, 100, false, requested_frame);
root["iterations"] = add_property_json("Iterations", iterations.GetValue(requested_frame), "float", "", &iterations, 0, 100, false, requested_frame);

// Set the parent effect which properties this effect will inherit
root["parent_effect_id"] = add_property_json("Parent Effect ID", 0.0, "string", info.parent_effect_id, NULL, -1, -1, false, requested_frame);

// Return formatted string
return root.toStyledString();
}
3 changes: 3 additions & 0 deletions src/effects/Caption.cpp
Expand Up @@ -429,6 +429,9 @@ std::string Caption::PropertiesJSON(int64_t requested_frame) const {
root["caption_text"] = add_property_json("Captions", 0.0, "caption", caption_text, NULL, -1, -1, false, requested_frame);
root["caption_font"] = add_property_json("Font", 0.0, "font", font_name, NULL, -1, -1, false, requested_frame);

// Set the parent effect which properties this effect will inherit
root["parent_effect_id"] = add_property_json("Parent Effect ID", 0.0, "string", info.parent_effect_id, NULL, -1, -1, false, requested_frame);

// Return formatted string
return root.toStyledString();
}
3 changes: 3 additions & 0 deletions src/effects/ChromaKey.cpp
Expand Up @@ -170,6 +170,9 @@ std::string ChromaKey::PropertiesJSON(int64_t requested_frame) const {
root["color"]["green"] = add_property_json("Green", color.green.GetValue(requested_frame), "float", "", &color.green, 0, 255, false, requested_frame);
root["fuzz"] = add_property_json("Fuzz", fuzz.GetValue(requested_frame), "float", "", &fuzz, 0, 25, false, requested_frame);

// Set the parent effect which properties this effect will inherit
root["parent_effect_id"] = add_property_json("Parent Effect ID", 0.0, "string", info.parent_effect_id, NULL, -1, -1, false, requested_frame);

// Return formatted string
return root.toStyledString();
}
3 changes: 3 additions & 0 deletions src/effects/ColorShift.cpp
Expand Up @@ -282,6 +282,9 @@ std::string ColorShift::PropertiesJSON(int64_t requested_frame) const {
root["alpha_x"] = add_property_json("Alpha X Shift", alpha_x.GetValue(requested_frame), "float", "", &alpha_x, -1, 1, false, requested_frame);
root["alpha_y"] = add_property_json("Alpha Y Shift", alpha_y.GetValue(requested_frame), "float", "", &alpha_y, -1, 1, false, requested_frame);

// Set the parent effect which properties this effect will inherit
root["parent_effect_id"] = add_property_json("Parent Effect ID", 0.0, "string", info.parent_effect_id, NULL, -1, -1, false, requested_frame);

// Return formatted string
return root.toStyledString();
}
3 changes: 3 additions & 0 deletions src/effects/Crop.cpp
Expand Up @@ -189,6 +189,9 @@ std::string Crop::PropertiesJSON(int64_t requested_frame) const {
root["right"] = add_property_json("Right Size", right.GetValue(requested_frame), "float", "", &right, 0.0, 1.0, false, requested_frame);
root["bottom"] = add_property_json("Bottom Size", bottom.GetValue(requested_frame), "float", "", &bottom, 0.0, 1.0, false, requested_frame);

// Set the parent effect which properties this effect will inherit
root["parent_effect_id"] = add_property_json("Parent Effect ID", 0.0, "string", info.parent_effect_id, NULL, -1, -1, false, requested_frame);

// Return formatted string
return root.toStyledString();
}
3 changes: 3 additions & 0 deletions src/effects/Deinterlace.cpp
Expand Up @@ -162,6 +162,9 @@ std::string Deinterlace::PropertiesJSON(int64_t requested_frame) const {
root["isOdd"]["choices"].append(add_property_choice_json("Yes", true, isOdd));
root["isOdd"]["choices"].append(add_property_choice_json("No", false, isOdd));

// Set the parent effect which properties this effect will inherit
root["parent_effect_id"] = add_property_json("Parent Effect ID", 0.0, "string", info.parent_effect_id, NULL, -1, -1, false, requested_frame);

// Return formatted string
return root.toStyledString();
}
3 changes: 3 additions & 0 deletions src/effects/Hue.cpp
Expand Up @@ -164,6 +164,9 @@ std::string Hue::PropertiesJSON(int64_t requested_frame) const {
// Keyframes
root["hue"] = add_property_json("Hue", hue.GetValue(requested_frame), "float", "", &hue, 0.0, 1.0, false, requested_frame);

// Set the parent effect which properties this effect will inherit
root["parent_effect_id"] = add_property_json("Parent Effect ID", 0.0, "string", info.parent_effect_id, NULL, -1, -1, false, requested_frame);

// Return formatted string
return root.toStyledString();
}
3 changes: 3 additions & 0 deletions src/effects/Mask.cpp
Expand Up @@ -293,6 +293,9 @@ std::string Mask::PropertiesJSON(int64_t requested_frame) const {
else
root["reader"] = add_property_json("Source", 0.0, "reader", "{}", NULL, 0, 1, false, requested_frame);

// Set the parent effect which properties this effect will inherit
root["parent_effect_id"] = add_property_json("Parent Effect ID", 0.0, "string", info.parent_effect_id, NULL, -1, -1, false, requested_frame);

// Return formatted string
return root.toStyledString();
}
3 changes: 3 additions & 0 deletions src/effects/Negate.cpp
Expand Up @@ -113,6 +113,9 @@ std::string Negate::PropertiesJSON(int64_t requested_frame) const {
root["end"] = add_property_json("End", End(), "float", "", NULL, 0, 30 * 60 * 60 * 48, false, requested_frame);
root["duration"] = add_property_json("Duration", Duration(), "float", "", NULL, 0, 30 * 60 * 60 * 48, true, requested_frame);

// Set the parent effect which properties this effect will inherit
root["parent_effect_id"] = add_property_json("Parent Effect ID", 0.0, "string", info.parent_effect_id, NULL, -1, -1, false, requested_frame);

// Return formatted string
return root.toStyledString();
}
3 changes: 3 additions & 0 deletions src/effects/Pixelate.cpp
Expand Up @@ -184,6 +184,9 @@ std::string Pixelate::PropertiesJSON(int64_t requested_frame) const {
root["right"] = add_property_json("Right Margin", right.GetValue(requested_frame), "float", "", &right, 0.0, 1.0, false, requested_frame);
root["bottom"] = add_property_json("Bottom Margin", bottom.GetValue(requested_frame), "float", "", &bottom, 0.0, 1.0, false, requested_frame);

// Set the parent effect which properties this effect will inherit
root["parent_effect_id"] = add_property_json("Parent Effect ID", 0.0, "string", info.parent_effect_id, NULL, -1, -1, false, requested_frame);

// Return formatted string
return root.toStyledString();
}
3 changes: 3 additions & 0 deletions src/effects/Saturation.cpp
Expand Up @@ -235,6 +235,9 @@ std::string Saturation::PropertiesJSON(int64_t requested_frame) const {
root["saturation_G"] = add_property_json("Saturation (Green)", saturation_G.GetValue(requested_frame), "float", "", &saturation_G, 0.0, 4.0, false, requested_frame);
root["saturation_B"] = add_property_json("Saturation (Blue)", saturation_B.GetValue(requested_frame), "float", "", &saturation_B, 0.0, 4.0, false, requested_frame);

// Set the parent effect which properties this effect will inherit
root["parent_effect_id"] = add_property_json("Parent Effect ID", 0.0, "string", info.parent_effect_id, NULL, -1, -1, false, requested_frame);

// Return formatted string
return root.toStyledString();
}
3 changes: 3 additions & 0 deletions src/effects/Shift.cpp
Expand Up @@ -199,6 +199,9 @@ std::string Shift::PropertiesJSON(int64_t requested_frame) const {
root["x"] = add_property_json("X Shift", x.GetValue(requested_frame), "float", "", &x, -1, 1, false, requested_frame);
root["y"] = add_property_json("Y Shift", y.GetValue(requested_frame), "float", "", &y, -1, 1, false, requested_frame);

// Set the parent effect which properties this effect will inherit
root["parent_effect_id"] = add_property_json("Parent Effect ID", 0.0, "string", info.parent_effect_id, NULL, -1, -1, false, requested_frame);

// Return formatted string
return root.toStyledString();
}
3 changes: 3 additions & 0 deletions src/effects/Stabilizer.cpp
Expand Up @@ -239,6 +239,9 @@ std::string Stabilizer::PropertiesJSON(int64_t requested_frame) const {

root["zoom"] = add_property_json("Zoom", zoom.GetValue(requested_frame), "float", "", &zoom, 0.0, 2.0, false, requested_frame);

// Set the parent effect which properties this effect will inherit
root["parent_effect_id"] = add_property_json("Parent Effect ID", 0.0, "string", info.parent_effect_id, NULL, -1, -1, false, requested_frame);

// Return formatted string
return root.toStyledString();
}
3 changes: 3 additions & 0 deletions src/effects/Wave.cpp
Expand Up @@ -187,6 +187,9 @@ std::string Wave::PropertiesJSON(int64_t requested_frame) const {
root["shift_x"] = add_property_json("X Shift", shift_x.GetValue(requested_frame), "float", "", &shift_x, 0.0, 1000.0, false, requested_frame);
root["speed_y"] = add_property_json("Vertical speed", speed_y.GetValue(requested_frame), "float", "", &speed_y, 0.0, 300.0, false, requested_frame);

// Set the parent effect which properties this effect will inherit
root["parent_effect_id"] = add_property_json("Parent Effect ID", 0.0, "string", info.parent_effect_id, NULL, -1, -1, false, requested_frame);

// Return formatted string
return root.toStyledString();
}

0 comments on commit 67895f7

Please sign in to comment.