-
Notifications
You must be signed in to change notification settings - Fork 8
/
fx-chart.rs
448 lines (377 loc) · 13.2 KB
/
fx-chart.rs
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
use crate::effects::{effect_in, transition_fx};
use crossterm::event::{Event, KeyCode, KeyEventKind};
use crossterm::event;
use ratatui::backend::CrosstermBackend;
use ratatui::buffer::Buffer;
use ratatui::layout::{Offset, Rect};
use ratatui::style::{Color, Modifier, Style};
use ratatui::text::{Line, Span};
use ratatui::widgets::{Block, Clear, Widget};
use ratatui::Frame;
use std::cell::RefCell;
use std::error::Error;
use std::io::Stdout;
use std::rc::Rc;
use std::sync::mpsc;
use std::{io, thread};
use tachyonfx::widget::{EffectTimeline, EffectTimelineRects};
use tachyonfx::{BufferRenderer, Duration, Effect, Shader};
type Result<T> = std::result::Result<T, Box<dyn Error>>;
type Terminal = ratatui::Terminal<CrosstermBackend<Stdout>>;
struct App {
sender: mpsc::Sender<AppEvent>,
is_running: bool,
last_tick: Duration,
aux_buffer: Rc<RefCell<Buffer>>,
inspected_effect_no: u8,
screen_area: Rect,
}
#[derive(Default)]
struct Effects {
pub post_process: Option<Effect>,
}
impl Effects {
fn push(&mut self, effect: Effect) {
self.post_process = Some(effect);
}
fn process_active_fx(
&mut self,
duration: Duration,
buf: &mut Buffer,
area: Rect
) {
self.post_process.iter_mut()
.for_each(|effect| { effect.process(duration, buf, area); });
if self.post_process.iter().all(Effect::done) {
self.post_process = None;
}
}
}
impl App {
fn new(
sender: mpsc::Sender<AppEvent>,
aux_buffer_area: Rect,
) -> Self {
Self {
sender,
is_running: true,
last_tick: Duration::ZERO,
aux_buffer: Rc::new(RefCell::new(Buffer::empty(aux_buffer_area))),
screen_area: Rect::default(),
inspected_effect_no: 0,
}
}
fn inspected_effect(&self, areas: EffectTimelineRects) -> Effect {
effect_in(self.inspected_effect_no, areas)
}
fn effect_timeline(&self, areas: EffectTimelineRects) -> EffectTimeline {
let idx = self.inspected_effect_no;
let area = self.aux_buffer.borrow().area;
let fx = transition_fx(area, self.sender.clone(), effect_in(idx, areas));
EffectTimeline::builder()
.effect(&fx)
.build()
}
fn inspected_transition_effect(&self) -> Effect {
let area = self.aux_buffer.borrow().area;
let layout = self.effect_timeline(baseline_rects()).layout(area);
transition_fx(area, self.sender.clone(), self.inspected_effect(layout))
}
fn refresh_aux_buffer(&self) {
let effect = self.inspected_transition_effect();
let mut buf = self.aux_buffer.borrow_mut();
Clear.render(buf.area, &mut buf);
Block::new()
.style(Style::default().bg(Color::Black))
.render(buf.area, &mut buf);
EffectTimeline::builder()
.effect(&effect)
.build()
.render(buf.area, &mut buf);
}
fn apply_event(&mut self, effects: &mut Effects, e: AppEvent) {
match e {
AppEvent::Tick => (),
AppEvent::KeyPressed(key) => match key {
KeyCode::Esc => self.is_running = false,
KeyCode::Char(' ') => {
// sends RefreshAufBuffer after transitioning out
effects.push(self.inspected_transition_effect())
}
KeyCode::Enter => {
self.inspected_effect_no = (self.inspected_effect_no + 1) % 3;
// sends RefreshAufBuffer after transitioning out
effects.push(self.inspected_transition_effect())
},
_ => (),
},
AppEvent::RefreshAufBuffer => {
self.refresh_aux_buffer();
},
AppEvent::Resize(r) => self.screen_area = r
}
}
}
mod effects {
use crate::AppEvent;
use ratatui::layout::Rect;
use ratatui::style::Color;
use std::sync::mpsc;
use tachyonfx::fx::*;
use tachyonfx::widget::EffectTimelineRects;
use tachyonfx::Interpolation::{BounceOut, CircIn, ExpoInOut, ExpoOut, QuadOut};
use tachyonfx::{CellFilter, Duration, Effect};
pub(super) fn transition_fx(
screen: Rect,
sender: mpsc::Sender<AppEvent>,
fx_in: Effect,
) -> Effect {
// refresh buffer after transitioning out
let update_inspected_effect = effect_fn_buf((), 1, move |_, _, _| {
sender.send(AppEvent::RefreshAufBuffer).unwrap();
});
sequence(&[
out_fx_1(screen),
update_inspected_effect,
fx_in,
])
}
pub(super) fn effect_in(idx: u8, areas: EffectTimelineRects) -> Effect {
match idx {
0 => effect_in_1(areas),
1 => effect_in_2(areas),
_ => effect_in_3(areas),
}
}
pub(super) fn effect_in_1(areas: EffectTimelineRects) -> Effect {
parallel(&[
tree_fx_1(areas.tree),
chart_fx_1(areas.chart),
cell_filter_and_area_fx(areas.cell_filter, areas.areas, areas.legend),
])
}
pub(super) fn effect_in_2(areas: EffectTimelineRects) -> Effect {
parallel(&[
tree_fx_1(areas.tree),
chart_fx_2(areas.chart),
cell_filter_and_area_fx(areas.cell_filter, areas.areas, areas.legend),
])
}
pub(super) fn effect_in_3(areas: EffectTimelineRects) -> Effect {
parallel(&[
tree_fx_1(areas.tree),
chart_fx_3(areas.chart),
cell_filter_and_area_fx(areas.cell_filter, areas.areas, areas.legend),
])
}
pub(super) fn out_fx_1(area: Rect) -> Effect {
let step = Duration::from_millis(100);
let bg = Color::Black;
with_duration(step * 7, parallel(&[
never_complete(dissolve((step * 5, ExpoInOut))),
never_complete(fade_to_fg(bg, (5 * step, BounceOut))),
]).with_area(area))
}
fn tree_fx_1(area: Rect) -> Effect {
let step = Duration::from_millis(100);
let bg = Color::Black;
parallel(&[
coalesce((step * 5, ExpoInOut))
.with_cell_selection(CellFilter::Text),
sweep_in(Direction::UpToDown, 1, 0, bg, step * 3),
]).with_area(area)
}
fn chart_fx_1(area: Rect) -> Effect {
let step = Duration::from_millis(100);
let bg = Color::Black;
prolong_start(step * 4, sweep_in(Direction::RightToLeft, 5, 0, bg, step * 3))
.with_area(area)
}
fn chart_fx_2(area: Rect) -> Effect {
let step = Duration::from_millis(100);
let color1 = Color::from_u32(0x102020);
let color2 = Color::from_u32(0x204040);
parallel(&[
parallel(&[
timed_never_complete(step * 10, fade_to(Color::Black, Color::Black, 0)),
timed_never_complete(step * 10, fade_to(color1, color1, (step * 5, QuadOut))),
]),
sequence(&[
sleep(step * 10),
parallel(&[
slide_in(Direction::DownToUp, 15, 30, color2, step * 5),
fade_from_fg(color1, (step * 10, ExpoOut)),
]),
])
]).with_area(area)
}
fn chart_fx_3(area: Rect) -> Effect {
let step = Duration::from_millis(100);
let bg = Color::Black;
let hsl_shift = [0.0, -100.0, -50.0];
parallel(&[
hsl_shift_fg(hsl_shift, (15 * step, CircIn)).reversed(),
sweep_in(Direction::LeftToRight, 80, 30, bg, step * 15),
]).with_area(area)
}
pub fn cell_filter_and_area_fx(
cell_filter_column: Rect,
area_column: Rect,
legend: Rect
) -> Effect {
let d = Duration::from_millis(500);
parallel(&[
prolong_start(d, sweep_in(Direction::DownToUp, 1, 0, Color::Black, (d, QuadOut)))
.with_area(cell_filter_column),
prolong_start(d * 2, sweep_in(Direction::UpToDown, 1, 0, Color::Black, (d, QuadOut)))
.with_area(area_column),
prolong_start(d * 3, fade_from_fg(Color::Black, (700, QuadOut)))
.with_area(legend),
])
}
}
fn main() -> Result<()> {
let mut terminal = ratatui::init();
// event handler
let event_handler = EventHandler::new(Duration::from_millis(33));
let sender = event_handler.sender();
// create app and run it
let app = App::new(sender, Rect::new(0, 0, 100, 40));
let res = run_app(&mut terminal, app, event_handler);
ratatui::restore();
if let Err(err) = res {
println!("{err:?}");
}
Ok(())
}
pub type AuxBuffer = Rc<RefCell<Buffer>>;
fn run_app(
terminal: &mut Terminal,
mut app: App,
event_handler: EventHandler,
) -> io::Result<()> {
let mut last_frame_instant = std::time::Instant::now();
let mut effects = Effects::default();
effects.push(app.inspected_effect(baseline_rects()));
app.refresh_aux_buffer();
while app.is_running {
event_handler.receive_events(|e| app.apply_event(&mut effects, e));
app.last_tick = last_frame_instant.elapsed().into();
last_frame_instant = std::time::Instant::now();
terminal.draw(|f| {
app.screen_area = f.area();
ui(f, &app, &mut effects)
})?;
}
Ok(())
}
fn ui(
f: &mut Frame,
app: &App,
effects: &mut Effects
) {
let rect = f.area();
if rect.area() == 0 { return; }
let buf: &mut Buffer = f.buffer_mut();
Clear.render(rect, buf);
let shortcut_key_style = Style::default()
.fg(Color::DarkGray)
.add_modifier(Modifier::BOLD);
let shortcut_label_style = Style::default()
.fg(Color::DarkGray);
app.aux_buffer.render_buffer(Offset::default(), buf);
effects.process_active_fx(app.last_tick, buf, rect);
let shortcuts = Line::from(vec![
Span::from("ENTER ").style(shortcut_key_style),
Span::from("next transition ").style(shortcut_label_style),
Span::from(" SPACE ").style(shortcut_key_style),
Span::from("replay transition ").style(shortcut_label_style),
Span::from(" ESC ").style(shortcut_key_style),
Span::from("quit").style(shortcut_label_style),
]);
let centered = Rect {
x: rect.x + (rect.width - shortcuts.width() as u16) / 2,
y: rect.y + rect.height - 1,
width: shortcuts.width() as u16,
height: 1,
};
shortcuts.render(centered, buf);
}
enum AppEvent {
Tick,
KeyPressed(KeyCode),
Resize(Rect),
RefreshAufBuffer,
}
pub struct EventHandler {
sender: mpsc::Sender<AppEvent>,
receiver: mpsc::Receiver<AppEvent>,
_handler: thread::JoinHandle<()>
}
impl EventHandler {
pub fn new(tick_rate: Duration) -> Self {
let (sender, receiver) = mpsc::channel();
let tick_rate: std::time::Duration = tick_rate.into();
let handler = {
let sender = sender.clone();
thread::spawn(move || {
let mut last_tick = std::time::Instant::now();
loop {
let timeout = tick_rate
.checked_sub(last_tick.elapsed())
.unwrap_or(tick_rate);
if event::poll(timeout).expect("unable to poll for events") {
Self::apply_event(&sender);
}
if last_tick.elapsed() >= tick_rate {
sender.send(AppEvent::Tick)
.expect("failed to send tick event");
last_tick = std::time::Instant::now();
}
}
})
};
Self { sender, receiver, _handler: handler }
}
pub(crate) fn sender(&self) -> mpsc::Sender<AppEvent> {
self.sender.clone()
}
fn next(&self) -> std::result::Result<AppEvent, mpsc::RecvError> {
self.receiver.recv()
}
fn try_next(&self) -> Option<AppEvent> {
match self.receiver.try_recv() {
Ok(e) => Some(e),
Err(_) => None
}
}
pub(crate) fn receive_events<F>(&self, mut f: F)
where F: FnMut(AppEvent)
{
f(self.next().unwrap());
while let Some(event) = self.try_next() { f(event) }
}
fn apply_event(sender: &mpsc::Sender<AppEvent>) {
match event::read().expect("unable to read event") {
Event::Key(e) if e.kind == KeyEventKind::Press =>
sender.send(AppEvent::KeyPressed(e.code)),
Event::Resize(w, h) =>
sender.send(AppEvent::Resize(Rect::new(0, 0, w, h))),
_ => Ok(())
}.expect("failed to send event")
}
}
fn baseline_rects() -> EffectTimelineRects {
// giving an approximate layout so that all rects resolve to unique values,
// enabling us to get the actual layout from the effect timeline. something
// of a hack...
EffectTimelineRects {
tree: Rect::new(0, 0, 25, 32),
chart: Rect::new(35, 0, 65, 32),
cell_filter: Rect::new(25, 0, 6, 32),
areas: Rect::new(31, 0, 4, 32),
legend: Rect::new(35, 34, 29, 6),
cell_filter_legend: Rect::new(35, 34, 9, 2),
areas_legend: Rect::new(48, 34, 16, 2),
}
}