use std::sync::Arc;
use gled_model::input::event::InputEvent;
use gled_protocol::{
message::ServerMessage,
midi::MidiMonitorEvent,
replica::StateReplica,
state::{EngineEvent, SoundSnapshot},
};
use crate::layout::ClientLayout;
#[derive(Debug, Default)]
pub(crate) struct ClientModel {
pub(crate) replica: StateReplica,
pub(crate) layout: ClientLayout,
pub(crate) sound: Arc<SoundSnapshot>,
pub(crate) preview: Option<Arc<Vec<u8>>>,
pub(crate) events: Vec<EngineEvent>,
pub(crate) input_events: Vec<InputEvent>,
pub(crate) midi_monitor: Vec<MidiMonitorEvent>,
pub(crate) midi_learn: Vec<[u8; 3]>,
pub(crate) server_version: Option<String>,
pub(crate) state_changed: bool,
pub(crate) welcomed: bool,
}
impl ClientModel {
pub(crate) fn apply(&mut self, message: ServerMessage) {
match message {
ServerMessage::Welcome { app_version, .. } => {
self.server_version = Some(app_version);
self.welcomed = true;
}
ServerMessage::Preview(output) => self.preview = Some(output.0),
ServerMessage::Sound(sound) => self.sound = sound,
ServerMessage::Event(event) => self.events.push(event),
ServerMessage::InputEvents(events) => self.input_events.extend(events),
ServerMessage::MidiMonitor(events) => self.midi_monitor.extend(events),
ServerMessage::MidiLearn(messages) => self.midi_learn.extend(messages),
message => {
if self.replica.apply(&message) {
self.state_changed = true;
let svg = self
.replica
.state
.project
.as_ref()
.and_then(|project| project.svg.as_ref());
self.layout.update(svg);
}
}
}
}
}