Skip to main content

oxide_engine_api/
lib.rs

1pub use std::path::PathBuf;
2pub use slotmap::DefaultKey;
3
4// === СОЗДАНИЕ СКРИПТА ===
5#[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
17// === События ===
18pub enum Event {
19	Key{ code: u32 }
20}
21
22// === ФУНКЦИИ СКРИПТА ===
23pub trait Script: Send {
24	/// Выполняется один раз во время загрузки файла библиотеки
25	fn init(&mut self, ctx: &impl Context) {
26		println!("Script Init");
27	}
28	/// Выполняется когда ---
29	fn ready(&mut self, ctx: &impl Context) {
30		println!("Script Ready");
31	}
32	/// Выполняется каждый main loop
33	fn update(&mut self, ctx: &impl Context, delta: f32) {
34		println!("Script Update");
35	}
36	/// Вызывается при вводе
37	fn event(&mut self, ctx: &impl Context, event: Event) {
38		println!("Script Event");
39	}
40}
41
42// === МЕТОДЫ КОНТЕКСТА ===
43pub trait Context {
44	/// Доступ к подсистеме объектов
45	fn objects(&self) -> &impl ObjectServer;
46
47	/// Логирование (можно вынести в отдельную подсистему позже)
48	fn log(&self, msg: &str);
49}
50
51// === ПОДСИСТЕМА ОБЪЕКТОВ ===
52pub trait ObjectServer {
53	/// Создаёт корневой объект
54	/// Возвращает `None`, если родитель не существует
55	fn create_root(&self, object_name: String, script_path: Option<PathBuf>) -> Option<DefaultKey>;
56
57	/// Создаёт дочерний объект для себя
58	/// Возвращает `None`, если родитель не существует
59	fn create_child(&self, object_name: String, script_path: Option<PathBuf>) -> Option<DefaultKey>;
60
61	/// Создаёт дочерний объект для parent_object
62	/// Возвращает `None`, если родитель не существует
63	fn create_child_for(&self, object_name: String, script_path: Option<PathBuf>, parent_object: DefaultKey) -> Option<DefaultKey>;
64
65	/// Удаляет объект и всё его поддерево
66	fn remove(&self, object_id: DefaultKey) -> bool;
67
68	///
69	fn set_script(&self, object_id: DefaultKey, script_path: PathBuf) -> bool;
70
71	///
72	fn remove_script(&self, object_id: DefaultKey) -> bool;
73
74	///
75	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}