OpenShot Library | libopenshot 1.1.1
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 <utility>
14#include "Point.h"
15#include "Exceptions.h"
16
17using namespace std;
18using namespace openshot;
19
20// Default constructor
22
23// Constructor which creates a single coordinate at X=1
25
26// Constructor which creates a Bezier curve with point at (x, y)
27Point::Point(float x, float y) : Point::Point(Coordinate(x, y), BEZIER, AUTO) {}
28
29// Constructor which also creates a Point, setting X,Y, and interpolation.
30Point::Point(float x, float y, InterpolationType interpolation)
31 : Point::Point(Coordinate(x, y), interpolation, AUTO) {}
32
33
34// Direct Coordinate-accepting constructors
36
37Point::Point(const Coordinate& co, InterpolationType interpolation)
38 : Point::Point(co, interpolation, AUTO) {}
39
40Point::Point(const Coordinate& co, InterpolationType interpolation, HandleType handle_type) :
41 co(co), interpolation(interpolation), handle_type(handle_type) {
42 // set handles
44}
45
47 // initialize left and right handles (in percentages from 0 to 1)
48 // default to a smooth curve
49 Initialize_LeftHandle(0.5, 1.0);
50 Initialize_RightHandle(0.5, 0.0);
51}
52
53void Point::Initialize_LeftHandle(float x, float y) {
54 // initialize left handle (in percentages from 0 to 1)
55 handle_left = Coordinate(x, y);
56}
57
58void Point::Initialize_RightHandle(float x, float y) {
59 // initialize right handle (in percentages from 0 to 1)
61}
62
63// Generate JSON string of this object
64std::string Point::Json() const {
65
66 // Return formatted string
67 return JsonValue().toStyledString();
68}
69
70// Generate Json::Value for this object
71Json::Value Point::JsonValue() const {
72
73 // Create root json object
74 Json::Value root;
75 root["co"] = co.JsonValue();
76 if (interpolation == BEZIER) {
77 root["handle_left"] = handle_left.JsonValue();
78 root["handle_right"] = handle_right.JsonValue();
79 root["handle_type"] = handle_type;
80 }
81 root["interpolation"] = interpolation;
82
83 // return JsonValue
84 return root;
85}
86
87// Load JSON string into this object
88void Point::SetJson(const std::string value) {
89
90 // Parse JSON string into JSON objects
91 try
92 {
93 Json::Value root = openshot::stringToJson(value);
94 // Set all values that match
95 SetJsonValue(std::move(root));
96 }
97 catch (const std::exception& e)
98 {
99 // Error parsing JSON (or missing keys)
100 throw InvalidJSON("JSON is invalid (missing keys or invalid data types)");
101 }
102}
103
104// Load Json::Value into this object
105void Point::SetJsonValue(Json::Value root) {
106
107 if (!root["co"].isNull())
108 co.SetJsonValue(std::move(root["co"])); // update coordinate
109 if (!root["handle_left"].isNull())
110 handle_left.SetJsonValue(std::move(root["handle_left"])); // update coordinate
111 if (!root["handle_right"].isNull())
112 handle_right.SetJsonValue(std::move(root["handle_right"])); // update coordinate
113 if (!root["interpolation"].isNull())
114 interpolation = (InterpolationType) root["interpolation"].asInt();
115 if (!root["handle_type"].isNull())
116 handle_type = (HandleType) root["handle_type"].asInt();
117
118}
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:224
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:88
Json::Value JsonValue() const
Generate Json::Value for this object.
Definition Point.cpp:71
void Initialize_RightHandle(float x, float y)
Set the right handle to a percent of the primary coordinate (0 to 1)
Definition Point.cpp:58
void Initialize_LeftHandle(float x, float y)
Set the left handle to a percent of the primary coordinate (0 to 1)
Definition Point.cpp:53
void Initialize_Handles()
Definition Point.cpp:46
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:21
void SetJsonValue(const Json::Value root)
Load Json::Value into this object.
Definition Point.cpp:105
std::string Json() const
Generate JSON string of this object.
Definition Point.cpp:64
This namespace is the default namespace for all code in the openshot library.
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