Skip to content

Commit 427e682

Browse files
sylbethBromeon
authored andcommitted
Implement inner methods in outer struct for NodePath
Add desktop.ini to gitignore (for icons).
1 parent 8eaf184 commit 427e682

File tree

3 files changed

+86
-3
lines changed

3 files changed

+86
-3
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,6 @@ exp.rs
2626

2727
# Mac specific
2828
.DS_Store
29+
30+
# Windows specific
31+
desktop.ini

godot-codegen/src/special_cases/special_cases.rs

+5
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,11 @@ pub fn is_builtin_method_exposed(builtin_ty: &TyName, godot_method_name: &str) -
395395
| ("StringName", "to_wchar_buffer")
396396

397397
// NodePath
398+
| ("NodePath", "is_absolute")
399+
| ("NodePath", "is_empty")
400+
| ("NodePath", "get_concatenated_names")
401+
| ("NodePath", "get_concatenated_subnames")
402+
//| ("NodePath", "get_as_property_path")
398403

399404
// Callable
400405
| ("Callable", "call")

godot-core/src/builtin/string/node_path.rs

+78-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,13 @@
55
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
66
*/
77

8-
use std::fmt;
8+
use std::{
9+
fmt,
10+
ops::{
11+
Bound::{Excluded, Included, Unbounded},
12+
RangeBounds,
13+
},
14+
};
915

1016
use godot_ffi as sys;
1117
use godot_ffi::{ffi_methods, GodotFfi};
@@ -41,8 +47,33 @@ impl NodePath {
4147
Self { opaque }
4248
}
4349

44-
pub fn is_empty(&self) -> bool {
45-
self.as_inner().is_empty()
50+
/// Returns the number of node names in the path. Property subnames are not included.
51+
pub fn get_name_count(&self) -> u32 {
52+
self.as_inner()
53+
.get_name_count()
54+
.try_into()
55+
.expect("Godot name counts are non negative int")
56+
}
57+
58+
/// Returns the node name indicated by `idx`, starting from 0. If `idx` is out of bounds, [`None`] is returned.
59+
/// See also [`NodePath::get_subname_count`] and [`NodePath::get_name_count`].
60+
pub fn get_name(&self, idx: u32) -> Option<StringName> {
61+
let inner = self.as_inner();
62+
let idx = idx as i64;
63+
// This check checks both data being empty (get_name_count returns 0) and index out of bounds.
64+
if idx >= inner.get_name_count() {
65+
None
66+
} else {
67+
Some(inner.get_name(idx))
68+
}
69+
}
70+
71+
/// Returns the number of property names ("subnames") in the path. Each subname in the node path is listed after a colon character (`:`).
72+
pub fn get_subname_count(&self) -> u32 {
73+
self.as_inner()
74+
.get_subname_count()
75+
.try_into()
76+
.expect("Godot subname counts are non negative int")
4677
}
4778

4879
/// Returns a 32-bit integer hash value representing the string.
@@ -53,6 +84,50 @@ impl NodePath {
5384
.expect("Godot hashes are uint32_t")
5485
}
5586

87+
/// Returns the property name indicated by `idx`, starting from 0. If `idx` is out of bounds, [`None`] is returned.
88+
/// See also [`NodePath::get_subname_count`].
89+
pub fn get_subname(&self, idx: u32) -> Option<StringName> {
90+
let inner = self.as_inner();
91+
let idx = idx as i64;
92+
// This check checks both data being empty (get_subname_count returns 0) and index out of bounds.
93+
if idx >= inner.get_subname_count() {
94+
None
95+
} else {
96+
Some(inner.get_subname(idx))
97+
}
98+
}
99+
100+
/// Returns the slice of the [`NodePath`] as a new [`NodePath`]
101+
pub fn slice(&self, range: impl RangeBounds<i64>) -> NodePath {
102+
self.as_inner().slice(
103+
match range.start_bound() {
104+
Excluded(&start) => {
105+
if start == -1 {
106+
// Default end from godot, since the start is excluded.
107+
i32::MAX as i64
108+
} else {
109+
start + 1
110+
}
111+
}
112+
Included(&start) => start,
113+
Unbounded => 0,
114+
},
115+
match range.end_bound() {
116+
Excluded(&end) => end,
117+
Included(&end) => {
118+
if end == -1 {
119+
// Default end from godot, since the end is excluded.
120+
i32::MAX as i64
121+
} else {
122+
end + 1
123+
}
124+
}
125+
// Default end from godot.
126+
Unbounded => i32::MAX as i64,
127+
},
128+
)
129+
}
130+
56131
crate::meta::declare_arg_method! {
57132
/// Use as argument for an [`impl AsArg<GString|StringName>`][crate::meta::AsArg] parameter.
58133
///

0 commit comments

Comments
 (0)