Skip to content

Commit ff070da

Browse files
committed
fix remaining issues with background color in examples (#14115)
# Objective - Fixes #14097 ## Solution - Switching the uses of `UiImage` in examples to `BackgroundColor` when needed
1 parent 1db0214 commit ff070da

File tree

10 files changed

+62
-52
lines changed

10 files changed

+62
-52
lines changed

examples/state/computed_states.rs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -224,20 +224,19 @@ fn menu(
224224
tutorial_state: Res<State<TutorialState>>,
225225
mut next_tutorial: ResMut<NextState<TutorialState>>,
226226
mut interaction_query: Query<
227-
(&Interaction, &mut UiImage, &MenuButton),
227+
(&Interaction, &mut BackgroundColor, &MenuButton),
228228
(Changed<Interaction>, With<Button>),
229229
>,
230230
) {
231-
for (interaction, mut image, menu_button) in &mut interaction_query {
232-
let color = &mut image.color;
231+
for (interaction, mut color, menu_button) in &mut interaction_query {
233232
match *interaction {
234233
Interaction::Pressed => {
235234
*color = if menu_button == &MenuButton::Tutorial
236235
&& tutorial_state.get() == &TutorialState::Active
237236
{
238-
PRESSED_ACTIVE_BUTTON
237+
PRESSED_ACTIVE_BUTTON.into()
239238
} else {
240-
PRESSED_BUTTON
239+
PRESSED_BUTTON.into()
241240
};
242241

243242
match menu_button {
@@ -255,18 +254,18 @@ fn menu(
255254
if menu_button == &MenuButton::Tutorial
256255
&& tutorial_state.get() == &TutorialState::Active
257256
{
258-
*color = HOVERED_ACTIVE_BUTTON;
257+
*color = HOVERED_ACTIVE_BUTTON.into();
259258
} else {
260-
*color = HOVERED_BUTTON;
259+
*color = HOVERED_BUTTON.into();
261260
}
262261
}
263262
Interaction::None => {
264263
if menu_button == &MenuButton::Tutorial
265264
&& tutorial_state.get() == &TutorialState::Active
266265
{
267-
*color = ACTIVE_BUTTON;
266+
*color = ACTIVE_BUTTON.into();
268267
} else {
269-
*color = NORMAL_BUTTON;
268+
*color = NORMAL_BUTTON.into();
270269
}
271270
}
272271
}

examples/state/custom_transitions.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -142,22 +142,21 @@ mod custom_transitions {
142142
fn menu(
143143
mut next_state: ResMut<NextState<AppState>>,
144144
mut interaction_query: Query<
145-
(&Interaction, &mut UiImage),
145+
(&Interaction, &mut BackgroundColor),
146146
(Changed<Interaction>, With<Button>),
147147
>,
148148
) {
149-
for (interaction, mut image) in &mut interaction_query {
150-
let color = &mut image.color;
149+
for (interaction, mut color) in &mut interaction_query {
151150
match *interaction {
152151
Interaction::Pressed => {
153-
*color = PRESSED_BUTTON;
152+
*color = PRESSED_BUTTON.into();
154153
next_state.set(AppState::InGame);
155154
}
156155
Interaction::Hovered => {
157-
*color = HOVERED_BUTTON;
156+
*color = HOVERED_BUTTON.into();
158157
}
159158
Interaction::None => {
160-
*color = NORMAL_BUTTON;
159+
*color = NORMAL_BUTTON.into();
161160
}
162161
}
163162
}

examples/state/states.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -95,21 +95,21 @@ fn setup_menu(mut commands: Commands) {
9595
fn menu(
9696
mut next_state: ResMut<NextState<AppState>>,
9797
mut interaction_query: Query<
98-
(&Interaction, &mut UiImage),
98+
(&Interaction, &mut BackgroundColor),
9999
(Changed<Interaction>, With<Button>),
100100
>,
101101
) {
102-
for (interaction, mut image) in &mut interaction_query {
102+
for (interaction, mut color) in &mut interaction_query {
103103
match *interaction {
104104
Interaction::Pressed => {
105-
image.color = PRESSED_BUTTON;
105+
*color = PRESSED_BUTTON.into();
106106
next_state.set(AppState::InGame);
107107
}
108108
Interaction::Hovered => {
109-
image.color = HOVERED_BUTTON;
109+
*color = HOVERED_BUTTON.into();
110110
}
111111
Interaction::None => {
112-
image.color = NORMAL_BUTTON;
112+
*color = NORMAL_BUTTON.into();
113113
}
114114
}
115115
}

examples/state/sub_states.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,22 +63,21 @@ fn main() {
6363
fn menu(
6464
mut next_state: ResMut<NextState<AppState>>,
6565
mut interaction_query: Query<
66-
(&Interaction, &mut UiImage),
66+
(&Interaction, &mut BackgroundColor),
6767
(Changed<Interaction>, With<Button>),
6868
>,
6969
) {
70-
for (interaction, mut image) in &mut interaction_query {
71-
let color = &mut image.color;
70+
for (interaction, mut color) in &mut interaction_query {
7271
match *interaction {
7372
Interaction::Pressed => {
74-
*color = PRESSED_BUTTON;
73+
*color = PRESSED_BUTTON.into();
7574
next_state.set(AppState::InGame);
7675
}
7776
Interaction::Hovered => {
78-
*color = HOVERED_BUTTON;
77+
*color = HOVERED_BUTTON.into();
7978
}
8079
Interaction::None => {
81-
*color = NORMAL_BUTTON;
80+
*color = NORMAL_BUTTON.into();
8281
}
8382
}
8483
}

examples/stress_tests/many_buttons.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,12 +100,15 @@ fn main() {
100100
struct IdleColor(Color);
101101

102102
fn button_system(
103-
mut interaction_query: Query<(&Interaction, &mut UiImage, &IdleColor), Changed<Interaction>>,
103+
mut interaction_query: Query<
104+
(&Interaction, &mut BackgroundColor, &IdleColor),
105+
Changed<Interaction>,
106+
>,
104107
) {
105-
for (interaction, mut image, &IdleColor(idle_color)) in interaction_query.iter_mut() {
106-
image.color = match interaction {
108+
for (interaction, mut color, &IdleColor(idle_color)) in interaction_query.iter_mut() {
109+
*color = match interaction {
107110
Interaction::Hovered => ORANGE_RED.into(),
108-
_ => idle_color,
111+
_ => idle_color.into(),
109112
};
110113
}
111114
}

examples/ui/button.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,27 +19,32 @@ const PRESSED_BUTTON: Color = Color::srgb(0.35, 0.75, 0.35);
1919

2020
fn button_system(
2121
mut interaction_query: Query<
22-
(&Interaction, &mut UiImage, &mut BorderColor, &Children),
22+
(
23+
&Interaction,
24+
&mut BackgroundColor,
25+
&mut BorderColor,
26+
&Children,
27+
),
2328
(Changed<Interaction>, With<Button>),
2429
>,
2530
mut text_query: Query<&mut Text>,
2631
) {
27-
for (interaction, mut image, mut border_color, children) in &mut interaction_query {
32+
for (interaction, mut color, mut border_color, children) in &mut interaction_query {
2833
let mut text = text_query.get_mut(children[0]).unwrap();
2934
match *interaction {
3035
Interaction::Pressed => {
3136
text.sections[0].value = "Press".to_string();
32-
image.color = PRESSED_BUTTON;
37+
*color = PRESSED_BUTTON.into();
3338
border_color.0 = RED.into();
3439
}
3540
Interaction::Hovered => {
3641
text.sections[0].value = "Hover".to_string();
37-
image.color = HOVERED_BUTTON;
42+
*color = HOVERED_BUTTON.into();
3843
border_color.0 = Color::WHITE;
3944
}
4045
Interaction::None => {
4146
text.sections[0].value = "Button".to_string();
42-
image.color = NORMAL_BUTTON;
47+
*color = NORMAL_BUTTON.into();
4348
border_color.0 = Color::BLACK;
4449
}
4550
}

examples/ui/display_and_visibility.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -460,13 +460,13 @@ fn buttons_handler<T>(
460460
}
461461

462462
fn text_hover(
463-
mut button_query: Query<(&Interaction, &mut UiImage, &Children), Changed<Interaction>>,
463+
mut button_query: Query<(&Interaction, &mut BackgroundColor, &Children), Changed<Interaction>>,
464464
mut text_query: Query<&mut Text>,
465465
) {
466-
for (interaction, mut image, children) in button_query.iter_mut() {
466+
for (interaction, mut color, children) in button_query.iter_mut() {
467467
match interaction {
468468
Interaction::Hovered => {
469-
image.color = Color::BLACK.with_alpha(0.6);
469+
*color = Color::BLACK.with_alpha(0.6).into();
470470
for &child in children {
471471
if let Ok(mut text) = text_query.get_mut(child) {
472472
// Bypass change detection to avoid recomputation of the text when only changing the color
@@ -475,7 +475,7 @@ fn text_hover(
475475
}
476476
}
477477
_ => {
478-
image.color = Color::BLACK.with_alpha(0.5);
478+
*color = Color::BLACK.with_alpha(0.5).into();
479479
for &child in children {
480480
if let Ok(mut text) = text_query.get_mut(child) {
481481
text.bypass_change_detection().sections[0].style.color =

examples/ui/size_constraints.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ fn spawn_button(
245245
margin: UiRect::horizontal(Val::Px(2.)),
246246
..Default::default()
247247
},
248-
background_color: if active {
248+
border_color: if active {
249249
ACTIVE_BORDER_COLOR
250250
} else {
251251
INACTIVE_BORDER_COLOR
@@ -359,7 +359,7 @@ fn update_buttons(
359359
fn update_radio_buttons_colors(
360360
mut event_reader: EventReader<ButtonActivatedEvent>,
361361
button_query: Query<(Entity, &Constraint, &Interaction)>,
362-
mut image_query: Query<&mut UiImage>,
362+
mut border_query: Query<&mut BorderColor>,
363363
mut color_query: Query<&mut BackgroundColor>,
364364
mut text_query: Query<&mut Text>,
365365
children_query: Query<&Children>,
@@ -382,7 +382,7 @@ fn update_radio_buttons_colors(
382382
)
383383
};
384384

385-
image_query.get_mut(id).unwrap().color = border_color;
385+
border_query.get_mut(id).unwrap().0 = border_color;
386386
for &child in children_query.get(id).into_iter().flatten() {
387387
color_query.get_mut(child).unwrap().0 = inner_color;
388388
for &grandchild in children_query.get(child).into_iter().flatten() {

examples/ui/ui_texture_atlas_slice.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,26 +19,31 @@ fn main() {
1919

2020
fn button_system(
2121
mut interaction_query: Query<
22-
(&Interaction, &mut TextureAtlas, &Children, &mut UiImage),
22+
(
23+
&Interaction,
24+
&mut TextureAtlas,
25+
&Children,
26+
&mut BackgroundColor,
27+
),
2328
(Changed<Interaction>, With<Button>),
2429
>,
2530
mut text_query: Query<&mut Text>,
2631
) {
27-
for (interaction, mut atlas, children, mut image) in &mut interaction_query {
32+
for (interaction, mut atlas, children, mut color) in &mut interaction_query {
2833
let mut text = text_query.get_mut(children[0]).unwrap();
2934
match *interaction {
3035
Interaction::Pressed => {
3136
text.sections[0].value = "Press".to_string();
3237
atlas.index = (atlas.index + 1) % 30;
33-
image.color = GOLD.into();
38+
*color = GOLD.into();
3439
}
3540
Interaction::Hovered => {
3641
text.sections[0].value = "Hover".to_string();
37-
image.color = ORANGE.into();
42+
*color = ORANGE.into();
3843
}
3944
Interaction::None => {
4045
text.sections[0].value = "Button".to_string();
41-
image.color = Color::WHITE;
46+
*color = Color::BLACK.into();
4247
}
4348
}
4449
}

examples/ui/ui_texture_slice.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,25 +19,25 @@ fn main() {
1919

2020
fn button_system(
2121
mut interaction_query: Query<
22-
(&Interaction, &Children, &mut UiImage),
22+
(&Interaction, &Children, &mut BackgroundColor),
2323
(Changed<Interaction>, With<Button>),
2424
>,
2525
mut text_query: Query<&mut Text>,
2626
) {
27-
for (interaction, children, mut image) in &mut interaction_query {
27+
for (interaction, children, mut color) in &mut interaction_query {
2828
let mut text = text_query.get_mut(children[0]).unwrap();
2929
match *interaction {
3030
Interaction::Pressed => {
3131
text.sections[0].value = "Press".to_string();
32-
image.color = GOLD.into();
32+
*color = GOLD.into();
3333
}
3434
Interaction::Hovered => {
3535
text.sections[0].value = "Hover".to_string();
36-
image.color = ORANGE.into();
36+
*color = ORANGE.into();
3737
}
3838
Interaction::None => {
3939
text.sections[0].value = "Button".to_string();
40-
image.color = Color::WHITE;
40+
*color = Color::BLACK.into();
4141
}
4242
}
4343
}

0 commit comments

Comments
 (0)