2024-07-27 20:53:47 -04:00
|
|
|
/*
|
|
|
|
* <Short Description>
|
|
|
|
* Copyright (C) 2024 Brett Terpstra
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
#include <loader.h>
|
|
|
|
#include <blt/std/logging.h>
|
|
|
|
#include <fstream>
|
|
|
|
#include <istream>
|
|
|
|
#include <random>
|
|
|
|
#include <nlohmann/json.hpp>
|
|
|
|
|
|
|
|
using json = nlohmann::json;
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
T load_with_default(const json& data, std::string_view key, T def)
|
|
|
|
{
|
|
|
|
if (data.contains(key))
|
|
|
|
return data[key].get<T>();
|
|
|
|
return def;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::optional<loader_t> loader_t::load_for(engine_t& engine, const blt::gfx::window_data& window_data, std::string_view path,
|
|
|
|
std::optional<std::string_view> save_path)
|
|
|
|
{
|
|
|
|
auto& graph = engine.graph;
|
2024-07-28 14:01:19 -04:00
|
|
|
graph.clear();
|
2024-07-27 20:53:47 -04:00
|
|
|
|
|
|
|
static std::random_device dev;
|
|
|
|
std::uniform_real_distribution pos_x_dist(0.0, static_cast<blt::f64>(window_data.width));
|
|
|
|
std::uniform_real_distribution pos_y_dist(0.0, static_cast<blt::f64>(window_data.height));
|
|
|
|
|
|
|
|
if (save_path && std::filesystem::exists(*save_path))
|
|
|
|
path = *save_path;
|
|
|
|
if (!std::filesystem::exists(path))
|
|
|
|
{
|
|
|
|
BLT_WARN("Unable to load graph file!");
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
std::ifstream file{std::string(path)};
|
|
|
|
json data = json::parse(file);
|
|
|
|
|
|
|
|
loader_t loader;
|
|
|
|
if (data.contains("textures"))
|
|
|
|
{
|
|
|
|
for (const auto& texture : data["textures"])
|
|
|
|
{
|
|
|
|
auto texture_path = texture["path"].get<std::string>();
|
|
|
|
auto texture_key = load_with_default(texture, "name", texture_path);
|
|
|
|
loader.textures.emplace_back(texture_key, texture_path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (data.contains("nodes"))
|
|
|
|
{
|
|
|
|
for (const auto& node : data["nodes"])
|
|
|
|
{
|
|
|
|
auto x = static_cast<blt::f32>(load_with_default(node, "x", pos_x_dist(dev)));
|
|
|
|
auto y = static_cast<blt::f32>(load_with_default(node, "y", pos_y_dist(dev)));
|
2024-07-28 14:01:19 -04:00
|
|
|
auto size = static_cast<blt::f32>(load_with_default(node, "size", static_cast<blt::f64>(conf::POINT_SIZE)));
|
2024-07-27 20:53:47 -04:00
|
|
|
auto name = load_with_default<std::string>(node, "name", "unnamed");
|
2024-07-28 14:01:19 -04:00
|
|
|
auto texture = load_with_default<std::string>(node, "texture", conf::DEFAULT_IMAGE);
|
2024-07-27 20:53:47 -04:00
|
|
|
|
|
|
|
graph.names_to_node.insert({name, static_cast<blt::u64>(graph.nodes.size())});
|
|
|
|
graph.nodes.emplace_back(blt::gfx::point2d_t{x, y, size});
|
|
|
|
graph.nodes.back().name = std::move(name);
|
2024-07-28 14:01:19 -04:00
|
|
|
graph.nodes.back().texture = std::move(texture);
|
2024-07-27 20:53:47 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-28 20:02:35 -04:00
|
|
|
if (data.contains("edges"))
|
2024-07-27 20:53:47 -04:00
|
|
|
{
|
2024-07-28 14:01:19 -04:00
|
|
|
for (const auto& edge : data["edges"])
|
|
|
|
{
|
2024-07-28 20:02:35 -04:00
|
|
|
std::string index1;
|
|
|
|
std::string index2;
|
|
|
|
if (edge.is_array())
|
|
|
|
{
|
|
|
|
index1 = edge[0].get<std::string>();
|
|
|
|
index2 = edge[1].get<std::string>();
|
|
|
|
} else
|
|
|
|
{
|
|
|
|
auto& nodes = edge["nodes"];
|
|
|
|
index1 = nodes[0].get<std::string>();
|
|
|
|
index2 = nodes[1].get<std::string>();
|
|
|
|
}
|
2024-07-28 14:01:19 -04:00
|
|
|
auto ideal_length = load_with_default(edge, "length", conf::DEFAULT_SPRING_LENGTH);
|
|
|
|
auto thickness = load_with_default(edge, "thickness", conf::DEFAULT_THICKNESS);
|
|
|
|
|
|
|
|
::edge e{graph.names_to_node[index1], graph.names_to_node[index2]};
|
|
|
|
e.ideal_spring_length = ideal_length;
|
|
|
|
e.thickness = thickness;
|
|
|
|
|
|
|
|
graph.connect(e);
|
|
|
|
}
|
2024-07-27 20:53:47 -04:00
|
|
|
}
|
|
|
|
|
2024-07-28 20:02:35 -04:00
|
|
|
if (data.contains("descriptions"))
|
|
|
|
{
|
|
|
|
for (const auto& desc : data["descriptions"])
|
|
|
|
{
|
|
|
|
if (auto node = graph.names_to_node.find(desc["name"].get<std::string>()); node != graph.names_to_node.end())
|
|
|
|
graph.nodes[node->second].description = desc["description"];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (data.contains("relationships"))
|
|
|
|
{
|
|
|
|
for (const auto& desc : data["relationships"])
|
|
|
|
{
|
|
|
|
auto nodes = desc["nodes"];
|
|
|
|
auto n1 = graph.names_to_node[nodes[0].get<std::string>()];
|
|
|
|
auto n2 = graph.names_to_node[nodes[2].get<std::string>()];
|
|
|
|
if (auto node = graph.edges.find({n1, n2}); node != graph.edges.end())
|
|
|
|
{
|
|
|
|
edge e = *node;
|
|
|
|
e.description = desc["description"];
|
|
|
|
graph.edges.erase({n1, n2});
|
|
|
|
graph.edges.insert(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-27 20:53:47 -04:00
|
|
|
return loader;
|
|
|
|
}
|
|
|
|
|
|
|
|
void loader_t::save_for(engine_t& engine, const loader_t& loader, std::string_view path)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|