OpenShot Library | libopenshot  0.3.3
Coordinate.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 "Coordinate.h"
14 #include "Exceptions.h"
15 
16 using namespace openshot;
17 
18 // Default constructor for a coordinate, delegating to the full signature
20 
21 // Constructor which also allows the user to set the X and Y
22 Coordinate::Coordinate(double x, double y) : X(x), Y(y) {}
23 
24 // Constructor which accepts a std::pair for (X, Y)
25 Coordinate::Coordinate(const std::pair<double, double>& co)
26  : X(co.first), Y(co.second) {}
27 
28 // Generate JSON string of this object
29 std::string Coordinate::Json() const {
30 
31  // Return formatted string
32  return JsonValue().toStyledString();
33 }
34 
35 // Generate Json::Value for this object
36 Json::Value Coordinate::JsonValue() const {
37 
38  // Create root json object
39  Json::Value root;
40  root["X"] = X;
41  root["Y"] = Y;
42  //root["increasing"] = increasing;
43  //root["repeated"] = Json::Value(Json::objectValue);
44  //root["repeated"]["num"] = repeated.num;
45  //root["repeated"]["den"] = repeated.den;
46  //root["delta"] = delta;
47 
48  // return JsonValue
49  return root;
50 }
51 
52 // Load JSON string into this object
53 void Coordinate::SetJson(const std::string value) {
54 
55  // Parse JSON string into JSON objects
56  try
57  {
58  const Json::Value root = openshot::stringToJson(value);
59  // Set all values that match
60  SetJsonValue(root);
61  }
62  catch (const std::exception& e)
63  {
64  // Error parsing JSON (or missing keys)
65  throw InvalidJSON("JSON is invalid (missing keys or invalid data types)");
66  }
67 }
68 
69 // Load Json::Value into this object
70 void Coordinate::SetJsonValue(const Json::Value root) {
71 
72  // Set data from Json (if key is found)
73  if (!root["X"].isNull())
74  X = root["X"].asDouble();
75  if (!root["Y"].isNull())
76  Y = root["Y"].asDouble();
77 }
openshot::stringToJson
const Json::Value stringToJson(const std::string value)
Definition: Json.cpp:16
openshot::Coordinate::Y
double Y
The Y value of the coordinate (usually representing the value of the property being animated)
Definition: Coordinate.h:41
openshot
This namespace is the default namespace for all code in the openshot library.
Definition: Compressor.h:28
openshot::Coordinate::Coordinate
Coordinate()
The default constructor, which defaults to (0,0)
Definition: Coordinate.cpp:19
openshot::Coordinate::SetJsonValue
void SetJsonValue(const Json::Value root)
Load Json::Value into this object.
Definition: Coordinate.cpp:70
openshot::Coordinate::SetJson
void SetJson(const std::string value)
Load JSON string into this object.
Definition: Coordinate.cpp:53
Coordinate.h
Header file for Coordinate class.
openshot::InvalidJSON
Exception for invalid JSON.
Definition: Exceptions.h:217
openshot::Coordinate::Json
std::string Json() const
Generate JSON string of this object.
Definition: Coordinate.cpp:29
openshot::Coordinate::JsonValue
Json::Value JsonValue() const
Generate Json::Value for this object.
Definition: Coordinate.cpp:36
openshot::Coordinate
A Cartesian coordinate (X, Y) used in the Keyframe animation system.
Definition: Coordinate.h:38
openshot::Coordinate::X
double X
The X value of the coordinate (usually representing the frame #)
Definition: Coordinate.h:40
Exceptions.h
Header file for all Exception classes.