OpenShot Library | libopenshot 0.3.3
Loading...
Searching...
No Matches
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
16using namespace std;
17using namespace openshot;
18
19// Default constructor
21
22// Constructor which creates a single coordinate at X=1
24
25// Constructor which creates a Bezier curve with point at (x, y)
26Point::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.
29Point::Point(float x, float y, InterpolationType interpolation)
30 : Point::Point(Coordinate(x, y), interpolation, AUTO) {}
31
32
33// Direct Coordinate-accepting constructors
35
36Point::Point(const Coordinate& co, InterpolationType interpolation)
37 : Point::Point(co, interpolation, AUTO) {}
38
39Point::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
52void 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
57void Point::Initialize_RightHandle(float x, float y) {
58 // initialize right handle (in percentages from 0 to 1)
60}
61
62// Generate JSON string of this object
63std::string Point::Json() const {
64
65 // Return formatted string
66 return JsonValue().toStyledString();
67}
68
69// Generate Json::Value for this object
70Json::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
87void 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
104void 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}
Header file for all Exception classes.
Header file for Point class.
A Cartesian coordinate (X, Y) used in the Keyframe animation system.
Definition Coordinate.h:38
void SetJsonValue(const Json::Value root)
Load Json::Value into this object.
Json::Value JsonValue() const
Generate Json::Value for this object.
Exception for invalid JSON.
Definition Exceptions.h:218
A Point is the basic building block of a key-frame curve.
Definition Point.h:64
Coordinate handle_left
This is the left handle coordinate (in percentages from 0 to 1)
Definition Point.h:67
void SetJson(const std::string value)
Load JSON string into this object.
Definition Point.cpp:87
Json::Value JsonValue() const
Generate Json::Value for this object.
Definition Point.cpp:70
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
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
void Initialize_Handles()
Definition Point.cpp:45
HandleType handle_type
This is the handle mode.
Definition Point.h:70
Coordinate co
This is the primary coordinate.
Definition Point.h:66
InterpolationType interpolation
This is the interpolation mode.
Definition Point.h:69
Coordinate handle_right
This is the right handle coordinate (in percentages from 0 to 1)
Definition Point.h:68
Point()
Default constructor (defaults to 1,0)
Definition Point.cpp:20
void SetJsonValue(const Json::Value root)
Load Json::Value into this object.
Definition Point.cpp:104
std::string Json() const
Generate JSON string of this object.
Definition Point.cpp:63
This namespace is the default namespace for all code in the openshot library.
Definition Compressor.h:29
HandleType
When BEZIER interpolation is used, the point's left and right handles are used to influence the direc...
Definition Point.h:41
@ AUTO
Automatically adjust the handles to achieve the smoothest curve.
Definition Point.h:42
const Json::Value stringToJson(const std::string value)
Definition Json.cpp:16
InterpolationType
This controls how a Keyframe uses this point to interpolate between two points.
Definition Point.h:28
@ CONSTANT
Constant curves jump from their previous position to a new one (with no interpolation).
Definition Point.h:31
@ BEZIER
Bezier curves are quadratic curves, which create a smooth curve.
Definition Point.h:29