From 759d5d27c7773c7fe8b165ce08b57204db990b74 Mon Sep 17 00:00:00 2001 From: Jon Santmyer Date: Sun, 3 May 2026 17:31:47 -0400 Subject: move topbar ui rendering into it's own module --- src/solar_system.rs | 7 ++-- src/ui.rs | 91 +++++---------------------------------------------- src/ui/camera_info.rs | 5 +-- src/ui/topbar.rs | 91 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/window.rs | 15 +++++---- 5 files changed, 113 insertions(+), 96 deletions(-) create mode 100644 src/ui/topbar.rs (limited to 'src') diff --git a/src/solar_system.rs b/src/solar_system.rs index cf6a36a..df1d246 100644 --- a/src/solar_system.rs +++ b/src/solar_system.rs @@ -1,6 +1,6 @@ use serde::{Deserialize}; -use crate::{GameState, known_stars::{KNOWN_STARS, StarNotFoundError}, timeman::{Second, TimeMan}}; -use std::{cell::RefCell, error::Error, f64::consts::PI}; +use crate::{known_stars::*, timeman::Second}; +use std::error::Error; const GRAVITATIONAL_CONSTANT: f64 = 6.67408e-20; @@ -33,7 +33,6 @@ pub struct SerialOrbitalBody pub struct OrbitalBody { body: SerialOrbitalBody, - id: BodyId, position: Option> } @@ -71,7 +70,6 @@ impl SolarSystem bodies.push(OrbitalBody { body: record, - id: bodies.len(), position: None }); } @@ -148,7 +146,6 @@ impl SolarSystem impl OrbitalBody { - pub fn id(&self) -> BodyId { self.id } pub fn name(&self) -> &String { &self.body.name } pub fn radius(&self) -> f32 { self.body.radius as f32 } pub fn position(&self) -> cgmath::Vector3 { self.position.unwrap() } diff --git a/src/ui.rs b/src/ui.rs index 42e5fa8..ab75001 100644 --- a/src/ui.rs +++ b/src/ui.rs @@ -1,10 +1,11 @@ +pub mod topbar; pub mod camera_info; use std::cell::RefCell; use cgmath::Vector3; -use crate::{GameState, eguictx::EguiCtx, solar_system, timeman::{self, Second, TimeMan}, ui::camera_info::CameraWindowState}; +use crate::{GameState, eguictx::EguiCtx, solar_system, timeman::{self, Second, TimeMan}, ui::{camera_info::CameraWindowState, topbar::TopBarState}}; mod ui { pub use super::camera_info; @@ -13,10 +14,7 @@ mod ui { #[derive(Default, Clone)] pub struct State { - pub current_system: Option, - pub auto_time: Option, - pub do_auto_tick: bool, - + pub topbar_sate: TopBarState, pub camera_info: CameraWindowState } @@ -27,87 +25,14 @@ impl State game_state: &RefCell, eguictx: &EguiCtx) { - State::render_topbar(self, game_state, eguictx); - + TopBarState::render( + &mut self.topbar_sate, + game_state, + eguictx); + CameraWindowState::render( self, game_state, eguictx); } - - fn render_topbar( - state: &mut State, - game_state: &RefCell, - eguictx: &EguiCtx) - { - let mut game_state = game_state.borrow_mut(); - - egui::TopBottomPanel::top("topbar").show( - eguictx.context(), - |ui| { - - ui.horizontal(|ui| { - let solar_systems = game_state.solar_systems(); - let selected_system_label = match state.current_system { - Some(id) => solar_systems[id].name(), - None => "" - }; - - egui::ComboBox::from_label("Current System") - .selected_text(selected_system_label) - .show_ui(ui, |ui| { - - for (i, system) in solar_systems.iter().enumerate() { - ui.selectable_value( - &mut state.current_system, - Some(i), - system.name() - ); - } - }); - }); - - ui.horizontal(|ui| { - let time_str = TimeMan::format_duration(game_state.timeman().seconds()); - ui.label( - egui::RichText::new(format!("Time: {: >16}", time_str)) - .font(egui::FontId::monospace(12.0)) - ); - - let button_seconds = [ - 1, - 5, - 30, - timeman::MINUTE, - timeman::MINUTE * 5, - timeman::MINUTE * 30, - timeman::HOUR, - timeman::DAY, - timeman::DAY * 5, - timeman::YEAR - ]; - let selected_button = state.auto_time; - - button_seconds.iter().for_each(|&seconds| { - ui.vertical(|ui| { - let auto_selected = match selected_button { - Some(o) => o == seconds, - None => false - }; - let label = TimeMan::format_duration(seconds); - - if ui.button(label.clone()).clicked() { - game_state.timeman_mut().advance(seconds); - } - - if ui.add(egui::Button::new(label.clone()).selected(auto_selected)).clicked() { - state.auto_time = Some(seconds); - } - }); - }); - - ui.checkbox(&mut state.do_auto_tick, "Auto"); - }); - }); - } } diff --git a/src/ui/camera_info.rs b/src/ui/camera_info.rs index ccd06c5..a7bc80b 100644 --- a/src/ui/camera_info.rs +++ b/src/ui/camera_info.rs @@ -19,14 +19,15 @@ impl CameraWindowState game_state: &RefCell, eguictx: &EguiCtx) { - if ui_state.current_system.is_none() { + let topbar_state = &ui_state.topbar_sate; + if topbar_state.current_system.is_none() { return; } let camera_state = &mut ui_state.camera_info; let game_state = game_state.borrow(); - let current_system = &game_state.solar_systems()[ui_state.current_system.unwrap()]; + let current_system = &game_state.solar_systems()[topbar_state.current_system.unwrap()]; egui::Window::new("Debug Camera Info") .title_bar(false) diff --git a/src/ui/topbar.rs b/src/ui/topbar.rs new file mode 100644 index 0000000..07c20a1 --- /dev/null +++ b/src/ui/topbar.rs @@ -0,0 +1,91 @@ +use std::cell::RefCell; + +use crate::{GameState, eguictx::EguiCtx, solar_system::{SolarSystem, SystemId}, timeman::{self, Second, TimeMan}}; + +#[derive(Default, Clone)] +pub struct TopBarState +{ + pub current_system: Option, + pub auto_tick: Option, + pub do_auto_tick: bool +} + +impl TopBarState +{ + pub fn render( + state: &mut TopBarState, + game_state: &RefCell, + eguictx: &EguiCtx) + { + let mut game_state = game_state.borrow_mut(); + + egui::TopBottomPanel::top("topbar").show( + eguictx.context(), + |ui| { + + ui.horizontal(|ui| { + let solar_systems = game_state.solar_systems(); + let selected_system_label = match state.current_system { + Some(id) => solar_systems[id].name(), + None => "" + }; + + egui::ComboBox::from_label("Current System") + .selected_text(selected_system_label) + .show_ui(ui, |ui| { + + for (i, system) in solar_systems.iter().enumerate() { + ui.selectable_value( + &mut state.current_system, + Some(i), + system.name() + ); + } + }); + + let button_seconds = [ + 1, + 5, + 30, + timeman::MINUTE, + timeman::MINUTE * 5, + timeman::MINUTE * 30, + timeman::HOUR, + timeman::DAY, + timeman::DAY * 5, + timeman::YEAR + ]; + let selected_button = state.auto_tick; + + let timeman = game_state.timeman_mut(); + button_seconds.iter().for_each(|&seconds| { + ui.vertical(|ui| { + let auto_selected = match selected_button { + Some(o) => o == seconds, + None => false + }; + let label = TimeMan::format_duration(seconds); + + if ui.button(label.clone()).clicked() { + timeman.advance(seconds); + } + + if ui.add(egui::Button::new(label.clone()).selected(auto_selected)).clicked() { + state.auto_tick = Some(seconds); + } + }); + }); + + ui.checkbox(&mut state.do_auto_tick, "Auto"); + }); + ui.vertical_centered_justified(|ui| { + let timeman = game_state.timeman_mut(); + let time_str = TimeMan::format_duration(timeman.seconds()); + ui.label( + egui::RichText::new(time_str) + .font(egui::FontId::monospace(16.0)) + ); + }); + }); + } +} diff --git a/src/window.rs b/src/window.rs index ce1b555..c64d653 100644 --- a/src/window.rs +++ b/src/window.rs @@ -44,7 +44,10 @@ impl GameWindow winit::dpi::LogicalSize::new(1.0, 1.0)); let ui_state = ui::State{ - current_system: Some(0), + topbar_sate: ui::topbar::TopBarState { + current_system : Some(0), + ..Default::default() + }, camera_info: ui::camera_info::CameraWindowState { target: Some(0), ..Default::default() @@ -68,13 +71,13 @@ impl GameWindow dt: Duration) { let mut game_state = game_state.borrow_mut(); - if self.ui_state.do_auto_tick { - game_state.timeman.auto_tick = self.ui_state.auto_time; + if self.ui_state.topbar_sate.do_auto_tick { + game_state.timeman.auto_tick = self.ui_state.topbar_sate.auto_tick; }else{ game_state.timeman.auto_tick = None; } - let current_system = match self.ui_state.current_system { + let current_system = match self.ui_state.topbar_sate.current_system { Some(id) => &game_state.solar_systems()[id], None => { return; } }; @@ -98,9 +101,9 @@ impl GameWindow return Ok(()); } - if self.ui_state.current_system.is_some() { + if self.ui_state.topbar_sate.current_system.is_some() { let game_state = game_state.borrow(); - let current_system = &game_state.solar_systems()[self.ui_state.current_system.unwrap()]; + let current_system = &game_state.solar_systems()[self.ui_state.topbar_sate.current_system.unwrap()]; self.tactical_map.draw( &self.wgpuctx, -- cgit v1.2.3