changed to use update message system

This commit is contained in:
Nickiel12 2021-12-11 01:59:14 -08:00
parent a27d7c7a0a
commit 2611de9b2b
2 changed files with 57 additions and 17 deletions

View file

@ -1,6 +1,18 @@
use super::enums::{self}; use super::enums::{self};
pub enum StateUpdate {
StreamRunning(bool),
StreamIsMuted(bool),
ComputerSoundIsOn(bool),
ChangeSceneOnChangeSlideHotkey(bool),
SceneIsAugmented(bool),
TimerCanRun(bool),
TimerLength(f32),
TimerText(String),
Scene(enums::Scenes),
}
#[derive(Debug)]
pub struct StreamStates { pub struct StreamStates {
pub stream_running: bool, pub stream_running: bool,
pub stream_is_muted: bool, pub stream_is_muted: bool,
@ -44,6 +56,22 @@ impl StreamStates {
StreamStates{..Default::default()} StreamStates{..Default::default()}
} }
pub fn update(mut self, updateMessage: StateUpdate) -> Self {
match updateMessage {
StateUpdate::StreamRunning(new_val) => {self.stream_running = new_val; self},
StateUpdate::StreamIsMuted(new_val) => {self.stream_is_muted = new_val; self},
StateUpdate::ComputerSoundIsOn(new_val) => {self.computer_sound_is_on = new_val; self},
StateUpdate::ChangeSceneOnChangeSlideHotkey(new_val) => {self.change_scene_on_change_slide_hotkey = new_val; self},
StateUpdate::TimerCanRun(new_val) => {self.timer_can_run = new_val; self},
StateUpdate::TimerLength(new_val) => {self.timer_length = new_val; self},
StateUpdate::TimerText(new_val) => {self.timer_text = new_val; self},
StateUpdate::Scene(new_val) => {self.change_scene(&new_val)},
StateUpdate::SceneIsAugmented(new_val) => {
self.scene_is_augmented = new_val;
self.change_scene(&enums::Scenes::Augmented)},
}
}
pub fn get_current_scene(&self) -> enums::Scenes { pub fn get_current_scene(&self) -> enums::Scenes {
self.current_scene self.current_scene
} }
@ -56,7 +84,7 @@ impl StreamStates {
self.screen_sub_scene self.screen_sub_scene
} }
pub fn change_scene(self, scene: &enums::Scenes) -> Self { pub fn change_scene(mut self, scene: &enums::Scenes) -> Self {
match scene { match scene {
enums::Scenes::CameraDefault | enums::Scenes::CameraWithUpperRight | enums::Scenes::CameraDefault | enums::Scenes::CameraWithUpperRight |
enums::Scenes::CameraWithLargeUpperRight | enums::Scenes::CameraWithLowerRight enums::Scenes::CameraWithLargeUpperRight | enums::Scenes::CameraWithLowerRight
@ -64,7 +92,7 @@ impl StreamStates {
enums::Scenes::ScreenDefault | enums::Scenes::ScreenWithUpperRight | enums::Scenes::ScreenDefault | enums::Scenes::ScreenWithUpperRight |
enums::Scenes::ScreenWithLowerRight enums::Scenes::ScreenWithLowerRight
=> {StreamStates::set_screen_scene(self, scene)}, => {StreamStates::set_screen_scene(self, scene)},
enums::Scenes::Augmented => {self.current_scene = *scene; self}
} }
} }

View file

@ -1,3 +1,5 @@
use std::sync::mpsc;
use std::thread;
use crate::modules::stream_states as s_s; use crate::modules::stream_states as s_s;
@ -48,37 +50,37 @@ fn scene_correctness(){
assert_eq!(stream_state.get_current_camera_sub_scene(), s_s::enums::Scenes::CameraDefault); assert_eq!(stream_state.get_current_camera_sub_scene(), s_s::enums::Scenes::CameraDefault);
assert_eq!(stream_state.get_current_screen_sub_scene(), s_s::enums::Scenes::ScreenDefault); assert_eq!(stream_state.get_current_screen_sub_scene(), s_s::enums::Scenes::ScreenDefault);
stream_state = stream_state.change_scene(&s_s::enums::Scenes::CameraWithUpperRight); stream_state = stream_state.update(s_s::stream_states_class::StateUpdate::Scene(s_s::enums::Scenes::CameraWithUpperRight));
assert_eq!(stream_state.get_current_scene(), s_s::enums::Scenes::CameraWithUpperRight); assert_eq!(stream_state.get_current_scene(), s_s::enums::Scenes::CameraWithUpperRight);
assert_eq!(stream_state.get_current_camera_sub_scene(), s_s::enums::Scenes::CameraWithUpperRight); assert_eq!(stream_state.get_current_camera_sub_scene(), s_s::enums::Scenes::CameraWithUpperRight);
assert_eq!(stream_state.get_current_screen_sub_scene(), s_s::enums::Scenes::ScreenDefault); assert_eq!(stream_state.get_current_screen_sub_scene(), s_s::enums::Scenes::ScreenDefault);
stream_state = stream_state.change_scene(&s_s::enums::Scenes::CameraWithLargeUpperRight); stream_state = stream_state.update(s_s::stream_states_class::StateUpdate::Scene(s_s::enums::Scenes::CameraWithLargeUpperRight));
assert_eq!(stream_state.get_current_scene(), s_s::enums::Scenes::CameraWithLargeUpperRight); assert_eq!(stream_state.get_current_scene(), s_s::enums::Scenes::CameraWithLargeUpperRight);
assert_eq!(stream_state.get_current_camera_sub_scene(), s_s::enums::Scenes::CameraWithLargeUpperRight); assert_eq!(stream_state.get_current_camera_sub_scene(), s_s::enums::Scenes::CameraWithLargeUpperRight);
assert_eq!(stream_state.get_current_screen_sub_scene(), s_s::enums::Scenes::ScreenDefault); assert_eq!(stream_state.get_current_screen_sub_scene(), s_s::enums::Scenes::ScreenDefault);
stream_state = stream_state.change_scene(&s_s::enums::Scenes::CameraWithLowerRight); stream_state = stream_state.update(s_s::stream_states_class::StateUpdate::Scene(s_s::enums::Scenes::CameraWithLowerRight));
assert_eq!(stream_state.get_current_scene(), s_s::enums::Scenes::CameraWithLowerRight); assert_eq!(stream_state.get_current_scene(), s_s::enums::Scenes::CameraWithLowerRight);
assert_eq!(stream_state.get_current_camera_sub_scene(), s_s::enums::Scenes::CameraWithLowerRight); assert_eq!(stream_state.get_current_camera_sub_scene(), s_s::enums::Scenes::CameraWithLowerRight);
assert_eq!(stream_state.get_current_screen_sub_scene(), s_s::enums::Scenes::ScreenDefault); assert_eq!(stream_state.get_current_screen_sub_scene(), s_s::enums::Scenes::ScreenDefault);
stream_state = stream_state.change_scene(&s_s::enums::Scenes::ScreenDefault); stream_state = stream_state.update(s_s::stream_states_class::StateUpdate::Scene(s_s::enums::Scenes::ScreenDefault));
assert_eq!(stream_state.get_current_scene(), s_s::enums::Scenes::ScreenDefault); assert_eq!(stream_state.get_current_scene(), s_s::enums::Scenes::ScreenDefault);
assert_eq!(stream_state.get_current_screen_sub_scene(), s_s::enums::Scenes::ScreenDefault); assert_eq!(stream_state.get_current_screen_sub_scene(), s_s::enums::Scenes::ScreenDefault);
assert_eq!(stream_state.get_current_camera_sub_scene(), s_s::enums::Scenes::CameraWithLowerRight); assert_eq!(stream_state.get_current_camera_sub_scene(), s_s::enums::Scenes::CameraWithLowerRight);
stream_state = stream_state.change_scene(&s_s::enums::Scenes::ScreenWithLowerRight); stream_state = stream_state.update(s_s::stream_states_class::StateUpdate::Scene(s_s::enums::Scenes::ScreenWithLowerRight));
assert_eq!(stream_state.get_current_scene(), s_s::enums::Scenes::ScreenWithLowerRight); assert_eq!(stream_state.get_current_scene(), s_s::enums::Scenes::ScreenWithLowerRight);
assert_eq!(stream_state.get_current_screen_sub_scene(), s_s::enums::Scenes::ScreenWithLowerRight); assert_eq!(stream_state.get_current_screen_sub_scene(), s_s::enums::Scenes::ScreenWithLowerRight);
assert_eq!(stream_state.get_current_camera_sub_scene(), s_s::enums::Scenes::CameraWithLowerRight); assert_eq!(stream_state.get_current_camera_sub_scene(), s_s::enums::Scenes::CameraWithLowerRight);
stream_state = stream_state.change_scene(&s_s::enums::Scenes::ScreenWithUpperRight); stream_state = stream_state.update(s_s::stream_states_class::StateUpdate::Scene(s_s::enums::Scenes::ScreenWithUpperRight));
assert_eq!(stream_state.get_current_scene(), s_s::enums::Scenes::ScreenWithUpperRight); assert_eq!(stream_state.get_current_scene(), s_s::enums::Scenes::ScreenWithUpperRight);
assert_eq!(stream_state.get_current_screen_sub_scene(), s_s::enums::Scenes::ScreenWithUpperRight); assert_eq!(stream_state.get_current_screen_sub_scene(), s_s::enums::Scenes::ScreenWithUpperRight);
@ -91,35 +93,45 @@ fn test_updating() {
let mut stream_state = s_s::stream_states_class::StreamStates::new(); let mut stream_state = s_s::stream_states_class::StreamStates::new();
assert_eq!(stream_state.timer_can_run, true); assert_eq!(stream_state.timer_can_run, true);
stream_state.timer_can_run = false; stream_state = stream_state.update(s_s::stream_states_class::StateUpdate::TimerCanRun(false));
assert_eq!(stream_state.timer_can_run, false); assert_eq!(stream_state.timer_can_run, false);
assert_eq!(stream_state.timer_length, 15.0); assert_eq!(stream_state.timer_length, 15.0);
stream_state.timer_length = 7.5; stream_state = stream_state.update(s_s::stream_states_class::StateUpdate::TimerLength(7.5));
assert_eq!(stream_state.timer_length, 7.5); assert_eq!(stream_state.timer_length, 7.5);
assert_eq!(stream_state.timer_text, "0.0"); assert_eq!(stream_state.timer_text, "0.0");
stream_state.timer_text = String::from("7.5"); stream_state = stream_state.update(s_s::stream_states_class::StateUpdate::TimerText(String::from("7.5")));
assert_eq!(stream_state.timer_text, "7.5"); assert_eq!(stream_state.timer_text, "7.5");
assert_eq!(stream_state.stream_running, false); assert_eq!(stream_state.stream_running, false);
stream_state.stream_running = true; stream_state = stream_state.update(s_s::stream_states_class::StateUpdate::StreamRunning(true));
assert_eq!(stream_state.stream_running, true); assert_eq!(stream_state.stream_running, true);
assert_eq!(stream_state.stream_is_muted, false); assert_eq!(stream_state.stream_is_muted, false);
stream_state.stream_is_muted = true; stream_state = stream_state.update(s_s::stream_states_class::StateUpdate::StreamIsMuted(true));
assert_eq!(stream_state.stream_is_muted, true); assert_eq!(stream_state.stream_is_muted, true);
assert_eq!(stream_state.computer_sound_is_on, true); assert_eq!(stream_state.computer_sound_is_on, true);
stream_state.computer_sound_is_on = false; stream_state = stream_state.update(s_s::stream_states_class::StateUpdate::ComputerSoundIsOn(false));
assert_eq!(stream_state.computer_sound_is_on, false); assert_eq!(stream_state.computer_sound_is_on, false);
assert_eq!(stream_state.change_scene_on_change_slide_hotkey, true); assert_eq!(stream_state.change_scene_on_change_slide_hotkey, true);
stream_state.change_scene_on_change_slide_hotkey = false; stream_state = stream_state.update(s_s::stream_states_class::StateUpdate::ChangeSceneOnChangeSlideHotkey(false));
assert_eq!(stream_state.change_scene_on_change_slide_hotkey, false); assert_eq!(stream_state.change_scene_on_change_slide_hotkey, false);
assert_eq!(stream_state.scene_is_augmented, false); assert_eq!(stream_state.scene_is_augmented, false);
stream_state.scene_is_augmented = true; stream_state = stream_state.update(s_s::stream_states_class::StateUpdate::SceneIsAugmented(true));
assert_eq!(stream_state.scene_is_augmented, true); assert_eq!(stream_state.scene_is_augmented, true);
} }
/*
#[test]
fn can_run_in_thread() {
let (tx, rx) = mpsc::channel();
thread::spawn(move || {
tx
});
}
*/