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

feat: character select screen tweak #915

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
138 changes: 90 additions & 48 deletions src/ui/main_menu/player_select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,50 @@ fn handle_match_setup_messages(
}
}

fn rotate_skins(to_right: bool, players: &Vec<Handle<PlayerMeta>>, current_skin: &mut Handle<PlayerMeta>) {
let current_id = players
.iter()
.enumerate()
.find(|(_, skin)| {skin == &current_skin})
.map(|(index, _)| index)
.unwrap_or(0);

let new_id: usize;
if to_right {
new_id = (current_id + 1) % players.len();
} else {
new_id = if current_id == 0 {
players.len() - 1
} else {
current_id - 1
};

}
*current_skin = players[new_id as usize];
}

fn rotate_hats(to_right: bool, hats: &Vec<Option<Handle<HatMeta>>>, current_hat: &mut Option<Handle<HatMeta>>) {
let current_id = hats
.iter()
.enumerate()
.find(|(_, hat)| {hat == &current_hat})
.map(|(index, _)| index)
.unwrap_or(0);

let new_id: usize;
if to_right {
new_id = (current_id + 1) % hats.len();
} else {
new_id =
if current_id == 0 {
hats.len() - 1
} else {
current_id - 1
};
}
*current_hat = hats[new_id];
}

fn player_select_panel(
mut params: In<(&mut egui::Ui, usize, &mut PlayerSelectState)>,
meta: Root<GameMeta>,
Expand Down Expand Up @@ -391,25 +435,11 @@ fn player_select_panel(

// Select a hat if the player has been confirmed
if slot.confirmed {
let current_hat_handle_idx = state
.hats
.iter()
.enumerate()
.find(|(_, handle)| **handle == slot.selected_hat)
.map(|(i, _)| i)
.unwrap_or(0);

let next_idx = if direction.x > 0.0 {
(current_hat_handle_idx + 1) % state.hats.len()
} else {
let idx = current_hat_handle_idx as i32 - 1;
if idx == -1 {
state.hats.len() - 1
} else {
idx as usize
}
};
slot.selected_hat = state.hats[next_idx];
rotate_hats(
direction.x > 0.0,
&state.hats,
&mut slot.selected_hat);

#[cfg(not(target_arch = "wasm32"))]
if let Some(socket) = network_socket.as_ref() {
Expand All @@ -424,24 +454,11 @@ fn player_select_panel(

// Select player skin if the player has not be confirmed
} else {
let current_player_handle_idx = state
.players
.iter()
.enumerate()
.find(|(_, handle)| *handle == player_handle)
.map(|(i, _)| i)
.unwrap_or(0);
let next_idx = if direction.x > 0.0 {
(current_player_handle_idx + 1) % state.players.len()
} else {
let idx = current_player_handle_idx as i32 - 1;
if idx == -1 {
state.players.len() - 1
} else {
idx as usize
}
};
*player_handle = state.players[next_idx];

rotate_skins(
direction.x > 0.0,
&state.players,
player_handle);

#[cfg(not(target_arch = "wasm32"))]
if let Some(socket) = network_socket.as_ref() {
Expand Down Expand Up @@ -575,20 +592,42 @@ fn player_select_panel(
});

ui.with_layout(egui::Layout::bottom_up(egui::Align::Center), |ui| {
let name_with_arrows = format!("< {} >", player_meta.name);
ui.label(normal_font.rich(if slot.confirmed {
player_meta.name.to_string()
//let name_with_arrows = format!("< {} >", player_meta.name);
ui.label(normal_font.rich(player_meta.name.to_string()));

if slot.confirmed {
let player_image_max_width = ui.available_width();
world.run_system(player_image, (ui, &player_meta, hat_meta.as_deref(), player_image_max_width));
} else {
name_with_arrows
}));
ui.with_layout(egui::Layout::left_to_right(egui::Align::Center), |ui| {
let left_arrow_label = ui.add(egui::Label::new(heading_font.rich("<")).sense(egui::Sense::click()));
let player_image_max_width = ui.available_width() - left_arrow_label.rect.width() * 2.0;
if left_arrow_label.clicked() {
rotate_skins(
false,
&state.players,
&mut slot.selected_player);
}

world.run_system(player_image, (ui, &player_meta, hat_meta.as_deref(), player_image_max_width));

let right_arrow_label = ui.add(egui::Label::new(heading_font.rich(">")).sense(egui::Sense::click()));
if right_arrow_label.clicked() {
rotate_skins(
true,
&state.players,
&mut slot.selected_player);
}
});
}

let hat_label = if let Some(hat_meta) = &hat_meta {
format!("< {} >", hat_meta.name)
} else {
format!("< {} >", localization.get("no-hat"))
};
ui.label(smaller_font.rich(if slot.confirmed { &hat_label } else { "" }));

world.run_system(player_image, (ui, &player_meta, hat_meta.as_deref()));

});
});

Expand Down Expand Up @@ -630,15 +669,15 @@ fn player_select_panel(
}

fn player_image(
mut params: In<(&mut egui::Ui, &PlayerMeta, Option<&HatMeta>)>,
mut params: In<(&mut egui::Ui, &PlayerMeta, Option<&HatMeta>, f32)>,
egui_textures: Res<EguiTextures>,
asset_server: Res<AssetServer>,
) {
let (ui, player_meta, hat_meta) = &mut *params;
let (ui, player_meta, hat_meta, image_max_width) = &mut *params;
let time = ui.ctx().input(|i| i.time as f32);
let width = ui.available_width();
let available_height = ui.available_width();

let width = *image_max_width;
let available_height = width;
let body_rect;
let body_scale;
let body_offset;
Expand Down Expand Up @@ -748,5 +787,8 @@ fn player_image(

mesh.add_rect_with_uv(rect, uv, egui::Color32::WHITE);
ui.painter().add(mesh);
ui.advance_cursor_after_rect(rect.union(body_rect));
}


}
Loading