Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions gdnative-core/src/export/property.rs
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,31 @@ impl PropertyUsage {
}
}

/// Marker for defining property groups.
#[derive(Debug)]
pub struct GroupMarker;

impl crate::core_types::ToVariant for GroupMarker {
#[inline]
fn to_variant(&self) -> Variant {
Variant::nil()
}
}

impl crate::core_types::FromVariant for GroupMarker {
#[inline]
fn from_variant(variant: &Variant) -> Result<Self, FromVariantError> {
if variant.is_nil() {
Ok(Self)
} else {
Err(FromVariantError::InvalidVariantType {
variant_type: variant.get_type(),
expected: VariantType::Nil,
})
}
}
}

/// Placeholder type for exported properties with no backing field.
///
/// This is the go-to type whenever you want to expose a getter/setter to GDScript, which
Expand Down Expand Up @@ -591,4 +616,13 @@ mod impl_export {
hint.unwrap_or_default().export_info()
}
}

impl Export for GroupMarker {
type Hint = hint::GroupHint;

#[inline]
fn export_info(hint: Option<Self::Hint>) -> ExportInfo {
hint.unwrap_or_default().export_info()
}
}
}
26 changes: 26 additions & 0 deletions gdnative-core/src/export/property/hint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -465,3 +465,29 @@ impl ArrayHint {
}
}
}

/// Provides a hint prefix for group members.
#[derive(Clone, Debug, Default)]
pub struct GroupHint {
prefix: String,
}

impl GroupHint {
/// Returns a `GroupHint` with a specific prefix.
#[inline]
pub fn new<S: ToString>(prefix: S) -> Self {
Self {
prefix: prefix.to_string(),
}
}

#[inline]
pub fn export_info(self) -> ExportInfo {
ExportInfo {
variant_type: VariantType::Nil,
// hint_kind: sys::godot_property_hint_GODOT_PROPERTY_HINT_TYPE_STRING,
hint_kind: sys::godot_property_hint_GODOT_PROPERTY_HINT_NONE,
hint_string: GodotString::from_str(&self.prefix),
}
}
}
24 changes: 23 additions & 1 deletion gdnative-derive/src/native_script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,32 @@ pub(crate) fn derive_native_class(derive_input: &DeriveInput) -> Result<TokenStr
.map(|(ident, config)| {
let with_default = config
.default
.as_ref()
.map(|default_value| quote!(.with_default(#default_value)));
let with_hint = config.hint.map(|hint_fn| quote!(.with_hint(#hint_fn())));
let with_hint = config
.hint
.as_ref()
.map(|hint_fn| quote!(.with_hint(#hint_fn())))
.or_else(|| {
if let Some(Some(prefix)) = &config.group {
Some(quote!(.with_hint(::gdnative::export::hint::GroupHint::new(#prefix))))
} else {
None
}
});

let with_usage = if config.no_editor {
Some(quote!(.with_usage(::gdnative::export::PropertyUsage::NOEDITOR)))
} else if config.group.is_some() {
Some(quote!(.with_usage({
::gdnative::export::PropertyUsage::DEFAULT
| ::gdnative::export::PropertyUsage::GROUP
})))
} else if config.category {
Some(quote!(.with_usage({
::gdnative::export::PropertyUsage::DEFAULT
| ::gdnative::export::PropertyUsage::CATEGORY
})))
} else {
None
};
Expand Down
29 changes: 29 additions & 0 deletions gdnative-derive/src/native_script/property_args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ pub struct PropertyAttrArgs {
pub set: Option<PropertySet>,
pub after_set: Option<syn::Path>,
pub no_editor: bool,
pub group: Option<Option<String>>,
pub category: bool,
}

pub struct PropertyAttrArgsBuilder {
Expand All @@ -39,6 +41,8 @@ pub struct PropertyAttrArgsBuilder {
set: Option<PropertySet>,
after_set: Option<syn::Path>,
no_editor: bool,
group: Option<Option<String>>,
category: bool,
}

impl PropertyAttrArgsBuilder {
Expand All @@ -55,6 +59,8 @@ impl PropertyAttrArgsBuilder {
set: None,
after_set: None,
no_editor: false,
group: None,
category: false,
}
}

Expand Down Expand Up @@ -120,6 +126,23 @@ impl PropertyAttrArgsBuilder {
));
}
}
"group" => {
let string = if let syn::Lit::Str(lit_str) = &pair.lit {
lit_str.value()
} else {
return Err(syn::Error::new(
pair.span(),
"group hint is not a string literal".to_string(),
));
};

if let Some(old) = self.group.replace(Some(string)) {
return Err(syn::Error::new(
pair.span(),
format!("there is already a group set: {:?}", old),
));
}
}
"before_get" => {
let string = if let syn::Lit::Str(lit_str) = &pair.lit {
lit_str.value()
Expand Down Expand Up @@ -285,6 +308,10 @@ impl PropertyAttrArgsBuilder {
pub fn add_path(&mut self, path: &syn::Path) -> Result<(), syn::Error> {
if path.is_ident("no_editor") {
self.no_editor = true;
} else if path.is_ident("group") {
self.group = Some(None);
} else if path.is_ident("category") {
self.category = true;
} else if path.is_ident("get") {
if let Some(get) = self.get.replace(PropertyGet::Default) {
return Err(syn::Error::new(
Expand Down Expand Up @@ -324,6 +351,8 @@ impl PropertyAttrArgsBuilder {
set: self.set,
after_set: self.after_set,
no_editor: self.no_editor,
group: self.group,
category: self.category,
}
}
}