forked from 1b-agent/opencadstudio-upstream
feat(nav): turntable orbit, selection/cursor pivot, Shift+MMB rotate (#229)
Rework 3D view navigation: - Turntable / lock-horizon orbit: horizontal drag yaws about world +Z, vertical drag pitches about the horizontal camera-right axis, so the view never banks. Pitch is pole-clamped but the straight-down default view can still be tilted out of. - Orbit centre: the selection's centre when something is selected, otherwise the point under the cursor — no longer only the file centre. The camera revolves rigidly about the pivot, so it stays put on screen and the view doesn't jump. - Mouse scheme: Zoom = wheel, Pan = MMB, Rotate = Shift+MMB. Right-drag no longer orbits (right-click stays context menu / Enter). Floating viewports orbit with Shift+MMB too. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
1 parent
c303900bd2
commit
98aa051a74
6 files changed
+106
-82
No files matched your search
@@ -3370,6 +3370,42 @@ impl Scene {
|
||||
result
|
||||
}
|
||||
|
||||
/// The AABB centre of the current selection, in absolute world coordinates
|
||||
/// (same space as `Camera::target`) — the point the 3D view orbits around
|
||||
/// when something is selected. `None` when nothing is selected; the caller
|
||||
/// then orbits about the point under the cursor. (#229)
|
||||
pub fn orbit_pivot(&self) -> Option<glam::DVec3> {
|
||||
if self.selected.is_empty() {
|
||||
return None;
|
||||
}
|
||||
let block = self.current_layout_block_handle();
|
||||
let wires = self.wires_for_block_culled(block, None, None, None, None);
|
||||
let mut min = glam::DVec2::splat(f64::INFINITY);
|
||||
let mut max = glam::DVec2::splat(f64::NEG_INFINITY);
|
||||
let mut any = false;
|
||||
for wire in &wires {
|
||||
let Some(h) = Self::handle_from_wire_name(&wire.name) else {
|
||||
continue;
|
||||
};
|
||||
if !self.selected.contains(&h) {
|
||||
continue;
|
||||
}
|
||||
for &[x, y, _] in &wire.points {
|
||||
if x.is_finite() && y.is_finite() {
|
||||
min = min.min(glam::DVec2::new(x as f64, y as f64));
|
||||
max = max.max(glam::DVec2::new(x as f64, y as f64));
|
||||
any = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
if any {
|
||||
let c = (min + max) * 0.5;
|
||||
Some(glam::DVec3::new(c.x, c.y, 0.0))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
fn compute_model_space_extents(&self) -> Option<(glam::Vec3, glam::Vec3)> {
|
||||
let model_block = self.model_space_block_handle();
|
||||
if model_block.is_null() {
|
||||
|
||||
+2
-1
@@ -220,7 +220,8 @@ impl Scene {
|
||||
Some(c) => c,
|
||||
None => return,
|
||||
};
|
||||
cam.orbit(delta_x, delta_y);
|
||||
// Floating viewport orbits about its own target (no selection pivot).
|
||||
cam.orbit(delta_x, delta_y, None);
|
||||
// yaw_pitch_to_quat(y,p)*Z = (cos(p)*sin(y), -cos(p)*cos(y), sin(p))
|
||||
// `camera_for_viewport` reconstructs the rotation so that
|
||||
// `rotation * Z == view_direction` exactly (its `yaw = atan2(x, -y)`
|
||||
|
||||
@@ -32,6 +32,10 @@ pub struct SelectionState {
|
||||
pub right_press_time: Option<Instant>,
|
||||
pub right_dragging: bool,
|
||||
pub right_last_pos: Option<Point>,
|
||||
/// World point the current orbit drag revolves around (selection or model
|
||||
/// centre), captured when the drag starts so it stays fixed for the whole
|
||||
/// gesture. `None` when no orbit is in progress. (#229)
|
||||
pub orbit_pivot: Option<glam::DVec3>,
|
||||
/// While a command is active, a right-click acts as Enter; the *next*
|
||||
/// consecutive right-click opens the context menu instead. This tracks
|
||||
/// whether the previous right-click already fired Enter. Reset by any
|
||||
|
||||
+26
-16
@@ -11,7 +11,7 @@
|
||||
//
|
||||
// Coordinate convention: Z-up world space (same as the rest of OpenCADStudio).
|
||||
|
||||
use glam::{vec3, DVec3, Mat4, Quat, Vec3};
|
||||
use glam::{DVec3, Mat4, Quat, Vec3};
|
||||
use iced::{Point, Rectangle};
|
||||
|
||||
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
|
||||
@@ -246,29 +246,39 @@ impl Camera {
|
||||
// ── Navigation ────────────────────────────────────────────────────────
|
||||
|
||||
/// Arcball orbit: drag delta (dx, dy) in screen pixels.
|
||||
pub fn orbit(&mut self, delta_x: f32, delta_y: f32) {
|
||||
/// Orbit the view by a screen drag, turntable-style: horizontal drag yaws
|
||||
/// about world +Z, vertical drag pitches about the camera's (always
|
||||
/// horizontal) right axis — so the horizon never banks. `pivot` is the world
|
||||
/// point to revolve around (selection or model centre); `None` keeps the
|
||||
/// current target as the centre. (#229)
|
||||
pub fn orbit(&mut self, delta_x: f32, delta_y: f32, pivot: Option<DVec3>) {
|
||||
if delta_x.abs() < 1e-6 && delta_y.abs() < 1e-6 {
|
||||
return;
|
||||
}
|
||||
|
||||
let old_rot = self.rotation;
|
||||
let speed = 0.005_f32;
|
||||
let angle = (delta_x * delta_x + delta_y * delta_y).sqrt() * speed;
|
||||
|
||||
// Screen drag → rotation axis: right drag = rotate around cam_up (Y),
|
||||
// down drag = rotate around cam_right (X). Negate so drag direction
|
||||
// matches intuitive "grab and spin" arcball behaviour.
|
||||
let screen_axis = vec3(-delta_y, -delta_x, 0.0).normalize_or_zero();
|
||||
|
||||
let yaw = Quat::from_rotation_z(-delta_x * speed);
|
||||
self.rotation = (yaw * self.rotation).normalize();
|
||||
let cam_right = self.rotation * Vec3::X;
|
||||
let cam_up = self.rotation * Vec3::Y;
|
||||
let world_axis = (cam_right * screen_axis.x + cam_up * screen_axis.y).normalize_or_zero();
|
||||
|
||||
if world_axis.length_squared() < 1e-12 {
|
||||
return;
|
||||
let cur_z = (self.rotation * Vec3::Z).z;
|
||||
let pitched =
|
||||
(Quat::from_axis_angle(cam_right, -delta_y * speed) * self.rotation).normalize();
|
||||
let new_z = (pitched * Vec3::Z).z;
|
||||
// Accept the pitch while the gaze stays clear of the poles, OR when it
|
||||
// moves back AWAY from a pole — so the straight-down default view (z = 1)
|
||||
// can still be tilted out of (a hard `< 0.9995` gate alone would freeze
|
||||
// small drags there). Only a step deeper INTO a pole is rejected.
|
||||
if new_z.abs() < 0.9995 || new_z.abs() < cur_z.abs() {
|
||||
self.rotation = pitched;
|
||||
}
|
||||
|
||||
let delta_rot = Quat::from_axis_angle(world_axis, angle);
|
||||
self.rotation = (delta_rot * self.rotation).normalize();
|
||||
// Revolve the target around the pivot by the same rotation delta so the
|
||||
// view orbits the chosen centre instead of the fixed file centre (#229).
|
||||
if let Some(p) = pivot {
|
||||
let delta = self.rotation * old_rot.conjugate();
|
||||
self.target = p + (delta * (self.target - p).as_vec3()).as_dvec3();
|
||||
}
|
||||
|
||||
// Sync legacy yaw/pitch for hit-test functions.
|
||||
self.sync_yaw_pitch();
|
||||
|
||||
Reference in new issue
Block a user