Skip to content

Added Godot exportable methods along with the expected signature #781

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 22, 2021
Merged
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
4 changes: 4 additions & 0 deletions gdnative-derive/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,7 @@ proc-macro = true
syn = { version = "1.0.74", features = ["full", "extra-traits", "visit"] }
quote = "1.0.9"
proc-macro2 = "1.0.28"

[dev-dependencies]
# This is included for the doc tests.
gdnative = { path = "../gdnative" }
147 changes: 132 additions & 15 deletions gdnative-derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ mod variant;
/// Collects method signatures of all functions in a `NativeClass` that have the `#[export]` attribute and registers them with Godot.
///
/// For example, in the following class
/// ```ignore
/// ```
/// use gdnative::prelude::*;
///
/// #[derive(NativeClass)]
/// #[inherit(Reference)]
/// #[no_constructor]
Expand All @@ -35,16 +37,25 @@ mod variant;
///
/// ```
/// Will expand to
/// ```ignore
/// ```
/// use gdnative::prelude::*;
/// struct Foo{}
/// impl NativeClass for Foo {
/// type Base = gdnative::api::Reference;
/// type UserData = gdnative::nativescript::user_data::LocalCellData<Self>;
/// fn class_name() -> &'static str {
/// "Foo"
/// }
/// }
/// impl gdnative::nativescript::NativeClassMethods for Foo {
/// fn register(builder: &ClassBuilder<Self>) {
/// use gdnative::nativescript::init::*;
/// builder.build_method("foo", gdnative::godot_wrap_method!(Foo::foo))
/// .with_rpc_mode(Rpc::Disabled)
/// builder.build_method("foo", gdnative::godot_wrap_method!(Foo, fn foo(&self, _owner: &Reference, bar: i64) -> i64))
/// .with_rpc_mode(RpcMode::Disabled)
/// .done_stateless();
/// }
///
/// }
/// impl Foo {
/// fn foo(&self, _owner: &Reference, bar: i64) -> i64 {
/// bar
/// }
Expand Down Expand Up @@ -150,18 +161,27 @@ pub fn profiled(meta: TokenStream, input: TokenStream) -> TokenStream {
/// Use a custom function to register signals, properties or methods, in addition
/// to the one generated by `#[methods]`:
///
/// ```ignore
/// ```
/// use gdnative::prelude::*;
/// use gdnative::nativescript::property::{RangeHint, FloatHint};
///
/// #[derive(NativeClass)]
/// #[inherit(Reference)]
/// #[register_with(my_register_function)]
/// #[register_with(Self::my_register_function)]
/// struct Foo;
///
/// fn my_register_function(builder: &ClassBuilder<Foo>) {
/// builder.add_signal(Signal { name: "foo", args: &[] });
/// builder.add_property::<f32>("bar")
/// .with_getter(|_, _| 42.0)
/// .with_hint(FloatHint::Range(RangeHint::new(0.0, 100.0)))
/// .done();
/// #[methods]
/// impl Foo {
/// fn new(_: &Reference) -> Self {
/// Self {}
/// }
/// fn my_register_function(builder: &ClassBuilder<Foo>) {
/// builder.add_signal(Signal { name: "foo", args: &[] });
/// builder.add_property::<f32>("bar")
/// .with_getter(|_, _| 42.0)
/// .with_hint(FloatHint::Range(RangeHint::new(0.0, 100.0)))
/// .done();
/// }
/// }
/// ```
///
Expand Down Expand Up @@ -220,7 +240,7 @@ pub fn profiled(meta: TokenStream, input: TokenStream) -> TokenStream {
///
/// ```ignore
/// #[export]
/// fn foo(&self, owner: &Reference)
/// fn foo(&self, owner: &Reference);
/// ```
/// **Note**: Marking a function with `#[export]` does not have any effect unless inside an `impl` block that has the `#[methods]` attribute.
///
Expand All @@ -241,9 +261,106 @@ pub fn profiled(meta: TokenStream, input: TokenStream) -> TokenStream {
/// - "puppet_sync" - `RPCMode::RPC_MODE_PUPPETSYNC`
///
/// This enables you to set the [Multiplayer API RPC Mode](https://docs.godotengine.org/en/stable/classes/class_multiplayerapi.html?highlight=RPC#enumerations) for the function.
///
/// Refer to [Godot's Remote Procedure documentation](https://docs.godotengine.org/en/stable/tutorials/networking/high_level_multiplayer.html#rpc) for more details.
/// #### `Node` virtual functions
///

/// This is a list of common Godot virtual functions that are automatically called via [notifications](https://docs.godotengine.org/en/stable/classes/class_object.html#class-object-method-notification).
///
/// ```ignore
/// fn _ready(&self, owner: &Node);
/// ```
/// Called when both the node and its children have entered the scene tree.
/// _See [Godot docs](https://docs.godotengine.org/en/stable/classes/class_node.html#class-node-method-ready) for more information._
/// <br><br>
///
/// ```ignore
/// fn _enter_tree(&self, owner: &Node);
/// ```
/// Called when the node enters the scene tree.
/// _See [Godot docs](https://docs.godotengine.org/en/stable/classes/class_node.html#class-node-method-enter-tree) for more information._
/// <br><br>
///
/// ```ignore
/// fn _exit_tree(&self, owner: &Node);
/// ```
/// Called when the node is removed from the scene tree.
/// _See [Godot docs](https://docs.godotengine.org/en/stable/classes/class_node.html#class-node-method-exit-tree) for more information._
/// <br><br>
///
/// ```ignore
/// fn _get_configuration_warning(&self, owner: &Node);
/// ```
/// The string returned from this method is displayed as a warning in the Scene Dock if the script that overrides it is a tool script.
/// _See [Godot docs](https://docs.godotengine.org/en/stable/classes/class_node.html#class-node-method-get-configuration-warning) for more information._
/// <br><br>
///
/// ```ignore
/// fn _process(&mut self, owner: &Node, delta: f64);
/// ```
/// Called during processing step of the main loop.
/// _See [Godot docs](https://docs.godotengine.org/en/stable/classes/class_node.html#class-node-method-process) for more information._
/// <br><br>
///
/// ```ignore
/// fn _physics_process(&self, owner: &Node, delta: f64);
/// ```
/// Called during physics update, with a fixed timestamp.
/// _See [Godot docs](https://docs.godotengine.org/en/stable/classes/class_node.html#class-node-method-physics-process) for more information._
/// <br><br>
///
/// ```ignore
/// fn _input(&self, owner: &Node, event: InputEvent);
/// ```
/// Called when there is an input event.
/// _See [Godot docs](https://docs.godotengine.org/en/stable/classes/class_node.html#class-node-method-input) for more information._
/// <br><br>
///
/// ```ignore
/// fn _unhandled_input(&self, owner: &Node, event: InputEvent);
/// ```
/// Called when an `InputEvent` hasn't been consumed by `_input()` or any GUI.
/// _See [Godot docs](https://docs.godotengine.org/en/stable/classes/class_node.html#class-node-method-unhandled-input) for more information._
/// <br><br>
///
/// ```ignore
/// fn _unhandled_key_input (&self, owner: &Node, event: InputKeyEvent);
/// ```
/// Called when an `InputEventKey` hasn't been consumed by `_input()` or any GUI.
/// _See [Godot docs](https://docs.godotengine.org/en/stable/classes/class_node.html#class-node-method-unhandled-key-input) for more information._
/// <br><br>
///
/// #### `Control` virtual functions
///
/// This is a list of common Godot virtual functions that are automatically called via [notifications](https://docs.godotengine.org/en/stable/classes/class_object.html#class-object-method-notification).
///
/// ```ignore
/// fn _clips_input(&self, owner: &Control) -> bool;
/// ```
/// Returns whether `_gui_input()` should not be called for children controls outside this control's rectangle.
/// _See [Godot docs](https://docs.godotengine.org/en/stable/classes/class_control.html#class-control-method-clips-input) for more information._
/// <br><br>
///
/// ```ignore
/// fn _get_minimum_size(&self, owner: &Control) -> Vector2;
/// ```
/// Returns the minimum size for this control.
/// _See [Godot docs](https://docs.godotengine.org/en/stable/classes/class_control.html#class-control-method-get-minimum-size) for more information._
/// <br><br>
///
/// ```ignore
/// fn _gui_input(&self, owner: &Control, event: InputEvent);
/// ```
/// Use this method to process and accept inputs on UI elements.
/// _See [Godot docs](https://docs.godotengine.org/en/stable/classes/class_control.html#class-control-method-gui-input) for more information._
/// <br><br>
///
/// ```ignore
/// fn _make_custom_tooltip(&self, owner: &Control, for_text: String) -> Ref<Control>;
/// ```
/// Returns a `Control` node that should be used as a tooltip instead of the default one.
/// _See [Godot docs](https://docs.godotengine.org/en/stable/classes/class_control.html#class-control-method-make-custom-tooltip) for more information._
/// <br><br>
#[proc_macro_derive(
NativeClass,
attributes(
Expand Down