OpenShot Library | libopenshot  0.3.3
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 
16 using namespace openshot;
17 
18 // Set position on timeline (in seconds)
19 void 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)
31 void 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)
42 void 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)
53 void 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
64 Json::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
80 void ClipBase::SetJsonValue(const Json::Value root) {
81 
82  // Set data from Json (if key is found)
83  if (!root["id"].isNull())
84  Id(root["id"].asString());
85  if (!root["position"].isNull())
86  Position(root["position"].asDouble());
87  if (!root["layer"].isNull())
88  Layer(root["layer"].asInt());
89  if (!root["start"].isNull())
90  Start(root["start"].asDouble());
91  if (!root["end"].isNull())
92  End(root["end"].asDouble());
93 }
94 
95 // Generate JSON for a property
96 Json::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 {
97 
98  // Requested Point
99  const Point requested_point(requested_frame, requested_frame);
100 
101  // Create JSON Object
102  Json::Value prop = Json::Value(Json::objectValue);
103  prop["name"] = name;
104  prop["value"] = value;
105  prop["memo"] = memo;
106  prop["type"] = type;
107  prop["min"] = min_value;
108  prop["max"] = max_value;
109  if (keyframe) {
110  prop["keyframe"] = keyframe->Contains(requested_point);
111  prop["points"] = int(keyframe->GetCount());
112  Point closest_point = keyframe->GetClosestPoint(requested_point);
113  prop["interpolation"] = closest_point.interpolation;
114  prop["closest_point_x"] = closest_point.co.X;
115  prop["previous_point_x"] = keyframe->GetPreviousPoint(closest_point).co.X;
116  }
117  else {
118  prop["keyframe"] = false;
119  prop["points"] = 0;
120  prop["interpolation"] = CONSTANT;
121  prop["closest_point_x"] = -1;
122  prop["previous_point_x"] = -1;
123  }
124 
125  prop["readonly"] = readonly;
126  prop["choices"] = Json::Value(Json::arrayValue);
127 
128  // return JsonValue
129  return prop;
130 }
131 
132 Json::Value ClipBase::add_property_choice_json(std::string name, int value, int selected_value) const {
133 
134  // Create choice
135  Json::Value new_choice = Json::Value(Json::objectValue);
136  new_choice["name"] = name;
137  new_choice["value"] = value;
138  new_choice["selected"] = (value == selected_value);
139 
140  // return JsonValue
141  return new_choice;
142 }
openshot::ClipBase::add_property_json
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:96
openshot::Point::interpolation
InterpolationType interpolation
This is the interpolation mode.
Definition: Point.h:69
openshot::ClipBase::End
virtual void End(float value)
Set end position (in seconds) of clip (trim end of video)
Definition: ClipBase.cpp:53
openshot
This namespace is the default namespace for all code in the openshot library.
Definition: Compressor.h:28
openshot::Point::co
Coordinate co
This is the primary coordinate.
Definition: Point.h:66
openshot::ClipBase::add_property_choice_json
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:132
Timeline.h
Header file for Timeline class.
openshot::Keyframe::Contains
bool Contains(Point p) const
Does this keyframe contain a specific point.
Definition: KeyFrame.cpp:184
openshot::ClipBase::Position
void Position(float value)
Set the Id of this clip object
Definition: ClipBase.cpp:19
openshot::ClipBase::SetJsonValue
virtual void SetJsonValue(const Json::Value root)=0
Load Json::Value into this object.
Definition: ClipBase.cpp:80
openshot::ClipBase::JsonValue
virtual Json::Value JsonValue() const =0
Generate Json::Value for this object.
Definition: ClipBase.cpp:64
openshot::ClipBase::position
float position
The position on the timeline where this clip should start playing.
Definition: ClipBase.h:36
openshot::Keyframe
A Keyframe is a collection of Point instances, which is used to vary a number or property over time.
Definition: KeyFrame.h:53
openshot::Timeline
This class represents a timeline.
Definition: Timeline.h:148
openshot::ClipBase::end
float end
The position in seconds to end playing (used to trim the ending of a clip)
Definition: ClipBase.h:39
openshot::ClipBase::Start
void Start(float value)
Set start position (in seconds) of clip (trim start of video)
Definition: ClipBase.cpp:42
openshot::ClipBase::start
float start
The position in seconds to start playing (used to trim the beginning of a clip)
Definition: ClipBase.h:38
openshot::Keyframe::GetClosestPoint
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
openshot::ClipBase::layer
int layer
The layer this clip is on. Lower clips are covered up by higher clips.
Definition: ClipBase.h:37
openshot::Timeline::SortTimeline
void SortTimeline()
Sort all clips and effects on timeline - which affects the internal order of clips and effects arrays...
Definition: Timeline.h:360
openshot::ClipBase::Id
void Id(std::string value)
Definition: ClipBase.h:94
openshot::Keyframe::GetCount
int64_t GetCount() const
Get the number of points (i.e. # of points)
Definition: KeyFrame.cpp:424
openshot::CONSTANT
@ CONSTANT
Constant curves jump from their previous position to a new one (with no interpolation).
Definition: Point.h:31
openshot::Keyframe::GetPreviousPoint
Point GetPreviousPoint(Point p) const
Get previous point (.
Definition: KeyFrame.cpp:226
openshot::ClipBase::Layer
void Layer(int value)
Set layer of clip on timeline (lower number is covered by higher numbers)
Definition: ClipBase.cpp:31
ClipBase.h
Header file for ClipBase class.
openshot::Point
A Point is the basic building block of a key-frame curve.
Definition: Point.h:64
openshot::Coordinate::X
double X
The X value of the coordinate (usually representing the frame #)
Definition: Coordinate.h:40