From c1250bbb7fe653cda8a9b51266fea9ed4a9613eb Mon Sep 17 00:00:00 2001 From: Arkitu <85173315+Arkitu@users.noreply.github.com> Date: Sat, 11 Jan 2025 17:44:42 +0100 Subject: [PATCH] move camera with bevy_picking + stop drag from clicking --- src/camera.rs | 2 +- src/map.rs | 7 ++++++- src/map/picking.rs | 3 ++- src/ui.rs | 50 ++++++++++++++++++++++++++++++++++++---------- 4 files changed, 49 insertions(+), 13 deletions(-) diff --git a/src/camera.rs b/src/camera.rs index 2c12fa2..d437801 100644 --- a/src/camera.rs +++ b/src/camera.rs @@ -8,7 +8,7 @@ pub struct Plugin; impl bevy::prelude::Plugin for Plugin { fn build(&self, app: &mut App) { app.add_systems(Startup, setup) - .add_systems(Update, move_cam) + // .add_systems(Update, move_cam) .init_resource::(); } } diff --git a/src/map.rs b/src/map.rs index 768e384..2d78c7b 100644 --- a/src/map.rs +++ b/src/map.rs @@ -1,3 +1,5 @@ +use std::time::Duration; + use bevy::{asset::RenderAssetUsages, picking::PickSet, prelude::*, render::mesh::{Indices, PrimitiveTopology}, utils::HashMap}; use noise::{Fbm, MultiFractal, NoiseFn, Perlin}; use rand::{thread_rng, Rng, SeedableRng}; @@ -137,12 +139,15 @@ fn setup( for cd in cells_data { let mut cmd = parent.spawn((cd, LastUpdate(0))); cmd.observe(|trigger: Trigger>, mut cells: Query<&mut CellData>| { + if trigger.duration > Duration::from_millis(100) { + return + } let mut cd = cells.get_mut(trigger.target).unwrap(); match cd.kind { CellKind::Dirt | CellKind::Grass => {cd.kind = CellKind::Forest;}, _ => {} } - dbg!(trigger.target); + dbg!(trigger.duration); }); cells_entities.push(cmd.id()); } diff --git a/src/map/picking.rs b/src/map/picking.rs index 3253a20..d4a8f3e 100644 --- a/src/map/picking.rs +++ b/src/map/picking.rs @@ -18,7 +18,8 @@ pub fn picking_backend( if let Some(mut pos) = l.location().map(|l| l.position) { pos -= window.size()/2.; 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() { last_cell = c; output.send(PointerHits { diff --git a/src/ui.rs b/src/ui.rs index 8bf61bc..676a3c0 100644 --- a/src/ui.rs +++ b/src/ui.rs @@ -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}; @@ -9,6 +11,9 @@ impl bevy::prelude::Plugin for Plugin { } } +#[derive(Component)] +struct PointersDragging(HashMap); + #[derive(Component)] pub struct MapUIComponent; @@ -27,14 +32,39 @@ fn setup( should_block_lower: false, is_hoverable: true }, - MapUIComponent)); - // )).observe(|mut trigger: Trigger>, map: Query<(&map::Voronoi, &map::CellsEntities), With>| { - // let event = trigger.event(); - // // dbg!(event); - // if event.button == PointerButton::Primary { - // let pos = event.pointer_location.position; - // } - // trigger.propagate(false); - // }); + MapUIComponent, + PointersDragging(HashMap::new()) + )).observe(|trigger: Trigger>, mut ptrs: Query<&mut PointersDragging>| { + let event = trigger.event(); + // dbg!(event); + if event.button == PointerButton::Primary { + ptrs.get_mut(event.target).unwrap().0.insert(event.pointer_id, event.pointer_location.position); + } + }).observe(|trigger: Trigger>, 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>, + mut ptrs: Query<&mut PointersDragging>, + mut cam: Query<&mut Transform, With>, + | { + 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 } \ No newline at end of file