skullball/rust/src/ui/credits.rs
2026-07-25 23:49:45 +03:00

97 lines
2.7 KiB
Rust

use godot::{
classes::{Control, IControl, Label, LinkButton, MarginContainer, VBoxContainer},
prelude::*,
};
use crate::credits::{CreditPerson, Credits};
const CREDITS: &'static str = include_str!("../../../credits.toml");
#[derive(GodotClass)]
#[class(base=Control, tool)]
pub struct CreditsMenu {
#[export]
credits_container: Option<Gd<VBoxContainer>>,
credits: Credits,
base: Base<Control>,
}
#[godot_api]
impl IControl for CreditsMenu {
fn init(base: Base<Self::Base>) -> Self {
let credits = toml::from_str(CREDITS).unwrap_or(Default::default());
CreditsMenu {
credits_container: None,
credits,
base,
}
}
fn ready(&mut self) {
if let Some(container) = &mut self.credits_container {
for child in container.get_children().iter_shared() {
container.remove_child(&child);
}
}
for person in self.credits.people.clone() {
self.add_credit(person);
}
}
}
#[godot_api]
impl CreditsMenu {
pub fn add_credit(&mut self, credit: CreditPerson) {
if let Some(container) = &mut self.credits_container {
let mut margin = MarginContainer::new_alloc();
margin.add_theme_constant_override("margin_bottom", 15);
let mut vbox = VBoxContainer::new_alloc();
let name = if let Some(uri) = credit.link {
let mut link = LinkButton::new_alloc();
link.set_text(&credit.name);
link.set_uri(&uri);
link.upcast::<Control>()
} else {
let mut label = Label::new_alloc();
label.set_text(&credit.name);
label.upcast()
};
vbox.add_child(&name);
for credit in credit.credits {
let mut credit_margin = MarginContainer::new_alloc();
credit_margin.add_theme_constant_override("margin_left", 20);
let credit = if let Some(uri) = credit.link {
let mut link = LinkButton::new_alloc();
link.set_text(&format!("- {}", credit.credit));
link.set_uri(&uri);
link.upcast::<Control>()
} else {
let mut label = Label::new_alloc();
label.set_text(&format!("- {}", credit.credit));
label.upcast()
};
credit_margin.add_child(&credit);
vbox.add_child(&credit_margin);
}
margin.add_child(&vbox);
container.add_child(&margin);
}
}
#[func]
pub fn close(&mut self) {
self.base_mut().queue_free();
}
}