use std::marker::PhantomData; use std::sync::Arc; use egui::{Rect, Style, Ui, Vec2}; #[cfg(feature = "persistence")] use serde::{Deserialize, Serialize}; use super::*; use crate::scale::Scale; const MIN_ZOOM: f32 = 0.1; const MAX_ZOOM: f32 = 2.5; #[derive(Clone)] #[cfg_attr(feature = "persistence", derive(Serialize, Deserialize))] pub struct GraphEditorState< NodeData, DataType, ValueType, NodeTemplate, UserState, > { pub graph: Graph, /// Nodes are drawn in this order. Draw order is important because nodes /// that are drawn last are on top. pub node_order: Vec, /// An ongoing connection interaction: The mouse has dragged away from a /// port and the user is holding the click pub connection_in_progress: Option<(NodeId, AnyParameterId)>, /// The currently selected node. Some interface actions depend on the /// currently selected node. pub selected_nodes: Vec, /// The mouse drag start position for an ongoing box selection. pub ongoing_box_selection: Option, /// The position of each node. pub node_positions: SecondaryMap, /// The node finder is used to create new nodes. pub node_finder: Option>, /// The panning of the graph viewport. pub pan_zoom: PanZoom, pub _user_state: PhantomData UserState>, } impl GraphEditorState { pub fn new(default_zoom: f32) -> Self { Self { pan_zoom: PanZoom::new(default_zoom), ..Default::default() } } } impl Default for GraphEditorState { fn default() -> Self { Self { graph: Default::default(), node_order: Default::default(), connection_in_progress: Default::default(), selected_nodes: Default::default(), ongoing_box_selection: Default::default(), node_positions: Default::default(), node_finder: Default::default(), pan_zoom: Default::default(), _user_state: Default::default(), } } } #[cfg(feature = "persistence")] fn _default_clip_rect() -> Rect { Rect::NOTHING } #[derive(Clone)] #[cfg_attr(feature = "persistence", derive(Serialize, Deserialize))] pub struct PanZoom { pub pan: Vec2, pub zoom: f32, #[cfg_attr( feature = "persistence", serde(skip, default = "_default_clip_rect") )] pub clip_rect: Rect, #[cfg_attr(feature = "persistence", serde(skip, default))] pub zoomed_style: Arc