mirror of https://codeberg.org/topola/topola.git
fix(interactor/interactor): Factor out the logic for on-event activity updates
This commit is contained in:
parent
a4b1b3893c
commit
c66089bca9
|
|
@ -138,51 +138,6 @@ impl Viewport {
|
|||
if let Some(workspace) = maybe_workspace {
|
||||
let latest_point = point! {x: latest_pos.x as f64, y: -latest_pos.y as f64};
|
||||
|
||||
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)
|
||||
} else if response.clicked_by(egui::PointerButton::Secondary) {
|
||||
Some(InteractiveEvent::PointerSecondaryButtonClicked)
|
||||
} else {
|
||||
None
|
||||
};
|
||||
if let Some(event) = interactive_event {
|
||||
log::debug!("got {:?}", event);
|
||||
}
|
||||
// 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 += if interactive_event.is_some() {
|
||||
// make sure we run the loop below at least once on clicks
|
||||
let mut dtx = menu_bar.frame_timestep;
|
||||
if dt > dtx {
|
||||
dtx = dt;
|
||||
}
|
||||
dtx
|
||||
} else {
|
||||
dt
|
||||
};
|
||||
while self.update_counter >= menu_bar.frame_timestep {
|
||||
self.update_counter -= menu_bar.frame_timestep;
|
||||
if let ControlFlow::Break(()) = workspace.update_state(
|
||||
tr,
|
||||
error_dialog,
|
||||
&InteractiveInput {
|
||||
active_layer,
|
||||
pointer_pos: latest_point,
|
||||
dt,
|
||||
},
|
||||
interactive_event,
|
||||
) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// 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.
|
||||
|
|
@ -199,11 +154,37 @@ impl Viewport {
|
|||
pointer_pos: point! {x: latest_pos.x as f64, y: latest_pos.y as f64},
|
||||
dt,
|
||||
},
|
||||
None,
|
||||
) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
} else if response.clicked_by(egui::PointerButton::Secondary) {
|
||||
Some(InteractiveEvent::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;
|
||||
let _ = workspace.update_state_for_event(
|
||||
tr,
|
||||
error_dialog,
|
||||
&InteractiveInput {
|
||||
active_layer,
|
||||
pointer_pos: latest_point,
|
||||
dt,
|
||||
},
|
||||
interactive_event,
|
||||
);
|
||||
}
|
||||
} else {
|
||||
let layers = &mut workspace.appearance_panel;
|
||||
let overlay = &mut workspace.overlay;
|
||||
let board = workspace.interactor.invoker().autorouter().board();
|
||||
|
|
|
|||
|
|
@ -61,12 +61,31 @@ impl Workspace {
|
|||
})
|
||||
}
|
||||
|
||||
pub fn update_state_for_event(
|
||||
&mut self,
|
||||
tr: &Translator,
|
||||
error_dialog: &mut ErrorDialog,
|
||||
interactive_input: &InteractiveInput,
|
||||
interactive_event: InteractiveEvent,
|
||||
) -> ControlFlow<()> {
|
||||
match self
|
||||
.interactor
|
||||
.update_for_event(interactive_input, interactive_event)
|
||||
{
|
||||
ControlFlow::Continue(()) => ControlFlow::Continue(()),
|
||||
ControlFlow::Break(Ok(())) => ControlFlow::Break(()),
|
||||
ControlFlow::Break(Err(err)) => {
|
||||
error_dialog.push_error("tr-module-invoker", format!("{}", err));
|
||||
ControlFlow::Break(())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn update_state(
|
||||
&mut self,
|
||||
tr: &Translator,
|
||||
error_dialog: &mut ErrorDialog,
|
||||
interactive_input: &InteractiveInput,
|
||||
interactive_event: Option<InteractiveEvent>,
|
||||
) -> ControlFlow<()> {
|
||||
if let Ok(data) = self.history_channel.1.try_recv() {
|
||||
match data {
|
||||
|
|
@ -92,7 +111,7 @@ impl Workspace {
|
|||
}
|
||||
}
|
||||
|
||||
match self.interactor.update(interactive_input, interactive_event) {
|
||||
match self.interactor.update(interactive_input) {
|
||||
ControlFlow::Continue(()) => ControlFlow::Continue(()),
|
||||
ControlFlow::Break(Ok(())) => ControlFlow::Break(()),
|
||||
ControlFlow::Break(Err(err)) => {
|
||||
|
|
|
|||
|
|
@ -79,20 +79,19 @@ impl<M: AccessMesadata> Interactor<M> {
|
|||
self.invoker.replay(history);
|
||||
}
|
||||
|
||||
/// Update the currently running execution or activity
|
||||
pub fn update(
|
||||
/// Update the currently running execution or activity, given an event
|
||||
pub fn update_for_event(
|
||||
&mut self,
|
||||
interactive_input: &InteractiveInput,
|
||||
interactive_event: Option<InteractiveEvent>,
|
||||
interactive_event: InteractiveEvent,
|
||||
) -> ControlFlow<Result<(), ActivityError>> {
|
||||
if let Some(ref mut activity) = self.activity {
|
||||
if let Some(event) = interactive_event {
|
||||
match activity.on_event(
|
||||
&mut ActivityContext {
|
||||
interactive_input,
|
||||
invoker: &mut self.invoker,
|
||||
},
|
||||
event,
|
||||
interactive_event,
|
||||
) {
|
||||
Ok(()) => ControlFlow::Continue(()),
|
||||
Err(err) => {
|
||||
|
|
@ -101,6 +100,16 @@ impl<M: AccessMesadata> Interactor<M> {
|
|||
}
|
||||
}
|
||||
} else {
|
||||
ControlFlow::Break(Ok(()))
|
||||
}
|
||||
}
|
||||
|
||||
/// Update the currently running execution or activity
|
||||
pub fn update(
|
||||
&mut self,
|
||||
interactive_input: &InteractiveInput,
|
||||
) -> ControlFlow<Result<(), ActivityError>> {
|
||||
if let Some(ref mut activity) = self.activity {
|
||||
match activity.step(&mut ActivityContext {
|
||||
interactive_input,
|
||||
invoker: &mut self.invoker,
|
||||
|
|
@ -115,7 +124,6 @@ impl<M: AccessMesadata> Interactor<M> {
|
|||
ControlFlow::Break(Err(err))
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
ControlFlow::Break(Ok(()))
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue