104 lines
2.6 KiB
Rust
104 lines
2.6 KiB
Rust
use super::*;
|
|
|
|
/// A wrapper around gtk::ListBox for Pawns
|
|
#[derive(Debug, Clone)]
|
|
pub struct PawnList {
|
|
inner: gtk::ListBox,
|
|
}
|
|
|
|
impl PawnList {
|
|
/// Initialize from the given existing ListBox
|
|
pub fn init(inner: gtk::ListBox) -> Self {
|
|
PawnList { inner }
|
|
}
|
|
|
|
/// Adds a Pawn to the list
|
|
pub fn add(&self, pawn: &Pawn) {
|
|
let row = gtk::ListBoxRow::new();
|
|
row.add(pawn.as_ref());
|
|
self.inner.add(&row);
|
|
self.inner.show_all();
|
|
}
|
|
}
|
|
|
|
/// Application data related to a Pawn
|
|
#[derive(Debug, Clone)]
|
|
pub struct PawnData {
|
|
pub name: String,
|
|
pub description: String,
|
|
pub position: Option<String>, // Content of label from CellWidget
|
|
}
|
|
|
|
/// A wrapper widget for pawns.
|
|
#[derive(Debug, Clone)]
|
|
pub struct Pawn {
|
|
data: PawnData,
|
|
widget: gtk::Box,
|
|
place_btn: gtk::Button,
|
|
stats_btn: gtk::Button,
|
|
}
|
|
|
|
impl Pawn {
|
|
pub fn new<S: Into<String>>(name: S, description: S) -> Self {
|
|
let pawn_src = include_str!("../res/pawn.glade");
|
|
let builder = gtk::Builder::new_from_string(pawn_src);
|
|
let name = name.into();
|
|
let label: gtk::Label = builder.get_object("name").unwrap();
|
|
label.set_text(&name);
|
|
let widget: gtk::Box = builder.get_object("pawn").unwrap();
|
|
Pawn {
|
|
data: PawnData {
|
|
name,
|
|
description: description.into(),
|
|
position: None,
|
|
},
|
|
widget,
|
|
place_btn: builder.get_object("place_btn").unwrap(),
|
|
stats_btn: builder.get_object("stats_btn").unwrap(),
|
|
}
|
|
}
|
|
|
|
pub fn from_data(data: PawnData) -> Self {
|
|
Self::new(data.name, data.description)
|
|
}
|
|
|
|
pub fn connect_place(&self, state: AppState) {
|
|
let data = self.data.clone();
|
|
self.place_btn.connect_clicked(move |_| {
|
|
println!("Placing {}...", &data.name);
|
|
let mut state = state.0.borrow_mut();
|
|
let _ = state.pending.replace(data.clone()); // ???
|
|
});
|
|
}
|
|
|
|
pub fn connect_stats(&self) {
|
|
self.stats_btn.connect_clicked(move |_| {
|
|
println!("Showing stats...");
|
|
});
|
|
}
|
|
}
|
|
|
|
impl AsRef<gtk::Box> for Pawn {
|
|
fn as_ref(&self) -> >k::Box {
|
|
&self.widget
|
|
}
|
|
}
|
|
|
|
pub fn pawn_factory() -> Vec<Pawn> {
|
|
let mut pawns = Vec::with_capacity(3);
|
|
for name in &["Lomion", "Oilosse", "Fefi"] {
|
|
pawns.push(Pawn::new(*name, ""));
|
|
}
|
|
pawns
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
#[test]
|
|
fn test_pawn_factory() {
|
|
let pawns = pawn_factory();
|
|
assert_eq!(pawns.len(), 3);
|
|
assert_eq!(pawns.get(2).unwrap(), "Fefi");
|
|
}
|
|
}
|