egui_pop_viewport/graph/
state.rs1use 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#[derive(Clone, Debug, PartialEq)]
14pub(crate) struct Wire {
15 pub from: PortRef,
17 pub side: Side,
18 pub by_pointer: bool,
21 pub detached: Option<Edge>,
23}
24
25#[derive(Clone, Debug, PartialEq)]
27pub(crate) struct Moving {
28 pub nodes: Vec<(String, [f32; 2])>,
30 pub offset: Vec2,
32}
33
34#[derive(Clone, Debug, PartialEq)]
36pub(crate) struct Drawn {
37 pub boxes: Vec<(Rect, bool)>,
39 pub bounds: Rect,
41 pub visible: Rect,
43 pub zoom: f32,
45 pub routes: Vec<(Edge, Vec<Pos2>)>,
47}
48
49#[derive(Clone, Debug)]
51pub struct GraphState {
52 pub view: Rect,
55 pub selection: BTreeSet<String>,
57 pub selected_edges: BTreeSet<Edge>,
60 pub collapsed: BTreeSet<String>,
62 pub folded: BTreeSet<String>,
64 pub unfolded: BTreeSet<String>,
67 pub(crate) wire: Option<Wire>,
68 pub(crate) moving: Option<Moving>,
69 pub(crate) band: Option<[Pos2; 2]>,
71 pub(crate) menu_at: Option<Pos2>,
73 pub(crate) menu_wire: Option<Edge>,
75 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 pub(crate) wire_style: WireStyle,
83 pub(crate) node_width: f32,
85 pub(crate) routes: RouteCache,
87 pub(crate) had_focus: bool,
89 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 pub fn fit(&mut self) {
140 self.fit = true;
141 }
142
143 pub fn request(&mut self, request: Request) {
146 self.requests.push(request);
147 }
148
149 pub fn level(&self) -> Option<Level> {
151 self.level
152 }
153
154 pub fn zoom(&self) -> f32 {
156 self.drawn.zoom
157 }
158
159 pub fn visible(&self) -> Rect {
161 self.drawn.visible
162 }
163
164 pub fn wire_style(&self) -> WireStyle {
166 self.wire_style
167 }
168
169 pub fn node_width(&self) -> f32 {
172 self.node_width
173 }
174
175 pub fn wiring(&self) -> bool {
177 self.wire.is_some()
178 }
179
180 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 pub fn pan(&mut self, d: Vec2) {
187 if self.has_view() {
188 self.view = self.view.translate(d);
189 }
190 }
191
192 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 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}