skullball/rust/src/ui/simple_score_view.rs

82 lines
3.1 KiB
Rust

use std::collections::HashMap;
use godot::{
classes::{HBoxContainer, IHBoxContainer, Label},
prelude::*,
};
use crate::{game_manager::Game, replay::StandaloneReplayManager};
#[derive(GodotClass)]
#[class(base=HBoxContainer, init)]
pub struct SimpleScoreView {
team_goals: HashMap<u8, Gd<Label>>,
base: Base<HBoxContainer>,
}
#[godot_api]
impl IHBoxContainer for SimpleScoreView {
fn ready(&mut self) {
self.run_deferred(|s| {
if let Some(game) = Game::singleton() {
for (id, team) in game.bind().get_teams().iter_shared().enumerate() {
let mut goals_label = Label::new_alloc();
goals_label.set_modulate(team.bind().color);
goals_label.set_text("0");
if !s.team_goals.is_empty() {
let mut dash = Label::new_alloc();
dash.set_text(" - ");
s.base_mut().add_child(&dash);
}
s.base_mut().add_child(&goals_label);
s.team_goals.insert(id as u8, goals_label);
}
game.signals()
.on_goal()
.connect_other(s, |s| s.update_goals());
} else if let Some(playback) = &StandaloneReplayManager::singleton().bind().playback {
for (id, team) in playback.bind().replay.teams.iter().enumerate() {
let mut goals_label = Label::new_alloc();
goals_label.set_modulate(team.color.clone().into());
goals_label.set_text("0");
if !s.team_goals.is_empty() {
let mut dash = Label::new_alloc();
dash.set_text(" - ");
s.base_mut().add_child(&dash);
}
s.base_mut().add_child(&goals_label);
s.team_goals.insert(id as u8, goals_label);
}
playback
.signals()
.on_data_changed()
.connect_other(s, |s| s.update_goals());
}
s.update_goals();
});
}
}
impl SimpleScoreView {
pub fn update_goals(&mut self) {
if let Some(game) = Game::singleton() {
for (id, _) in game.bind().get_teams().iter_shared().enumerate() {
if let Some(goals) = game.bind().goals.get(&(id as u8))
&& let Some(label) = self.team_goals.get_mut(&(id as u8))
{
label.set_text(&goals.to_string());
}
}
} else if let Some(playback) = &StandaloneReplayManager::singleton().bind().playback {
for (id, _) in playback.bind().replay.teams.iter().enumerate() {
if let Some(goals) = playback.bind().running_goals.get(&(id as u8))
&& let Some(label) = self.team_goals.get_mut(&(id as u8))
{
label.set_text(&goals.to_string());
}
}
}
}
}