1mod audio;
6pub mod descriptor;
7mod host;
8mod input;
9pub mod physics_bridge;
10mod python;
11pub mod vec2;
12
13use anyhow::Result;
14
15pub use descriptor::{child, node, PyChildDescriptor, PyNodeDescriptor};
16pub use host::{
17 fire_area_entered, fire_area_exited, fire_body_entered, fire_body_exited, fire_pressed,
18 fire_timeout, proxy_for, resolve_caller, resolve_caller_py, set_current_host, NodeProxy,
19 ScriptHost,
20};
21pub use input::{
22 input_begin_frame, input_clear, input_held, input_mouse, input_pressed, input_released,
23 input_set_key, input_set_mouse,
24};
25pub use physics_bridge::{current_physics, set_current_physics, with_physics};
26pub use pyo3;
27pub use python::{is_stdlib_module, InputQueryKind, PyInputQuery, Pyo3Backend, ScriptError};
28pub use vec2::Vec2;
29
30pub const RESERVED_METHODS: &[&str] = &["emit", "connect"];
31
32static PROJECT_DIR: std::sync::Mutex<Option<std::path::PathBuf>> = std::sync::Mutex::new(None);
34
35pub fn set_project_dir(dir: Option<std::path::PathBuf>) {
36 *PROJECT_DIR.lock().expect("project dir lock") = dir;
37}
38
39pub fn project_dir() -> Option<std::path::PathBuf> {
40 PROJECT_DIR.lock().expect("project dir lock").clone()
41}
42
43pub fn is_reserved(name: &str) -> bool {
44 RESERVED_METHODS.contains(&name)
45}
46
47pub trait ScriptBackend {
48 fn load(&mut self, path: &str, class: &str) -> Result<()>;
49 fn call_ready(&mut self) -> Result<()>;
50 fn call_process(&mut self, delta: f64) -> Result<()>;
51 fn call_physics_process(&mut self, _delta: f64) -> Result<()> {
52 Ok(())
53 }
54 fn reload(&mut self) -> Result<()>;
55
56 fn position(&self) -> Option<(f64, f64)> {
57 None
58 }
59
60 fn set_position(&mut self, _x: f64, _y: f64) {}
61
62 fn velocity(&self) -> Option<(f64, f64)> {
63 None
64 }
65
66 fn set_velocity(&mut self, _vx: f64, _vy: f64) {}
67
68 fn set_on_floor(&mut self, _on_floor: bool) {}
69 fn set_on_wall(&mut self, _on_wall: bool) {}
70 fn set_on_ceiling(&mut self, _on_ceiling: bool) {}
71
72 fn text(&self) -> Option<String> {
73 None
74 }
75
76 fn set_text(&mut self, _text: &str) {}
77
78 fn size(&self) -> Option<(f64, f64)> {
79 None
80 }
81
82 fn set_size(&mut self, _w: f64, _h: f64) {}
83
84 fn wait_time(&self) -> Option<f64> {
85 None
86 }
87
88 fn set_wait_time(&mut self, _wait: f64) {}
89
90 fn last_error(&self) -> Option<String> {
91 None
92 }
93
94 fn last_script_error(&self) -> Option<ScriptError> {
95 None
96 }
97}
98
99#[derive(Debug, Default)]
100pub struct NoopBackend;
101
102impl ScriptBackend for NoopBackend {
103 fn load(&mut self, _path: &str, _class: &str) -> Result<()> {
104 Ok(())
105 }
106
107 fn call_ready(&mut self) -> Result<()> {
108 Ok(())
109 }
110
111 fn call_process(&mut self, _delta: f64) -> Result<()> {
112 Ok(())
113 }
114
115 fn reload(&mut self) -> Result<()> {
116 Ok(())
117 }
118}