Skip to content

Commit d92b1fc

Browse files
authored
small improvements on the color_grading example (#13542)
# Objective - Small improvements on the `color_grading` example ## Solution - Simplify button creation by creating them in the default state, the selected one is automatically selected - Don't update the UI if not needed - Also invert the border of the selected button - Simplify text update
1 parent 2e8abee commit d92b1fc

File tree

1 file changed

+21
-52
lines changed

1 file changed

+21
-52
lines changed

examples/3d/color_grading.rs

Lines changed: 21 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -125,12 +125,7 @@ fn setup(
125125
// Create the root UI element.
126126
let font = asset_server.load(FONT_PATH);
127127
let color_grading = ColorGrading::default();
128-
add_buttons(
129-
&mut commands,
130-
&currently_selected_option,
131-
&font,
132-
&color_grading,
133-
);
128+
add_buttons(&mut commands, &font, &color_grading);
134129

135130
// Spawn help text.
136131
add_help_text(&mut commands, &font, &currently_selected_option);
@@ -140,12 +135,7 @@ fn setup(
140135
}
141136

142137
/// Adds all the buttons on the bottom of the scene.
143-
fn add_buttons(
144-
commands: &mut Commands,
145-
currently_selected_option: &SelectedColorGradingOption,
146-
font: &Handle<Font>,
147-
color_grading: &ColorGrading,
148-
) {
138+
fn add_buttons(commands: &mut Commands, font: &Handle<Font>, color_grading: &ColorGrading) {
149139
// Spawn the parent node that contains all the buttons.
150140
commands
151141
.spawn(NodeBundle {
@@ -161,26 +151,15 @@ fn add_buttons(
161151
})
162152
.with_children(|parent| {
163153
// Create the first row, which contains the global controls.
164-
add_buttons_for_global_controls(
165-
parent,
166-
*currently_selected_option,
167-
color_grading,
168-
font,
169-
);
154+
add_buttons_for_global_controls(parent, color_grading, font);
170155

171156
// Create the rows for individual controls.
172157
for section in [
173158
SelectedColorGradingSection::Highlights,
174159
SelectedColorGradingSection::Midtones,
175160
SelectedColorGradingSection::Shadows,
176161
] {
177-
add_buttons_for_section(
178-
parent,
179-
section,
180-
*currently_selected_option,
181-
color_grading,
182-
font,
183-
);
162+
add_buttons_for_section(parent, section, color_grading, font);
184163
}
185164
});
186165
}
@@ -189,7 +168,6 @@ fn add_buttons(
189168
/// whole as opposed to shadows, midtones, or highlights).
190169
fn add_buttons_for_global_controls(
191170
parent: &mut ChildBuilder,
192-
currently_selected_option: SelectedColorGradingOption,
193171
color_grading: &ColorGrading,
194172
font: &Handle<Font>,
195173
) {
@@ -219,7 +197,6 @@ fn add_buttons_for_global_controls(
219197
add_button_for_value(
220198
parent,
221199
SelectedColorGradingOption::Global(option),
222-
currently_selected_option,
223200
color_grading,
224201
font,
225202
);
@@ -232,7 +209,6 @@ fn add_buttons_for_global_controls(
232209
fn add_buttons_for_section(
233210
parent: &mut ChildBuilder,
234211
section: SelectedColorGradingSection,
235-
currently_selected_option: SelectedColorGradingOption,
236212
color_grading: &ColorGrading,
237213
font: &Handle<Font>,
238214
) {
@@ -263,7 +239,6 @@ fn add_buttons_for_section(
263239
add_button_for_value(
264240
parent,
265241
SelectedColorGradingOption::Section(section, option),
266-
currently_selected_option,
267242
color_grading,
268243
font,
269244
);
@@ -275,18 +250,9 @@ fn add_buttons_for_section(
275250
fn add_button_for_value(
276251
parent: &mut ChildBuilder,
277252
option: SelectedColorGradingOption,
278-
currently_selected_option: SelectedColorGradingOption,
279253
color_grading: &ColorGrading,
280254
font: &Handle<Font>,
281255
) {
282-
let is_selected = currently_selected_option == option;
283-
284-
let (bg_color, fg_color) = if is_selected {
285-
(Color::WHITE, Color::BLACK)
286-
} else {
287-
(Color::BLACK, Color::WHITE)
288-
};
289-
290256
// Add the button node.
291257
parent
292258
.spawn(ButtonBundle {
@@ -301,7 +267,7 @@ fn add_button_for_value(
301267
},
302268
border_color: BorderColor(Color::WHITE),
303269
border_radius: BorderRadius::MAX,
304-
image: UiImage::default().with_color(bg_color),
270+
image: UiImage::default().with_color(Color::BLACK),
305271
..default()
306272
})
307273
.insert(ColorGradingOptionWidget {
@@ -314,7 +280,7 @@ fn add_button_for_value(
314280
SelectedColorGradingOption::Global(option) => option.to_string(),
315281
SelectedColorGradingOption::Section(_, option) => option.to_string(),
316282
};
317-
add_text(parent, &label, font, fg_color).insert(ColorGradingOptionWidget {
283+
add_text(parent, &label, font, Color::WHITE).insert(ColorGradingOptionWidget {
318284
widget_type: ColorGradingOptionWidgetType::Label,
319285
option,
320286
});
@@ -333,7 +299,7 @@ fn add_button_for_value(
333299
parent,
334300
&format!("{:.3}", option.get(color_grading)),
335301
font,
336-
fg_color,
302+
Color::WHITE,
337303
)
338304
.insert(ColorGradingOptionWidget {
339305
widget_type: ColorGradingOptionWidgetType::Value,
@@ -599,19 +565,26 @@ fn handle_button_presses(
599565

600566
/// Updates the state of the UI based on the current state.
601567
fn update_ui_state(
602-
mut buttons: Query<(&mut UiImage, &ColorGradingOptionWidget)>,
568+
mut buttons: Query<(&mut UiImage, &mut BorderColor, &ColorGradingOptionWidget)>,
603569
mut button_text: Query<(&mut Text, &ColorGradingOptionWidget), Without<HelpText>>,
604570
mut help_text: Query<&mut Text, With<HelpText>>,
605571
cameras: Query<&ColorGrading>,
606572
currently_selected_option: Res<SelectedColorGradingOption>,
607573
) {
574+
// Exit early if the UI didn't change
575+
if !currently_selected_option.is_changed() {
576+
return;
577+
}
578+
608579
// The currently-selected option is drawn with inverted colors.
609-
for (mut image, widget) in buttons.iter_mut() {
610-
image.color = if *currently_selected_option == widget.option {
611-
Color::WHITE
580+
for (mut image, mut border_color, widget) in buttons.iter_mut() {
581+
if *currently_selected_option == widget.option {
582+
image.color = Color::WHITE;
583+
*border_color = Color::BLACK.into();
612584
} else {
613-
Color::BLACK
614-
};
585+
image.color = Color::BLACK;
586+
*border_color = Color::WHITE.into();
587+
}
615588
}
616589

617590
let value_label = cameras
@@ -646,11 +619,7 @@ fn update_ui_state(
646619
}
647620

648621
// Update the help text.
649-
for mut help_text in help_text.iter_mut() {
650-
for section in &mut help_text.sections {
651-
section.value = create_help_text(&currently_selected_option);
652-
}
653-
}
622+
help_text.single_mut().sections[0].value = create_help_text(&currently_selected_option);
654623
}
655624

656625
/// Creates the help text at the top left of the window.

0 commit comments

Comments
 (0)