Skip to content

Commit

Permalink
Improve minimap controls
Browse files Browse the repository at this point in the history
  • Loading branch information
Zakru committed Jul 24, 2023
1 parent 7f9d89b commit 7adbac3
Showing 1 changed file with 69 additions and 8 deletions.
77 changes: 69 additions & 8 deletions crates/controller/src/hud/minimap/interaction.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
use std::fmt;

use bevy::{
input::{mouse::MouseButtonInput, ButtonState},
input::{
mouse::{MouseButtonInput, MouseMotion},
ButtonState,
},
prelude::*,
window::PrimaryWindow,
};
Expand All @@ -20,17 +23,25 @@ pub(super) struct InteractionPlugin;
impl Plugin for InteractionPlugin {
fn build(&self, app: &mut App) {
app.add_event::<MinimapClickEvent>()
.insert_resource(DraggingCamera(false))
.add_system(
click_handler
.in_base_set(GameSet::Input)
.run_if(in_state(GameState::Playing))
.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::<MouseButtonInput>()),
)
.add_system(
move_camera_drag_system
.in_base_set(GameSet::Input)
.run_if(in_state(GameState::Playing))
.run_if(on_event::<MouseMotion>())
.after(move_camera_click_system),
)
.add_system(
send_units_system
Expand Down Expand Up @@ -81,6 +92,9 @@ impl fmt::Debug for MinimapClickEvent {
}
}

#[derive(Resource)]
struct DraggingCamera(bool);

fn click_handler(
window_query: Query<&Window, With<PrimaryWindow>>,
mut input_events: EventReader<MouseButtonInput>,
Expand All @@ -105,15 +119,62 @@ fn click_handler(
}
}

fn move_camera_system(
mut click_events: EventReader<MinimapClickEvent>,
fn move_camera_click_system(
window_query: Query<&Window, With<PrimaryWindow>>,
mut input_events: EventReader<MouseButtonInput>,
hud: HudNodes<With<MinimapNode>>,
bounds: Res<MapBounds>,
mut dragging: ResMut<DraggingCamera>,
mut camera_events: EventWriter<MoveFocusEvent>,
) {
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<PrimaryWindow>>,
mut input_events: EventReader<MouseMotion>,
hud: HudNodes<With<MinimapNode>>,
bounds: Res<MapBounds>,
mouse_buttons: Res<Input<MouseButton>>,
dragging: Res<DraggingCamera>,
mut camera_events: EventWriter<MoveFocusEvent>,
) {
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);
}
}

Expand Down

0 comments on commit 7adbac3

Please sign in to comment.