OpenShot Library | libopenshot
1.1.1
Toggle main menu visibility
Loading...
Searching...
No Matches
Point.cpp
Go to the documentation of this file.
1
8
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
17
using namespace
std;
18
using namespace
openshot
;
19
20
// Default constructor
21
Point::Point
() :
Point
::
Point
(
Coordinate
(1, 0),
BEZIER
,
AUTO
) {}
22
23
// Constructor which creates a single coordinate at X=1
24
Point::Point
(
float
y) :
Point
::
Point
(
Coordinate
(1, y),
CONSTANT
,
AUTO
) {}
25
26
// Constructor which creates a Bezier curve with point at (x, y)
27
Point::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.
30
Point::Point
(
float
x,
float
y,
InterpolationType
interpolation
)
31
:
Point
::
Point
(
Coordinate
(x, y),
interpolation
,
AUTO
) {}
32
33
34
// Direct Coordinate-accepting constructors
35
Point::Point
(
const
Coordinate
&
co
) :
Point
::
Point
(
co
,
BEZIER
,
AUTO
) {}
36
37
Point::Point
(
const
Coordinate
&
co
,
InterpolationType
interpolation
)
38
:
Point
::
Point
(
co
,
interpolation
,
AUTO
) {}
39
40
Point::Point
(
const
Coordinate
&
co
,
InterpolationType
interpolation
,
HandleType
handle_type
) :
41
co
(
co
),
interpolation
(
interpolation
),
handle_type
(
handle_type
) {
42
// set handles
43
Initialize_Handles
();
44
}
45
46
void
Point::Initialize_Handles
() {
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
53
void
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
58
void
Point::Initialize_RightHandle
(
float
x,
float
y) {
59
// initialize right handle (in percentages from 0 to 1)
60
handle_right
=
Coordinate
(x, y);
61
}
62
63
// Generate JSON string of this object
64
std::string
Point::Json
()
const
{
65
66
// Return formatted string
67
return
JsonValue
().toStyledString();
68
}
69
70
// Generate Json::Value for this object
71
Json::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
88
void
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
105
void
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
}
Exceptions.h
Header file for all Exception classes.
Point.h
Header file for Point class.
openshot::Coordinate
A Cartesian coordinate (X, Y) used in the Keyframe animation system.
Definition
Coordinate.h:38
openshot::InvalidJSON
Exception for invalid JSON.
Definition
Exceptions.h:224
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::SetJson
void SetJson(const std::string value)
Load JSON string into this object.
Definition
Point.cpp:88
openshot::Point::JsonValue
Json::Value JsonValue() const
Generate Json::Value for this object.
Definition
Point.cpp:71
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:58
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:53
openshot::Point::Initialize_Handles
void Initialize_Handles()
Definition
Point.cpp:46
openshot::Point::handle_type
HandleType handle_type
This is the handle mode.
Definition
Point.h:70
openshot::Point::co
Coordinate co
This is the primary coordinate.
Definition
Point.h:66
openshot::Point::interpolation
InterpolationType interpolation
This is the interpolation mode.
Definition
Point.h:69
openshot::Point::handle_right
Coordinate handle_right
This is the right handle coordinate (in percentages from 0 to 1).
Definition
Point.h:68
openshot::Point::Point
Point()
Default constructor (defaults to 1,0).
Definition
Point.cpp:21
openshot::Point::SetJsonValue
void SetJsonValue(const Json::Value root)
Load Json::Value into this object.
Definition
Point.cpp:105
openshot::Point::Json
std::string Json() const
Generate JSON string of this object.
Definition
Point.cpp:64
openshot
This namespace is the default namespace for all code in the openshot library.
Definition
AnimatedCurve.h:24
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::stringToJson
const Json::Value stringToJson(const std::string value)
Definition
Json.cpp:16
openshot::InterpolationType
InterpolationType
This controls how a Keyframe uses this point to interpolate between two points.
Definition
Point.h:28
openshot::CONSTANT
@ CONSTANT
Constant curves jump from their previous position to a new one (with no interpolation).
Definition
Point.h:31
openshot::BEZIER
@ BEZIER
Bezier curves are quadratic curves, which create a smooth curve.
Definition
Point.h:29
Point.cpp
Generated by
1.17.0