Skip to content

Commit

Permalink
Changes refactor (#28)
Browse files Browse the repository at this point in the history
* Make enum for changes
* Send one by one.
  • Loading branch information
blitzarx1 authored May 2, 2023
1 parent 7e5f4f8 commit 81277fd
Show file tree
Hide file tree
Showing 10 changed files with 145 additions and 215 deletions.
8 changes: 4 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "egui_graphs"
version = "0.3.1"
version = "0.4.0"
authors = ["Dmitrii Samsonov <[email protected]>"]
license = "MIT"
homepage = "https://github.com/blitzarx1/egui_graphs"
Expand Down
2 changes: 1 addition & 1 deletion examples/basic/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "basic"
version = "0.3.1"
version = "0.4.0"
authors = ["Dmitrii Samsonov <[email protected]>"]
license = "MIT"
edition = "2021"
Expand Down
2 changes: 1 addition & 1 deletion examples/configurable/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "configurable"
version = "0.3.1"
version = "0.4.0"
authors = ["Dmitrii Samsonov <[email protected]>"]
license = "MIT"
edition = "2021"
Expand Down
8 changes: 4 additions & 4 deletions examples/configurable/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use eframe::{run_native, App, CreationContext};
use egui::plot::{Line, Plot, PlotPoints};
use egui::{CollapsingHeader, Color32, Context, Pos2, Rect, ScrollArea, Slider, Ui, Vec2, Visuals};
use egui_graphs::{
Changes, Edge, GraphView, Node, SettingsInteraction, SettingsNavigation, SettingsStyle,
Change, Edge, GraphView, Node, SettingsInteraction, SettingsNavigation, SettingsStyle,
};
use fdg_sim::glam::Vec3;
use fdg_sim::{ForceGraph, ForceGraphHelper, Simulation, SimulationParameters};
Expand All @@ -32,7 +32,7 @@ pub struct ConfigurableApp {

selected_nodes: Vec<Node<()>>,
selected_edges: Vec<Edge<()>>,
last_changes: Vec<Changes>,
last_changes: Vec<Change>,

simulation_stopped: bool,
dark_mode: bool,
Expand All @@ -42,8 +42,8 @@ pub struct ConfigurableApp {
last_update_time: Instant,
frames_last_time_span: usize,

changes_receiver: Receiver<Changes>,
changes_sender: Sender<Changes>,
changes_receiver: Receiver<Change>,
changes_sender: Sender<Change>,
}

impl ConfigurableApp {
Expand Down
2 changes: 1 addition & 1 deletion examples/interactive/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "interactive"
version = "0.3.1"
version = "0.4.0"
authors = ["Dmitrii Samsonov <[email protected]>"]
license = "MIT"
edition = "2021"
Expand Down
95 changes: 95 additions & 0 deletions src/change.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
use egui::Vec2;
use petgraph::stable_graph::{NodeIndex, EdgeIndex};

/// `ChangeNode` is a enum that stores the changes to `Node` properties.
#[derive(Debug, Clone)]
pub enum ChangeNode {
Location { id: NodeIndex, old: Vec2, new: Vec2 },

Selected { id: NodeIndex, old: bool, new: bool },

Dragged { id: NodeIndex, old: bool, new: bool },
}

impl ChangeNode {
pub(crate) fn change_location(id: NodeIndex, old: Vec2, new: Vec2) -> Self {
Self::Location { id, old, new }
}

pub(crate) fn change_selected(id: NodeIndex, old: bool, new: bool) -> Self {
Self::Selected { id, old, new }
}

pub(crate) fn change_dragged(id: NodeIndex, old: bool, new: bool) -> Self {
Self::Dragged { id, old, new }
}
}

/// `ChangeEdge` is a enum that stores the changes to `Edge` properties.
#[derive(Debug, Clone)]
pub enum ChangeEdge {
Selected { id: EdgeIndex, old: bool, new: bool },
}

impl ChangeEdge {
pub(crate) fn change_selected(id: EdgeIndex, old: bool, new: bool) -> Self {
Self::Selected { id, old, new }
}
}

/// Change is a enum that stores the changes to `Node` or `Edge` properties.
#[derive(Debug, Clone)]
pub enum Change {
Node(ChangeNode),
Edge(ChangeEdge),
}

impl Change {
pub(crate) fn node(change: ChangeNode) -> Self {
Self::Node(change)
}

pub(crate) fn edge(change: ChangeEdge) -> Self {
Self::Edge(change)
}
}

#[cfg(test)]
mod tests {
use super::*;
use egui::vec2;

#[test]
fn test_change_enum() {
let node_id = NodeIndex::new(0);
let edge_id = EdgeIndex::new(0);

let old_node_location = vec2(0.0, 0.0);
let new_node_location = vec2(10.0, 10.0);

let node_change = Change::node(ChangeNode::change_location(node_id, old_node_location, new_node_location));

let node_selected_old = false;
let node_selected_new = true;

let edge_change = Change::edge(ChangeEdge::change_selected(edge_id, node_selected_old, node_selected_new));

match node_change {
Change::Node(ChangeNode::Location { id, old, new }) => {
assert_eq!(id, node_id);
assert_eq!(old, old_node_location);
assert_eq!(new, new_node_location);
}
_ => panic!("Unexpected node change type"),
}

match edge_change {
Change::Edge(ChangeEdge::Selected { id, old, new }) => {
assert_eq!(id, edge_id);
assert_eq!(old, node_selected_old);
assert_eq!(new, node_selected_new);
}
_ => panic!("Unexpected edge change type"),
}
}
}
146 changes: 0 additions & 146 deletions src/changes.rs

This file was deleted.

Loading

0 comments on commit 81277fd

Please sign in to comment.