use crate::get_api;
use crate::sys;
use crate::FromVariant;
use crate::GodotString;
use crate::ToVariant;
use crate::Variant;
use std::fmt;
pub struct NodePath(pub(crate) sys::godot_node_path);
impl NodePath {
pub fn from_str(path: &str) -> Self {
unsafe {
let mut dest = sys::godot_node_path::default();
let api = get_api();
let mut from = (api.godot_string_chars_to_utf8_with_len)(
path.as_ptr() as *const _,
path.len() as _,
);
(api.godot_node_path_new)(&mut dest, &from);
(api.godot_string_destroy)(&mut from);
NodePath(dest)
}
}
pub fn new(path: &GodotString) -> Self {
unsafe {
let mut dest = sys::godot_node_path::default();
(get_api().godot_node_path_new)(&mut dest, &path.0);
NodePath(dest)
}
}
pub fn is_empty(&self) -> bool {
unsafe { (get_api().godot_node_path_is_empty)(&self.0) }
}
pub fn is_absolute(&self) -> bool {
unsafe { (get_api().godot_node_path_is_absolute)(&self.0) }
}
pub fn name_count(&mut self) -> i32 {
unsafe { (get_api().godot_node_path_get_name_count)(&mut self.0) }
}
pub fn get_subname(&self, idx: i32) -> GodotString {
unsafe { GodotString((get_api().godot_node_path_get_subname)(&self.0, idx)) }
}
pub fn get_subname_count(&self) -> i32 {
unsafe { (get_api().godot_node_path_get_subname_count)(&self.0) }
}
pub fn get_concatenated_subnames(&self) -> GodotString {
unsafe {
GodotString((get_api().godot_node_path_get_concatenated_subnames)(
&self.0,
))
}
}
pub fn to_godot_string(&self) -> GodotString {
unsafe { GodotString((get_api().godot_node_path_as_string)(&self.0)) }
}
pub fn to_string(&self) -> String {
self.to_godot_string().to_string()
}
#[doc(hidden)]
pub fn sys(&self) -> *const sys::godot_node_path {
&self.0
}
#[doc(hidden)]
pub fn from_sys(sys: sys::godot_node_path) -> Self {
NodePath(sys)
}
impl_common_methods! {
pub fn new_ref(&self) -> NodePath : godot_node_path_new_copy;
}
}
impl<S> From<S> for NodePath
where
S: AsRef<str>,
{
fn from(s: S) -> NodePath {
NodePath::from_str(&s.as_ref())
}
}
impl Into<String> for NodePath {
fn into(self) -> String {
self.to_string()
}
}
impl From<GodotString> for NodePath {
fn from(s: GodotString) -> NodePath {
NodePath::new(&s)
}
}
impl Into<GodotString> for NodePath {
fn into(self) -> GodotString {
self.to_godot_string()
}
}
impl_basic_traits!(
for NodePath as godot_node_path {
Drop => godot_node_path_destroy;
Eq => godot_node_path_operator_equal;
}
);
impl ToVariant for NodePath {
fn to_variant(&self) -> Variant {
Variant::from_node_path(self)
}
}
impl FromVariant for NodePath {
fn from_variant(variant: &Variant) -> Option<Self> {
variant.try_to_node_path()
}
}
impl fmt::Debug for NodePath {
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
write!(f, "NodePath({})", self.to_string())
}
}