Skip to content

Commit

Permalink
Re-add focus shortcut (F)
Browse files Browse the repository at this point in the history
  • Loading branch information
cohaereo committed Sep 3, 2024
1 parent 7f1e274 commit a84adce
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 15 deletions.
8 changes: 8 additions & 0 deletions crates/alkahest-renderer/src/camera/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,14 @@ impl Camera {
// pub fn look_at(&mut self, target: Vec3) {
// self.controller.look_at(target);
// }

pub fn fov(&self) -> f32 {
match &self.projection {
CameraProjection::Perspective { fov, .. }
| CameraProjection::PerspectiveBounded { fov, .. } => *fov,
CameraProjection::Orthographic { .. } => 90.0,
}
}
}

impl View for Camera {
Expand Down
12 changes: 7 additions & 5 deletions crates/alkahest/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use alkahest_renderer::{
Scene,
},
gpu::{texture::LOW_RES, GpuContext},
gpu_event,
gpu_event, gpu_profile_event,
input::InputState,
renderer::{Renderer, RendererShared},
};
Expand Down Expand Up @@ -277,7 +277,11 @@ impl AlkahestApp {
WindowEvent::RedrawRequested => {
resources.get_mut::<SelectedEntity>().changed_this_frame = false;
renderer.data.lock().asset_manager.poll();

gctx.begin_frame();

{
gpu_profile_event!(gctx, "main");
if gui.input_mut(|i| {
i.consume_shortcut(&KeyboardShortcut::new(
Modifiers::ALT,
Expand All @@ -299,8 +303,6 @@ impl AlkahestApp {
});
}

gctx.begin_frame();

{
let mut action_list = resources.get_mut::<ActionList>();
action_list.process(resources);
Expand Down Expand Up @@ -388,9 +390,9 @@ impl AlkahestApp {

renderer
.gpu
.begin_event("interface_and_hud", "")
.begin_event_span("interface_and_hud", "")
.scoped(|| {
gpu_event!(renderer.gpu, "egui");
gpu_profile_event!(renderer.gpu, "egui");
gui.draw_frame(window, |ctx, ectx| {
hotkeys::process_hotkeys(ectx, resources);

Expand Down
8 changes: 5 additions & 3 deletions crates/alkahest/src/gui/configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@ impl GuiView for RenderSettingsPanel {
.ui(ui);
});
});
// ui.checkbox(&mut c.renderer.depth_prepass, "⚠ Depth Prepass");

render_feat_vis(ui, "Crosshair", &mut c.visual.draw_crosshair);
render_feat_vis(ui, "Node Visualization", &mut c.visual.node_nametags);
ui.collapsing("Node filters", |ui| {
Expand Down Expand Up @@ -178,11 +180,11 @@ impl GuiView for RenderSettingsPanel {
"Trees/Decorators",
&mut c.renderer.feature_decorators,
);
render_feat_vis(ui, "Atmosphere", &mut c.renderer.feature_atmosphere);
render_feat_vis(ui, "Cubemaps", &mut c.renderer.feature_cubemaps);
render_feat_vis(ui, "Atmosphere", &mut c.renderer.feature_atmosphere);
render_feat_vis(ui, "Cubemaps", &mut c.renderer.feature_cubemaps);
render_feat_vis(
ui,
"Global Lighting",
"Global Lighting",
&mut c.renderer.feature_global_lighting,
);
});
Expand Down
72 changes: 65 additions & 7 deletions crates/alkahest/src/gui/hotkeys.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
use alkahest_data::occlusion::Aabb;
use alkahest_renderer::{
camera::{tween::ease_out_exponential, Camera},
ecs::{hierarchy::Parent, resources::SelectedEntity, visibility::Visibility, Scene},
camera::{
tween::{ease_out_exponential, Tween},
Camera,
},
ecs::{
hierarchy::Parent, resources::SelectedEntity, transform::Transform, visibility::Visibility,
Scene,
},
renderer::RendererShared,
};
use bevy_ecs::entity::Entity;
use glam::Vec3;
use rustc_hash::FxHashSet;

use crate::{
Expand All @@ -29,8 +37,8 @@ pub const SHORTCUT_DESELECT: egui::KeyboardShortcut = egui::KeyboardShortcut::ne
egui::Key::A,
);

// pub const SHORTCUT_FOCUS: egui::KeyboardShortcut =
// egui::KeyboardShortcut::new(egui::Modifiers::NONE, egui::Key::F);
pub const SHORTCUT_FOCUS: egui::KeyboardShortcut =
egui::KeyboardShortcut::new(egui::Modifiers::NONE, egui::Key::F);

pub const SHORTCUT_GAZE: egui::KeyboardShortcut =
egui::KeyboardShortcut::new(egui::Modifiers::NONE, egui::Key::G);
Expand Down Expand Up @@ -75,6 +83,57 @@ pub fn process_hotkeys(ctx: &egui::Context, resources: &mut AppResources) {
if ctx.input_mut(|i| i.consume_shortcut(&SHORTCUT_GAZE)) {
goto_gaze(resources);
}

if ctx.input_mut(|i| i.consume_shortcut(&SHORTCUT_FOCUS)) {
focus_selected(resources);
}
}

fn focus_selected(resources: &mut AppResources) {
let mut maps = resources.get_mut::<MapList>();
let Some(map) = maps.current_map_mut() else {
return;
};

let selected_entity = resources.get::<SelectedEntity>().selected();
let Some(selected_entity) = selected_entity else {
return;
};

let mut cam = resources.get_mut::<Camera>();
let bounds = map.scene.get::<Aabb>(selected_entity).cloned();

let (center, radius) = if let Some(transform) = map.scene.get::<Transform>(selected_entity) {
if let Some(bounds) = bounds {
(
transform.local_to_world().transform_point3(bounds.center()),
bounds.radius(),
)
} else {
(transform.translation, 1.0)
}
} else {
if let Some(bounds) = bounds {
(bounds.center(), bounds.radius())
} else {
return;
}
};

// Calculate the vertical field of view in radians
let half_fov_y = (cam.fov() * 0.5).to_radians();

// Calculate the distance required to fit the sphere in the frustum vertically
let distance = radius / half_fov_y.tan();

// Adjust the camera's position to ensure the sphere fits in the frustum
let target_position = center - cam.forward().normalize() * (distance * 1.75);
cam.tween = Some(Tween::new(
ease_out_exponential,
Some((cam.position(), target_position)),
None,
0.5,
));
}

fn hide_unselected(resources: &mut AppResources) {
Expand All @@ -84,10 +143,9 @@ fn hide_unselected(resources: &mut AppResources) {
};

let selected_entity = resources.get::<SelectedEntity>().selected();
if selected_entity.is_none() {
let Some(selected_entity) = selected_entity else {
return;
}
let selected_entity = selected_entity.unwrap();
};

let entity_parents: FxHashSet<Entity> = get_ancestors(&map.scene, selected_entity)
.into_iter()
Expand Down

0 comments on commit a84adce

Please sign in to comment.