ruffle/core/src/library.rs

72 lines
2.1 KiB
Rust
Raw Normal View History

2019-05-05 22:55:27 +00:00
use crate::backend::audio::SoundHandle;
2019-05-03 18:44:12 +00:00
use crate::button::Button;
2019-04-25 17:52:22 +00:00
use crate::character::Character;
2019-05-07 10:22:58 +00:00
use crate::display_object::{DisplayObject, DisplayObjectImpl};
2019-05-04 18:45:11 +00:00
use crate::font::Font;
2019-04-25 17:52:22 +00:00
use crate::graphic::Graphic;
use crate::movie_clip::MovieClip;
use std::collections::HashMap;
use swf::CharacterId;
pub struct Library {
characters: HashMap<CharacterId, Character>,
jpeg_tables: Option<Vec<u8>>,
2019-04-25 17:52:22 +00:00
}
impl Library {
pub fn new() -> Library {
Library {
characters: HashMap::new(),
jpeg_tables: None,
2019-04-25 17:52:22 +00:00
}
}
pub fn register_character(&mut self, id: CharacterId, character: Character) {
// TODO(Herschel): What is the behavior if id already exists?
self.characters.insert(id, character);
}
pub fn contains_character(&self, id: CharacterId) -> bool {
self.characters.contains_key(&id)
}
pub fn instantiate_display_object(
&self,
id: CharacterId,
2019-04-29 05:55:44 +00:00
) -> Result<DisplayObject, Box<std::error::Error>> {
2019-05-07 10:22:58 +00:00
let obj: Box<DisplayObjectImpl> = match self.characters.get(&id) {
Some(Character::Graphic(graphic)) => graphic.clone(),
Some(Character::MovieClip(movie_clip)) => movie_clip.clone(),
Some(Character::Button(button)) => button.clone(),
Some(Character::Text(text)) => text.clone(),
Some(_) => return Err("Not a DisplayObject".into()),
None => return Err("Character id doesn't exist".into()),
};
Ok(DisplayObject::new(obj))
2019-04-25 17:52:22 +00:00
}
2019-05-04 18:45:11 +00:00
pub fn get_font(&self, id: CharacterId) -> Option<&Font> {
if let Some(&Character::Font(ref font)) = self.characters.get(&id) {
Some(font)
} else {
None
}
}
2019-05-05 22:55:27 +00:00
pub fn get_sound(&self, id: CharacterId) -> Option<SoundHandle> {
if let Some(Character::Sound(sound)) = self.characters.get(&id) {
Some(*sound)
} else {
None
}
}
pub fn set_jpeg_tables(&mut self, data: Vec<u8>) {
self.jpeg_tables = Some(data);
}
pub fn jpeg_tables(&self) -> Option<&[u8]> {
self.jpeg_tables.as_ref().map(|data| &data[..])
}
2019-04-25 17:52:22 +00:00
}