mirror of
https://github.com/storytold/storyteller-bevy.git
synced 2026-10-25 08:30:24 +00:00
Gizmo refactor for translation local version
This commit is contained in:
@@ -23,18 +23,15 @@ pub(super) enum TranslationWidget {
|
||||
#[inline]
|
||||
pub(super) fn spawn_translation_gizmo(
|
||||
cmd: &mut Commands,
|
||||
ent: Entity,
|
||||
xform: &GlobalTransform,
|
||||
target: Entity,
|
||||
target_global_transform: &GlobalTransform,
|
||||
space: TransformSpace,
|
||||
normalizing_scale: f32,
|
||||
meshes: &GizmoMeshes,
|
||||
mats: &GizmoMats,
|
||||
) {
|
||||
cmd.entity(ent)
|
||||
.insert(TransformGizmoRoot)
|
||||
.with_children(|cmd| {
|
||||
cmd.spawn((
|
||||
TransformGizmoBundle::new(xform, space, normalizing_scale),
|
||||
cmd.spawn((
|
||||
TransformGizmoBundle::new(target, target_global_transform, space, normalizing_scale),
|
||||
On::<Pointer<Over>>::run(on_gizmo_element_hover),
|
||||
On::<Pointer<Out>>::run(on_gizmo_element_unhover),
|
||||
On::<Pointer<DragStart>>::run(on_translation_start),
|
||||
@@ -202,17 +199,19 @@ pub(super) fn spawn_translation_gizmo(
|
||||
},
|
||||
));
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
pub(super) fn on_translation_start(
|
||||
_: Listener<Pointer<DragStart>>,
|
||||
mut cmd: Commands,
|
||||
q_xform: Query<(Entity, &GlobalTransform), With<TransformGizmoRoot>>,
|
||||
q_xform: Query<(Entity, &GlobalTransform)>,
|
||||
q_gizmos: Query<&TransformGizmo>
|
||||
) {
|
||||
if let Ok((ent, &world_xform)) = q_xform.get_single() {
|
||||
cmd.entity(ent).insert(InitialTransform(world_xform));
|
||||
};
|
||||
for gizmo_data in q_gizmos.iter() {
|
||||
if let Ok((ent, &world_xform)) = q_xform.get(gizmo_data.target) {
|
||||
cmd.entity(ent).insert(InitialTransform(world_xform));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn on_translation_end(
|
||||
@@ -224,17 +223,21 @@ pub(super) fn on_translation_end(
|
||||
|
||||
pub(super) fn on_translate(
|
||||
event: Listener<Pointer<Drag>>,
|
||||
mut q_root: Query<(&mut Transform, &InitialTransform), With<TransformGizmoRoot>>,
|
||||
mut q_root: Query<(&mut Transform, &InitialTransform, Option<&Parent>)>,
|
||||
q_camera: Query<(&Camera, &GlobalTransform), With<MainCamera>>,
|
||||
q_gizmo: Query<&GlobalTransform, (With<TransformGizmo>, Without<MainCamera>)>,
|
||||
q_gizmo: Query<(&GlobalTransform, &TransformGizmo), Without<MainCamera>>,
|
||||
q_widget: Query<&TranslationWidget>,
|
||||
q_all_global_transforms : Query<&GlobalTransform>
|
||||
) {
|
||||
let (mut xform, init_xform) = q_root.single_mut();
|
||||
let gizmo_xform = q_gizmo.single();
|
||||
let (gizmo_xform, gizmo_data) = q_gizmo.single();
|
||||
let Ok((mut target_transform, target_global_transform, target_parent)) = q_root.get_mut(gizmo_data.target) else {
|
||||
error!("Translation gizmo target not found");
|
||||
return;
|
||||
};
|
||||
let (cam, cam_xform) = q_camera.single();
|
||||
|
||||
// Find the initial origin point in screen-space and add the total screen-space drag distance
|
||||
let origin_ws = init_xform.translation();
|
||||
let origin_ws = target_global_transform.translation();
|
||||
let origin_ss = cam.world_to_viewport(cam_xform, origin_ws).unwrap();
|
||||
let target_ss = origin_ss + event.distance;
|
||||
|
||||
@@ -265,7 +268,14 @@ pub(super) fn on_translate(
|
||||
let projection_length = drag_vector.dot(axis_ws);
|
||||
let target_ws = origin_ws + axis_ws * projection_length;
|
||||
|
||||
xform.translation = target_ws;
|
||||
if let Some(target_parent) = target_parent {
|
||||
if let Ok(parent_global_transform) = q_all_global_transforms.get(target_parent.get()) {
|
||||
let real_local_transform = GlobalTransform::from(Transform::from_translation(target_ws)).reparented_to(parent_global_transform);
|
||||
target_transform.translation = real_local_transform.translation;
|
||||
}
|
||||
} else {
|
||||
target_transform.translation = target_ws;
|
||||
}
|
||||
}
|
||||
Ok(plane) => {
|
||||
// Find the world-space normal of the plane we're translating on
|
||||
@@ -281,7 +291,14 @@ pub(super) fn on_translate(
|
||||
};
|
||||
let target_ws = ray.origin + ray.direction * hit_dist;
|
||||
|
||||
xform.translation = target_ws;
|
||||
if let Some(target_parent) = target_parent {
|
||||
if let Ok(parent_global_transform) = q_all_global_transforms.get(target_parent.get()) {
|
||||
let real_local_transform = GlobalTransform::from(Transform::from_translation(target_ws)).reparented_to(parent_global_transform);
|
||||
target_transform.translation = real_local_transform.translation;
|
||||
}
|
||||
} else {
|
||||
target_transform.translation = target_ws;
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user