From f3afb2616c3575c2f0b40f2ee0b45ddcf7e44b82 Mon Sep 17 00:00:00 2001 From: Elijah Voigt Date: Thu, 16 Jul 2026 14:02:54 -0700 Subject: [PATCH] use actions for zooming w/ buttons, keys, and scroll wheel --- life/src/actions.rs | 56 ++++++++++++++++++++++++++++++--------------- life/src/main.rs | 4 ++-- 2 files changed, 40 insertions(+), 20 deletions(-) diff --git a/life/src/actions.rs b/life/src/actions.rs index 55cad0b..0e0f9fd 100644 --- a/life/src/actions.rs +++ b/life/src/actions.rs @@ -11,9 +11,11 @@ impl Plugin for GameActionsPlugin { .bind(SimAction::Reset, [Binding::Key(KeyCode::KeyR)]), ActionsPlugin::::new() .bind(AudioAction::Toggle, [Binding::Key(KeyCode::KeyM)]), + ActionsPlugin::::new() + .bind(ZoomAction::Zoom, [Binding::Scroll]) + .bind(ZoomAction::ZoomIn, [Binding::Key(KeyCode::Equal)]) + .bind(ZoomAction::ZoomOut, [Binding::Key(KeyCode::Minus)]), ActionsPlugin::::new() - .bind(Action::ZoomIn, [Binding::Key(KeyCode::Equal)]) - .bind(Action::ZoomOut, [Binding::Key(KeyCode::Minus)]) .bind(Action::Quit, [Binding::Key(KeyCode::Escape)]), )); } @@ -86,34 +88,52 @@ impl SimAction { } #[derive(Copy, Clone, Eq, PartialEq, Hash, GameAction, Default)] -pub(crate) enum Action { - #[action_handler(Action::zoom_in)] +pub(crate) enum ZoomAction { + #[action_kind(axis)] + #[action_handler(ZoomAction::zoom)] + Zoom, + #[action_handler(ZoomAction::zoom_in)] ZoomIn, - #[action_handler(Action::zoom_out)] + #[action_handler(ZoomAction::zoom_out)] ZoomOut, + #[default] + None +} + +#[derive(Copy, Clone, Eq, PartialEq, Hash, GameAction, Default)] +pub(crate) enum Action { #[action_handler(Action::quit)] Quit, #[default] None, } -impl Action { - fn zoom_in(mut projection: Query<&mut Projection>) { - projection.iter_mut().for_each(|mut p| match *p { - Projection::Orthographic(ref mut o) => { - o.scale *= 0.5; +impl ZoomAction { + fn zoom_by(factor: f32, mut projection: Query<&mut Projection>) { + projection.iter_mut().for_each(|mut p| { + if let Projection::Orthographic(o) = &mut *p { + o.scale = (o.scale * factor).clamp(0.05, 20.0); } - _ => todo!(), }); } - fn zoom_out(mut projection: Query<&mut Projection>) { - projection.iter_mut().for_each(|mut p| match *p { - Projection::Orthographic(ref mut o) => { - o.scale /= 0.5; - } - _ => todo!(), - }); + + // Simple UI zoom out + fn zoom(In(delta): In, projection: Query<&mut Projection>) { + if delta.y != 0.0 { + ZoomAction::zoom_by(0.9_f32.powf(delta.y), projection); // up = zoom in + } + } + + fn zoom_in(projection: Query<&mut Projection>) { + ZoomAction::zoom_by(0.5, projection); + } + + fn zoom_out(projection: Query<&mut Projection>) { + ZoomAction::zoom_by(2.0, projection); } +} + +impl Action { fn quit(mut writer: MessageWriter) { writer.write(AppExit::Success); } diff --git a/life/src/main.rs b/life/src/main.rs index de35cb2..08e0831 100644 --- a/life/src/main.rs +++ b/life/src/main.rs @@ -375,9 +375,9 @@ fn primary_ui() -> impl Scene { ui_button(UiButton::Wipe) ActionSource(SimAction::Wipe), ui_button(UiButton::ZoomOut) - ActionSource(Action::ZoomOut), + ActionSource(ZoomAction::ZoomOut), ui_button(UiButton::ZoomIn) - ActionSource(Action::ZoomIn), + ActionSource(ZoomAction::ZoomIn), ui_button(UiButton::MuteAudio) AudioPlayback::Mute ActionSource(AudioAction::Toggle),