diff --git a/refbox/src/app/message.rs b/refbox/src/app/message.rs index c14df471..3669d89e 100644 --- a/refbox/src/app/message.rs +++ b/refbox/src/app/message.rs @@ -171,6 +171,7 @@ pub enum BoolGameParameter { RefAlertEnabled, AutoSoundStartPlay, AutoSoundStopPlay, + HideTime, } #[derive(Debug, Clone, Copy, PartialEq, Eq)] diff --git a/refbox/src/app/mod.rs b/refbox/src/app/mod.rs index bd96ae6a..665a7293 100644 --- a/refbox/src/app/mod.rs +++ b/refbox/src/app/mod.rs @@ -483,7 +483,8 @@ impl Application for RefBoxApp { let tm = Arc::new(Mutex::new(tm)); - let update_sender = UpdateSender::new(serial_ports, binary_port, json_port); + let update_sender = + UpdateSender::new(serial_ports, binary_port, json_port, config.hide_time); let sound = SoundController::new(config.sound.clone(), update_sender.get_trigger_flash_fn()); @@ -912,6 +913,7 @@ impl Application for RefBoxApp { games: self.games.clone(), sound: self.config.sound.clone(), mode: self.config.mode, + hide_time: self.config.hide_time, }; self.edited_settings = Some(edited_settings); @@ -1008,6 +1010,13 @@ impl Application for RefBoxApp { self.sound.update_settings(self.config.sound.clone()); self.config.mode = edited_settings.mode; + if self.config.hide_time != edited_settings.hide_time { + self.config.hide_time = edited_settings.hide_time; + self.update_sender + .set_hide_time(self.config.hide_time) + .unwrap(); + } + confy::store(APP_NAME, None, &self.config).unwrap(); AppState::MainPage } @@ -1025,6 +1034,13 @@ impl Application for RefBoxApp { self.sound.update_settings(self.config.sound.clone()); self.config.mode = edited_settings.mode; + if self.config.hide_time != edited_settings.hide_time { + self.config.hide_time = edited_settings.hide_time; + self.update_sender + .set_hide_time(self.config.hide_time) + .unwrap(); + } + confy::store(APP_NAME, None, &self.config).unwrap(); let next_game_info = if edited_settings.using_uwhscores { @@ -1067,6 +1083,13 @@ impl Application for RefBoxApp { self.sound.update_settings(self.config.sound.clone()); self.config.mode = edited_settings.mode; + if self.config.hide_time != edited_settings.hide_time { + self.config.hide_time = edited_settings.hide_time; + self.update_sender + .set_hide_time(self.config.hide_time) + .unwrap(); + } + confy::store(APP_NAME, None, &self.config).unwrap(); AppState::MainPage } @@ -1246,6 +1269,7 @@ impl Application for RefBoxApp { BoolGameParameter::AutoSoundStopPlay => { edited_settings.sound.auto_sound_stop_play ^= true } + BoolGameParameter::HideTime => edited_settings.hide_time ^= true, } } Message::CycleParameter(param) => { diff --git a/refbox/src/app/update_sender.rs b/refbox/src/app/update_sender.rs index d88440eb..0da5da39 100644 --- a/refbox/src/app/update_sender.rs +++ b/refbox/src/app/update_sender.rs @@ -35,7 +35,12 @@ pub struct UpdateSender { } impl UpdateSender { - pub fn new(initial: Vec, binary_port: u16, json_port: u16) -> Self { + pub fn new( + initial: Vec, + binary_port: u16, + json_port: u16, + hide_time: bool, + ) -> Self { let (tx, rx) = mpsc::channel(8); let initial = initial @@ -43,7 +48,7 @@ impl UpdateSender { .map(|builder| builder.open_native_async().unwrap()) .collect(); - let server_join = task::spawn(Server::new(rx, initial).run_loop()); + let server_join = task::spawn(Server::new(rx, initial, hide_time).run_loop()); let listener_join = task::spawn(listener_loop(tx.clone(), binary_port, json_port)); @@ -78,6 +83,20 @@ impl UpdateSender { let tx = self.tx.clone(); move || tx.try_send(ServerMessage::TriggerFlash) } + + pub fn set_hide_time(&self, hide_time: bool) -> Result<(), TrySendError> { + self.tx + .try_send(ServerMessage::SetHideTime(hide_time)) + .map_err(|e| match e { + TrySendError::Full(ServerMessage::SetHideTime(hide_time)) => { + TrySendError::Full(hide_time) + } + TrySendError::Closed(ServerMessage::SetHideTime(hide_time)) => { + TrySendError::Closed(hide_time) + } + _ => unreachable!(), + }) + } } impl Drop for UpdateSender { @@ -267,6 +286,7 @@ pub enum ServerMessage { NewSnapshot(GameSnapshot, bool), TriggerFlash, Stop, + SetHideTime(bool), } #[derive(Debug)] @@ -281,10 +301,15 @@ struct Server { flash: bool, binary: Vec, json: Vec, + hide_time: bool, } impl Server { - pub fn new(rx: mpsc::Receiver, initial: Vec) -> Self { + pub fn new( + rx: mpsc::Receiver, + initial: Vec, + hide_time: bool, + ) -> Self { let mut server = Server { next_id: 0, senders: HashMap::new(), @@ -296,6 +321,7 @@ impl Server { flash: false, binary: Vec::new(), json: Vec::new(), + hide_time, }; for stream in initial { @@ -355,25 +381,27 @@ impl Server { self.snapshot = new_snapshot.into(); - match self.snapshot.current_period { - GamePeriod::BetweenGames - | GamePeriod::HalfTime - | GamePeriod::OvertimeHalfTime - | GamePeriod::PreOvertime => { - if self.snapshot.secs_in_period < 15 { - self.snapshot.secs_in_period = next_time; - }; - } - GamePeriod::PreSuddenDeath => { - if self.snapshot.secs_in_period < 15 { - self.snapshot.secs_in_period = 0; + if self.hide_time { + match self.snapshot.current_period { + GamePeriod::BetweenGames + | GamePeriod::HalfTime + | GamePeriod::OvertimeHalfTime + | GamePeriod::PreOvertime => { + if self.snapshot.secs_in_period < 15 { + self.snapshot.secs_in_period = next_time; + }; } + GamePeriod::PreSuddenDeath => { + if self.snapshot.secs_in_period < 15 { + self.snapshot.secs_in_period = 0; + } + } + GamePeriod::FirstHalf + | GamePeriod::OvertimeFirstHalf + | GamePeriod::OvertimeSecondHalf + | GamePeriod::SecondHalf + | GamePeriod::SuddenDeath => {} } - GamePeriod::FirstHalf - | GamePeriod::OvertimeFirstHalf - | GamePeriod::OvertimeSecondHalf - | GamePeriod::SecondHalf - | GamePeriod::SuddenDeath => {} } self.encode_flash(); @@ -457,6 +485,9 @@ impl Server { Some(ServerMessage::Stop) => { break; } + Some(ServerMessage::SetHideTime(hide_time)) => { + self.hide_time = hide_time + } None => { break; } @@ -581,7 +612,7 @@ mod test { #[tokio::test] async fn test_update_sender() { - let update_sender = UpdateSender::new(vec![], BINARY_PORT, JSON_PORT); + let update_sender = UpdateSender::new(vec![], BINARY_PORT, JSON_PORT, false); let mut binary_conn; let mut fail_count = 0; diff --git a/refbox/src/app/view_builders/configuration.rs b/refbox/src/app/view_builders/configuration.rs index a34398eb..df7e702b 100644 --- a/refbox/src/app/view_builders/configuration.rs +++ b/refbox/src/app/view_builders/configuration.rs @@ -29,6 +29,7 @@ pub(in super::super) struct EditableSettings { pub games: Option>, pub sound: SoundSettings, pub mode: Mode, + pub hide_time: bool, } pub(in super::super) trait Cyclable @@ -194,12 +195,24 @@ fn make_main_config_page<'a>( ) .style(style::Button::LightGray), ) - .push(make_value_button( - "MODE", - settings.mode.to_string().to_uppercase(), - (true, true), - Some(Message::CycleParameter(CyclingParameter::Mode)), - )) + .push( + row() + .spacing(SPACING) + .width(Length::Fill) + .height(Length::Fill) + .push(make_value_button( + "MODE", + settings.mode.to_string().to_uppercase(), + (true, true), + Some(Message::CycleParameter(CyclingParameter::Mode)), + )) + .push(make_value_button( + "HIDE TIME FOR\nLAST 15 SECONDS", + bool_string(settings.hide_time), + (false, true), + Some(Message::ToggleBoolParameter(BoolGameParameter::HideTime)), + )), + ) .push( row() .spacing(SPACING) diff --git a/refbox/src/config.rs b/refbox/src/config.rs index e69218a3..748e1e70 100644 --- a/refbox/src/config.rs +++ b/refbox/src/config.rs @@ -45,6 +45,7 @@ impl Default for UwhScores { #[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)] pub struct Config { pub mode: Mode, + pub hide_time: bool, pub game: Game, pub hardware: Hardware, pub uwhscores: UwhScores,