refactor: Move mouse click to grid position logic into utils.rs
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
use crate::features::{
|
||||
input::utils::mouse_to_grid,
|
||||
phase::messages::{NextPhaseMessage, PhaseTimerPauseMessage},
|
||||
pom::messages::InvalidMoveMessage,
|
||||
shop::ui::open_shop,
|
||||
@@ -7,6 +8,8 @@ use crate::prelude::*;
|
||||
use bevy::input::mouse::MouseButton;
|
||||
use bevy::window::PrimaryWindow;
|
||||
|
||||
pub mod utils;
|
||||
|
||||
pub struct InputPlugin;
|
||||
|
||||
impl Plugin for InputPlugin {
|
||||
@@ -52,20 +55,7 @@ fn move_click(
|
||||
}
|
||||
|
||||
if mouse_btn.just_pressed(MouseButton::Right) {
|
||||
let (cam, cam_transform) = *camera;
|
||||
|
||||
let Some(cursor_pos) = window.cursor_position() else {
|
||||
return;
|
||||
};
|
||||
if ui_blocks(window, cursor_pos, ui_query) {
|
||||
return;
|
||||
}
|
||||
let Ok(world_pos) = cam.viewport_to_world(cam_transform, cursor_pos) else {
|
||||
return;
|
||||
};
|
||||
let Ok((x, y)) =
|
||||
world_to_grid_coords(world_pos.origin, config.grid_width, config.grid_height)
|
||||
else {
|
||||
let Some((x, y)) = mouse_to_grid(window, camera, config, ui_query) else {
|
||||
return;
|
||||
};
|
||||
|
||||
@@ -93,20 +83,7 @@ fn interact_click(
|
||||
if keys.pressed(KeyCode::ShiftLeft) || keys.pressed(KeyCode::ShiftRight) {
|
||||
return;
|
||||
}
|
||||
let (cam, cam_transform) = *camera;
|
||||
|
||||
let Some(cursor_pos) = window.cursor_position() else {
|
||||
return;
|
||||
};
|
||||
if ui_blocks(window, cursor_pos, ui_query) {
|
||||
return;
|
||||
}
|
||||
let Ok(world_pos) = cam.viewport_to_world(cam_transform, cursor_pos) else {
|
||||
return;
|
||||
};
|
||||
let Ok((x, y)) =
|
||||
world_to_grid_coords(world_pos.origin, config.grid_width, config.grid_height)
|
||||
else {
|
||||
let Some((x, y)) = mouse_to_grid(window, camera, config, ui_query) else {
|
||||
return;
|
||||
};
|
||||
|
||||
@@ -135,21 +112,7 @@ fn debug_click(
|
||||
if !keys.pressed(KeyCode::ShiftLeft) && !keys.pressed(KeyCode::ShiftRight) {
|
||||
return;
|
||||
}
|
||||
|
||||
let (cam, cam_transform) = *camera;
|
||||
|
||||
let Some(cursor_pos) = window.cursor_position() else {
|
||||
return;
|
||||
};
|
||||
if ui_blocks(window, cursor_pos, ui_query) {
|
||||
return;
|
||||
}
|
||||
let Ok(world_pos) = cam.viewport_to_world(cam_transform, cursor_pos) else {
|
||||
return;
|
||||
};
|
||||
let Ok((x, y)) =
|
||||
world_to_grid_coords(world_pos.origin, config.grid_width, config.grid_height)
|
||||
else {
|
||||
let Some((x, y)) = mouse_to_grid(window, camera, config, ui_query) else {
|
||||
return;
|
||||
};
|
||||
|
||||
|
||||
28
src/features/input/utils.rs
Normal file
28
src/features/input/utils.rs
Normal file
@@ -0,0 +1,28 @@
|
||||
use crate::prelude::*;
|
||||
use bevy::window::PrimaryWindow;
|
||||
|
||||
pub fn mouse_to_grid(
|
||||
window: Single<&Window, With<PrimaryWindow>>,
|
||||
camera: Single<(&Camera, &GlobalTransform), With<Camera2d>>,
|
||||
config: Res<GameConfig>,
|
||||
ui_query: Query<(&ComputedNode, &GlobalTransform), With<Node>>,
|
||||
) -> Option<(u32, u32)> {
|
||||
let (cam, cam_transform) = *camera;
|
||||
|
||||
let Some(cursor_pos) = window.cursor_position() else {
|
||||
return None;
|
||||
};
|
||||
if ui_blocks(window, cursor_pos, ui_query) {
|
||||
return None;
|
||||
}
|
||||
let Ok(world_pos) = cam.viewport_to_world(cam_transform, cursor_pos) else {
|
||||
return None;
|
||||
};
|
||||
let Ok(grid_pos) =
|
||||
world_to_grid_coords(world_pos.origin, config.grid_width, config.grid_height)
|
||||
else {
|
||||
return None;
|
||||
};
|
||||
|
||||
Some(grid_pos)
|
||||
}
|
||||
Reference in New Issue
Block a user