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
//! Utility functions and extension traits that depend on generated bindings

use gdnative_core::core_types::NodePath;
use gdnative_core::export::NativeClass;
use gdnative_core::object::{SubClass, TInstance, TRef};

use super::generated::{Engine, Node, SceneTree};

/// 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 NodeResolveExt<P: Into<NodePath>> {
    /// 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: P) -> 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: P) -> Option<TInstance<'a, T>>
    where
        T: NativeClass,
        T::Base: SubClass<Node>,
    {
        self.get_node_as::<T::Base>(path)?.cast_instance()
    }
}

impl<N: SubClass<Node>, P: Into<NodePath>> NodeResolveExt<P> for N {
    unsafe fn get_node_as<'a, T>(&self, path: P) -> Option<TRef<'a, T>>
    where
        T: SubClass<Node>,
    {
        self.upcast().get_node(path)?.assume_safe().cast()
    }
}