Skip to content

Commit

Permalink
Merge pull request godot-rust#564 from lilizoey/feature/class-hide
Browse files Browse the repository at this point in the history
Add `hide` annotation to classes
  • Loading branch information
Bromeon authored Jan 11, 2024
2 parents 8d93420 + 268f72a commit e3b61cc
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 1 deletion.
8 changes: 7 additions & 1 deletion godot-core/src/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,8 @@ pub enum PluginComponent {

#[cfg(since_api = "4.1")]
EditorPlugin,
#[cfg(since_api = "4.2")]
Unexposed,
}

// ----------------------------------------------------------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -394,6 +396,10 @@ fn fill_class_info(component: PluginComponent, c: &mut ClassRegistrationInfo) {
PluginComponent::EditorPlugin => {
c.is_editor_plugin = true;
}
#[cfg(since_api = "4.2")]
PluginComponent::Unexposed => {
c.godot_params.is_exposed = false as sys::GDExtensionBool;
}
}
// out!("| reg (after): {c:?}");
// out!();
Expand Down Expand Up @@ -771,6 +777,6 @@ fn default_creation_info() -> sys::GDExtensionClassCreationInfo2 {
call_virtual_with_data_func: None,
get_rid_func: None,
class_userdata: ptr::null_mut(),
is_exposed: true as u8,
is_exposed: true as sys::GDExtensionBool,
}
}
21 changes: 21 additions & 0 deletions godot-macros/src/class/derive_godot_class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,18 @@ pub fn derive_godot_class(decl: Declaration) -> ParseResult<TokenStream> {
quote! {}
};

let hidden = if struct_cfg.is_hidden {
quote! {
::godot::sys::plugin_add!(__GODOT_PLUGIN_REGISTRY in #prv; #prv::ClassPlugin {
class_name: #class_name_obj,
component: #prv::PluginComponent::Unexposed,
init_level: <#class_name as ::godot::obj::GodotClass>::INIT_LEVEL,
});
}
} else {
quote! {}
};

let godot_withbase_impl = if let Some(Field { name, .. }) = &fields.base_field {
quote! {
impl ::godot::obj::WithBaseField for #class_name {
Expand Down Expand Up @@ -144,6 +156,7 @@ pub fn derive_godot_class(decl: Declaration) -> ParseResult<TokenStream> {
});

#editor_plugin
#hidden

#prv::class_macros::#inherits_macro!(#class_name);
})
Expand Down Expand Up @@ -223,6 +236,7 @@ fn parse_struct_attributes(class: &Struct) -> ParseResult<ClassAttributes> {
let mut has_generated_init = false;
let mut is_tool = false;
let mut is_editor_plugin = false;
let mut is_hidden = false;
let mut rename: Option<Ident> = None;

// #[class] attribute on struct
Expand All @@ -243,6 +257,11 @@ fn parse_struct_attributes(class: &Struct) -> ParseResult<ClassAttributes> {
if parser.handle_alone_ident("editor_plugin")?.is_some() {
is_editor_plugin = true;
}

if parser.handle_alone("hide")? {
is_hidden = true;
}

rename = parser.handle_ident("rename")?;

parser.finish()?;
Expand All @@ -253,6 +272,7 @@ fn parse_struct_attributes(class: &Struct) -> ParseResult<ClassAttributes> {
has_generated_init,
is_tool,
is_editor_plugin,
is_hidden,
rename,
})
}
Expand Down Expand Up @@ -342,6 +362,7 @@ struct ClassAttributes {
has_generated_init: bool,
is_tool: bool,
is_editor_plugin: bool,
is_hidden: bool,
rename: Option<Ident>,
}

Expand Down
14 changes: 14 additions & 0 deletions godot-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,20 @@ use crate::util::ident;
/// ```
///
/// These classes will appear in the Godot editor and GDScript as "AnimalToad" or "NpcToad".
///
/// # Hiding Classes
///
/// If you want to register a class with Godot, but not have it show up in the editor then you can use `#[class(hide)]`.
///
/// ```
/// # use godot::prelude::*;
/// #[derive(GodotClass)]
/// #[class(base = Node, init, hide)]
/// pub struct Foo {}
/// ```
///
/// Even though this class is a `Node` and it has an init function, it still won't show up in the editor as a node you can add to a scene
/// because we have added a `hide` key to the class. This will also prevent it from showing up in documentation.
#[proc_macro_derive(GodotClass, attributes(class, base, var, export, init, signal))]
pub fn derive_godot_class(input: TokenStream) -> TokenStream {
translate(input, class::derive_godot_class)
Expand Down

0 comments on commit e3b61cc

Please sign in to comment.