OpenShot Library | libopenshot 1.1.1
ClipBase.cpp
Go to the documentation of this file.
1
9// Copyright (c) 2008-2019 OpenShot Studios, LLC
10//
11// SPDX-License-Identifier: LGPL-3.0-or-later
12
13#include "ClipBase.h"
14#include "Timeline.h"
15
16using namespace openshot;
17
18// Set position on timeline (in seconds)
19void ClipBase::Position(float value) {
20
21 position = value;
22
23 if (ParentTimeline()) {
24 // Resort timeline items (internal clips/effects arrays)
25 Timeline *parentTimeline = (Timeline *) ParentTimeline();
26 parentTimeline->SortTimeline();
27 }
28}
29
30// Set layer of clip on timeline (lower number is covered by higher numbers)
31void ClipBase::Layer(int value) {
32 layer = value;
33
34 if (ParentTimeline()) {
35 // Resort timeline items (internal clips/effects arrays)
36 Timeline *parentTimeline = (Timeline *) ParentTimeline();
37 parentTimeline->SortTimeline();
38 }
39}
40
41// Set start position (in seconds) of clip (trim start of video)
42void ClipBase::Start(float value) {
43 start = value;
44
45 if (ParentTimeline()) {
46 // Resort timeline items (internal clips/effects arrays)
47 Timeline *parentTimeline = (Timeline *) ParentTimeline();
48 parentTimeline->SortTimeline();
49 }
50}
51
52// Set end position (in seconds) of clip (trim end of video)
53void ClipBase::End(float value) {
54 end = value;
55
56 if (ParentTimeline()) {
57 // Resort timeline items (internal clips/effects arrays)
58 Timeline *parentTimeline = (Timeline *) ParentTimeline();
59 parentTimeline->SortTimeline();
60 }
61}
62
63// Generate Json::Value for this object
64Json::Value ClipBase::JsonValue() const {
65
66 // Create root json object
67 Json::Value root;
68 root["id"] = Id();
69 root["position"] = Position();
70 root["layer"] = Layer();
71 root["start"] = Start();
72 root["end"] = End();
73 root["duration"] = Duration();
74
75 // return JsonValue
76 return root;
77}
78
79// Load Json::Value into this object
80void ClipBase::SetJsonValue(const Json::Value root) {
81 SetBaseJsonValue(root);
82}
83
84void ClipBase::SetBaseJsonValue(const Json::Value& root) {
85
86 // Set data from Json (if key is found)
87 if (!root["id"].isNull())
88 Id(root["id"].asString());
89 if (!root["position"].isNull())
90 Position(root["position"].asDouble());
91 if (!root["layer"].isNull())
92 Layer(root["layer"].asInt());
93 if (!root["start"].isNull())
94 Start(root["start"].asDouble());
95 if (!root["end"].isNull())
96 End(root["end"].asDouble());
97}
98
99// Generate JSON for a property
100Json::Value ClipBase::add_property_json(std::string name, float value, std::string type, std::string memo, const Keyframe* keyframe, float min_value, float max_value, bool readonly, int64_t requested_frame) const {
101
102 // Requested Point
103 const Point requested_point(requested_frame, requested_frame);
104
105 // Create JSON Object
106 Json::Value prop = Json::Value(Json::objectValue);
107 prop["name"] = name;
108 prop["value"] = value;
109 prop["memo"] = memo;
110 prop["type"] = type;
111 prop["min"] = min_value;
112 prop["max"] = max_value;
113 if (keyframe) {
114 prop["keyframe"] = keyframe->Contains(requested_point);
115 prop["points"] = int(keyframe->GetCount());
116 Point closest_point = keyframe->GetClosestPoint(requested_point);
117 prop["interpolation"] = closest_point.interpolation;
118 prop["closest_point_x"] = closest_point.co.X;
119 prop["previous_point_x"] = keyframe->GetPreviousPoint(closest_point).co.X;
120 }
121 else {
122 prop["keyframe"] = false;
123 prop["points"] = 0;
124 prop["interpolation"] = CONSTANT;
125 prop["closest_point_x"] = -1;
126 prop["previous_point_x"] = -1;
127 }
128
129 prop["readonly"] = readonly;
130 prop["choices"] = Json::Value(Json::arrayValue);
131
132 // return JsonValue
133 return prop;
134}
135
136Json::Value ClipBase::add_property_choice_json(std::string name, int value, int selected_value) const {
137
138 // Create choice
139 Json::Value new_choice = Json::Value(Json::objectValue);
140 new_choice["name"] = name;
141 new_choice["value"] = value;
142 new_choice["selected"] = (value == selected_value);
143
144 // return JsonValue
145 return new_choice;
146}
Header file for ClipBase class.
Header file for Timeline class.
void SetBaseJsonValue(const Json::Value &root)
Load common clip fields without copying derived animation data.
Definition: ClipBase.cpp:84
float Start() const
Get start position (in seconds) of clip (trim start of video)
Definition: ClipBase.h:91
float start
The position in seconds to start playing (used to trim the beginning of a clip)
Definition: ClipBase.h:37
float Duration() const
Get the length of this clip (in seconds)
Definition: ClipBase.h:93
virtual float End() const
Get end position (in seconds) of clip (trim end of video)
Definition: ClipBase.h:92
std::string Id() const
Get the Id of this clip object.
Definition: ClipBase.h:88
virtual Json::Value JsonValue() const =0
Generate Json::Value for this object.
Definition: ClipBase.cpp:64
Json::Value add_property_choice_json(std::string name, int value, int selected_value) const
Generate JSON choice for a property (dropdown properties)
Definition: ClipBase.cpp:136
int Layer() const
Get layer of clip on timeline (lower number is covered by higher numbers)
Definition: ClipBase.h:90
virtual void SetJsonValue(const Json::Value root)=0
Load Json::Value into this object.
Definition: ClipBase.cpp:80
int layer
The layer this clip is on. Lower clips are covered up by higher clips.
Definition: ClipBase.h:36
float Position() const
Get position on timeline (in seconds)
Definition: ClipBase.h:89
virtual openshot::TimelineBase * ParentTimeline()
Get the associated Timeline pointer (if any)
Definition: ClipBase.h:94
float position
The position on the timeline where this clip should start playing.
Definition: ClipBase.h:35
float end
The position in seconds to end playing (used to trim the ending of a clip)
Definition: ClipBase.h:38
Json::Value add_property_json(std::string name, float value, std::string type, std::string memo, const Keyframe *keyframe, float min_value, float max_value, bool readonly, int64_t requested_frame) const
Generate JSON for a property.
Definition: ClipBase.cpp:100
double X
The X value of the coordinate (usually representing the frame #)
Definition: Coordinate.h:40
A Keyframe is a collection of Point instances, which is used to vary a number or property over time.
Definition: KeyFrame.h:53
bool Contains(Point p) const
Does this keyframe contain a specific point.
Definition: KeyFrame.cpp:184
Point GetPreviousPoint(Point p) const
Get previous point (.
Definition: KeyFrame.cpp:226
int64_t GetCount() const
Get the number of points (i.e. # of points)
Definition: KeyFrame.cpp:430
Point GetClosestPoint(Point p) const
Get current point (or closest point to the right) from the X coordinate (i.e. the frame number)
Definition: KeyFrame.cpp:221
A Point is the basic building block of a key-frame curve.
Definition: Point.h:64
Coordinate co
This is the primary coordinate.
Definition: Point.h:66
InterpolationType interpolation
This is the interpolation mode.
Definition: Point.h:69
This class represents a timeline.
Definition: Timeline.h:154
void SortTimeline()
Sort all clips and effects on timeline - which affects the internal order of clips and effects arrays...
Definition: Timeline.h:385
This namespace is the default namespace for all code in the openshot library.
Definition: AnimatedCurve.h:24
@ CONSTANT
Constant curves jump from their previous position to a new one (with no interpolation).
Definition: Point.h:31