Skip to main content

egui_pop_viewport/graph/
state.rs

1//! What the editor keeps between frames: the view, the selection, the
2//! collapsed nodes and folded groups (all the application's to save),
3//! and the gestures under way.
4
5use std::collections::BTreeSet;
6
7use egui::{Pos2, Rect, Vec2};
8
9use super::model::{Edge, Level, PortRef, Request, Side};
10use super::wires::{RouteCache, WireStyle};
11
12/// A wire being drawn.
13#[derive(Clone, Debug, PartialEq)]
14pub(crate) struct Wire {
15    /// The port it starts from.
16    pub from: PortRef,
17    pub side: Side,
18    /// Dragged by the pointer (released on a port, or nowhere), or
19    /// picked by a click or a key (finished by a click on another port).
20    pub by_pointer: bool,
21    /// The wire dragged off an input, if that is how it began.
22    pub detached: Option<Edge>,
23}
24
25/// Nodes being dragged.
26#[derive(Clone, Debug, PartialEq)]
27pub(crate) struct Moving {
28    /// The nodes and where they were.
29    pub nodes: Vec<(String, [f32; 2])>,
30    /// How far they have gone, in graph units.
31    pub offset: Vec2,
32}
33
34/// What the last frame drew, for the minimap and for agents.
35#[derive(Clone, Debug, PartialEq)]
36pub(crate) struct Drawn {
37    /// Every box drawn (nodes and stacks) and whether it is selected.
38    pub boxes: Vec<(Rect, bool)>,
39    /// Around them all.
40    pub bounds: Rect,
41    /// The part of the graph in view.
42    pub visible: Rect,
43    /// Screen points per graph unit.
44    pub zoom: f32,
45    /// Each wire's orthogonal route, when the style is orthogonal.
46    pub routes: Vec<(Edge, Vec<Pos2>)>,
47}
48
49/// A node editor's state: keep it with the graph's view.
50#[derive(Clone, Debug)]
51pub struct GraphState {
52    /// The part of the graph to show (graph units); empty until the
53    /// first frame fits the graph.
54    pub view: Rect,
55    /// The selected nodes.
56    pub selection: BTreeSet<String>,
57    /// The selected wires (beside the nodes: a click on a wire selects it
58    /// alone, Ctrl or Shift adds it to the nodes and wires selected).
59    pub selected_edges: BTreeSet<Edge>,
60    /// Nodes drawn as their title bar only.
61    pub collapsed: BTreeSet<String>,
62    /// Groups folded into a stack in the ports and detail views.
63    pub folded: BTreeSet<String>,
64    /// Groups left unfolded in the overview (where groups fold by
65    /// default).
66    pub unfolded: BTreeSet<String>,
67    pub(crate) wire: Option<Wire>,
68    pub(crate) moving: Option<Moving>,
69    /// A rubber band: where it began and where the pointer is.
70    pub(crate) band: Option<[Pos2; 2]>,
71    /// Where the canvas's menu was opened (graph units).
72    pub(crate) menu_at: Option<Pos2>,
73    /// The wire the canvas's menu was opened on, if any.
74    pub(crate) menu_wire: Option<Edge>,
75    /// The pointer was over the canvas at the last frame.
76    pub(crate) over_canvas: bool,
77    pub(crate) level: Option<Level>,
78    pub(crate) drawn: Drawn,
79    pub(crate) requests: Vec<Request>,
80    pub(crate) fit: bool,
81    /// How wires were drawn at the last frame.
82    pub(crate) wire_style: WireStyle,
83    /// A node's width at the last frame, in graph units.
84    pub(crate) node_width: f32,
85    /// Orthogonal routes, by layout.
86    pub(crate) routes: RouteCache,
87    /// The canvas or a node had the keyboard at the last frame.
88    pub(crate) had_focus: bool,
89    /// Where the overview was drawn at the last frame (screen).
90    pub(crate) overview_rect: Option<Rect>,
91}
92
93impl Default for Drawn {
94    fn default() -> Self {
95        Drawn {
96            boxes: Vec::new(),
97            bounds: Rect::NOTHING,
98            visible: Rect::NOTHING,
99            zoom: 1.0,
100            routes: Vec::new(),
101        }
102    }
103}
104
105impl Default for GraphState {
106    fn default() -> Self {
107        GraphState {
108            view: Rect::NOTHING,
109            selection: BTreeSet::new(),
110            selected_edges: BTreeSet::new(),
111            collapsed: BTreeSet::new(),
112            folded: BTreeSet::new(),
113            unfolded: BTreeSet::new(),
114            wire: None,
115            moving: None,
116            band: None,
117            menu_at: None,
118            menu_wire: None,
119            over_canvas: false,
120            level: None,
121            drawn: Drawn::default(),
122            requests: Vec::new(),
123            fit: false,
124            wire_style: WireStyle::default(),
125            node_width: super::NODE_WIDTH,
126            routes: RouteCache::default(),
127            had_focus: false,
128            overview_rect: None,
129        }
130    }
131}
132
133impl GraphState {
134    pub fn new() -> GraphState {
135        GraphState::default()
136    }
137
138    /// Fits the whole graph in view at the next frame.
139    pub fn fit(&mut self) {
140        self.fit = true;
141    }
142
143    /// Asks the editor to do something at its next frame (as
144    /// [`GraphEditor::requests`](super::GraphEditor::requests)).
145    pub fn request(&mut self, request: Request) {
146        self.requests.push(request);
147    }
148
149    /// The level drawn last (`None` before the first frame).
150    pub fn level(&self) -> Option<Level> {
151        self.level
152    }
153
154    /// Screen points per graph unit, as drawn last.
155    pub fn zoom(&self) -> f32 {
156        self.drawn.zoom
157    }
158
159    /// The part of the graph in view at the last frame.
160    pub fn visible(&self) -> Rect {
161        self.drawn.visible
162    }
163
164    /// How wires were drawn at the last frame.
165    pub fn wire_style(&self) -> WireStyle {
166        self.wire_style
167    }
168
169    /// A node's width at the last frame, in graph units (180 before
170    /// the first).
171    pub fn node_width(&self) -> f32 {
172        self.node_width
173    }
174
175    /// Whether a wire is being drawn.
176    pub fn wiring(&self) -> bool {
177        self.wire.is_some()
178    }
179
180    /// The view is usable (not empty, finite).
181    pub(crate) fn has_view(&self) -> bool {
182        self.view.is_finite() && self.view.width() > 0.0 && self.view.height() > 0.0
183    }
184
185    /// Moves the view by `d` graph units.
186    pub fn pan(&mut self, d: Vec2) {
187        if self.has_view() {
188            self.view = self.view.translate(d);
189        }
190    }
191
192    /// Centres the view on a point of the graph.
193    pub fn center_on(&mut self, p: Pos2) {
194        if self.has_view() {
195            self.view = Rect::from_center_size(p, self.view.size());
196        }
197    }
198
199    /// Zooms by `factor` (above 1 closer) around a point of the graph.
200    pub fn zoom_by(&mut self, factor: f32, around: Pos2) {
201        if self.has_view() && factor > 0.0 {
202            let v = self.view;
203            let min = around + (v.min - around) / factor;
204            let max = around + (v.max - around) / factor;
205            self.view = Rect::from_min_max(min, max);
206        }
207    }
208}