Skip to content

Addition of step in export_range #484

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
Nov 21, 2023
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
12 changes: 8 additions & 4 deletions godot-core/src/property.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,22 +153,26 @@ pub mod export_info_functions {
pub fn export_range(
min: f64,
max: f64,
step: Option<f64>,
or_greater: bool,
or_less: bool,
exp: bool,
radians: bool,
degrees: bool,
hide_slider: bool,
) -> PropertyHintInfo {
let min_max = format!("{},{}", min, max);

let hint_beginning = if let Some(step) = step {
format!("{min},{max},{step}")
} else {
format!("{min},{max}")
};
let rest =
comma_separate_boolean_idents!(or_greater, or_less, exp, radians, degrees, hide_slider);

let hint_string = if rest.is_empty() {
min_max
hint_beginning
} else {
format!("{min_max},{rest}")
format!("{hint_beginning},{rest}")
};

PropertyHintInfo {
Expand Down
14 changes: 13 additions & 1 deletion godot-macros/src/class/data_models/field_export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ pub enum FieldExport {
Range {
min: TokenStream,
max: TokenStream,
step: TokenStream,
or_greater: bool,
or_less: bool,
exp: bool,
Expand Down Expand Up @@ -265,6 +266,15 @@ impl FieldExport {

let min = parser.next_expr()?;
let max = parser.next_expr()?;
// TODO: During parser refactor, try to remove the need for `is_next_ident` there. Currently needed only for this functionality.
// See discussion for rationale here: https://github.com/godot-rust/gdext/pull/484#pullrequestreview-1738612069
let step = match parser.is_next_ident() {
Some(false) => {
let value = parser.next_expr()?;
quote! { Some(#value) }
}
_ => quote! { None },
};

let mut options = HashSet::new();

Expand All @@ -277,6 +287,7 @@ impl FieldExport {
Ok(FieldExport::Range {
min,
max,
step,
or_greater: options.contains("or_greater"),
or_less: options.contains("or_less"),
exp: options.contains("exp"),
Expand Down Expand Up @@ -360,14 +371,15 @@ impl FieldExport {
FieldExport::Range {
min,
max,
step,
or_greater,
or_less,
exp,
radians,
degrees,
hide_slider,
} => quote_export_func! {
export_range(#min, #max, #or_greater, #or_less, #exp, #radians, #degrees, #hide_slider)
export_range(#min, #max, #step, #or_greater, #or_less, #exp, #radians, #degrees, #hide_slider)
},

FieldExport::Enum { variants } => {
Expand Down
13 changes: 13 additions & 0 deletions godot-macros/src/util/list_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,19 @@ impl ListParser {
Ok(Some(kv.ident()?))
}

/// Check if the next element of the list is an identifier.
///
/// Returns `None` if there are no more elements left, `Some(true)` if the next element is identifier and `Some(false)` if it is not.
pub fn is_next_ident(&mut self) -> Option<bool> {
let Some(kv) = self.peek() else {
return None;
};

let res = kv.as_ident();

Some(res.is_ok())
}

/// Take the next element of the list, if it is an identifier.
///
/// Returns `Ok(None)` if there are no more elements left or the next element isn't an identifier.
Expand Down
6 changes: 3 additions & 3 deletions itest/rust/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -424,8 +424,8 @@ fn generate_property_template(inputs: &[Input]) -> PropertyTests {
export_multiline: GString,
#[export(range = (0.0, 20.0))]
export_range_float_0_20: f64,
// We're missing step currently.
// #[export(range = (-10, 20 /*, 0.2 */))] export_range_float_neg_10_20_02: f64,
#[export(range = (-10.0, 20.0, 0.2))]
export_range_float_neg10_20_02: f64,
// We can only export ranges of floats currently.
// #[export(range = (0, 100, 1, "or_greater", "or_less"))] export_range_int_0_100_1_or_greater_or_less: int,
#[export(exp_easing)]
Expand Down Expand Up @@ -473,7 +473,7 @@ fn generate_property_template(inputs: &[Input]) -> PropertyTests {
@export_global_dir var export_global_dir: String
@export_multiline var export_multiline: String
@export_range(0, 20) var export_range_float_0_20: float
@export_range(-10, 20, 0.2) var export_range_float_neg_10_20_02: float
@export_range(-10, 20, 0.2) var export_range_float_neg10_20_02: float
@export_range(0, 100, 1, "or_greater", "or_less") var export_range_int_0_100_1_or_greater_or_less: int
@export_exp_easing var export_exp_easing: float
@export_color_no_alpha var export_color_no_alpha: Color
Expand Down
3 changes: 3 additions & 0 deletions itest/rust/src/object_tests/property_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,9 @@ struct CheckAllExports {
#[export(range = (0.0, 10.0, or_greater, or_less, exp, radians, hide_slider))]
range_exported: f64,

#[export(range = (0.0, 10.0, 0.2, or_greater, or_less, exp, radians, hide_slider))]
range_exported_with_step: f64,

#[export(enum = (A = 10, B, C, D = 20))]
enum_exported: i64,

Expand Down