aboutsummaryrefslogtreecommitdiffstats
path: root/src/bin/ttyplay/event.rs
blob: da8cdd43c4fc57a4a371e7cb5f1ab92716b55d32 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
pub enum Event {
    FrameTransition((usize, vt100::Screen)),
    FrameLoaded(Option<usize>),
    Paused(bool),
    TimerAction(TimerAction),
    ToggleUi,
    Quit,
}

pub enum TimerAction {
    Pause,
    FirstFrame,
    LastFrame,
    NextFrame,
    PreviousFrame,
    SpeedUp,
    SlowDown,
    Quit,
}

struct Reader {
    pending: async_std::sync::Mutex<Pending>,
    cvar: async_std::sync::Condvar,
}

impl Reader {
    fn new(
        input: async_std::channel::Receiver<Event>,
    ) -> async_std::sync::Arc<Self> {
        let this = Self {
            pending: async_std::sync::Mutex::new(Pending::new()),
            cvar: async_std::sync::Condvar::new(),
        };
        let this = async_std::sync::Arc::new(this);
        {
            let this = this.clone();
            async_std::task::spawn(async move {
                while let Ok(event) = input.recv().await {
                    this.event(event).await;
                }
                this.event(Event::Quit).await;
            });
        }
        this
    }

    async fn read(&self) -> Option<Event> {
        let mut pending = self
            .cvar
            .wait_until(self.pending.lock().await, |pending| {
                pending.has_event()
            })
            .await;
        pending.get_event()
    }

    async fn event(&self, event: Event) {
        let mut pending = self.pending.lock().await;
        pending.event(event);
        self.cvar.notify_one();
    }
}

#[derive(Default)]
struct Pending {
    render: Option<(usize, vt100::Screen)>,
    frame_loaded: Option<usize>,
    done_loading: bool,
    paused: Option<bool>,
    timer_actions: std::collections::VecDeque<TimerAction>,
    toggle_ui: bool,
    quit: bool,
}

impl Pending {
    fn new() -> Self {
        Self::default()
    }

    fn event(&mut self, event: Event) {
        match event {
            Event::FrameTransition((idx, screen)) => {
                self.render = Some((idx, screen));
            }
            Event::FrameLoaded(idx) => {
                if let Some(idx) = idx {
                    self.frame_loaded = Some(idx);
                } else {
                    self.done_loading = true;
                }
            }
            Event::Paused(paused) => {
                self.paused = Some(paused);
            }
            Event::TimerAction(action) => {
                self.timer_actions.push_back(action);
            }
            Event::ToggleUi => {
                self.toggle_ui = !self.toggle_ui;
            }
            Event::Quit => {
                self.quit = true;
            }
        }
    }

    fn has_event(&self) -> bool {
        self.render.is_some()
            || self.frame_loaded.is_some()
            || self.done_loading
            || self.paused.is_some()
            || !self.timer_actions.is_empty()
            || self.toggle_ui
            || self.quit
    }

    fn get_event(&mut self) -> Option<Event> {
        if self.quit {
            self.quit = false;
            Some(Event::Quit)
        } else if let Some(action) = self.timer_actions.pop_front() {
            Some(Event::TimerAction(action))
        } else if self.toggle_ui {
            self.toggle_ui = false;
            Some(Event::ToggleUi)
        } else if let Some(paused) = self.paused.take() {
            Some(Event::Paused(paused))
        } else if let Some(frame) = self.frame_loaded.take() {
            Some(Event::FrameLoaded(Some(frame)))
        } else if self.done_loading {
            self.done_loading = false;
            Some(Event::FrameLoaded(None))
        } else if let Some((idx, screen)) = self.render.take() {
            Some(Event::FrameTransition((idx, screen)))
        } else {
            None
        }
    }
}

pub async fn handle_events(
    event_r: async_std::channel::Receiver<Event>,
    timer_w: async_std::channel::Sender<TimerAction>,
    mut output: textmode::Output,
) -> anyhow::Result<()> {
    let mut display = crate::display::Display::new();
    let mut current_screen = vt100::Parser::default().screen().clone();
    let events = Reader::new(event_r);
    while let Some(event) = events.read().await {
        match event {
            Event::TimerAction(action) => {
                timer_w.send(action).await?;
                continue;
            }
            Event::FrameTransition((idx, screen)) => {
                current_screen = screen;
                display.current_frame(idx);
            }
            Event::FrameLoaded(n) => {
                if let Some(n) = n {
                    display.total_frames(n);
                } else {
                    display.done_loading();
                }
            }
            Event::Paused(paused) => {
                display.paused(paused);
            }
            Event::ToggleUi => {
                display.toggle_ui();
            }
            Event::Quit => {
                break;
            }
        }
        display.render(&current_screen, &mut output).await?;
    }

    Ok(())
}