diff --git a/.idea/vcs.xml b/.idea/vcs.xml index 9701a56..60cfec6 100644 --- a/.idea/vcs.xml +++ b/.idea/vcs.xml @@ -7,5 +7,6 @@ + \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt index 5640615..8aead71 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,5 +1,5 @@ cmake_minimum_required(VERSION 3.25) -project(graphs VERSION 0.1.4) +project(graphs VERSION 0.1.5) option(ENABLE_ADDRSAN "Enable the address sanitizer" OFF) option(ENABLE_UBSAN "Enable the ub sanitizer" OFF) diff --git a/include/graph_base.h b/include/graph_base.h index aca9362..195f9ec 100644 --- a/include/graph_base.h +++ b/include/graph_base.h @@ -28,6 +28,7 @@ struct node { float repulsiveness = 24.0f; std::string name = "unnamed"; + std::string description; std::string texture = conf::DEFAULT_IMAGE; blt::gfx::point2d_t point; @@ -65,6 +66,7 @@ struct edge float ideal_spring_length = conf::DEFAULT_SPRING_LENGTH; float thickness = conf::DEFAULT_THICKNESS; blt::color4 color = conf::EDGE_COLOR; + std::string description; edge(blt::u64 i1, blt::u64 i2): i1(i1), i2(i2) { diff --git a/src/loader.cpp b/src/loader.cpp index 918427b..9c348eb 100644 --- a/src/loader.cpp +++ b/src/loader.cpp @@ -80,13 +80,22 @@ std::optional loader_t::load_for(engine_t& engine, const blt::gfx::win } } - if (data.contains("connections")) + if (data.contains("edges")) { for (const auto& edge : data["edges"]) { - auto& nodes = edge["nodes"]; - auto index1 = nodes[0].get(); - auto index2 = nodes[1].get(); + std::string index1; + std::string index2; + if (edge.is_array()) + { + index1 = edge[0].get(); + index2 = edge[1].get(); + } else + { + auto& nodes = edge["nodes"]; + index1 = nodes[0].get(); + index2 = nodes[1].get(); + } auto ideal_length = load_with_default(edge, "length", conf::DEFAULT_SPRING_LENGTH); auto thickness = load_with_default(edge, "thickness", conf::DEFAULT_THICKNESS); @@ -98,6 +107,32 @@ std::optional loader_t::load_for(engine_t& engine, const blt::gfx::win } } + if (data.contains("descriptions")) + { + for (const auto& desc : data["descriptions"]) + { + if (auto node = graph.names_to_node.find(desc["name"].get()); 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()]; + auto n2 = graph.names_to_node[nodes[2].get()]; + 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); + } + } + } + return loader; }