From dd08def18fc6bd22207fae215242b3e055c91c72 Mon Sep 17 00:00:00 2001 From: Treeco <5021038-Treeco@users.noreply.gitlab.com> Date: Thu, 23 Apr 2020 22:59:34 +0000 Subject: [PATCH] Add camera smoothing setting --- assets/voxygen/i18n/en.ron | 1 + voxygen/examples/character_renderer.rs | 5 +++- voxygen/src/hud/mod.rs | 4 +++ voxygen/src/hud/settings_window.rs | 35 +++++++++++++++++++++++++- voxygen/src/menu/char_selection/mod.rs | 1 + voxygen/src/scene/camera.rs | 16 +++++++----- voxygen/src/scene/mod.rs | 6 +++-- voxygen/src/scene/simple.rs | 4 ++- voxygen/src/session.rs | 7 +++++- voxygen/src/settings.rs | 2 ++ 10 files changed, 69 insertions(+), 12 deletions(-) diff --git a/assets/voxygen/i18n/en.ron b/assets/voxygen/i18n/en.ron index 219335f322..5659b9ad51 100644 --- a/assets/voxygen/i18n/en.ron +++ b/assets/voxygen/i18n/en.ron @@ -246,6 +246,7 @@ Enjoy your stay in the World of Veloren."#, "hud.settings.zoom_sensitivity": "Zoom Sensitivity", "hud.settings.invert_scroll_zoom": "Invert Scroll Zoom", "hud.settings.invert_mouse_y_axis": "Invert Mouse Y Axis", + "hud.settings.enable_mouse_smoothing": "Camera Smoothing", "hud.settings.free_look_behavior": "Free look behavior", "hud.settings.view_distance": "View Distance", diff --git a/voxygen/examples/character_renderer.rs b/voxygen/examples/character_renderer.rs index 289227bcb1..a26255d38d 100644 --- a/voxygen/examples/character_renderer.rs +++ b/voxygen/examples/character_renderer.rs @@ -53,10 +53,13 @@ fn main() { tick: 0, body: Some(body.clone()), gamma: 1.0, + mouse_smoothing: true, }; scene.camera_mut().set_focus_pos(Vec3::unit_z() * 0.8); scene.camera_mut().set_distance(1.5); - scene.camera_mut().update(0.0, 1.0 / 60.0); + scene + .camera_mut() + .update(0.0, 1.0 / 60.0, scene_data.mouse_smoothing); scene.maintain(&mut renderer, scene_data); // Render diff --git a/voxygen/src/hud/mod.rs b/voxygen/src/hud/mod.rs index 1f713de20e..cf1b4a174a 100644 --- a/voxygen/src/hud/mod.rs +++ b/voxygen/src/hud/mod.rs @@ -208,6 +208,7 @@ pub enum Event { AdjustMouseZoom(u32), ToggleZoomInvert(bool), ToggleMouseYInvert(bool), + ToggleSmoothPan(bool), AdjustViewDistance(u32), AdjustMusicVolume(f32), AdjustSfxVolume(f32), @@ -1794,6 +1795,9 @@ impl Hud { settings_window::Event::ToggleMouseYInvert(mouse_y_inverted) => { events.push(Event::ToggleMouseYInvert(mouse_y_inverted)); }, + settings_window::Event::ToggleSmoothPan(smooth_pan_enabled) => { + events.push(Event::ToggleSmoothPan(smooth_pan_enabled)); + }, settings_window::Event::AdjustViewDistance(view_distance) => { events.push(Event::AdjustViewDistance(view_distance)); }, diff --git a/voxygen/src/hud/settings_window.rs b/voxygen/src/hud/settings_window.rs index 8ead5fb68d..7f2e517c51 100644 --- a/voxygen/src/hud/settings_window.rs +++ b/voxygen/src/hud/settings_window.rs @@ -66,6 +66,8 @@ widget_ids! { mouse_zoom_invert_label, mouse_y_invert_button, mouse_y_invert_label, + smooth_pan_toggle_button, + smooth_pan_toggle_label, ch_title, ch_transp_slider, ch_transp_label, @@ -205,6 +207,7 @@ pub enum Event { AdjustMouseZoom(u32), ToggleZoomInvert(bool), ToggleMouseYInvert(bool), + ToggleSmoothPan(bool), AdjustViewDistance(u32), AdjustFOV(u16), AdjustGamma(f32), @@ -1239,7 +1242,7 @@ impl<'a> Widget for SettingsWindow<'a> { self.imgs.checkbox_checked, ) .w_h(18.0, 18.0) - .right_from(state.ids.mouse_zoom_invert_button, 250.0) + .right_from(state.ids.mouse_zoom_invert_button, 175.0) .hover_images(self.imgs.checkbox_mo, self.imgs.checkbox_checked_mo) .press_images(self.imgs.checkbox_press, self.imgs.checkbox_checked) .set(state.ids.mouse_y_invert_button, ui); @@ -1262,6 +1265,36 @@ impl<'a> Widget for SettingsWindow<'a> { .color(TEXT_COLOR) .set(state.ids.mouse_y_invert_label, ui); + // Mouse Smoothing Toggle + let smooth_pan_enabled = ToggleButton::new( + self.global_state.settings.gameplay.smooth_pan_enable, + self.imgs.checkbox, + self.imgs.checkbox_checked, + ) + .w_h(18.0, 18.0) + .right_from(state.ids.mouse_y_invert_button, 175.0) + .hover_images(self.imgs.checkbox_mo, self.imgs.checkbox_checked_mo) + .press_images(self.imgs.checkbox_press, self.imgs.checkbox_checked) + .set(state.ids.smooth_pan_toggle_button, ui); + + if self.global_state.settings.gameplay.smooth_pan_enable != smooth_pan_enabled { + events.push(Event::ToggleSmoothPan( + !self.global_state.settings.gameplay.smooth_pan_enable, + )); + } + + Text::new( + &self + .localized_strings + .get("hud.settings.enable_mouse_smoothing"), + ) + .right_from(state.ids.smooth_pan_toggle_button, 10.0) + .font_size(self.fonts.cyri.scale(14)) + .font_id(self.fonts.cyri.conrod_id) + .graphics_for(state.ids.smooth_pan_toggle_button) + .color(TEXT_COLOR) + .set(state.ids.smooth_pan_toggle_label, ui); + // Free look behaviour Text::new( &self diff --git a/voxygen/src/menu/char_selection/mod.rs b/voxygen/src/menu/char_selection/mod.rs index 3ca05435d4..c05eca5ad3 100644 --- a/voxygen/src/menu/char_selection/mod.rs +++ b/voxygen/src/menu/char_selection/mod.rs @@ -106,6 +106,7 @@ impl PlayState for CharSelectionState { tick: client.get_tick(), body: humanoid_body.clone(), gamma: global_state.settings.graphics.gamma, + mouse_smoothing: global_state.settings.gameplay.smooth_pan_enable, }; self.scene .maintain(global_state.window.renderer_mut(), scene_data); diff --git a/voxygen/src/scene/camera.rs b/voxygen/src/scene/camera.rs index 020263f035..2dc24fa000 100644 --- a/voxygen/src/scene/camera.rs +++ b/voxygen/src/scene/camera.rs @@ -190,7 +190,7 @@ impl Camera { /// Set the distance of the camera from the target (i.e., zoom). pub fn set_distance(&mut self, dist: f32) { self.tgt_dist = dist; } - pub fn update(&mut self, time: f64, dt: f32) { + pub fn update(&mut self, time: f64, dt: f32, smoothing_enabled: bool) { // This is horribly frame time dependent, but so is most of the game let delta = self.last_time.replace(time).map_or(0.0, |t| time - t); if (self.dist - self.tgt_dist).abs() > 0.01 { @@ -217,11 +217,15 @@ impl Camera { Lerp::lerp(a, b + *offs, rate) }; - self.set_ori_instant(Vec3::new( - lerp_angle(self.ori.x, self.tgt_ori.x, LERP_ORI_RATE * dt), - Lerp::lerp(self.ori.y, self.tgt_ori.y, LERP_ORI_RATE * dt), - lerp_angle(self.ori.z, self.tgt_ori.z, LERP_ORI_RATE * dt), - )); + if smoothing_enabled { + self.set_ori_instant(Vec3::new( + lerp_angle(self.ori.x, self.tgt_ori.x, LERP_ORI_RATE * dt), + Lerp::lerp(self.ori.y, self.tgt_ori.y, LERP_ORI_RATE * dt), + lerp_angle(self.ori.z, self.tgt_ori.z, LERP_ORI_RATE * dt), + )); + } else { + self.set_ori_instant(self.tgt_ori) + }; } pub fn interp_time(&self) -> f32 { diff --git a/voxygen/src/scene/mod.rs b/voxygen/src/scene/mod.rs index 8edf8e4635..f3b7771e2d 100644 --- a/voxygen/src/scene/mod.rs +++ b/voxygen/src/scene/mod.rs @@ -74,6 +74,8 @@ pub struct SceneData<'a> { pub view_distance: u32, pub tick: u64, pub thread_pool: &'a uvth::ThreadPool, + pub gamma: f32, + pub mouse_smoothing: bool, } impl Scene { @@ -177,7 +179,6 @@ impl Scene { renderer: &mut Renderer, audio: &mut AudioFrontend, scene_data: &SceneData, - gamma: f32, ) { // Get player position. let ecs = scene_data.state.ecs(); @@ -243,6 +244,7 @@ impl Scene { self.camera.update( scene_data.state.get_time(), scene_data.state.get_delta_time(), + scene_data.mouse_smoothing, ); // Compute camera matrices. @@ -349,7 +351,7 @@ impl Scene { .map(|b| b.kind()) .unwrap_or(BlockKind::Air), self.select_pos, - gamma, + scene_data.gamma, self.camera.get_mode(), )]) .expect("Failed to update global constants"); diff --git a/voxygen/src/scene/simple.rs b/voxygen/src/scene/simple.rs index 7a17754231..77c1dbcf0f 100644 --- a/voxygen/src/scene/simple.rs +++ b/voxygen/src/scene/simple.rs @@ -73,6 +73,7 @@ pub struct SceneData { pub tick: u64, pub body: Option, pub gamma: f32, + pub mouse_smoothing: bool, } impl Scene { @@ -146,7 +147,8 @@ impl Scene { } pub fn maintain(&mut self, renderer: &mut Renderer, scene_data: SceneData) { - self.camera.update(scene_data.time, 1.0 / 60.0); + self.camera + .update(scene_data.time, 1.0 / 60.0, scene_data.mouse_smoothing); self.camera.compute_dependents(&VoidVol); let camera::Dependents { diff --git a/voxygen/src/session.rs b/voxygen/src/session.rs index 564a2c375f..25b8f7e1df 100644 --- a/voxygen/src/session.rs +++ b/voxygen/src/session.rs @@ -579,6 +579,10 @@ impl PlayState for SessionState { global_state.settings.gameplay.mouse_y_inversion = mouse_y_inverted; global_state.settings.save_to_file_warn(); }, + HudEvent::ToggleSmoothPan(smooth_pan_enabled) => { + global_state.settings.gameplay.smooth_pan_enable = smooth_pan_enabled; + global_state.settings.save_to_file_warn(); + }, HudEvent::AdjustViewDistance(view_distance) => { self.client.borrow_mut().set_view_distance(view_distance); @@ -728,12 +732,13 @@ impl PlayState for SessionState { view_distance: client.view_distance().unwrap_or(1), tick: client.get_tick(), thread_pool: client.thread_pool(), + gamma: global_state.settings.graphics.gamma, + mouse_smoothing: global_state.settings.gameplay.smooth_pan_enable, }; self.scene.maintain( global_state.window.renderer_mut(), &mut global_state.audio, &scene_data, - global_state.settings.graphics.gamma, ); } diff --git a/voxygen/src/settings.rs b/voxygen/src/settings.rs index 899e85261d..11ac56aa6c 100644 --- a/voxygen/src/settings.rs +++ b/voxygen/src/settings.rs @@ -448,6 +448,7 @@ pub struct GameplaySettings { pub sct_player_batch: bool, pub sct_damage_batch: bool, pub mouse_y_inversion: bool, + pub smooth_pan_enable: bool, pub crosshair_transp: f32, pub chat_transp: f32, pub crosshair_type: CrosshairType, @@ -466,6 +467,7 @@ impl Default for GameplaySettings { zoom_sensitivity: 100, zoom_inversion: false, mouse_y_inversion: false, + smooth_pan_enable: true, toggle_debug: false, sct: true, sct_player_batch: true,