1pub use std::path::PathBuf;
2pub use slotmap::DefaultKey;
3
4#[macro_export]
6macro_rules! create_script {
7 ($struct_name:ident { $($field:ident: $value:expr),* $(,)? }) => {
8 #[unsafe(no_mangle)]
9 pub extern "Rust" fn create_script() -> Box<dyn Script> {
10 Box::new($struct_name {
11 $($field: $value),*
12 })
13 }
14 };
15}
16
17pub enum Event {
19 Key{ code: u32 }
20}
21
22pub trait Script: Send {
24 fn init(&mut self, ctx: &impl Context) {
26 println!("Script Init");
27 }
28 fn ready(&mut self, ctx: &impl Context) {
30 println!("Script Ready");
31 }
32 fn update(&mut self, ctx: &impl Context, delta: f32) {
34 println!("Script Update");
35 }
36 fn event(&mut self, ctx: &impl Context, event: Event) {
38 println!("Script Event");
39 }
40}
41
42pub trait Context {
44 fn objects(&self) -> &impl ObjectServer;
46
47 fn log(&self, msg: &str);
49}
50
51pub trait ObjectServer {
53 fn create_root(&self, object_name: String, script_path: Option<PathBuf>) -> Option<DefaultKey>;
56
57 fn create_child(&self, object_name: String, script_path: Option<PathBuf>) -> Option<DefaultKey>;
60
61 fn create_child_for(&self, object_name: String, script_path: Option<PathBuf>, parent_object: DefaultKey) -> Option<DefaultKey>;
64
65 fn remove(&self, object_id: DefaultKey) -> bool;
67
68 fn set_script(&self, object_id: DefaultKey, script_path: PathBuf) -> bool;
70
71 fn remove_script(&self, object_id: DefaultKey) -> bool;
73
74 fn move_to_parent(&self, child_object_id: DefaultKey, new_parent_id: DefaultKey) -> bool;
76
77 fn get_by_id(&self, object_id: DefaultKey) -> Option<DefaultKey>;
78
79 fn get_by_name(&self, object_name: PathBuf) -> Option<DefaultKey>;
80
81 fn test_create_root(&self, object_name: String, script_path: Option<PathBuf>) -> Option<&impl Object>;
82}
83
84pub trait Object {
85 fn add_child(&self, object_name: String, script_path: Option<PathBuf>) -> Option<&impl Object>;
86}