From 33819a663dff2f13e408dda2600571364effe747 Mon Sep 17 00:00:00 2001 From: Nickiel12 <35903114+Nickiel12@users.noreply.github.com> Date: Thu, 30 Dec 2021 11:58:59 -0800 Subject: [PATCH] implemented most of the stuff --- src/main.rs | 45 +++++++++++++++--- src/modules/external_interface.rs | 18 +++++++- src/modules/message_handler.rs | 76 ++++++++++++++++++++++++++----- 3 files changed, 120 insertions(+), 19 deletions(-) diff --git a/src/main.rs b/src/main.rs index 4f6e17e..1d2ed16 100644 --- a/src/main.rs +++ b/src/main.rs @@ -42,26 +42,57 @@ fn main() { println!("{}", message); let json = serde_json::from_str(&message).unwrap(); let update = StateUpdate::json_to_state_update(json); - let state_update = state.handle_update(update); + if update == StateUpdate::UpdateClient { + update_all(&state, &socket); + } + let updates = state.handle_update(update); + if updates.0.is_some() { + socket.send(updates.0.unwrap().to_json().to_string()); + } + if updates.1.is_some() { + handle_instructions(updates.1.unwrap(), &mut state, &socket); + } }, Err(_) => {continue}, } let tick_update = state.tick(); - if tick_update.0.is_some() { - state.handle_update(tick_update.0.unwrap()); - } - if tick_update.1.is_some() { - state.handle_update(tick_update.1.unwrap()); - } + if tick_update.0.is_some() {state.handle_update(tick_update.0.unwrap());} + if tick_update.1.is_some() {state.handle_update(tick_update.1.unwrap());} } socket.close(); hotkey_handle.join().unwrap(); } +fn handle_instructions(mut instructions: Vec, state: &mut StreamState, socket: &Socket) { + for i in instructions.iter_mut() { + let updates = state.handle_update(i.to_owned()); + if updates.0.is_some() { + socket.send(updates.0.unwrap().to_json().to_string()); + } + if updates.1.is_some() { + handle_instructions(updates.1.unwrap(), state, socket); + } + } +} + fn setup_control_c(mut control_c_flag_tx: sync_flag::SyncFlagTx) { ctrlc::set_handler(move || { control_c_flag_tx.set(true); }).expect("control C handler failed!"); } +fn update_all(state: &StreamState, socket: &Socket) { + socket.send(StateUpdate::StreamRunning(state.stream_running).to_json().to_string()); + socket.send(StateUpdate::StreamSoundToggleOn(state.stream_is_muted).to_json().to_string()); + socket.send(StateUpdate::ToggleComputerSoundOn(state.computer_sound_is_on).to_json().to_string()); + socket.send(StateUpdate::ChangeSceneOnChangeSlide(state.change_scene_on_change_slide_hotkey).to_json().to_string()); + socket.send(StateUpdate::SceneIsAugmented(state.scene_is_augmented).to_json().to_string()); + socket.send(StateUpdate::TimerCanRun(state.timer_can_run).to_json().to_string()); + socket.send(StateUpdate::TimerLength(state.timer_length).to_json().to_string()); + socket.send(StateUpdate::TimerText(state.timer_text.clone()).to_json().to_string()); + socket.send(StateUpdate::SubScene(state.camera_sub_scene).to_json().to_string()); + socket.send(StateUpdate::SubScene(state.screen_sub_scene).to_json().to_string()); + socket.send(StateUpdate::Scene(state.current_scene).to_json().to_string()); +} + diff --git a/src/modules/external_interface.rs b/src/modules/external_interface.rs index 8d6cfad..d5a1ef1 100644 --- a/src/modules/external_interface.rs +++ b/src/modules/external_interface.rs @@ -1,4 +1,4 @@ -use super::stream_states::{state_update::StateUpdate, enums::SlideChange}; +use super::stream_states::{state_update::StateUpdate, enums::{SlideChange, SubScenes, Scenes}}; pub fn create_keyboard_hooks(channel_tx: crossbeam_channel::Sender) { @@ -21,4 +21,20 @@ pub fn next_slide() { pub fn prev_slide() { todo!() +} + +pub fn toggle_stream_sound(value: bool) { + todo!() +} + +pub fn toggle_computer_sound(value: bool) { + todo!() +} + +pub fn toggle_media_play_pause(value: bool) { + todo!() +} + +pub fn change_scene(scene: Scenes, sub_scene: Option) { + todo!() } \ No newline at end of file diff --git a/src/modules/message_handler.rs b/src/modules/message_handler.rs index a8d7b7a..636a51b 100644 --- a/src/modules/message_handler.rs +++ b/src/modules/message_handler.rs @@ -1,26 +1,27 @@ use std::time::{SystemTime, Duration}; -use super::{stream_states::{state_update::StateUpdate, stream_states_class::StreamState, enums::{SlideChange, Scenes}}, external_interface, socket_handler::Socket}; +use super::{stream_states::{state_update::StateUpdate, stream_states_class::StreamState, enums::{SlideChange, Scenes}}, external_interface}; -pub trait MessageHandler { - fn handle_update(&mut self, update: StateUpdate) -> Option; +pub trait MessageHandler { //the first one goes to socket, the second propogates + fn handle_update(&mut self, update: StateUpdate) -> (Option, Option>); fn get_states(&self) -> StreamState; fn tick(&mut self) -> (Option, Option); } impl MessageHandler for StreamState { - fn handle_update(&mut self, update: StateUpdate) -> Option { + fn handle_update(&mut self, update: StateUpdate) -> (Option, Option>) { self.update(update.clone()); + if self.debug_mode { + return (None, None) + } + match update { StateUpdate::ChangeSlide(direction) => { if self.timer_can_run { self.timer_finished = false; self.timer_start = SystemTime::now(); } - if self.change_scene_on_change_slide_hotkey { - self.handle_update(StateUpdate::Scene(Scenes::Screen)); - } match direction { SlideChange::Next => { external_interface::next_slide(); @@ -29,10 +30,60 @@ impl MessageHandler for StreamState { external_interface::prev_slide(); } } + if self.change_scene_on_change_slide_hotkey { + let mut instructions = Vec::new(); + instructions.push(StateUpdate::Scene(Scenes::Screen)); + + return (None, Some(instructions)) + } else {return (None, None)} } + StateUpdate::ChangeSceneOnChangeSlide(value) => {self.change_scene_on_change_slide_hotkey = value; return (Some(update), None)}, + StateUpdate::SceneIsAugmented(value) => { + if value { + let mut instructions = Vec::new(); + instructions.push(StateUpdate::ChangeSceneOnChangeSlide(false)); + instructions.push(StateUpdate::Scene(Scenes::Augmented)); + instructions.push(StateUpdate::TimerCanRun(false)); + return (Some(update), Some(instructions)) + } else { + let mut instructions = Vec::new(); + instructions.push(StateUpdate::Scene(Scenes::Camera)); + instructions.push(StateUpdate::ChangeSceneOnChangeSlide(true)); + instructions.push(StateUpdate::TimerCanRun(true)); + return (Some(update), Some(instructions)); + } + }, + StateUpdate::TimerCanRun(value) => {self.timer_can_run = value; return (Some(update), None)}, + StateUpdate::TimerLength(value) => {self.timer_length = value; return (Some(update), None)}, + StateUpdate::TimerText(value) => {self.timer_text = value.clone(); return (Some(StateUpdate::TimerText(value)), None)}, + StateUpdate::SubScene(value) => { + if value.get_type() == Scenes::Camera { + self.camera_sub_scene = value; + if self.current_scene == Scenes::Camera { + external_interface::change_scene(Scenes::Camera, Some(self.camera_sub_scene)); + } + return (Some(update), None) + } else if value.get_type() == Scenes::Screen { + self.screen_sub_scene = value; + if self.current_scene == Scenes::Screen { + external_interface::change_scene(Scenes::Screen, Some(self.screen_sub_scene)); + } + return (Some(update), None) + } + }, + StateUpdate::Scene(value) => { + external_interface::change_scene(value, None); + self.current_scene = value; + return (Some(update), None); + }, + StateUpdate::StreamSoundToggleOn(value) => {external_interface::toggle_stream_sound(value); return (Some(update), None)}, + StateUpdate::ToggleComputerSoundOn(value) => {external_interface::toggle_computer_sound(value); return (Some(update), None)}, + StateUpdate::ComputerMediaDoPause(value) => {external_interface::toggle_media_play_pause(value); return (Some(update), None)}, + StateUpdate::UpdateClient => {}, + StateUpdate::StreamRunning(_) => {}, _ => {} } - None + (None, None) } fn tick(&mut self) -> (Option, Option) { @@ -69,7 +120,8 @@ fn test_tick_1() { state.timer_finished = false; state.tick(); std::thread::sleep(Duration::from_millis(1000)); - state.tick(); + let update = state.tick(); + state.update(update.0.unwrap()); assert_eq!(state.timer_text, "14.0"); } @@ -79,7 +131,8 @@ fn test_tick_one_half() { state.timer_finished = false; state.tick(); std::thread::sleep(Duration::from_millis(500)); - state.tick(); + let update = state.tick(); + state.update(update.0.unwrap()); assert_eq!(state.timer_text, "14.5"); } @@ -90,6 +143,7 @@ fn test_tick_10() { state.timer_finished = false; state.tick(); std::thread::sleep(Duration::from_millis(10000)); - state.tick(); + let update = state.tick(); + state.update(update.0.unwrap()); assert_eq!(state.timer_text, "5.0"); } \ No newline at end of file