diff --git a/crates/controller/src/hud/minimap/interaction.rs b/crates/controller/src/hud/minimap/interaction.rs index a2240bbb2..dcbd6fc9a 100644 --- a/crates/controller/src/hud/minimap/interaction.rs +++ b/crates/controller/src/hud/minimap/interaction.rs @@ -1,7 +1,10 @@ use std::fmt; use bevy::{ - input::{mouse::MouseButtonInput, ButtonState}, + input::{ + mouse::{MouseButtonInput, MouseMotion}, + ButtonState, + }, prelude::*, window::PrimaryWindow, }; @@ -20,6 +23,7 @@ pub(super) struct InteractionPlugin; impl Plugin for InteractionPlugin { fn build(&self, app: &mut App) { app.add_event::() + .insert_resource(DraggingCamera(false)) .add_system( click_handler .in_base_set(GameSet::Input) @@ -27,10 +31,17 @@ impl Plugin for InteractionPlugin { .in_set(InteractionSet::ClickHandler), ) .add_system( - move_camera_system + move_camera_click_system .in_base_set(GameSet::Input) .run_if(in_state(GameState::Playing)) - .after(InteractionSet::ClickHandler), + .run_if(on_event::()), + ) + .add_system( + move_camera_drag_system + .in_base_set(GameSet::Input) + .run_if(in_state(GameState::Playing)) + .run_if(on_event::()) + .after(move_camera_click_system), ) .add_system( send_units_system @@ -81,6 +92,9 @@ impl fmt::Debug for MinimapClickEvent { } } +#[derive(Resource)] +struct DraggingCamera(bool); + fn click_handler( window_query: Query<&Window, With>, mut input_events: EventReader, @@ -105,15 +119,62 @@ fn click_handler( } } -fn move_camera_system( - mut click_events: EventReader, +fn move_camera_click_system( + window_query: Query<&Window, With>, + mut input_events: EventReader, + hud: HudNodes>, + bounds: Res, + mut dragging: ResMut, mut camera_events: EventWriter, ) { - for click in click_events.iter() { - if click.button() != MouseButton::Left { + let Some(cursor) = window_query.single().cursor_position() else { + return; + }; + + for event in input_events.iter() { + if event.button != MouseButton::Left { + continue; + } + + if !event.state.is_pressed() { + if dragging.0 { + dragging.0 = false; + } continue; } - camera_events.send(MoveFocusEvent::new(click.position())); + + if let Some(relative) = hud.relative_position(cursor) { + dragging.0 = true; + let proportional = Vec2::new(relative.x, 1. - relative.y); + let event = MoveFocusEvent::new(bounds.rel_to_abs(proportional)); + + camera_events.send(event); + } + } +} + +fn move_camera_drag_system( + window_query: Query<&Window, With>, + mut input_events: EventReader, + hud: HudNodes>, + bounds: Res, + mouse_buttons: Res>, + dragging: Res, + mut camera_events: EventWriter, +) { + input_events.clear(); + if !mouse_buttons.pressed(MouseButton::Left) || !dragging.0 { + return; + } + + let Some(cursor) = window_query.single().cursor_position() else { + return; + }; + + if let Some(relative) = hud.relative_position(cursor) { + let proportional = Vec2::new(relative.x, 1. - relative.y); + let event = MoveFocusEvent::new(bounds.rel_to_abs(proportional)); + camera_events.send(event); } }