Skip to content

Commit 9340d31

Browse files
Less colors, more brevity
1 parent 404de4f commit 9340d31

File tree

1 file changed

+13
-22
lines changed

1 file changed

+13
-22
lines changed

examples/ecs/per_entity_events.rs

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ struct InteractableBundle {
5353
#[bundle]
5454
text_bundle: Text2dBundle,
5555
selectable: Selectable,
56-
rainbow: Rainbow,
56+
rainbow: ColorChoices,
5757
cycle_color_events: Events<CycleColorAction>,
5858
add_number_events: Events<AddNumberAction>,
5959
}
@@ -79,46 +79,37 @@ impl InteractableBundle {
7979
..Default::default()
8080
},
8181
selectable: Selectable,
82-
rainbow: Rainbow::Red,
82+
rainbow: ColorChoices::Red,
8383
cycle_color_events: Events::<CycleColorAction>::default(),
8484
add_number_events: Events::<AddNumberAction>::default(),
8585
}
8686
}
8787
}
8888

89-
enum Rainbow {
89+
enum ColorChoices {
9090
Red,
91-
Orange,
92-
Yellow,
93-
Green,
9491
Blue,
9592
Violet,
9693
}
9794

98-
impl Iterator for Rainbow {
95+
impl Iterator for ColorChoices {
9996
type Item = Self;
10097

101-
fn next(&mut self) -> Option<Rainbow> {
102-
use Rainbow::*;
98+
fn next(&mut self) -> Option<ColorChoices> {
99+
use ColorChoices::*;
103100
Some(match *self {
104-
Red => Orange,
105-
Orange => Yellow,
106-
Yellow => Green,
107-
Green => Blue,
101+
Red => Blue,
108102
Blue => Violet,
109103
Violet => Red,
110104
})
111105
}
112106
}
113107

114-
impl From<&Rainbow> for Color {
115-
fn from(rainbow: &Rainbow) -> Color {
116-
use Rainbow::*;
108+
impl From<&ColorChoices> for Color {
109+
fn from(rainbow: &ColorChoices) -> Color {
110+
use ColorChoices::*;
117111
match rainbow {
118112
Red => Color::RED,
119-
Orange => Color::ORANGE,
120-
Yellow => Color::YELLOW,
121-
Green => Color::GREEN,
122113
Blue => Color::BLUE,
123114
Violet => Color::VIOLET,
124115
}
@@ -256,15 +247,15 @@ fn input_dispatch(
256247

257248
// FIXME: make this work without duplication using `EventReader<T>` syntax and specialized behavior
258249
fn cycle_color(mut query: Query<(&mut Rainbow, &mut Events<CycleColorAction>)>) {
259-
for (mut rainbow, action_queue) in query.iter_mut() {
250+
for (mut color, action_queue) in query.iter_mut() {
260251
let mut reader = action_queue.get_reader();
261252
for _ in reader.iter(&action_queue) {
262-
*rainbow = rainbow.next().unwrap();
253+
*color = color.next().unwrap();
263254
}
264255
}
265256
}
266257

267-
fn update_text_color(mut query: Query<(&mut Text, &Rainbow), Changed<Rainbow>>) {
258+
fn update_text_color(mut query: Query<(&mut Text, &ColorChoices), Changed<ColorChoices>>) {
268259
for (mut text, rainbow) in query.iter_mut() {
269260
text.sections[0].style.color = rainbow.into();
270261
}

0 commit comments

Comments
 (0)