location.rs (1316B)
1 use std::{fmt::Display, rc::Rc}; 2 3 use xplm::data::{borrowed::DataRef, DataRead, ReadOnly}; 4 5 use crate::error::Error; 6 7 pub(crate) struct LocationRefs { 8 pub(crate) latitude: DataRef<f64, ReadOnly>, 9 pub(crate) longitude: DataRef<f64, ReadOnly>, 10 pub(crate) altitude: DataRef<f32, ReadOnly>, 11 } 12 13 #[derive(Clone)] 14 pub(crate) struct Location { 15 inner: Rc<LocationRefs>, 16 } 17 18 impl Location { 19 pub(crate) fn new() -> Result<Self, Error> { 20 Ok(Self { 21 inner: Rc::new(LocationRefs { 22 latitude: DataRef::find("sim/flightmodel/position/latitude")?, 23 longitude: DataRef::find("sim/flightmodel/position/longitude")?, 24 altitude: DataRef::find("sim/cockpit2/gauges/indicators/altitude_ft_pilot")?, 25 }), 26 }) 27 } 28 29 pub(crate) fn lat(&self) -> f64 { 30 self.inner.latitude.get() 31 } 32 pub(crate) fn lon(&self) -> f64 { 33 self.inner.longitude.get() 34 } 35 pub(crate) fn alt(&self) -> f64 { 36 self.inner.altitude.get() as f64 37 } 38 } 39 40 impl Display for Location { 41 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { 42 write!( 43 f, 44 "Latitude: {}, Longitude: {}, Altitude: {}", 45 self.lat(), 46 self.lon(), 47 self.alt() 48 ) 49 } 50 }