ruffle/core/src/library.rs

87 lines
2.5 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-04-29 05:55:44 +00:00
use crate::display_object::DisplayObject;
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-04-25 17:52:22 +00:00
match self.characters.get(&id) {
2019-04-26 03:27:44 +00:00
Some(Character::Graphic {
x_min,
y_min,
2019-04-27 17:54:37 +00:00
shape_handle,
2019-04-29 05:55:44 +00:00
}) => Ok(DisplayObject::new(Box::new(Graphic::new(
2019-04-27 17:54:37 +00:00
*shape_handle,
*x_min,
*y_min,
2019-04-29 05:55:44 +00:00
)))),
2019-04-25 17:52:22 +00:00
Some(Character::MovieClip {
2019-04-26 03:27:44 +00:00
tag_stream_start,
2019-04-25 17:52:22 +00:00
num_frames,
2019-04-29 05:55:44 +00:00
}) => Ok(DisplayObject::new(Box::new(MovieClip::new_with_data(
2019-04-26 03:27:44 +00:00
*tag_stream_start,
2019-04-25 17:52:22 +00:00
*num_frames,
2019-04-29 05:55:44 +00:00
)))),
2019-05-03 18:44:12 +00:00
Some(Character::Button(button)) => {
Ok(DisplayObject::new(Box::new(Button::new(&*button, self))))
}
2019-05-04 18:45:11 +00:00
Some(Character::Text(text)) => Ok(DisplayObject::new(text.clone())),
2019-04-25 17:52:22 +00:00
Some(_) => Err("Not a DisplayObject".into()),
None => Err("Character id doesn't exist".into()),
}
}
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
}