summaryrefslogtreecommitdiffstats
path: root/src/ui/bodies_window.rs
blob: 6300f5fce0c1eeae029a51b5f9a053221b532b70 (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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
use egui::Sense;

use crate::eguictx::EguiCtx;
use crate::ntree::{NTree, NTreeNode};
use crate::solar_system::body::{BodyId, OrbitalBody};
use crate::solar_system::{SolarSystem, SystemId};


#[derive(Default, Clone)]
pub struct BodiesWindowState
{
    last_system: Option<SystemId>,
    system_heirarchy: NTree<SystemId>,
    selected_body: Option<BodyId>
}

#[derive(Default, Clone)]
pub struct BodiesWindowAction
{
    pub focus_body: Option<BodyId>
}

impl BodiesWindowState
{
    fn build_system_heirarchy_rec(
        bodies: &[OrbitalBody],
        node: &mut NTreeNode<BodyId>)
    {
        for body in bodies {
            let orbit = body.get_orbit();
            match orbit {
                Some(orbit) => {
                    if orbit.parent() != *node.value() {
                        continue;
                    }
                },
                None => {
                    continue;
                }
            }
            let mut subnode = NTreeNode::new(body.id());
            BodiesWindowState::build_system_heirarchy_rec(bodies, &mut subnode);
            node.insert_node(subnode);
        }
    }

    fn rebuild_system_heirarchy(
        &mut self,
        star_system: &SolarSystem)
    {
        let mut root_node = NTreeNode::<BodyId>::new(0);

        let bodies = star_system.bodies();
        BodiesWindowState::build_system_heirarchy_rec(bodies, &mut root_node);
        self.system_heirarchy.set_root(root_node);
    }

    pub fn render(
        &mut self,
        current_system: &SolarSystem,
        eguictx: &EguiCtx)
    -> BodiesWindowAction
    {
        match self.last_system {
            Some(last_system) => {
                if last_system != current_system.id() {
                    self.rebuild_system_heirarchy(current_system);
                }
            },
            None => {
                self.rebuild_system_heirarchy(current_system);
            }
        }
        self.last_system = Some(current_system.id());

        let mut action = BodiesWindowAction::default();

        egui::Window::new("Bodies")
            .resizable(true)
            .show(eguictx.context(), |ui| {
            
            ui.horizontal(|ui| {
                self.paint_bodies_list(current_system, ui);
                self.paint_body_info_panel(&mut action, current_system, ui);
            });
        });
        action
    }

    fn paint_bodies_node_rec(
        &self,
        star_system: &SolarSystem,
        node: &NTreeNode<BodyId>,
        ui: &mut egui::Ui)
    -> Option<BodyId>
    {
        let body = star_system.body(*node.value());
        let children = node.children();

        let selected = self.selected_body.is_some_and(|v| { v == *node.value() });
        let mut new_selected = None;

        if children.is_empty() {
            if ui.selectable_label(selected, body.name()).clicked() {
                new_selected = Some(*node.value());
            }
        }else{
            egui::collapsing_header::CollapsingState::load_with_default_open(
                ui.ctx(),
                ui.make_persistent_id(format!("bodies_window_body_{}", body.id())),
                true)
            .show_header(ui, |ui| {
                if ui.selectable_label(selected, body.name()).clicked() {
                    new_selected = Some(*node.value());
                }
            })
            .body(|ui| {
                for child in children {
                    let child_selected = self.paint_bodies_node_rec(star_system, child, ui);
                    if child_selected.is_some() {
                        new_selected = child_selected;
                    }
                }
            });
        };
        new_selected
    }

    fn paint_bodies_list(
        &mut self,
        star_system: &SolarSystem,
        ui: &mut egui::Ui)
    -> Option<egui::Response>
    {
        let resp = egui::ScrollArea::vertical()
            .auto_shrink(true)
            .min_scrolled_height(200.0)
            .show(ui, |ui| {
                ui.vertical(|ui| {
                    let root = self.system_heirarchy.root();
                    let new_sel = self.paint_bodies_node_rec(star_system, root.as_ref().unwrap(), ui);
                    if new_sel.is_some() {
                        self.selected_body = new_sel;
                    }
            }).response
        });
        Some(resp.inner)
    }

    fn paint_body_info_panel(
        &mut self,
        action: &mut BodiesWindowAction,
        star_system: &SolarSystem,
        ui: &mut egui::Ui)
    {
        let selected_body = match self.selected_body {
            Some(id) => { star_system.body(id) },
            None => { return; }
        };

        ui.separator();
        egui::Frame::canvas(ui.style())
            .show(ui, |ui| {
            ui.set_width(200.0);
            ui.vertical_centered(|ui| {
                ui.label(
                    egui::RichText::new(selected_body.name())
                    .heading());
            });

            ui.horizontal(|ui| {
                let focus_resp = ui.button("Focus");

                if focus_resp.clicked() {
                    action.focus_body = Some(selected_body.id());
                }
            });
        });
    }
}