OpenShot Library | libopenshot  0.1.2
Point.cpp
Go to the documentation of this file.
1 /**
2  * @file
3  * @brief Source file for Point class
4  * @author Jonathan Thomas <jonathan@openshot.org>
5  *
6  * @section LICENSE
7  *
8  * Copyright (c) 2008-2014 OpenShot Studios, LLC
9  * <http://www.openshotstudios.com/>. This file is part of
10  * OpenShot Library (libopenshot), an open-source project dedicated to
11  * delivering high quality video editing and animation solutions to the
12  * world. For more information visit <http://www.openshot.org/>.
13  *
14  * OpenShot Library (libopenshot) is free software: you can redistribute it
15  * and/or modify it under the terms of the GNU Lesser General Public License
16  * as published by the Free Software Foundation, either version 3 of the
17  * License, or (at your option) any later version.
18  *
19  * OpenShot Library (libopenshot) is distributed in the hope that it will be
20  * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22  * GNU Lesser General Public License for more details.
23  *
24  * You should have received a copy of the GNU Lesser General Public License
25  * along with OpenShot Library. If not, see <http://www.gnu.org/licenses/>.
26  */
27 
28 #include "../include/Point.h"
29 
30 using namespace std;
31 using namespace openshot;
32 
33 // Default constructor (defaults to 0,0)
34 Point::Point() : interpolation(BEZIER), handle_type(AUTO)
35 {
36  // set new coorinate
37  co = Coordinate(1, 0);
38 
39  // set handles
41 }
42 
43 // Constructor which creates a single coordinate at X=0
44 Point::Point(float y) :
46  // set new coorinate
47  co = Coordinate(1, y);
48 
49  // set handles
51 }
52 
53 Point::Point(float x, float y) :
55  // set new coorinate
56  co = Coordinate(x, y);
57 
58  // set handles
60 }
61 
62 // Constructor which also creates a Point and sets the X,Y, and interpolation of the Point.
64  handle_type(AUTO), interpolation(interpolation) {
65  // set new coorinate
66  co = Coordinate(x, y);
67 
68  // set handles
70 }
71 
74  // set handles
76 }
77 
79  co(co), interpolation(interpolation), handle_type(AUTO) {
80  // set handles
82 }
83 
85  co(co), interpolation(interpolation), handle_type(handle_type) {
86  // set handles
88 }
89 
90 void Point::Initialize_Handles(float Offset) {
91  // initialize left and right handles
92  handle_left = Coordinate(co.X - Offset, co.Y);
93  handle_right = Coordinate(co.X + Offset, co.Y);
94 }
95 
96 // Generate JSON string of this object
97 string Point::Json() {
98 
99  // Return formatted string
100  return JsonValue().toStyledString();
101 }
102 
103 // Generate Json::JsonValue for this object
104 Json::Value Point::JsonValue() {
105 
106  // Create root json object
107  Json::Value root;
108  root["co"] = co.JsonValue();
109  if (interpolation == BEZIER) {
110  root["handle_left"] = handle_left.JsonValue();
111  root["handle_right"] = handle_right.JsonValue();
112  root["handle_type"] = handle_type;
113  }
114  root["interpolation"] = interpolation;
115 
116  // return JsonValue
117  return root;
118 }
119 
120 // Load JSON string into this object
121 void Point::SetJson(string value) throw(InvalidJSON) {
122 
123  // Parse JSON string into JSON objects
124  Json::Value root;
125  Json::Reader reader;
126  bool success = reader.parse( value, root );
127  if (!success)
128  // Raise exception
129  throw InvalidJSON("JSON could not be parsed (or is invalid)", "");
130 
131  try
132  {
133  // Set all values that match
134  SetJsonValue(root);
135  }
136  catch (exception e)
137  {
138  // Error parsing JSON (or missing keys)
139  throw InvalidJSON("JSON is invalid (missing keys or invalid data types)", "");
140  }
141 }
142 
143 // Load Json::JsonValue into this object
144 void Point::SetJsonValue(Json::Value root) {
145 
146  if (!root["co"].isNull())
147  co.SetJsonValue(root["co"]); // update coordinate
148  if (!root["handle_left"].isNull())
149  handle_left.SetJsonValue(root["handle_left"]); // update coordinate
150  if (!root["handle_right"].isNull())
151  handle_right.SetJsonValue(root["handle_right"]); // update coordinate
152  if (!root["interpolation"].isNull())
153  interpolation = (InterpolationType) root["interpolation"].asInt();
154  if (!root["handle_type"].isNull())
155  handle_type = (HandleType) root["handle_type"].asInt();
156 
157 }
string Json()
Get and Set JSON methods.
Definition: Point.cpp:97
Json::Value JsonValue()
Generate Json::JsonValue for this object.
Definition: Point.cpp:104
This class represents a Cartesian coordinate (X, Y) used in the Keyframe animation system...
Definition: Coordinate.h:54
void SetJsonValue(Json::Value root)
Load Json::JsonValue into this object.
Definition: Coordinate.cpp:92
Bezier curves are quadratic curves, which create a smooth curve.
Definition: Point.h:46
InterpolationType interpolation
This is the interpolation mode.
Definition: Point.h:86
Coordinate handle_right
This is the right handle coordinate.
Definition: Point.h:85
void Initialize_Handles(float Offset=0.0f)
Definition: Point.cpp:90
Coordinate handle_left
This is the left handle coordinate.
Definition: Point.h:84
HandleType handle_type
This is the handle mode.
Definition: Point.h:87
void SetJson(string value)
Load JSON string into this object.
Definition: Point.cpp:121
float X
The X value of the coordinate (usually representing the frame #)
Definition: Coordinate.h:61
HandleType
When BEZIER interpolation is used, the point&#39;s left and right handles are used to influence the direc...
Definition: Point.h:58
Point()
Default constructor (defaults to 1,0)
Definition: Point.cpp:34
Automatically adjust the handles to achieve the smoothest curve.
Definition: Point.h:59
InterpolationType
This controls how a Keyframe uses this point to interpolate between two points.
Definition: Point.h:45
This namespace is the default namespace for all code in the openshot library.
Coordinate co
This is the primary coordinate.
Definition: Point.h:83
Exception for invalid JSON.
Definition: Exceptions.h:152
void SetJsonValue(Json::Value root)
Load Json::JsonValue into this object.
Definition: Point.cpp:144
float Y
The Y value of the coordinate (usually representing the value of the property being animated) ...
Definition: Coordinate.h:62
Json::Value JsonValue()
Generate Json::JsonValue for this object.
Definition: Coordinate.cpp:52
Constant curves jump from their previous position to a new one (with no interpolation).
Definition: Point.h:48