summaryrefslogtreecommitdiffstats
path: root/src/ui.rs
blob: fd40ac9f17f77f68cac10ca050ad111faa7ba986 (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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
pub mod contact;
mod current_system_widget;
mod contacts_widget;
mod fleets_widget;
mod schedule_widget;
mod time_control_widget;

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

use crate::GameState;
use crate::fleet::FleetsManager;
use crate::solar_system::{Kilometers, SolarSystem, SystemId};
use crate::timeman::Second;
use crate::ui::contact::MapContact;
use crate::ui::contacts_widget::{ContactsListWidget, FocusedContactWidget};
use crate::ui::current_system_widget::CurrentSystemWidget;
use crate::ui::fleets_widget::{FleetsControlPanel, FleetsListWidget};
use crate::ui::time_control_widget::TimeControlWidget;

#[derive(Default, Eq, PartialEq)]
pub enum LeftPanelMenuMode 
{
    #[default]
    Contacts,
    Fleets
}

#[derive(Default)]
pub struct State
{
    pub current_system: Option<SystemId>,
    pub camera_target: Option<MapContact>,
    pub selected_contact: Option<MapContact>,

    pub manual_tick: Second,
    pub auto_tick: (bool, Second),

    pub left_panel_menu: LeftPanelMenuMode,
    pub fleets_control_panel: FleetsControlPanel
}

pub fn set_theme(ctx: &egui::Context)
{
    ctx.set_theme(egui::Theme::Dark);
    ctx.style_mut_of(egui::Theme::Dark, |style| {
        let bg_color = egui::Color32::from_gray(64);
        style.visuals.panel_fill = bg_color;
        style.visuals.window_fill =bg_color;

        style.visuals.window_stroke = egui::Stroke::NONE;
        
        style.visuals.menu_corner_radius = egui::CornerRadius::ZERO;
        style.visuals.widgets.noninteractive.corner_radius = egui::CornerRadius::ZERO;
        style.visuals.widgets.inactive.corner_radius = egui::CornerRadius::ZERO;
        style.visuals.widgets.hovered.corner_radius = egui::CornerRadius::ZERO;
        style.visuals.widgets.active.corner_radius = egui::CornerRadius::ZERO;
        style.visuals.widgets.open.corner_radius = egui::CornerRadius::ZERO;

        style.visuals.widgets.hovered.bg_fill = egui::Color32::from_gray(128);

        style.visuals.widgets.inactive.bg_fill = egui::Color32::from_gray(96);
        style.visuals.widgets.inactive.weak_bg_fill = egui::Color32::from_gray(96);
        style.visuals.widgets.inactive.fg_stroke = egui::Stroke::new(2.0, egui::Color32::from_gray(196));
        
        style.visuals.widgets.active.bg_fill = egui::Color32::from_gray(128);
        style.visuals.widgets.active.fg_stroke = egui::Stroke::new(2.0, egui::Color32::from_gray(255));

        style.visuals.selection.bg_fill = egui::Color32::from_gray(96);
        style.visuals.selection.stroke.color = egui::Color32::WHITE;
        style.visuals.override_text_color = Some(egui::Color32::WHITE);
        style.override_text_style = Some(egui::TextStyle::Monospace);

        style.spacing.window_margin = egui::Margin::same(12);

        style.visuals.widgets.noninteractive.bg_stroke = egui::Stroke::NONE;

        style.interaction.selectable_labels = false;
    });
}

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

        egui::Panel::top("Top panel")
            .resizable(false)
            .show_inside(ui, 
        |panel_ui| {
            self.show_topbar(&mut game_state.borrow_mut(), panel_ui);
        });

        let remaining_rect = ui.max_rect();
        let square_size = remaining_rect.height();
        let panel_width = ((remaining_rect.width() - square_size) / 2.0).floor().max(200.0);
        
        egui::Panel::left("Left panel")
            .resizable(false)
            .exact_size(panel_width)
            .show_inside(ui,
        |panel_ui| {
            self.show_left_panel(&mut game_state.borrow_mut(), panel_ui);
        });

        egui::Panel::right("Right panel")
            .resizable(false)
            .exact_size(panel_width)
            .show_inside(ui,
        |panel_ui| {

        });
}

    pub fn show_topbar(
        &mut self,
        game_state: &mut GameState,
        ui: &mut egui::Ui)
    {
        ui.horizontal(|horizontal| {
            let star_systems = game_state.solar_systems();
            let fleets_man = game_state.fleets();

            horizontal.add(CurrentSystemWidget::new(star_systems, &mut self.current_system));

            if let Some(system_id) = self.current_system {
                let current_system = &star_systems[system_id];

                horizontal.add(egui::Separator::default().spacing(12.0));
                horizontal.add(FocusedContactWidget::new(
                        self.camera_target.clone(), 
                        current_system, fleets_man));
            }

            //Horrible hack to allow for right-alignment.
            //I chose to use EGUI so i'll stick with EGUI!
            horizontal.add_space(horizontal.available_width()-224.0);
            let tcw = TimeControlWidget::new(&mut self.manual_tick, &mut self.auto_tick)
                .show_ui(horizontal);

            if let Some(tick) = tcw.inner {
                game_state.timeman_mut().advance(tick);
            }
        });
    }

    pub fn show_left_panel(
        &mut self,
        game_state: &mut GameState,
        ui: &mut egui::Ui)
    {
        if self.current_system.is_none() {
            return;
        }

        ui.vertical(|vertical| {
            let star_systems = game_state.solar_systems();
            let fleets = game_state.fleets();
            let current_system = &star_systems[self.current_system.unwrap()];
            
            vertical.horizontal(|horizontal| {
                if horizontal.selectable_label(
                        self.left_panel_menu == LeftPanelMenuMode::Contacts,
                        "Contacts")
                    .clicked() {
                    self.left_panel_menu = LeftPanelMenuMode::Contacts;
                }
                if horizontal.selectable_label(
                        self.left_panel_menu == LeftPanelMenuMode::Fleets,
                        "Fleets")
                    .clicked() {
                    self.left_panel_menu = LeftPanelMenuMode::Fleets;
                }
            });

            match self.left_panel_menu {
                LeftPanelMenuMode::Contacts => {
                    let camera_refocus = ContactsListWidget::new(
                        &mut self.selected_contact,
                        current_system,
                        fleets)
                        .show_ui(vertical);
                    if camera_refocus.inner.is_some() {
                        self.camera_target = camera_refocus.inner;
                    }
                },
                LeftPanelMenuMode::Fleets => {
                    let flw_resp = 
                        FleetsListWidget::new(fleets, &mut self.selected_contact)
                        .show_ui(vertical);


                    let fcp_resp = self.fleets_control_panel.show(
                        &self.selected_contact,
                        fleets, current_system,
                        vertical);

                    if let Some(refocus) = flw_resp.refocus {
                        self.current_system = Some(refocus.0);
                        self.camera_target = Some(refocus.1);
                    }

                    if let Some(new_fleet) = fcp_resp.new_fleet {
                        game_state.new_fleet_from_modal(new_fleet, current_system.id(), self.selected_contact);
                    }
                }
            };
        });
    }
}