refactor(topola-egui): Move InteractiveEvent handling from Viewport into Workspace

This commit is contained in:
Ellen Emilia Anna Zscheile 2025-06-27 06:50:01 +02:00
parent c66089bca9
commit c8848ef269
5 changed files with 159 additions and 117 deletions

View File

@ -131,13 +131,14 @@ impl Overlay {
_board: &Board<impl AccessMesadata>,
_appearance_panel: &AppearancePanel,
at: Point,
modifiers: &egui::Modifiers,
ctrl: bool,
shift: bool,
) {
if self.reselect_bbox.is_none() {
// handle bounding box selection
let selmode = if modifiers.ctrl {
let selmode = if ctrl {
SelectionMode::Toggling
} else if modifiers.shift {
} else if shift {
SelectionMode::Addition
} else {
SelectionMode::Substitution

View File

@ -2,7 +2,6 @@
//
// SPDX-License-Identifier: MIT
use core::ops::ControlFlow;
use geo::point;
use petgraph::{
data::DataMap,
@ -10,12 +9,8 @@ use petgraph::{
};
use rstar::{Envelope, AABB};
use topola::{
autorouter::{
execution::Command,
invoker::{
GetGhosts, GetMaybeNavcord, GetMaybeThetastarStepper, GetNavmeshDebugTexts,
GetObstacles,
},
autorouter::invoker::{
GetGhosts, GetMaybeNavcord, GetMaybeThetastarStepper, GetNavmeshDebugTexts, GetObstacles,
},
board::AccessMesadata,
drawing::{
@ -25,10 +20,10 @@ use topola::{
geometry::{shape::AccessShape, GenericNode},
graph::MakeRef,
interactor::{
activity::{ActivityStepper, InteractiveEvent, InteractiveInput},
activity::{ActivityStepper, InteractiveEvent, InteractiveEventKind, InteractiveInput},
interaction::InteractionStepper,
},
layout::{poly::MakePolygon, via::ViaWeight},
layout::poly::MakePolygon,
math::{Circle, RotationSense},
router::navmesh::NavnodeIndex,
};
@ -138,85 +133,43 @@ impl Viewport {
if let Some(workspace) = maybe_workspace {
let latest_point = point! {x: latest_pos.x as f64, y: -latest_pos.y as f64};
// Advances the app's state by the delta time `dt`. May call
// `.update_state()` more than once if the delta time is more than a multiple of
// the timestep.
let dt = ctx.input(|i| i.stable_dt);
let active_layer = workspace.appearance_panel.active_layer;
self.update_counter += dt;
while self.update_counter >= menu_bar.frame_timestep {
self.update_counter -= menu_bar.frame_timestep;
if let ControlFlow::Break(()) = workspace.update_state(
let interactive_input = InteractiveInput {
active_layer: workspace.appearance_panel.active_layer,
pointer_pos: latest_point,
dt: ctx.input(|i| i.stable_dt),
};
workspace.advance_state_by_dt(
tr,
error_dialog,
&InteractiveInput {
active_layer,
pointer_pos: point! {x: latest_pos.x as f64, y: latest_pos.y as f64},
dt,
},
) {
break;
}
}
menu_bar.frame_timestep,
&interactive_input,
);
if !workspace.interactor.maybe_activity().as_ref().map_or(true, |activity| {
matches!(activity.maybe_status(), Some(ControlFlow::Break(..)))
}) {
// there is currently some activity
let interactive_event = if response.clicked_by(egui::PointerButton::Primary) {
Some(InteractiveEvent::PointerPrimaryButtonClicked)
let interactive_event_kind =
if response.clicked_by(egui::PointerButton::Primary) {
Some(InteractiveEventKind::PointerPrimaryButtonClicked)
} else if response.drag_started_by(egui::PointerButton::Primary) {
Some(InteractiveEventKind::PointerPrimaryButtonDragStarted)
} else if response.drag_stopped_by(egui::PointerButton::Primary) {
Some(InteractiveEventKind::PointerPrimaryButtonDragStopped)
} else if response.clicked_by(egui::PointerButton::Secondary) {
Some(InteractiveEvent::PointerSecondaryButtonClicked)
Some(InteractiveEventKind::PointerSecondaryButtonClicked)
} else {
None
};
if let Some(interactive_event) = interactive_event {
let dt = ctx.input(|i| i.stable_dt);
let active_layer = workspace.appearance_panel.active_layer;
if let Some(kind) = interactive_event_kind {
let (ctrl, shift) = response
.ctx
.input(|i| (i.modifiers.ctrl, i.modifiers.shift));
let _ = workspace.update_state_for_event(
tr,
error_dialog,
&InteractiveInput {
active_layer,
pointer_pos: latest_point,
dt,
},
interactive_event,
menu_bar,
&interactive_input,
InteractiveEvent { kind, ctrl, shift },
);
}
} else {
let layers = &mut workspace.appearance_panel;
let overlay = &mut workspace.overlay;
let board = workspace.interactor.invoker().autorouter().board();
if response.clicked_by(egui::PointerButton::Primary) {
if menu_bar.is_placing_via {
workspace.interactor.execute(Command::PlaceVia(ViaWeight {
from_layer: 0,
to_layer: 0,
circle: Circle {
pos: latest_point,
r: menu_bar
.autorouter_options
.router_options
.routed_band_width
/ 2.0,
},
maybe_net: Some(1234),
}));
} else {
overlay.click(board, layers, latest_point);
}
} else if response.drag_started_by(egui::PointerButton::Primary) {
overlay.drag_start(
board,
layers,
latest_point,
&response.ctx.input(|i| i.modifiers),
);
} else if response.drag_stopped_by(egui::PointerButton::Primary) {
overlay.drag_stop(board, layers, latest_point);
} else if let Some((_, bsk, cur_bbox)) =
overlay.get_bbox_reselect(latest_point)
workspace.overlay.get_bbox_reselect(latest_point)
{
use topola::autorouter::selection::BboxSelectionKind;
painter.paint_bbox_with_color(
@ -227,7 +180,6 @@ impl Viewport {
},
);
}
}
let layers = &mut workspace.appearance_panel;
let overlay = &mut workspace.overlay;
@ -529,8 +481,13 @@ impl Viewport {
.paint_primitive(ghost, egui::Color32::from_rgb(75, 75, 150));
}
if let ActivityStepper::Interaction(InteractionStepper::RoutePlan(rp)) = activity.activity() {
painter.paint_linestring(&rp.lines, egui::Color32::from_rgb(245, 182, 66));
if let ActivityStepper::Interaction(InteractionStepper::RoutePlan(rp)) =
activity.activity()
{
painter.paint_linestring(
&rp.lines,
egui::Color32::from_rgb(245, 182, 66),
);
}
if let Some(ref navmesh) =

View File

@ -8,18 +8,19 @@ use std::{
};
use topola::{
autorouter::history::History,
autorouter::{execution::Command, history::History},
interactor::{
activity::{InteractiveEvent, InteractiveInput},
activity::{InteractiveEvent, InteractiveEventKind, InteractiveInput},
Interactor,
},
layout::LayoutEdit,
layout::{via::ViaWeight, LayoutEdit},
math::Circle,
specctra::{design::SpecctraDesign, mesadata::SpecctraMesadata},
};
use crate::{
appearance_panel::AppearancePanel, error_dialog::ErrorDialog, overlay::Overlay,
translator::Translator,
appearance_panel::AppearancePanel, error_dialog::ErrorDialog, menu_bar::MenuBar,
overlay::Overlay, translator::Translator,
};
/// A loaded design and associated structures.
@ -33,6 +34,8 @@ pub struct Workspace {
Sender<std::io::Result<Result<History, serde_json::Error>>>,
Receiver<std::io::Result<Result<History, serde_json::Error>>>,
),
update_counter: f32,
}
impl Workspace {
@ -58,25 +61,94 @@ impl Workspace {
)
})?,
history_channel: channel(),
update_counter: 0.0,
})
}
/// Advances the app's state by the delta time `dt`. May call
/// `.update_state()` more than once if the delta time is more than a multiple of
/// the timestep.
pub fn advance_state_by_dt(
&mut self,
tr: &Translator,
error_dialog: &mut ErrorDialog,
frame_timestep: f32,
interactive_input: &InteractiveInput,
) {
self.update_counter += interactive_input.dt;
while self.update_counter >= frame_timestep {
self.update_counter -= frame_timestep;
if let ControlFlow::Break(()) = self.update_state(tr, error_dialog, interactive_input) {
break;
}
}
}
pub fn update_state_for_event(
&mut self,
tr: &Translator,
error_dialog: &mut ErrorDialog,
menu_bar: &MenuBar,
interactive_input: &InteractiveInput,
interactive_event: InteractiveEvent,
) -> ControlFlow<()> {
) {
if !self
.interactor
.maybe_activity()
.as_ref()
.map_or(true, |activity| {
matches!(activity.maybe_status(), Some(ControlFlow::Break(..)))
})
{
match self
.interactor
.update_for_event(interactive_input, interactive_event)
{
ControlFlow::Continue(()) => ControlFlow::Continue(()),
ControlFlow::Break(Ok(())) => ControlFlow::Break(()),
ControlFlow::Continue(()) | ControlFlow::Break(Ok(())) => {}
ControlFlow::Break(Err(err)) => {
error_dialog.push_error("tr-module-invoker", format!("{}", err));
ControlFlow::Break(())
}
}
} else {
let board = self.interactor.invoker().autorouter().board();
match interactive_event.kind {
InteractiveEventKind::PointerPrimaryButtonClicked => {
if menu_bar.is_placing_via {
self.interactor.execute(Command::PlaceVia(ViaWeight {
from_layer: 0,
to_layer: 0,
circle: Circle {
pos: interactive_input.pointer_pos,
r: menu_bar.autorouter_options.router_options.routed_band_width
/ 2.0,
},
maybe_net: Some(1234),
}));
} else {
self.overlay.click(
board,
&self.appearance_panel,
interactive_input.pointer_pos,
);
}
}
InteractiveEventKind::PointerPrimaryButtonDragStarted => {
self.overlay.drag_start(
board,
&self.appearance_panel,
interactive_input.pointer_pos,
interactive_event.ctrl,
interactive_event.shift,
);
}
InteractiveEventKind::PointerPrimaryButtonDragStopped => {
self.overlay.drag_stop(
board,
&self.appearance_panel,
interactive_input.pointer_pos,
);
}
_ => {}
}
}
}

View File

@ -35,13 +35,25 @@ pub struct InteractiveInput {
pub dt: f32,
}
/// An event received from the user
#[derive(Clone, Copy, Debug)]
pub enum InteractiveEvent {
pub enum InteractiveEventKind {
PointerPrimaryButtonClicked,
PointerPrimaryButtonDragStarted,
PointerPrimaryButtonDragStopped,
PointerSecondaryButtonClicked,
}
/// An event received from the user
pub struct InteractiveEvent {
pub kind: InteractiveEventKind,
/// `true` if the `Ctrl` key pressed during the event
pub ctrl: bool,
/// `true` if the `Shift` key pressed during the event
pub shift: bool,
}
/// This is the execution context passed to the stepper on each step
pub struct ActivityContext<'a, M> {
pub interactive_input: &'a InteractiveInput,

View File

@ -15,7 +15,7 @@ use crate::{
};
use super::{
activity::{ActivityContext, InteractiveEvent},
activity::{ActivityContext, InteractiveEvent, InteractiveEventKind},
interaction::InteractionError,
};
@ -128,8 +128,8 @@ impl<M: AccessMesadata> OnEvent<ActivityContext<'_, M>, InteractiveEvent> for Ro
context: &mut ActivityContext<M>,
event: InteractiveEvent,
) -> Result<(), InteractionError> {
match event {
InteractiveEvent::PointerPrimaryButtonClicked if self.end_pin.is_none() => {
match event.kind {
InteractiveEventKind::PointerPrimaryButtonClicked if self.end_pin.is_none() => {
if let Some((layer, idx, pos)) = try_select_pin(context) {
// make sure double-click or such doesn't corrupt state
if let Some(start_pin) = self.start_pin {
@ -151,7 +151,7 @@ impl<M: AccessMesadata> OnEvent<ActivityContext<'_, M>, InteractiveEvent> for Ro
}
}
}
InteractiveEvent::PointerSecondaryButtonClicked => {
InteractiveEventKind::PointerSecondaryButtonClicked => {
if self.end_pin.is_some() {
log::debug!("un-clicked end pin");
self.end_pin = None;