Add config resource for transform gizmo

This commit is contained in:
Danny McGee
2023-10-15 16:41:38 -04:00
parent c573737f06
commit b8f9e92d67
2 changed files with 34 additions and 5 deletions
+29 -1
View File
@@ -10,8 +10,36 @@ pub(super) struct StudioGizmosPlugin;
impl Plugin for StudioGizmosPlugin {
fn build(&self, app: &mut App) {
app.add_systems(PreUpdate, (model::update_spawns, view::configure))
app.insert_resource(StudioGizmoConfig::default())
.add_systems(PreUpdate, (model::update_spawns, view::configure))
.add_systems(Update, model::update)
.add_systems(PostUpdate, view::draw);
}
}
#[derive(Resource, Clone, Copy)]
pub struct StudioGizmoConfig {
/// Measured (roughly) as a fraction of the viewport height
pub base_size: f32,
/// Measured in device-independent "pixel" units
pub line_thickness: f32,
}
impl Default for StudioGizmoConfig {
fn default() -> Self {
Self {
base_size: 0.125,
line_thickness: 4.,
}
}
}
impl StudioGizmoConfig {
fn base_size_world_units(&self, gizmo_origin: Vec3, camera_origin: Vec3) -> f32 {
let dist = Vec3::distance(gizmo_origin, camera_origin);
// The magic numbers here are because I first decided that dist/20
// looked pretty good, then measured it as ~1/8 of the viewport height,
// and then factored that measurement in as a configurable parameter.
self.base_size * 8. * (dist / 20.)
}
}
+5 -4
View File
@@ -1,7 +1,7 @@
use bevy::prelude::*;
use bitflags::bitflags;
use super::{InteractionMode, Selected, TransformSpace, TransformType};
use super::{InteractionMode, Selected, StudioGizmoConfig, TransformSpace, TransformType};
pub(super) fn update_spawns(
mut cmd: Commands,
@@ -24,6 +24,7 @@ pub(super) fn update(
mut q_gizmo: Query<(&mut Gizmo, &GlobalTransform)>,
q_camera_xform: Query<&Transform, With<Camera>>,
mode: Res<InteractionMode>,
config: Res<StudioGizmoConfig>,
) {
let Ok((mut gizmo, world_xform)) = q_gizmo.get_single_mut() else {
return;
@@ -41,7 +42,7 @@ pub(super) fn update(
};
for el in gizmo.0.iter_mut() {
el.update(mode.transform, &xform, &basis, cam_xform);
el.update(mode.transform, &xform, &basis, cam_xform, &config);
}
}
@@ -63,9 +64,9 @@ impl GizmoElement {
xform: &Transform,
basis: &Transform,
cam_xform: &Transform,
config: &StudioGizmoConfig,
) {
let dist = Vec3::distance(cam_xform.translation, xform.translation);
let base_size = dist / 20.;
let base_size = config.base_size_world_units(xform.translation, cam_xform.translation);
match self.control {
Control::Translation(axes) => match axes {