yog_api/video.rs
1//! Videos (`docs/design/video.md`): a client plays a mod's `.yvideo`
2//! (`resources/video/intro.yvideo` is `mymod:intro`) on anything drawn with
3//! a picture — a screen, a quad in the world, an entity's or a model's
4//! texture. Its frame is decoded when it's asked for, each frame it's drawn.
5//! Nothing plays on a dedicated server.
6//!
7//! ```ignore
8//! use yog_api::video::Player;
9//! let intro = Player::open("mymod:intro").expect("the mod's video");
10//! intro.play();
11//! // each frame it's drawn:
12//! if let Some(p) = intro.frame() {
13//! gfx.draw2d().image(&p.texture, rect);
14//! }
15//! ```
16
17pub use yog_event::video::Picture;
18use yog_event::video::{Close, Cutscene, Frame, Open, Play, Seek};
19
20use crate::call;
21
22/// Plays video `id` full screen over the game till its end (a cutscene);
23/// `skippable`: Esc ends it early. Whether it started.
24pub fn cutscene(id: &str, skippable: bool) -> bool {
25 call(&Cutscene {
26 id: id.to_owned(),
27 skippable,
28 }) == Some(true)
29}
30
31/// A player of one video, paused at its start when opened; closed when dropped.
32#[derive(Debug, PartialEq, Eq)]
33pub struct Player(u32);
34
35impl Player {
36 /// A player of video `id` (none: no such video, or no client).
37 pub fn open(id: &str) -> Option<Player> {
38 call(&Open { id: id.to_owned() }).flatten().map(Player)
39 }
40
41 /// Plays it (from its start again once it's ended).
42 pub fn play(&self) {
43 call(&Play {
44 handle: self.0,
45 playing: true,
46 });
47 }
48
49 pub fn pause(&self) {
50 call(&Play {
51 handle: self.0,
52 playing: false,
53 });
54 }
55
56 /// Goes to `seconds` from its start.
57 pub fn seek(&self, seconds: f64) {
58 call(&Seek {
59 handle: self.0,
60 seconds,
61 });
62 }
63
64 /// Its picture now, to draw (ask each frame it's drawn).
65 pub fn frame(&self) -> Option<Picture> {
66 call(&Frame { handle: self.0 }).flatten()
67 }
68}
69
70impl Drop for Player {
71 fn drop(&mut self) {
72 call(&Close { handle: self.0 });
73 }
74}