Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restore previous non-tracked camera pose on esc #1102

Merged
merged 4 commits into from
Feb 6, 2023
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions crates/re_viewer/src/ui/view_spatial/ui_3d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ pub struct View3DState {

/// Currently tracked camera.
tracked_camera: Option<InstancePath>,
/// Camera pose just before we took over another camera via [Self::tracked_camera].
camera_before_tracked_camera: Option<Eye>,

#[serde(skip)]
eye_interpolation: Option<EyeInterpolation>,
Expand Down Expand Up @@ -64,6 +66,7 @@ impl Default for View3DState {
Self {
orbit_eye: Default::default(),
tracked_camera: None,
camera_before_tracked_camera: None,
eye_interpolation: Default::default(),
hovered_point: Default::default(),
spin: false,
Expand All @@ -79,6 +82,7 @@ impl View3DState {
pub fn reset_camera(&mut self, scene_bbox_accum: &BoundingBox) {
self.interpolate_to_eye(default_eye(scene_bbox_accum, &self.space_specs).to_eye());
self.tracked_camera = None;
self.camera_before_tracked_camera = None;
}

fn update_eye(
Expand Down Expand Up @@ -274,6 +278,7 @@ pub fn view_3d(
state.state_3d.last_eye_interact_time = ui.input(|i| i.time);
state.state_3d.eye_interpolation = None;
state.state_3d.tracked_camera = None;
state.state_3d.camera_before_tracked_camera = None;
}

// TODO(andreas): This isn't part of the camera, but of the transform https://github.com/rerun-io/rerun/issues/753
Expand Down Expand Up @@ -377,10 +382,13 @@ pub fn view_3d(
// Double click changes camera
if response.double_clicked() {
state.state_3d.tracked_camera = None;
state.state_3d.camera_before_tracked_camera = None;

// While hovering an entity, focuses the camera on it.
if let Some(Selection::InstancePath(_, instance_path)) = ctx.hovered().first() {
if let Some(camera) = find_camera(&scene.space_cameras, &instance_path.hash()) {
state.state_3d.camera_before_tracked_camera =
state.state_3d.orbit_eye.map(|eye| eye.to_eye());
state.state_3d.interpolate_to_eye(camera);
state.state_3d.tracked_camera = Some(instance_path.clone());
} else if let Some(clicked_point) = state.state_3d.hovered_point {
Expand All @@ -400,6 +408,19 @@ pub fn view_3d(
}
}

// Allow to restore the camera state with escape if a camera was tracked before.
if response.hovered() && ui.input(|i| i.key_pressed(egui::Key::Escape)) {
if let Some(camera_before_changing_tracked_state) =
state.state_3d.camera_before_tracked_camera
{
state
.state_3d
.interpolate_to_eye(camera_before_changing_tracked_state);
state.state_3d.camera_before_tracked_camera = None;
state.state_3d.tracked_camera = None;
}
}

show_projections_from_2d_space(ctx, &mut scene, &state.scene_bbox_accum);

if state.state_3d.show_axes {
Expand Down