Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Merged by Bors] - Slight perf improvements and tidy for contributors example #3764

Closed
wants to merge 13 commits into from
51 changes: 32 additions & 19 deletions examples/2d/contributors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ fn main() {
.add_system(move_system)
.add_system(collision_system)
.add_system(select_system)
.insert_resource(SelectTimer(Timer::from_seconds(SHOWCASE_TIMER_SECS, true)))
.insert_resource(SelectionState::default())
.run();
}

Expand All @@ -27,8 +27,19 @@ struct ContributorSelection {
idx: usize,
}

#[derive(Deref, DerefMut)]
struct SelectTimer(Timer);
struct SelectionState {
timer: Timer,
has_triggered: bool,
}

impl Default for SelectionState {
fn default() -> Self {
Self {
timer: Timer::from_seconds(SHOWCASE_TIMER_SECS, true),
has_triggered: false,
}
}
}

#[derive(Component)]
struct ContributorDisplay;
Expand Down Expand Up @@ -70,7 +81,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 @@ -156,37 +167,40 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {

/// Finds the next contributor to display and selects the entity
fn select_system(
mut timer: ResMut<SelectTimer>,
mut timer: ResMut<SelectionState>,
mut contributor_selection: ResMut<ContributorSelection>,
mut text_query: Query<&mut Text, With<ContributorDisplay>>,
mut query: Query<(&Contributor, &mut Sprite, &mut Transform)>,
time: Res<Time>,
) {
if !timer.tick(time.delta()).just_finished() {
if !timer.timer.tick(time.delta()).just_finished() {
return;
}
if !timer.has_triggered {
let mut text = text_query.single_mut();
text.sections[0].value = "Contributor: ".to_string();

let prev = contributor_selection.idx;

if (contributor_selection.idx + 1) < contributor_selection.order.len() {
contributor_selection.idx += 1;
} else {
contributor_selection.idx = 0;
timer.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 @@ -197,7 +211,7 @@ fn select(
contributor: &Contributor,
transform: &mut Transform,
text: &mut Text,
name: &str,
name: &String,
) {
sprite.color = Color::hsla(
contributor.hue,
Expand All @@ -208,8 +222,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