use serde::{Deserialize, Serialize};
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct Color {
pub r: u8,
pub g: u8,
pub b: u8,
pub a: u8,
}
impl Color {
pub const fn rgb(r: u8, g: u8, b: u8) -> Self {
Self { r, g, b, a: 255 }
}
pub const fn rgba(r: u8, g: u8, b: u8, a: u8) -> Self {
Self { r, g, b, a }
}
pub const TRANSPARENT: Color = Color::rgba(0, 0, 0, 0);
pub const WHITE: Color = Color::rgb(255, 255, 255);
pub const GRAY: Color = Color::rgb(128, 128, 128);
pub fn dim(self, k: f32) -> Color {
Color::rgba(self.r, self.g, self.b, (self.a as f32 * k) as u8)
}
}
impl From<egui::Color32> for Color {
fn from(c: egui::Color32) -> Self {
let [r, g, b, a] = c.to_srgba_unmultiplied();
Color { r, g, b, a }
}
}
impl From<Color> for egui::Color32 {
fn from(c: Color) -> Self {
egui::Color32::from_rgba_unmultiplied(c.r, c.g, c.b, c.a)
}
}
#[derive(Clone, Copy, Debug, PartialEq, Serialize, Deserialize)]
pub struct Pos {
pub x: f32,
pub y: f32,
}
impl Pos {
pub const fn new(x: f32, y: f32) -> Self {
Self { x, y }
}
}
pub const BOX_W: f32 = 184.0;
pub const BOX_H: f32 = 34.0;
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct GraphNode {
pub id: String,
pub label: String,
pub fill: Color,
pub stroke: Color,
pub pos: Pos,
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct GraphEdge {
pub from: String,
pub to: String,
pub color: Color,
pub dashed: bool,
pub label: Option<String>,
}
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
pub struct GraphModel {
pub nodes: Vec<GraphNode>,
pub edges: Vec<GraphEdge>,
}
impl GraphModel {
pub fn world_bounds(&self) -> Option<(f32, f32, f32, f32)> {
let mut it = self.nodes.iter();
let first = it.next()?;
let (hw, hh) = (BOX_W * 0.5, BOX_H * 0.5);
let mut b = (first.pos.x - hw, first.pos.y - hh, first.pos.x + hw, first.pos.y + hh);
for n in it {
b.0 = b.0.min(n.pos.x - hw);
b.1 = b.1.min(n.pos.y - hh);
b.2 = b.2.max(n.pos.x + hw);
b.3 = b.3.max(n.pos.y + hh);
}
Some(b)
}
}
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
pub struct NodeDecoration {
pub ring: Option<Color>,
pub badge: Option<String>,
pub badge_color: Option<Color>,
pub scale: Option<f32>,
}
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
pub struct Decorations {
pub nodes: std::collections::HashMap<String, NodeDecoration>,
pub edges: Vec<GraphEdge>,
}
#[derive(Clone, Copy, Debug)]
pub struct Camera {
pub pan_x: f32,
pub pan_y: f32,
pub zoom: f32,
}
impl Default for Camera {
fn default() -> Self {
Self { pan_x: 0.0, pan_y: 0.0, zoom: 1.0 }
}
}
impl Camera {
pub fn fit(model: &GraphModel, w: f32, h: f32, margin: f32) -> Camera {
let Some((min_x, min_y, max_x, max_y)) = model.world_bounds() else {
return Camera::default();
};
let (bw, bh) = ((max_x - min_x).max(1.0), (max_y - min_y).max(1.0));
let zoom = ((w - 2.0 * margin) / bw).min((h - 2.0 * margin) / bh).clamp(0.05, 4.0);
let (cx, cy) = ((min_x + max_x) * 0.5, (min_y + max_y) * 0.5);
Camera { pan_x: w * 0.5 - cx * zoom, pan_y: h * 0.5 - cy * zoom, zoom }
}
#[inline]
pub fn project(&self, p: Pos) -> (f32, f32) {
(p.x * self.zoom + self.pan_x, p.y * self.zoom + self.pan_y)
}
}