use egui::Ui;
use crate::widget::scalar_field_view::FieldPick;
use crate::widget::stats_widget::format_g_python;
#[derive(Clone, Copy, Debug, Default)]
pub struct ScenePositionInfo {
last: Option<FieldPick>,
}
impl ScenePositionInfo {
pub fn new() -> Self {
Self::default()
}
pub fn set(&mut self, pick: Option<FieldPick>) {
self.last = pick;
}
pub fn clear(&mut self) {
self.last = None;
}
pub fn last(&self) -> Option<FieldPick> {
self.last
}
pub fn ui(&self, ui: &mut Ui) {
let (x, y, z) = match self.last {
Some(p) => (g(p.position.x), g(p.position.y), g(p.position.z)),
None => (dash(), dash(), dash()),
};
let data = match self.last.and_then(|p| p.value) {
Some(v) => g(v),
None => dash(),
};
let item = match self.last {
Some(p) => p.item.label().to_string(),
None => dash(),
};
ui.horizontal(|ui| {
ui.label(format!("X: {x}"));
ui.separator();
ui.label(format!("Y: {y}"));
ui.separator();
ui.label(format!("Z: {z}"));
ui.separator();
ui.label(format!("Data: {data}"));
ui.separator();
ui.label(format!("Item: {item}"));
});
}
}
fn dash() -> String {
"-".to_string()
}
fn g(v: f32) -> String {
format_g_python(f64::from(v), 6)
}
#[cfg(test)]
mod tests {
use super::g;
#[test]
fn g_rounds_to_six_significant_digits_like_python_g() {
let v = 0.123_456_79_f32;
assert_eq!(g(v), "0.123457");
assert_ne!(g(v), format!("{v}"));
}
#[test]
fn g_drops_trailing_zeros_like_python_g() {
assert_eq!(g(5.0), "5");
assert_eq!(g(1.5), "1.5");
assert_eq!(g(-0.25), "-0.25");
}
}