-
Notifications
You must be signed in to change notification settings - Fork 208
/
utils.rs
85 lines (79 loc) · 3.15 KB
/
utils.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
//! Utility functions and extension traits that depend on generated bindings
use super::generated::{Engine, Node, SceneTree};
use gdnative_core::nativescript::{NativeClass, RefInstance};
use gdnative_core::object::ownership::Shared;
use gdnative_core::object::{SubClass, TRef};
/// Convenience method to obtain a reference to an "auto-load" node, that is a child of the root
/// node. Returns `None` if the node does not exist or is not of the correct type.
///
/// # Safety
///
/// This method accesses the scene tree. As a result, any calls to this function must
/// follow the official [thread-safety guidelines][thread-safety]. `assume_safe`
/// invariants must be observed for the resulting node during `'a`, if any.
///
/// [thread-safety]: https://docs.godotengine.org/en/stable/tutorials/threads/thread_safe_apis.html
pub unsafe fn autoload<'a, T>(name: &str) -> Option<TRef<'a, T>>
where
T: SubClass<Node>,
{
Engine::godot_singleton()
.get_main_loop()?
.assume_safe()
.cast::<SceneTree>()?
.root()?
.assume_safe()
.get_node(name)?
.assume_safe()
.cast::<T>()
}
pub trait NodeExt {
/// Convenience method to obtain a reference to a node at `path` relative to `self`,
/// and cast it to the desired type. Returns `None` if the node does not exist or is
/// not of the correct type.
///
/// # Safety
///
/// This method accesses the scene tree. As a result, any calls to this function must
/// follow the official [thread-safety guidelines][thread-safety]. `assume_safe`
/// invariants must be observed for the resulting node during `'a`, if any.
///
/// [thread-safety]: https://docs.godotengine.org/en/stable/tutorials/threads/thread_safe_apis.html
unsafe fn get_node_as<'a, T>(&self, path: &str) -> Option<TRef<'a, T>>
where
T: SubClass<Node>;
/// Convenience method to obtain a reference to a node at `path` relative to `self`,
/// and cast it to an instance of the desired `NativeClass` type. Returns `None` if
/// the node does not exist or is not of the correct type.
///
/// # Safety
///
/// This method accesses the scene tree. As a result, any calls to this function must
/// follow the official [thread-safety guidelines][thread-safety]. `assume_safe`
/// invariants must be observed for the resulting node during `'a`, if any.
///
/// [thread-safety]: https://docs.godotengine.org/en/stable/tutorials/threads/thread_safe_apis.html
unsafe fn get_node_as_instance<'a, T>(&self, path: &str) -> Option<RefInstance<'a, T, Shared>>
where
T: NativeClass,
T::Base: SubClass<Node>,
{
self.get_node_as::<T::Base>(path)?.cast_instance()
}
}
impl<'n, N: SubClass<Node>> NodeExt for &'n N {
unsafe fn get_node_as<'a, T>(&self, path: &str) -> Option<TRef<'a, T>>
where
T: SubClass<Node>,
{
self.upcast().get_node(path)?.assume_safe().cast()
}
}
impl<'n, N: SubClass<Node>> NodeExt for TRef<'n, N> {
unsafe fn get_node_as<'a, T>(&self, path: &str) -> Option<TRef<'a, T>>
where
T: SubClass<Node>,
{
self.as_ref().get_node_as(path)
}
}