Olympe Engine 2.0
2D Game Engine with ECS Architecture
Loading...
Searching...
No Matches
Graph.cpp
Go to the documentation of this file.
1#include "Graph.h"
2#include "../json_helper.h"
3
5
7{
8 json j;
9 j["nodes"] = json::array();
10 for (size_t i = 0; i < nodes.size(); ++i)
11 {
12 const Node& n = nodes[i];
13 json nj;
14 nj["id"] = n.id;
15 nj["type"] = n.type;
16 nj["x"] = n.x;
17 nj["y"] = n.y;
18 j["nodes"].push_back(nj);
19 }
20 return j;
21}
22
24{
25 Graph g;
26
27 if (JsonHelper::IsArray(j, "nodes"))
28 {
29 JsonHelper::ForEachInArray(j, "nodes", [&g](const json& nj, size_t idx)
30 {
31 Node n;
32 n.id = JsonHelper::GetInt(nj, "id", 0);
33 n.type = JsonHelper::GetString(nj, "type", "");
34 n.x = JsonHelper::GetFloat(nj, "x", 0.0f);
35 n.y = JsonHelper::GetFloat(nj, "y", 0.0f);
36 g.nodes.push_back(n);
37 });
38 }
39
40 return g;
41}
nlohmann::json json
ComponentTypeID GetComponentTypeID_Static()
Definition ECS_Entity.h:56
std::string GetString(const json &j, const std::string &key, const std::string &defaultValue="")
Safely get a string value from JSON.
int GetInt(const json &j, const std::string &key, int defaultValue=0)
Safely get an integer value from JSON.
void ForEachInArray(const json &j, const std::string &key, std::function< void(const json &, size_t)> callback)
Iterate over an array with a callback function.
float GetFloat(const json &j, const std::string &key, float defaultValue=0.0f)
Safely get a float value from JSON.
bool IsArray(const json &j, const std::string &key)
Check if a key contains an array.
nlohmann::json json
Definition Graph.h:15
std::vector< Node > nodes
Definition Graph.h:16
static Graph FromJson(const nlohmann::json &j)
Definition Graph.cpp:23
nlohmann::json ToJson() const
Definition Graph.cpp:6
Definition Graph.h:8
int id
Definition Graph.h:9