diff --git a/CMakeLists.txt b/CMakeLists.txt index 7d21df6..627e01c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,5 +1,5 @@ cmake_minimum_required(VERSION 3.25) -project(graphs VERSION 0.1.8) +project(graphs VERSION 0.1.9) option(ENABLE_ADDRSAN "Enable the address sanitizer" OFF) option(ENABLE_UBSAN "Enable the ub sanitizer" OFF) diff --git a/include/graph.h b/include/graph.h index dc5a120..d5524e6 100644 --- a/include/graph.h +++ b/include/graph.h @@ -46,7 +46,9 @@ struct bounding_box class graph_t { friend struct loader_t; + friend class selector_t; + private: std::vector nodes; blt::hashmap_t names_to_node; @@ -60,7 +62,7 @@ class graph_t int current_iterations = 0; int max_iterations = 5000; std::unique_ptr equation; - + void create_random_graph(bounding_box bb, blt::size_t min_nodes, blt::size_t max_nodes, blt::f64 connectivity, blt::f64 scaling_connectivity, blt::f64 distance_factor); @@ -100,6 +102,18 @@ class graph_t edges.insert({n1, n2}); } + void disconnect(const blt::u64 n1, const blt::u64 n2) + { + connected_nodes[n1].erase(n2); + connected_nodes[n2].erase(n1); + edges.erase({n1, n2}); + } + + bool is_connected(const blt::u64 n1, const blt::u64 n2) + { + return edges.contains({n1, n2}); + } + void connect(const edge_t& edge) { connected_nodes[edge.getFirst()].insert(edge.getSecond()); @@ -188,7 +202,7 @@ class engine_t friend struct loader_t; private: graph_t graph; - selector_t selector {graph}; + selector_t selector{graph}; void draw_gui(const blt::gfx::window_data& data); diff --git a/src/selector.cpp b/src/selector.cpp index 9e802d6..52f7798 100644 --- a/src/selector.cpp +++ b/src/selector.cpp @@ -115,10 +115,19 @@ void selector_t::process_keyboard(blt::i32 width, blt::i32 height) { if (blt::gfx::isKeyPressed(GLFW_KEY_C)) { - if (placement) - destroy_node(primary_selection); - else - create_placement_node(mouse_pos); + if (primary_selection != -1 && secondary_selection != -1) + { + if (graph.is_connected(primary_selection, secondary_selection)) + graph.disconnect(primary_selection, secondary_selection); + else + graph.connect(primary_selection, secondary_selection); + } else + { + if (placement) + destroy_node(primary_selection); + else + create_placement_node(mouse_pos); + } } else if (blt::gfx::isKeyPressed(GLFW_KEY_X)) { if (primary_selection != -1)