Skip to main content

sb3_decoder/model/
sprite.rs

1use std::collections::BTreeMap;
2
3use crate::{Error, prelude::*};
4
5/// The representation of a Scratch sprite.
6#[derive(Debug, Clone)]
7#[non_exhaustive]
8pub struct Sprite {
9    pub name: String,
10    pub scripts: Vec<code::Script>,
11    pub loose_reporters: Vec<code::Reporter>,
12
13    pub x: f64,
14    pub y: f64,
15    pub direction: f64,
16    pub rotation_style: RotationStyle,
17    pub draggable: bool,
18
19    pub current_costume: usize,
20    pub costumes: Vec<Costume>,
21    pub visible: bool,
22    pub layer: u16,
23    pub size: f64,
24
25    pub sounds: Vec<Sound>,
26    pub volume: f64,
27
28    pub variables: BTreeMap<code::VarId, code::Variable>,
29    pub lists: BTreeMap<code::ListId, code::List>,
30}
31
32/// The representation of a Scratch sprite's rotation style.
33#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
34pub enum RotationStyle {
35    #[default]
36    AllAround,
37    LeftRight,
38    DontRotate,
39}
40
41impl std::str::FromStr for RotationStyle {
42    type Err = Error;
43
44    fn from_str(value: &str) -> Result<Self, Error> {
45        match value {
46            "all around" => Ok(RotationStyle::AllAround),
47            "left-right" => Ok(RotationStyle::LeftRight),
48            "don't rotate" => Ok(RotationStyle::DontRotate),
49            _ => Err(Error::InvalidRotationStyle(value.to_string())),
50        }
51    }
52}