-
Notifications
You must be signed in to change notification settings - Fork 208
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add SignalBuilder struct; rename SignalArgument -> SignalParam; integ…
…rate into ClassBuilder
- Loading branch information
Showing
12 changed files
with
194 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
use crate::core_types::{GodotString, Variant, VariantType}; | ||
use crate::export::{ClassBuilder, ExportInfo, NativeClass, PropertyUsage}; | ||
|
||
/// Class to construct a signal. Make sure to call [`Self::done()`] in the end. | ||
/// | ||
/// Signal parameters can be added with the various `param*()` methods. | ||
/// Keep in mind that unlike function parameters, signal parameters (both their lengths and types) | ||
/// are not statically checked in Godot. The parameter signature you specify is simply to assist you | ||
/// in the editor UI and with auto-generation of GDScript signal handlers. | ||
pub struct SignalBuilder<'a, C> { | ||
class_builder: &'a ClassBuilder<C>, | ||
name: GodotString, | ||
args: Vec<SignalParam>, | ||
} | ||
|
||
impl<'a, C: NativeClass> SignalBuilder<'a, C> { | ||
pub(super) fn new(class_builder: &'a ClassBuilder<C>, signal_name: GodotString) -> Self { | ||
Self { | ||
class_builder, | ||
name: signal_name, | ||
args: vec![], | ||
} | ||
} | ||
|
||
/// Add a (untyped) parameter for the signal with a name. | ||
#[inline] | ||
pub fn param(self, parameter_name: impl Into<GodotString>) -> Self { | ||
// Note: the use of 'Nil' to express "untyped" is not following official documentation and could be improved. | ||
|
||
self.param_custom(SignalParam { | ||
name: parameter_name.into(), | ||
default: Variant::nil(), | ||
export_info: ExportInfo::new(VariantType::Nil), | ||
usage: PropertyUsage::DEFAULT, | ||
}) | ||
} | ||
|
||
/// Add a parameter for the signal with a name and default value. | ||
/// | ||
/// The type is inferred from the default value. | ||
/// Note that GDScript signal parameters are generally untyped and not checked at runtime. | ||
/// The type is solely used for UI purposes. | ||
#[inline] | ||
pub fn param_default( | ||
self, | ||
parameter_name: impl Into<GodotString>, | ||
default_value: Variant, | ||
) -> Self { | ||
let variant_type = default_value.get_type(); | ||
|
||
self.param_custom(SignalParam { | ||
name: parameter_name.into(), | ||
default: default_value, | ||
export_info: ExportInfo::new(variant_type), | ||
usage: PropertyUsage::DEFAULT, | ||
}) | ||
} | ||
|
||
/// Add a parameter for the signal with a name and type. | ||
/// | ||
/// Note that GDScript signal parameters are generally untyped and not checked at runtime. | ||
/// The type is solely used for UI purposes. | ||
#[inline] | ||
pub fn param_typed( | ||
self, | ||
parameter_name: impl Into<GodotString>, | ||
parameter_type: VariantType, | ||
) -> Self { | ||
self.param_custom(SignalParam { | ||
name: parameter_name.into(), | ||
default: Variant::nil(), | ||
export_info: ExportInfo::new(parameter_type), | ||
usage: PropertyUsage::DEFAULT, | ||
}) | ||
} | ||
|
||
/// Add a parameter for the signal, manually configured. | ||
#[inline] | ||
pub fn param_custom(mut self, parameter: SignalParam) -> Self { | ||
self.args.push(parameter); | ||
self | ||
} | ||
|
||
/// Finish registering the signal. | ||
#[inline] | ||
pub fn done(self) { | ||
self.class_builder.add_signal(Signal { | ||
name: self.name, | ||
args: self.args, | ||
}); | ||
} | ||
} | ||
|
||
pub(crate) struct Signal { | ||
pub name: GodotString, | ||
pub args: Vec<SignalParam>, | ||
} | ||
|
||
/// Parameter in a signal declaration. | ||
/// | ||
/// Instead of providing values for each field, check out the `param*()` methods in [`SignalBuilder`]. | ||
pub struct SignalParam { | ||
/// Parameter name. | ||
pub name: GodotString, | ||
|
||
/// Default value, used when no argument is provided. | ||
pub default: Variant, | ||
|
||
/// Metadata and UI hints about exporting, e.g. parameter type. | ||
pub export_info: ExportInfo, | ||
|
||
/// In which context the signal parameter is used. | ||
pub usage: PropertyUsage, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters