Minor refactoring of box_shadow
example (#19404)
# Objective Minimal effort to address feedback here: https://github.com/bevyengine/bevy/pull/19345#discussion_r2107844018 more thoroughly. ## Solution - Remove hardcoded label string comparisons and make more use of the new enum added during review - Resist temptation to let this snowball this into a huge refactor - Maybe come back later for a few other small improvements ## Testing `cargo run --example box_shadow`
This commit is contained in:
parent
13e89a1678
commit
3aaadd9b54
@ -94,6 +94,19 @@ enum SettingType {
|
|||||||
Shape,
|
Shape,
|
||||||
Samples,
|
Samples,
|
||||||
}
|
}
|
||||||
|
impl SettingType {
|
||||||
|
fn label(&self) -> &str {
|
||||||
|
match self {
|
||||||
|
SettingType::XOffset => "X Offset",
|
||||||
|
SettingType::YOffset => "Y Offset",
|
||||||
|
SettingType::Blur => "Blur",
|
||||||
|
SettingType::Spread => "Spread",
|
||||||
|
SettingType::Count => "Count",
|
||||||
|
SettingType::Shape => "Shape",
|
||||||
|
SettingType::Samples => "Samples",
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Resource, Default)]
|
#[derive(Resource, Default)]
|
||||||
struct HeldButton {
|
struct HeldButton {
|
||||||
@ -191,42 +204,42 @@ fn setup(
|
|||||||
))
|
))
|
||||||
.insert(children![
|
.insert(children![
|
||||||
build_setting_row(
|
build_setting_row(
|
||||||
"Shape:",
|
SettingType::Shape,
|
||||||
SettingsButton::ShapePrev,
|
SettingsButton::ShapePrev,
|
||||||
SettingsButton::ShapeNext,
|
SettingsButton::ShapeNext,
|
||||||
shape.index as f32,
|
shape.index as f32,
|
||||||
&asset_server,
|
&asset_server,
|
||||||
),
|
),
|
||||||
build_setting_row(
|
build_setting_row(
|
||||||
"X Offset:",
|
SettingType::XOffset,
|
||||||
SettingsButton::XOffsetDec,
|
SettingsButton::XOffsetDec,
|
||||||
SettingsButton::XOffsetInc,
|
SettingsButton::XOffsetInc,
|
||||||
shadow.x_offset,
|
shadow.x_offset,
|
||||||
&asset_server,
|
&asset_server,
|
||||||
),
|
),
|
||||||
build_setting_row(
|
build_setting_row(
|
||||||
"Y Offset:",
|
SettingType::YOffset,
|
||||||
SettingsButton::YOffsetDec,
|
SettingsButton::YOffsetDec,
|
||||||
SettingsButton::YOffsetInc,
|
SettingsButton::YOffsetInc,
|
||||||
shadow.y_offset,
|
shadow.y_offset,
|
||||||
&asset_server,
|
&asset_server,
|
||||||
),
|
),
|
||||||
build_setting_row(
|
build_setting_row(
|
||||||
"Blur:",
|
SettingType::Blur,
|
||||||
SettingsButton::BlurDec,
|
SettingsButton::BlurDec,
|
||||||
SettingsButton::BlurInc,
|
SettingsButton::BlurInc,
|
||||||
shadow.blur,
|
shadow.blur,
|
||||||
&asset_server,
|
&asset_server,
|
||||||
),
|
),
|
||||||
build_setting_row(
|
build_setting_row(
|
||||||
"Spread:",
|
SettingType::Spread,
|
||||||
SettingsButton::SpreadDec,
|
SettingsButton::SpreadDec,
|
||||||
SettingsButton::SpreadInc,
|
SettingsButton::SpreadInc,
|
||||||
shadow.spread,
|
shadow.spread,
|
||||||
&asset_server,
|
&asset_server,
|
||||||
),
|
),
|
||||||
build_setting_row(
|
build_setting_row(
|
||||||
"Count:",
|
SettingType::Count,
|
||||||
SettingsButton::CountDec,
|
SettingsButton::CountDec,
|
||||||
SettingsButton::CountInc,
|
SettingsButton::CountInc,
|
||||||
shadow.count as f32,
|
shadow.count as f32,
|
||||||
@ -234,7 +247,7 @@ fn setup(
|
|||||||
),
|
),
|
||||||
// Add BoxShadowSamples as a setting row
|
// Add BoxShadowSamples as a setting row
|
||||||
build_setting_row(
|
build_setting_row(
|
||||||
"Samples:",
|
SettingType::Samples,
|
||||||
SettingsButton::SamplesDec,
|
SettingsButton::SamplesDec,
|
||||||
SettingsButton::SamplesInc,
|
SettingsButton::SamplesInc,
|
||||||
shadow.samples as f32,
|
shadow.samples as f32,
|
||||||
@ -278,22 +291,18 @@ fn setup(
|
|||||||
|
|
||||||
// Helper to return an input to the children! macro for a setting row
|
// Helper to return an input to the children! macro for a setting row
|
||||||
fn build_setting_row(
|
fn build_setting_row(
|
||||||
label: &str,
|
setting_type: SettingType,
|
||||||
dec: SettingsButton,
|
dec: SettingsButton,
|
||||||
inc: SettingsButton,
|
inc: SettingsButton,
|
||||||
value: f32,
|
value: f32,
|
||||||
asset_server: &Res<AssetServer>,
|
asset_server: &Res<AssetServer>,
|
||||||
) -> impl Bundle {
|
) -> impl Bundle {
|
||||||
let label_type = match label {
|
let value_text = match setting_type {
|
||||||
"X Offset:" => SettingType::XOffset,
|
SettingType::Shape => SHAPES[value as usize % SHAPES.len()].0.to_string(),
|
||||||
"Y Offset:" => SettingType::YOffset,
|
SettingType::Count => format!("{}", value as usize),
|
||||||
"Blur:" => SettingType::Blur,
|
_ => format!("{:.1}", value),
|
||||||
"Spread:" => SettingType::Spread,
|
|
||||||
"Count:" => SettingType::Count,
|
|
||||||
"Shape:" => SettingType::Shape,
|
|
||||||
"Samples:" => SettingType::Samples,
|
|
||||||
_ => panic!("Unknown label: {}", label),
|
|
||||||
};
|
};
|
||||||
|
|
||||||
(
|
(
|
||||||
Node {
|
Node {
|
||||||
flex_direction: FlexDirection::Row,
|
flex_direction: FlexDirection::Row,
|
||||||
@ -311,7 +320,7 @@ fn build_setting_row(
|
|||||||
},
|
},
|
||||||
// Attach SettingType to the value label node, not the parent row
|
// Attach SettingType to the value label node, not the parent row
|
||||||
children![(
|
children![(
|
||||||
Text::new(label),
|
Text::new(setting_type.label()),
|
||||||
TextFont {
|
TextFont {
|
||||||
font: asset_server.load("fonts/FiraSans-Bold.ttf"),
|
font: asset_server.load("fonts/FiraSans-Bold.ttf"),
|
||||||
font_size: 16.0,
|
font_size: 16.0,
|
||||||
@ -333,7 +342,11 @@ fn build_setting_row(
|
|||||||
BorderRadius::all(Val::Px(6.)),
|
BorderRadius::all(Val::Px(6.)),
|
||||||
dec,
|
dec,
|
||||||
children![(
|
children![(
|
||||||
Text::new(if label == "Shape:" { "<" } else { "-" }),
|
Text::new(if setting_type == SettingType::Shape {
|
||||||
|
"<"
|
||||||
|
} else {
|
||||||
|
"-"
|
||||||
|
}),
|
||||||
TextFont {
|
TextFont {
|
||||||
font: asset_server.load("fonts/FiraSans-Bold.ttf"),
|
font: asset_server.load("fonts/FiraSans-Bold.ttf"),
|
||||||
font_size: 18.0,
|
font_size: 18.0,
|
||||||
@ -352,31 +365,15 @@ fn build_setting_row(
|
|||||||
},
|
},
|
||||||
BorderRadius::all(Val::Px(6.)),
|
BorderRadius::all(Val::Px(6.)),
|
||||||
children![{
|
children![{
|
||||||
if label_type == SettingType::Shape {
|
(
|
||||||
(
|
Text::new(value_text),
|
||||||
Text::new(SHAPES[value as usize % SHAPES.len()].0),
|
TextFont {
|
||||||
TextFont {
|
font: asset_server.load("fonts/FiraSans-Bold.ttf"),
|
||||||
font: asset_server.load("fonts/FiraSans-Bold.ttf"),
|
font_size: 16.0,
|
||||||
font_size: 16.0,
|
..default()
|
||||||
..default()
|
},
|
||||||
},
|
setting_type,
|
||||||
label_type,
|
)
|
||||||
)
|
|
||||||
} else {
|
|
||||||
(
|
|
||||||
Text::new(if label_type == SettingType::Count {
|
|
||||||
format!("{}", value as usize)
|
|
||||||
} else {
|
|
||||||
format!("{:.1}", value)
|
|
||||||
}),
|
|
||||||
TextFont {
|
|
||||||
font: asset_server.load("fonts/FiraSans-Bold.ttf"),
|
|
||||||
font_size: 16.0,
|
|
||||||
..default()
|
|
||||||
},
|
|
||||||
label_type,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}],
|
}],
|
||||||
),
|
),
|
||||||
(
|
(
|
||||||
@ -392,7 +389,11 @@ fn build_setting_row(
|
|||||||
BorderRadius::all(Val::Px(6.)),
|
BorderRadius::all(Val::Px(6.)),
|
||||||
inc,
|
inc,
|
||||||
children![(
|
children![(
|
||||||
Text::new(if label == "Shape:" { ">" } else { "+" }),
|
Text::new(if setting_type == SettingType::Shape {
|
||||||
|
">"
|
||||||
|
} else {
|
||||||
|
"+"
|
||||||
|
}),
|
||||||
TextFont {
|
TextFont {
|
||||||
font: asset_server.load("fonts/FiraSans-Bold.ttf"),
|
font: asset_server.load("fonts/FiraSans-Bold.ttf"),
|
||||||
font_size: 18.0,
|
font_size: 18.0,
|
||||||
|
Loading…
Reference in New Issue
Block a user