Add Facepile and PlayerStack components

This commit is contained in:
Marshall Bowers 2023-10-07 12:02:42 -04:00
parent 5e7954f152
commit f33d41af63
9 changed files with 314 additions and 2 deletions

View file

@ -0,0 +1,33 @@
use std::marker::PhantomData;
use crate::prelude::*;
use crate::{theme, Avatar, Player};
#[derive(Element)]
pub struct Facepile<S: 'static + Send + Sync> {
state_type: PhantomData<S>,
players: Vec<Player>,
}
impl<S: 'static + Send + Sync> Facepile<S> {
pub fn new<P: Iterator<Item = Player>>(players: P) -> Self {
Self {
state_type: PhantomData,
players: players.collect(),
}
}
fn render(&mut self, cx: &mut ViewContext<S>) -> impl Element<State = S> {
let theme = theme(cx);
let player_count = self.players.len();
let player_list = self.players.iter().enumerate().map(|(ix, player)| {
let isnt_last = ix < player_count - 1;
div()
// TODO: Blocked on negative margins.
// .when(isnt_last, |div| div.neg_mr_1())
.child(Avatar::new(player.avatar_src().to_string()))
});
div().p_1().flex().items_center().children(player_list)
}
}