move camera with bevy_picking + stop drag from clicking

This commit is contained in:
Arkitu 2025-01-11 17:44:42 +01:00
parent 108d6afbd0
commit c1250bbb7f
4 changed files with 49 additions and 13 deletions

View File

@ -8,7 +8,7 @@ pub struct Plugin;
impl bevy::prelude::Plugin for Plugin { impl bevy::prelude::Plugin for Plugin {
fn build(&self, app: &mut App) { fn build(&self, app: &mut App) {
app.add_systems(Startup, setup) app.add_systems(Startup, setup)
.add_systems(Update, move_cam) // .add_systems(Update, move_cam)
.init_resource::<Pointers>(); .init_resource::<Pointers>();
} }
} }

View File

@ -1,3 +1,5 @@
use std::time::Duration;
use bevy::{asset::RenderAssetUsages, picking::PickSet, prelude::*, render::mesh::{Indices, PrimitiveTopology}, utils::HashMap}; use bevy::{asset::RenderAssetUsages, picking::PickSet, prelude::*, render::mesh::{Indices, PrimitiveTopology}, utils::HashMap};
use noise::{Fbm, MultiFractal, NoiseFn, Perlin}; use noise::{Fbm, MultiFractal, NoiseFn, Perlin};
use rand::{thread_rng, Rng, SeedableRng}; use rand::{thread_rng, Rng, SeedableRng};
@ -137,12 +139,15 @@ fn setup(
for cd in cells_data { for cd in cells_data {
let mut cmd = parent.spawn((cd, LastUpdate(0))); let mut cmd = parent.spawn((cd, LastUpdate(0)));
cmd.observe(|trigger: Trigger<Pointer<Click>>, mut cells: Query<&mut CellData>| { cmd.observe(|trigger: Trigger<Pointer<Click>>, mut cells: Query<&mut CellData>| {
if trigger.duration > Duration::from_millis(100) {
return
}
let mut cd = cells.get_mut(trigger.target).unwrap(); let mut cd = cells.get_mut(trigger.target).unwrap();
match cd.kind { match cd.kind {
CellKind::Dirt | CellKind::Grass => {cd.kind = CellKind::Forest;}, CellKind::Dirt | CellKind::Grass => {cd.kind = CellKind::Forest;},
_ => {} _ => {}
} }
dbg!(trigger.target); dbg!(trigger.duration);
}); });
cells_entities.push(cmd.id()); cells_entities.push(cmd.id());
} }

View File

@ -18,7 +18,8 @@ pub fn picking_backend(
if let Some(mut pos) = l.location().map(|l| l.position) { if let Some(mut pos) = l.location().map(|l| l.position) {
pos -= window.size()/2.; pos -= window.size()/2.;
pos *= cam.scale.xy(); pos *= cam.scale.xy();
pos -= cam.translation.xy(); pos.x += cam.translation.x;
pos.y -= cam.translation.y;
if let Some(c) = voronoi.0.cell(last_cell).iter_path(Point { x: pos.x as f64, y: -pos.y as f64 }).last() { if let Some(c) = voronoi.0.cell(last_cell).iter_path(Point { x: pos.x as f64, y: -pos.y as f64 }).last() {
last_cell = c; last_cell = c;
output.send(PointerHits { output.send(PointerHits {

View File

@ -1,4 +1,6 @@
use bevy::prelude::*; use std::collections::BTreeMap;
use bevy::{math::VectorSpace, picking::pointer::PointerId, prelude::*, utils::HashMap};
use crate::map::{self, MapMarker}; use crate::map::{self, MapMarker};
@ -9,6 +11,9 @@ impl bevy::prelude::Plugin for Plugin {
} }
} }
#[derive(Component)]
struct PointersDragging(HashMap<PointerId, Vec2>);
#[derive(Component)] #[derive(Component)]
pub struct MapUIComponent; pub struct MapUIComponent;
@ -27,14 +32,39 @@ fn setup(
should_block_lower: false, should_block_lower: false,
is_hoverable: true is_hoverable: true
}, },
MapUIComponent)); MapUIComponent,
// )).observe(|mut trigger: Trigger<Pointer<Click>>, map: Query<(&map::Voronoi, &map::CellsEntities), With<MapMarker>>| { PointersDragging(HashMap::new())
// let event = trigger.event(); )).observe(|trigger: Trigger<Pointer<DragStart>>, mut ptrs: Query<&mut PointersDragging>| {
// // dbg!(event); let event = trigger.event();
// if event.button == PointerButton::Primary { // dbg!(event);
// let pos = event.pointer_location.position; if event.button == PointerButton::Primary {
// } ptrs.get_mut(event.target).unwrap().0.insert(event.pointer_id, event.pointer_location.position);
// trigger.propagate(false); }
// }); }).observe(|trigger: Trigger<Pointer<DragEnd>>, mut ptrs: Query<&mut PointersDragging>| {
let event = trigger.event();
// dbg!(event);
if event.button == PointerButton::Primary {
ptrs.get_mut(event.target).unwrap().0.remove(&event.pointer_id);
}
}).observe(|
trigger: Trigger<Pointer<Drag>>,
mut ptrs: Query<&mut PointersDragging>,
mut cam: Query<&mut Transform, With<Camera2d>>,
| {
let event = trigger.event();
// dbg!(event);
if event.button == PointerButton::Primary {
let mut cam = cam.single_mut();
let mut ptrs = ptrs.get_mut(event.target).unwrap();
let old_midpoint = ptrs.0.values().fold(Vec2::ZERO, |acc, pos| acc + (pos/ptrs.0.len() as f32));
ptrs.0.insert(event.pointer_id, event.pointer_location.position);
let new_midpoint = ptrs.0.values().fold(Vec2::ZERO, |acc, pos| acc + (pos/ptrs.0.len() as f32));
cam.translation.x -= (new_midpoint.x - old_midpoint.x)*cam.scale.x;
cam.translation.y += (new_midpoint.y - old_midpoint.y)*cam.scale.y;
}
}
);
// Spawn all ui elements as children of this one // Spawn all ui elements as children of this one
} }