OpenShot Library | libopenshot  0.3.2
Point.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 "Point.h"
14 #include "Exceptions.h"
15 
16 using namespace std;
17 using namespace openshot;
18 
19 // Default constructor
20 Point::Point() : Point::Point(Coordinate(1, 0), BEZIER, AUTO) {}
21 
22 // Constructor which creates a single coordinate at X=1
24 
25 // Constructor which creates a Bezier curve with point at (x, y)
26 Point::Point(float x, float y) : Point::Point(Coordinate(x, y), BEZIER, AUTO) {}
27 
28 // Constructor which also creates a Point, setting X,Y, and interpolation.
29 Point::Point(float x, float y, InterpolationType interpolation)
30  : Point::Point(Coordinate(x, y), interpolation, AUTO) {}
31 
32 
33 // Direct Coordinate-accepting constructors
34 Point::Point(const Coordinate& co) : Point::Point(co, BEZIER, AUTO) {}
35 
36 Point::Point(const Coordinate& co, InterpolationType interpolation)
37  : Point::Point(co, interpolation, AUTO) {}
38 
39 Point::Point(const Coordinate& co, InterpolationType interpolation, HandleType handle_type) :
40  co(co), interpolation(interpolation), handle_type(handle_type) {
41  // set handles
43 }
44 
46  // initialize left and right handles (in percentages from 0 to 1)
47  // default to a smooth curve
48  Initialize_LeftHandle(0.5, 1.0);
49  Initialize_RightHandle(0.5, 0.0);
50 }
51 
52 void Point::Initialize_LeftHandle(float x, float y) {
53  // initialize left handle (in percentages from 0 to 1)
54  handle_left = Coordinate(x, y);
55 }
56 
57 void Point::Initialize_RightHandle(float x, float y) {
58  // initialize right handle (in percentages from 0 to 1)
59  handle_right = Coordinate(x, y);
60 }
61 
62 // Generate JSON string of this object
63 std::string Point::Json() const {
64 
65  // Return formatted string
66  return JsonValue().toStyledString();
67 }
68 
69 // Generate Json::Value for this object
70 Json::Value Point::JsonValue() const {
71 
72  // Create root json object
73  Json::Value root;
74  root["co"] = co.JsonValue();
75  if (interpolation == BEZIER) {
76  root["handle_left"] = handle_left.JsonValue();
77  root["handle_right"] = handle_right.JsonValue();
78  root["handle_type"] = handle_type;
79  }
80  root["interpolation"] = interpolation;
81 
82  // return JsonValue
83  return root;
84 }
85 
86 // Load JSON string into this object
87 void Point::SetJson(const std::string value) {
88 
89  // Parse JSON string into JSON objects
90  try
91  {
92  const Json::Value root = openshot::stringToJson(value);
93  // Set all values that match
94  SetJsonValue(root);
95  }
96  catch (const std::exception& e)
97  {
98  // Error parsing JSON (or missing keys)
99  throw InvalidJSON("JSON is invalid (missing keys or invalid data types)");
100  }
101 }
102 
103 // Load Json::Value into this object
104 void Point::SetJsonValue(const Json::Value root) {
105 
106  if (!root["co"].isNull())
107  co.SetJsonValue(root["co"]); // update coordinate
108  if (!root["handle_left"].isNull())
109  handle_left.SetJsonValue(root["handle_left"]); // update coordinate
110  if (!root["handle_right"].isNull())
111  handle_right.SetJsonValue(root["handle_right"]); // update coordinate
112  if (!root["interpolation"].isNull())
113  interpolation = (InterpolationType) root["interpolation"].asInt();
114  if (!root["handle_type"].isNull())
115  handle_type = (HandleType) root["handle_type"].asInt();
116 
117 }
openshot::stringToJson
const Json::Value stringToJson(const std::string value)
Definition: Json.cpp:16
openshot::Point::Point
Point()
Default constructor (defaults to 1,0)
Definition: Point.cpp:20
Point.h
Header file for Point class.
openshot::Point::Initialize_LeftHandle
void Initialize_LeftHandle(float x, float y)
Set the left handle to a percent of the primary coordinate (0 to 1)
Definition: Point.cpp:52
openshot::Point::interpolation
InterpolationType interpolation
This is the interpolation mode.
Definition: Point.h:69
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::Coordinate::SetJsonValue
void SetJsonValue(const Json::Value root)
Load Json::Value into this object.
Definition: Coordinate.cpp:70
openshot::Point::handle_type
HandleType handle_type
This is the handle mode.
Definition: Point.h:70
openshot::HandleType
HandleType
When BEZIER interpolation is used, the point's left and right handles are used to influence the direc...
Definition: Point.h:41
openshot::AUTO
@ AUTO
Automatically adjust the handles to achieve the smoothest curve.
Definition: Point.h:42
openshot::InvalidJSON
Exception for invalid JSON.
Definition: Exceptions.h:217
openshot::Point::SetJsonValue
void SetJsonValue(const Json::Value root)
Load Json::Value into this object.
Definition: Point.cpp:104
openshot::Point::Json
std::string Json() const
Generate JSON string of this object.
Definition: Point.cpp:63
openshot::Point::SetJson
void SetJson(const std::string value)
Load JSON string into this object.
Definition: Point.cpp:87
openshot::Point::JsonValue
Json::Value JsonValue() const
Generate Json::Value for this object.
Definition: Point.cpp:70
openshot::Point::Initialize_RightHandle
void Initialize_RightHandle(float x, float y)
Set the right handle to a percent of the primary coordinate (0 to 1)
Definition: Point.cpp:57
openshot::Point::handle_left
Coordinate handle_left
This is the left handle coordinate (in percentages from 0 to 1)
Definition: Point.h:67
openshot::Point::Initialize_Handles
void Initialize_Handles()
Definition: Point.cpp:45
openshot::CONSTANT
@ CONSTANT
Constant curves jump from their previous position to a new one (with no interpolation).
Definition: Point.h:31
openshot::Coordinate::JsonValue
Json::Value JsonValue() const
Generate Json::Value for this object.
Definition: Coordinate.cpp:36
openshot::InterpolationType
InterpolationType
This controls how a Keyframe uses this point to interpolate between two points.
Definition: Point.h:28
openshot::Point::handle_right
Coordinate handle_right
This is the right handle coordinate (in percentages from 0 to 1)
Definition: Point.h:68
openshot::BEZIER
@ BEZIER
Bezier curves are quadratic curves, which create a smooth curve.
Definition: Point.h:29
openshot::Coordinate
A Cartesian coordinate (X, Y) used in the Keyframe animation system.
Definition: Coordinate.h:38
openshot::Point
A Point is the basic building block of a key-frame curve.
Definition: Point.h:64
Exceptions.h
Header file for all Exception classes.