Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added add_scripting_api function to AppBuilder #23

Merged
merged 8 commits into from
Jul 17, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,11 +210,13 @@
mod assets;
mod callback;
mod components;
mod plugin_builder;
mod promise;
mod systems;

pub mod runtimes;

pub use crate::plugin_builder::ScriptingApiBuilder;
pub use crate::components::Script;
use assets::GetExtensions;
use promise::Promise;
Expand Down
55 changes: 55 additions & 0 deletions src/plugin_builder.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
use std::{marker::PhantomData, sync::{Arc, Mutex}};
use bevy::{app::App, prelude::World};

use crate::{callback::{Callback, IntoCallbackSystem}, Callbacks, Runtime, ScriptingRuntimeBuilder};

pub struct ApiBuilder<'a, R: Runtime> {
_phantom_data: PhantomData<R>,
world: &'a mut World,
}

impl<'a, R: Runtime> ApiBuilder<'a, R> {
fn new(world: &'a mut World) -> Self {
Self {
_phantom_data: PhantomData,
world,
}
}

/// Registers a function for calling from within a script.
/// Provided function needs to be a valid bevy system and its
/// arguments and return value need to be convertible to runtime
/// value types.
pub fn add_function<In, Out, Marker>(
self,
name: String,
fun: impl IntoCallbackSystem<R, In, Out, Marker>,
) -> Self {
let system = fun.into_callback_system(self.world);

let mut callbacks_resource = self.world.resource_mut::<Callbacks<R>>();

callbacks_resource.uninitialized_callbacks.push(Callback {
dansionGit marked this conversation as resolved.
Show resolved Hide resolved
name,
system: Arc::new(Mutex::new(system)),
calls: Arc::new(Mutex::new(vec![])),
});

self
}
}

pub trait ScriptingApiBuilder {
fn add_scripting_api<R: Runtime>(&mut self, f: impl Fn(ScriptingRuntimeBuilder<R>)) -> &mut Self;
}

impl ScriptingApiBuilder for App {
fn add_scripting_api<R: Runtime>(&mut self, f: impl Fn(ScriptingRuntimeBuilder<R>)) -> &mut Self {
let runtime = ScriptingRuntimeBuilder::<R>::new(&mut self.world);

f(runtime);

self
}
}

Loading