Skip to content

Commit fd61780

Browse files
committed
Pass the field type to export functions, if requested
1 parent eef8a6a commit fd61780

File tree

3 files changed

+42
-12
lines changed

3 files changed

+42
-12
lines changed

godot-core/src/registry/property.rs

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,7 @@ pub mod export_info_functions {
227227
use crate::builtin::GString;
228228
use crate::global::PropertyHint;
229229
use crate::meta::PropertyHintInfo;
230+
use crate::registry::property::Export;
230231

231232
/// Turn a list of variables into a comma separated string containing only the identifiers corresponding
232233
/// to a true boolean variable.
@@ -412,18 +413,39 @@ pub mod export_info_functions {
412413
/// Equivalent to `@export_file` in Godot.
413414
///
414415
/// Pass an empty string to have no filter.
415-
pub fn export_file<S: AsRef<str>>(filter: S) -> PropertyHintInfo {
416-
export_file_inner(false, filter)
416+
pub fn export_file<T: Export>(filter: impl AsRef<str>) -> PropertyHintInfo {
417+
//T::var_hint()
418+
export_file_inner::<T>(false, filter)
419+
}
420+
421+
/// Equivalent to `@export_dir` in Godot.
422+
///
423+
/// Pass an empty string to have no filter.
424+
pub fn export_dir<T: Export>() -> PropertyHintInfo {
425+
PropertyHintInfo {
426+
hint: PropertyHint::DIR,
427+
hint_string: GString::new(),
428+
}
429+
}
430+
431+
/// Equivalent to `@export_global_dir` in Godot.
432+
///
433+
/// Pass an empty string to have no filter.
434+
pub fn export_global_dir<T: Export>() -> PropertyHintInfo {
435+
PropertyHintInfo {
436+
hint: PropertyHint::GLOBAL_DIR,
437+
hint_string: GString::new(),
438+
}
417439
}
418440

419441
/// Equivalent to `@export_global_file` in Godot.
420442
///
421443
/// Pass an empty string to have no filter.
422-
pub fn export_global_file<S: AsRef<str>>(filter: S) -> PropertyHintInfo {
423-
export_file_inner(true, filter)
444+
pub fn export_global_file<T: Export>(filter: impl AsRef<str>) -> PropertyHintInfo {
445+
export_file_inner::<T>(true, filter)
424446
}
425447

426-
pub fn export_file_inner<S: AsRef<str>>(global: bool, filter: S) -> PropertyHintInfo {
448+
pub fn export_file_inner<T: Export>(global: bool, filter: impl AsRef<str>) -> PropertyHintInfo {
427449
let hint = if global {
428450
PropertyHint::GLOBAL_FILE
429451
} else {
@@ -468,8 +490,6 @@ pub mod export_info_functions {
468490
export_flags_3d_physics => LAYERS_3D_PHYSICS,
469491
export_flags_3d_render => LAYERS_3D_RENDER,
470492
export_flags_3d_navigation => LAYERS_3D_NAVIGATION,
471-
export_dir => DIR,
472-
export_global_dir => GLOBAL_DIR,
473493
export_multiline => MULTILINE_TEXT,
474494
export_color_no_alpha => COLOR_NO_ALPHA,
475495
);

godot-macros/src/class/data_models/field_export.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,15 @@ macro_rules! quote_export_func {
388388
Some(quote! {
389389
::godot::register::property::export_info_functions::$function_name($($tt)*)
390390
})
391-
}
391+
};
392+
393+
// Passes in a previously declared local `type FieldType = ...` as first generic argument.
394+
// Doesn't work if function takes other generic arguments -- in that case it could be converted to a Type<...> parameter.
395+
($function_name:ident < T > ($($tt:tt)*)) => {
396+
Some(quote! {
397+
::godot::register::property::export_info_functions::$function_name::<FieldType>($($tt)*)
398+
})
399+
};
392400
}
393401

394402
impl ExportType {
@@ -489,20 +497,20 @@ impl ExportType {
489497
Self::File {
490498
global: false,
491499
kind: FileKind::Dir,
492-
} => quote_export_func! { export_dir() },
500+
} => quote_export_func! { export_dir<T>() },
493501

494502
Self::File {
495503
global: true,
496504
kind: FileKind::Dir,
497-
} => quote_export_func! { export_global_dir() },
505+
} => quote_export_func! { export_global_dir<T>() },
498506

499507
Self::File {
500508
global,
501509
kind: FileKind::File { filter },
502510
} => {
503511
let filter = filter.clone().unwrap_or(quote! { "" });
504512

505-
quote_export_func! { export_file_inner(#global, #filter) }
513+
quote_export_func! { export_file_inner<T>(#global, #filter) }
506514
}
507515

508516
Self::Multiline => quote_export_func! { export_multiline() },

godot-macros/src/class/data_models/property.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,9 @@ pub fn make_property_impl(class_name: &Ident, fields: &Fields) -> TokenStream {
154154
);
155155

156156
export_tokens.push(quote! {
157-
::godot::register::private::#registration_fn::<#class_name, #field_type>(
157+
// This type may be reused in #hint, in case of generic functions.
158+
type FieldType = #field_type;
159+
::godot::register::private::#registration_fn::<#class_name, FieldType>(
158160
#field_name,
159161
#getter_tokens,
160162
#setter_tokens,

0 commit comments

Comments
 (0)