use hashbrown::HashMap;
#[derive(Clone, Copy, Default, Debug)]
pub struct Vec2 {
pub x: f32,
pub y: f32,
}
impl Vec2 {
pub fn new(x: f32, y: f32) -> Self {
Self { x, y }
}
pub fn length(self) -> f32 {
(self.x * self.x + self.y * self.y).sqrt()
}
pub fn normalized(self) -> Self {
let len = self.length();
if len < 1e-6 {
Self::default()
} else {
Self {
x: self.x / len,
y: self.y / len,
}
}
}
}
impl std::ops::Add for Vec2 {
type Output = Self;
fn add(self, rhs: Self) -> Self {
Self {
x: self.x + rhs.x,
y: self.y + rhs.y,
}
}
}
impl std::ops::AddAssign for Vec2 {
fn add_assign(&mut self, rhs: Self) {
self.x += rhs.x;
self.y += rhs.y;
}
}
impl std::ops::Sub for Vec2 {
type Output = Self;
fn sub(self, rhs: Self) -> Self {
Self {
x: self.x - rhs.x,
y: self.y - rhs.y,
}
}
}
impl std::ops::Mul<f32> for Vec2 {
type Output = Self;
fn mul(self, s: f32) -> Self {
Self {
x: self.x * s,
y: self.y * s,
}
}
}
impl std::ops::Div<f32> for Vec2 {
type Output = Self;
fn div(self, s: f32) -> Self {
Self {
x: self.x / s,
y: self.y / s,
}
}
}
#[derive(Clone, Default)]
pub struct GraphData {
pub nodes: Vec<NodeVisual>,
pub edges: Vec<EdgeVisual>,
pub node_index: HashMap<String, usize>,
}
impl GraphData {
pub fn new() -> Self {
Self::default()
}
pub fn clear(&mut self) {
self.nodes.clear();
self.edges.clear();
self.node_index.clear();
}
}
#[derive(Clone, Debug)]
pub struct NodeVisual {
pub id: String,
pub label: String,
pub properties: Vec<(String, String)>,
pub pos: Vec2,
pub vel: Vec2,
pub pinned: bool,
pub color_idx: u8,
}
#[derive(Clone, Debug)]
pub struct EdgeVisual {
pub src: usize,
pub dst: usize,
pub rel_type: String,
pub properties: Vec<(String, String)>,
}
#[derive(Clone, Copy, Debug)]
pub struct CameraState {
pub offset_x: f32,
pub offset_y: f32,
pub zoom: f32,
}
impl Default for CameraState {
fn default() -> Self {
Self {
offset_x: 0.0,
offset_y: 0.0,
zoom: 1.0,
}
}
}
impl CameraState {
pub fn clamp_zoom(&mut self) {
self.zoom = self.zoom.clamp(0.1, 5.0);
}
}
#[derive(Clone, Copy, Default, Debug, PartialEq, Eq)]
pub enum Selection {
#[default]
None,
Node(usize),
Edge(usize),
}
#[derive(Clone, Default, Debug)]
pub struct SchemaData {
pub node_tables: Vec<SchemaTable>,
pub rel_tables: Vec<SchemaTable>,
}
#[derive(Clone, Debug)]
pub struct SchemaTable {
pub name: String,
pub num_rows: u64,
pub properties: Vec<(String, String)>, }