core: Add initial ExternalInterface structs

This commit is contained in:
Nathan Adams 2020-08-27 23:27:54 +02:00 committed by Mike Welsh
parent 8d74b031e4
commit 8f11141426
3 changed files with 34 additions and 0 deletions

20
core/src/external.rs Normal file
View File

@ -0,0 +1,20 @@
pub trait ExternalInterfaceProvider {}
#[derive(Default)]
pub struct ExternalInterface {
providers: Vec<Box<dyn ExternalInterfaceProvider>>,
}
impl ExternalInterface {
pub fn new() -> Self {
Self::default()
}
pub fn add_provider(&mut self, provider: Box<dyn ExternalInterfaceProvider>) {
self.providers.push(provider);
}
pub fn available(&self) -> bool {
!self.providers.is_empty()
}
}

View File

@ -38,6 +38,7 @@ mod transform;
mod xml; mod xml;
pub mod backend; pub mod backend;
pub mod external;
pub use chrono; pub use chrono;
pub use events::PlayerEvent; pub use events::PlayerEvent;

View File

@ -12,6 +12,7 @@ use crate::backend::{audio::AudioBackend, render::Letterbox, render::RenderBacke
use crate::context::{ActionQueue, ActionType, RenderContext, UpdateContext}; use crate::context::{ActionQueue, ActionType, RenderContext, UpdateContext};
use crate::display_object::{EditText, MorphShape, MovieClip}; use crate::display_object::{EditText, MorphShape, MovieClip};
use crate::events::{ButtonKeyCode, ClipEvent, ClipEventResult, KeyCode, PlayerEvent}; use crate::events::{ButtonKeyCode, ClipEvent, ClipEventResult, KeyCode, PlayerEvent};
use crate::external::{ExternalInterface, ExternalInterfaceProvider};
use crate::library::Library; use crate::library::Library;
use crate::loader::LoadManager; use crate::loader::LoadManager;
use crate::prelude::*; use crate::prelude::*;
@ -180,6 +181,9 @@ pub struct Player {
/// contexts to other parts of the player. It can be used to ensure the /// contexts to other parts of the player. It can be used to ensure the
/// player lives across `await` calls in async code. /// player lives across `await` calls in async code.
self_reference: Option<Weak<Mutex<Self>>>, self_reference: Option<Weak<Mutex<Self>>>,
/// External interface for (for example) Javascript <-> Actionscript interaction
external_interface: ExternalInterface,
} }
impl Player { impl Player {
@ -262,6 +266,7 @@ impl Player {
instance_counter: 0, instance_counter: 0,
time_til_next_timer: None, time_til_next_timer: None,
storage, storage,
external_interface: ExternalInterface::new(),
}; };
player.mutate_with_update_context(|context| Avm2::load_player_globals(context))?; player.mutate_with_update_context(|context| Avm2::load_player_globals(context))?;
@ -1153,6 +1158,14 @@ impl Player {
pub fn should_prevent_scrolling(&mut self) -> bool { pub fn should_prevent_scrolling(&mut self) -> bool {
self.mutate_with_update_context(|context| context.avm1.has_mouse_listener()) self.mutate_with_update_context(|context| context.avm1.has_mouse_listener())
} }
pub fn external_interface(&self) -> &ExternalInterface {
&self.external_interface
}
pub fn add_external_interface(&mut self, provider: Box<dyn ExternalInterfaceProvider>) {
self.external_interface.add_provider(provider);
}
} }
pub struct DragObject<'gc> { pub struct DragObject<'gc> {