summaryrefslogtreecommitdiffstats
path: root/src/ui.rs
blob: 73389a852f5ca943908b169b54e9df99496c8611 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
pub mod topbar;
pub mod bodies_window;
pub mod fleet_window;

use std::{borrow::Borrow, cell::RefCell};

use crate::GameState;
use crate::eguictx::EguiCtx;
use crate::solar_system::body::BodyId;
use crate::ui::bodies_window::BodiesWindowState;
use crate::ui::fleet_window::FleetWindowState;
use crate::ui::topbar::TopBarState;

#[derive(Default, Clone)]
pub struct State
{
    pub camera_target: Option<BodyId>,

    pub topbar_sate: TopBarState,
    pub bodies_window: BodiesWindowState,
    pub fleet_window: FleetWindowState
}

impl State
{
    pub fn paint(
        &mut self,
        game_state: &RefCell<GameState>,
        ui: &mut egui::Ui)
    {
        let mut game_state = game_state.borrow_mut();

        let topbar_action = self.topbar_sate.paint(
            ui,
            &game_state,
            &self.bodies_window,
            &self.fleet_window);

        if let Some(by) = topbar_action.advance_tick {
            game_state.timeman_mut().advance(by)
        }
        if topbar_action.toggle_bodies_window { self.bodies_window.open = !self.bodies_window.open; }
        if topbar_action.toggle_fleets_window { self.fleet_window.open = !self.fleet_window.open; }

        let current_system = match self.topbar_sate.current_system {
            Some(id) => &game_state.solar_systems()[id],
            None => return
        };

        let bodies_window_action =
            self.bodies_window.paint(ui, current_system);
        if bodies_window_action.focus_body.is_some() {
            self.camera_target = bodies_window_action.focus_body;
        }

        let fleet_window_action = 
            self.fleet_window.paint(
                ui, 
                game_state.borrow(), 
                &self.topbar_sate.current_system,
                &self.camera_target);

        if let Some(new_fleet) = fleet_window_action.new_fleet {
            game_state.new_fleet_from_ui(new_fleet);
        }
    }
}