Skip to content
Closed
61 changes: 31 additions & 30 deletions examples/2d/contributors.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
use bevy::{prelude::*, utils::HashSet};
use rand::{prelude::SliceRandom, Rng};
use std::{
env::VarError,
io::{self, BufRead, BufReader},
process::Stdio,
};

use rand::{prelude::SliceRandom, Rng};

use bevy::{prelude::*, utils::HashSet};

fn main() {
App::new()
.add_plugins(DefaultPlugins)
Expand All @@ -26,8 +28,10 @@ struct ContributorSelection {
idx: usize,
}

#[derive(Component)]
struct SelectTimer;
struct SelectTimerState {
timer: Timer,
has_triggered: bool,
}

#[derive(Component)]
struct ContributorDisplay;
Expand Down Expand Up @@ -69,7 +73,7 @@ fn setup_contributor_selection(mut commands: Commands, asset_server: Res<AssetSe
let texture_handle = asset_server.load("branding/icon.png");

let mut contributor_selection = ContributorSelection {
order: vec![],
order: Vec::with_capacity(contribs.len()),
idx: 0,
};

Expand Down Expand Up @@ -120,7 +124,10 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
commands.spawn_bundle(OrthographicCameraBundle::new_2d());
commands.spawn_bundle(UiCameraBundle::default());

commands.spawn_bundle((SelectTimer, Timer::from_seconds(SHOWCASE_TIMER_SECS, true)));
commands.insert_resource(SelectTimerState {
timer: Timer::from_seconds(SHOWCASE_TIMER_SECS, true),
has_triggered: false,
});

commands
.spawn()
Expand Down Expand Up @@ -159,44 +166,39 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
fn select_system(
mut contributor_selection: ResMut<ContributorSelection>,
mut text_query: Query<&mut Text, With<ContributorDisplay>>,
mut timer_query: Query<&mut Timer, With<SelectTimer>>,
mut query: Query<(&Contributor, &mut Sprite, &mut Transform)>,
mut timer_state: ResMut<SelectTimerState>,
time: Res<Time>,
) {
let mut timer_fired = false;
for mut timer in timer_query.iter_mut() {
if !timer.tick(time.delta()).just_finished() {
continue;
}
timer.reset();
timer_fired = true;
}

if !timer_fired {
if !timer_state.timer.tick(time.delta()).just_finished() {
return;
}
if !timer_state.has_triggered {
let mut text = text_query.single_mut();
text.sections[0].value.clear();
text.sections[0].value.push_str("Contributor: ");

let prev = contributor_selection.idx;

if (contributor_selection.idx + 1) < contributor_selection.order.len() {
contributor_selection.idx += 1;
} else {
contributor_selection.idx = 0;
timer_state.has_triggered = true;
}

{
let (_, entity) = &contributor_selection.order[prev];
let (_, entity) = &contributor_selection.order[contributor_selection.idx];
if let Ok((contributor, mut sprite, mut transform)) = query.get_mut(*entity) {
deselect(&mut sprite, contributor, &mut *transform);
}
}

if (contributor_selection.idx + 1) < contributor_selection.order.len() {
contributor_selection.idx += 1;
} else {
contributor_selection.idx = 0;
}

let (name, entity) = &contributor_selection.order[contributor_selection.idx];

if let Ok((contributor, mut sprite, mut transform)) = query.get_mut(*entity) {
if let Some(mut text) = text_query.iter_mut().next() {
select(&mut sprite, contributor, &mut *transform, &mut *text, name);
}
let mut text = text_query.single_mut();
select(&mut sprite, contributor, &mut *transform, &mut *text, name);
}
}

Expand All @@ -207,7 +209,7 @@ fn select(
contributor: &Contributor,
transform: &mut Transform,
text: &mut Text,
name: &str,
#[allow(clippy::ptr_arg)] name: &String,
) {
sprite.color = Color::hsla(
contributor.hue,
Expand All @@ -218,8 +220,7 @@ fn select(

transform.translation.z = 100.0;

text.sections[0].value = "Contributor: ".to_string();
text.sections[1].value = name.to_string();
text.sections[1].value.clone_from(name);
text.sections[1].style.color = sprite.color;
}

Expand Down