strafe-client/src/settings.rs

139 lines
4.2 KiB
Rust
Raw Normal View History

2023-09-27 09:12:20 +00:00
use crate::integer::{Ratio64,Ratio64Vec2};
#[derive(Clone)]
2023-10-10 00:09:24 +00:00
struct Ratio{
ratio:f64,
}
#[derive(Clone)]
2023-10-10 00:09:24 +00:00
enum DerivedFov{
FromScreenAspect,
FromAspect(Ratio),
}
#[derive(Clone)]
2023-10-10 00:09:24 +00:00
enum Fov{
Exactly{x:f64,y:f64},
2023-09-27 09:12:20 +00:00
SpecifyXDeriveY{x:f64,y:DerivedFov},
SpecifyYDeriveX{x:DerivedFov,y:f64},
2023-10-10 00:09:24 +00:00
}
impl Default for Fov{
2023-09-27 09:12:20 +00:00
fn default()->Self{
Fov::SpecifyYDeriveX{x:DerivedFov::FromScreenAspect,y:1.0}
2023-10-10 00:09:24 +00:00
}
}
#[derive(Clone)]
2023-09-27 09:12:20 +00:00
enum DerivedSensitivity{
FromRatio(Ratio64),
}
#[derive(Clone)]
2023-10-10 00:09:24 +00:00
enum Sensitivity{
2023-09-27 09:12:20 +00:00
Exactly{x:Ratio64,y:Ratio64},
SpecifyXDeriveY{x:Ratio64,y:DerivedSensitivity},
SpecifyYDeriveX{x:DerivedSensitivity,y:Ratio64},
2023-10-10 00:09:24 +00:00
}
impl Default for Sensitivity{
2023-09-27 09:12:20 +00:00
fn default()->Self{
Sensitivity::SpecifyXDeriveY{x:Ratio64::ONE*524288,y:DerivedSensitivity::FromRatio(Ratio64::ONE)}
2023-10-10 00:09:24 +00:00
}
}
#[derive(Default,Clone)]
2023-10-10 00:09:24 +00:00
pub struct UserSettings{
fov:Fov,
sensitivity:Sensitivity,
}
2023-10-10 01:29:25 +00:00
impl UserSettings{
pub fn calculate_fov(&self,zoom:f64,screen_size:&glam::UVec2)->glam::DVec2{
zoom*match &self.fov{
&Fov::Exactly{x,y}=>glam::dvec2(x,y),
2023-09-27 09:12:20 +00:00
Fov::SpecifyXDeriveY{x,y}=>match y{
2023-10-10 01:29:25 +00:00
DerivedFov::FromScreenAspect=>glam::dvec2(*x,x*(screen_size.y as f64/screen_size.x as f64)),
DerivedFov::FromAspect(ratio)=>glam::dvec2(*x,x*ratio.ratio),
},
2023-09-27 09:12:20 +00:00
Fov::SpecifyYDeriveX{x,y}=>match x{
DerivedFov::FromScreenAspect=>glam::dvec2(y*(screen_size.x as f64/screen_size.y as f64),*y),
DerivedFov::FromAspect(ratio)=>glam::dvec2(y*ratio.ratio,*y),
},
2023-10-10 01:29:25 +00:00
}
}
2023-09-27 09:12:20 +00:00
pub fn calculate_sensitivity(&self)->Ratio64Vec2{
2023-10-10 01:29:25 +00:00
match &self.sensitivity{
2023-09-27 09:12:20 +00:00
Sensitivity::Exactly{x,y}=>Ratio64Vec2::new(x.clone(),y.clone()),
Sensitivity::SpecifyXDeriveY{x,y}=>match y{
DerivedSensitivity::FromRatio(ratio)=>Ratio64Vec2::new(x.clone(),x.mul_ref(ratio)),
}
Sensitivity::SpecifyYDeriveX{x,y}=>match x{
DerivedSensitivity::FromRatio(ratio)=>Ratio64Vec2::new(y.mul_ref(ratio),y.clone()),
}
2023-10-10 01:29:25 +00:00
}
}
}
2023-10-10 00:09:24 +00:00
/*
//sensitivity is raw input dots (i.e. dpi = dots per inch) to radians conversion factor
sensitivity_x=0.001
sensitivity_y_from_x_ratio=1
Sensitivity::DeriveY{x:0.0.001,y:DerivedSensitivity{ratio:1.0}}
*/
pub fn read_user_settings()->UserSettings{
let mut cfg=configparser::ini::Ini::new();
if let Ok(_)=cfg.load("settings.conf"){
let (cfg_fov_x,cfg_fov_y)=(cfg.getfloat("camera","fov_x"),cfg.getfloat("camera","fov_y"));
let fov=match(cfg_fov_x,cfg_fov_y){
(Ok(Some(fov_x)),Ok(Some(fov_y)))=>Fov::Exactly {
x:fov_x,
y:fov_y
},
2023-09-27 09:12:20 +00:00
(Ok(Some(fov_x)),Ok(None))=>Fov::SpecifyXDeriveY{
2023-10-10 00:09:24 +00:00
x:fov_x,
y:if let Ok(Some(fov_y_from_x_ratio))=cfg.getfloat("camera","fov_y_from_x_ratio"){
DerivedFov::FromAspect(Ratio{ratio:fov_y_from_x_ratio})
}else{
DerivedFov::FromScreenAspect
}
},
2023-09-27 09:12:20 +00:00
(Ok(None),Ok(Some(fov_y)))=>Fov::SpecifyYDeriveX{
2023-10-10 00:09:24 +00:00
x:if let Ok(Some(fov_x_from_y_ratio))=cfg.getfloat("camera","fov_x_from_y_ratio"){
DerivedFov::FromAspect(Ratio{ratio:fov_x_from_y_ratio})
}else{
DerivedFov::FromScreenAspect
},
y:fov_y,
},
_=>{
Fov::default()
},
};
let (cfg_sensitivity_x,cfg_sensitivity_y)=(cfg.getfloat("camera","sensitivity_x"),cfg.getfloat("camera","sensitivity_y"));
let sensitivity=match(cfg_sensitivity_x,cfg_sensitivity_y){
(Ok(Some(sensitivity_x)),Ok(Some(sensitivity_y)))=>Sensitivity::Exactly {
2023-09-27 09:12:20 +00:00
x:Ratio64::try_from(sensitivity_x).unwrap(),
y:Ratio64::try_from(sensitivity_y).unwrap(),
2023-10-10 00:09:24 +00:00
},
2023-09-27 09:12:20 +00:00
(Ok(Some(sensitivity_x)),Ok(None))=>Sensitivity::SpecifyXDeriveY{
x:Ratio64::try_from(sensitivity_x).unwrap(),
y:if let Ok(Some(sensitivity_y_from_x_ratio))=cfg.getfloat("camera","sensitivity_y_from_x_ratio"){
DerivedSensitivity::FromRatio(Ratio64::try_from(sensitivity_y_from_x_ratio).unwrap())
}else{
DerivedSensitivity::FromRatio(Ratio64::ONE)
},
2023-10-10 00:09:24 +00:00
},
2023-09-27 09:12:20 +00:00
(Ok(None),Ok(Some(sensitivity_y)))=>Sensitivity::SpecifyYDeriveX{
x:if let Ok(Some(sensitivity_x_from_y_ratio))=cfg.getfloat("camera","sensitivity_x_from_y_ratio"){
DerivedSensitivity::FromRatio(Ratio64::try_from(sensitivity_x_from_y_ratio).unwrap())
}else{
DerivedSensitivity::FromRatio(Ratio64::ONE)
2023-10-10 00:09:24 +00:00
},
2023-09-27 09:12:20 +00:00
y:Ratio64::try_from(sensitivity_y).unwrap(),
2023-10-10 00:09:24 +00:00
},
_=>{
Sensitivity::default()
},
};
UserSettings{
fov,
sensitivity,
}
}else{
UserSettings::default()
}
}