skullball/rust/src/ui/player_listings.rs
2026-07-23 23:40:43 +03:00

67 lines
2.2 KiB
Rust

use godot::{
classes::{HBoxContainer, IHBoxContainer, Label, VBoxContainer, box_container::AlignmentMode},
global::HorizontalAlignment,
prelude::*,
};
use crate::{game_manager::Game, ui::player_listing::PlayerListing};
#[derive(GodotClass)]
#[class(base=HBoxContainer, init)]
pub struct PlayerListings {
#[export]
player_listing_scene: Option<Gd<PackedScene>>,
base: Base<HBoxContainer>,
}
#[godot_api]
impl IHBoxContainer for PlayerListings {
fn ready(&mut self) {
self.run_deferred(|s| s.update_player_listings());
}
}
impl PlayerListings {
pub fn update_player_listings(&mut self) {
for child in self.base().get_children().iter_shared() {
self.base_mut().remove_child(&child);
}
if let Some(game) = Game::singleton() {
for (id, team) in game.bind().get_teams().iter_shared().enumerate() {
let mut vbox = VBoxContainer::new_alloc();
vbox.set_alignment(AlignmentMode::BEGIN);
vbox.set_custom_minimum_size(Vector2::new(300., 0.));
let mut team_label = Label::new_alloc();
team_label.set_modulate(team.bind().color);
team_label.set_text(&team.bind().name);
team_label.set_horizontal_alignment(HorizontalAlignment::CENTER);
vbox.add_child(&team_label);
if let Some(listing_scene) = &self.player_listing_scene {
let game = game.bind();
let players = game
.players
.iter()
.filter(|p| p.data.team == id as u8)
.collect::<Vec<_>>();
let header = listing_scene.instantiate_as::<PlayerListing>();
vbox.add_child(&header);
for player in players {
let mut listing = listing_scene.instantiate_as::<PlayerListing>();
listing.bind_mut().player_id = Some(player.id());
vbox.add_child(&listing);
}
}
self.base_mut().add_child(&vbox);
}
}
}
}