diff --git a/src/gui.rs b/src/gui.rs index a355cc8..bb70084 100644 --- a/src/gui.rs +++ b/src/gui.rs @@ -114,29 +114,6 @@ impl Sam { self.tab = GuiTab::Configure; } - let shortcut_mode = if ctx.input_mut(|i| i.consume_key(Modifiers::SHIFT, Key::F5)) { - Some(FlightMode::Idle) - } else if ctx.input_mut(|i| i.consume_key(Modifiers::SHIFT, Key::F6)) { - Some(FlightMode::HardwareArmed) - } else if ctx.input_mut(|i| i.consume_key(Modifiers::SHIFT, Key::F7)) { - Some(FlightMode::Armed) - } else if ctx.input_mut(|i| i.consume_key(Modifiers::SHIFT, Key::F8)) { - Some(FlightMode::Flight) - } else if ctx.input_mut(|i| i.consume_key(Modifiers::SHIFT, Key::F9)) { - Some(FlightMode::RecoveryDrogue) - } else if ctx.input_mut(|i| i.consume_key(Modifiers::SHIFT, Key::F10)) { - Some(FlightMode::RecoveryMain) - } else if ctx.input_mut(|i| i.consume_key(Modifiers::SHIFT, Key::F11)) { - Some(FlightMode::Landed) - } else { - None - }; - - // Set new flight mode if keyboard shortcut was used - if let Some(fm) = shortcut_mode { - self.data_source_mut().send_command(Command::SetFlightMode(fm)).unwrap(); - } - // Redefine text_styles let colors = ThemeColors::new(ctx); let mut style = (*ctx.style()).clone(); diff --git a/src/gui/top_bar.rs b/src/gui/top_bar.rs index bd3a4a8..21b293f 100644 --- a/src/gui/top_bar.rs +++ b/src/gui/top_bar.rs @@ -3,7 +3,7 @@ use eframe::egui; use egui::widgets::{Button, ProgressBar}; -use egui::{Color32, Label, RichText, SelectableLabel, Stroke, Rect, Vec2}; +use egui::{Color32, Label, RichText, SelectableLabel, Stroke, Rect, Vec2, Key, Modifiers}; use mithril::telemetry::*; @@ -11,16 +11,16 @@ use crate::data_source::*; use crate::telemetry_ext::*; // TODO: move to telemetry_ext? -fn flight_mode_style(fm: FlightMode) -> (&'static str, &'static str, Color32, Color32) { +fn flight_mode_style(fm: FlightMode) -> (&'static str, Color32, Color32) { let fg = Color32::from_rgb(0x28, 0x28, 0x28); match fm { - FlightMode::Idle => ("IDLE", "F5", fg, fm.color()), - FlightMode::HardwareArmed => ("HWARMED", "F6", fg, fm.color()), - FlightMode::Armed => ("ARMED", "F7", fg, fm.color()), - FlightMode::Flight => ("FLIGHT", "F8", fg, fm.color()), - FlightMode::RecoveryDrogue => ("DROGUE", "F9", fg, fm.color()), - FlightMode::RecoveryMain => ("MAIN", "F10", fg, fm.color()), - FlightMode::Landed => ("LANDED", "F11", fg, fm.color()), + FlightMode::Idle => ("IDLE", fg, fm.color()), + FlightMode::HardwareArmed => ("HWARMED", fg, fm.color()), + FlightMode::Armed => ("ARMED", fg, fm.color()), + FlightMode::Flight => ("FLIGHT", fg, fm.color()), + FlightMode::RecoveryDrogue => ("DROGUE", fg, fm.color()), + FlightMode::RecoveryMain => ("MAIN", fg, fm.color()), + FlightMode::Landed => ("LANDED", fg, fm.color()), } } @@ -47,6 +47,7 @@ pub trait TopBarUiExt { fm: FlightMode, current: Option, data_source: &mut dyn DataSource, + shortcut: Key, ); fn flight_mode_buttons(&mut self, current: Option, data_source: &mut dyn DataSource); } @@ -174,10 +175,15 @@ impl TopBarUiExt for egui::Ui { fm: FlightMode, current: Option, data_source: &mut dyn DataSource, + shortcut: Key, ) { - let (label, shortcut, fg, bg) = flight_mode_style(fm); + let (label, fg, bg) = flight_mode_style(fm); let is_current = current.map(|c| c == fm).unwrap_or(false); + // We disable the serious flight modes until we are software armed + let is_armed = current.map(|c| c >= FlightMode::Armed).unwrap_or(false); + let is_enabled = (fm <= FlightMode::Armed) || is_armed; + let main_text = RichText::new(label).monospace(); let button = if is_current { Button::new(main_text.color(fg)).wrap(true).fill(bg) @@ -192,27 +198,33 @@ impl TopBarUiExt for egui::Ui { let pos = self.next_widget_position(); let size = Vec2::new(self.available_width(), 50.0); - let shortcut = if is_current { - Label::new(RichText::new(format!("Shift+{}", shortcut)).size(9.0).color(fg)) + let shortcut_text = if is_current { + Label::new(RichText::new(format!("Shift+{:?}", shortcut)).size(9.0).color(fg)) } else { - Label::new(RichText::new(format!("Shift+{}", shortcut)).size(9.0).weak()) + Label::new(RichText::new(format!("Shift+{:?}", shortcut)).size(9.0).weak()) }; - self.put(Rect::from_two_pos(pos + size * Vec2::new(0.0, 0.6), pos + size), shortcut); - if self.put(Rect::from_two_pos(pos, pos + size), button).clicked() { + self.add_enabled_ui(is_enabled, |ui| { + ui.put(Rect::from_two_pos(pos + size * Vec2::new(0.0, 0.6), pos + size), shortcut_text); + if ui.put(Rect::from_two_pos(pos, pos + size), button).clicked() && is_enabled { + data_source.send_command(Command::SetFlightMode(fm)).unwrap(); + } + }); + + if self.ctx().input_mut(|i| i.consume_key(Modifiers::SHIFT, shortcut)) && is_enabled { data_source.send_command(Command::SetFlightMode(fm)).unwrap(); } } fn flight_mode_buttons(&mut self, current: Option, data_source: &mut dyn DataSource) { self.columns(7, |columns| { - columns[0].flight_mode_button(FlightMode::Idle, current, data_source); - columns[1].flight_mode_button(FlightMode::HardwareArmed, current, data_source); - columns[2].flight_mode_button(FlightMode::Armed, current, data_source); - columns[3].flight_mode_button(FlightMode::Flight, current, data_source); - columns[4].flight_mode_button(FlightMode::RecoveryDrogue, current, data_source); - columns[5].flight_mode_button(FlightMode::RecoveryMain, current, data_source); - columns[6].flight_mode_button(FlightMode::Landed, current, data_source); + columns[0].flight_mode_button(FlightMode::Idle, current, data_source, Key::F5); + columns[1].flight_mode_button(FlightMode::HardwareArmed, current, data_source, Key::F6); + columns[2].flight_mode_button(FlightMode::Armed, current, data_source, Key::F7); + columns[3].flight_mode_button(FlightMode::Flight, current, data_source, Key::F8); + columns[4].flight_mode_button(FlightMode::RecoveryDrogue, current, data_source, Key::F9); + columns[5].flight_mode_button(FlightMode::RecoveryMain, current, data_source, Key::F10); + columns[6].flight_mode_button(FlightMode::Landed, current, data_source, Key::F11); }); } }