2024-08-25 12:30:03 +00:00
|
|
|
use std::sync::{Arc, Weak};
|
|
|
|
|
|
|
|
use crate::*;
|
|
|
|
|
|
|
|
pub trait ContentWrapper: ContentUpdate + Send + Sync {
|
|
|
|
fn refresh(&mut self) -> Result<()>;
|
2024-08-26 08:08:03 +00:00
|
|
|
fn next_tab(&mut self, engine: &Arc<Engine>, hero: Entity) -> Result<()>;
|
|
|
|
fn previous_tab(&mut self, engine: &Arc<Engine>, hero: Entity) -> Result<()>;
|
2024-08-25 12:30:03 +00:00
|
|
|
fn base(&self) -> &Arc<GuiSnippet>;
|
|
|
|
fn is_empty(&self) -> bool;
|
|
|
|
}
|
|
|
|
|
|
|
|
pub trait ContentUpdate {
|
2024-08-26 08:08:03 +00:00
|
|
|
fn update(&mut self, engine: &Arc<Engine>, hero: Entity) -> Result<()>;
|
2024-08-25 12:30:03 +00:00
|
|
|
fn select(&self) -> Result<()>;
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Content<T: Send + Sync> {
|
|
|
|
pub reference: Weak<CharacterWindow>,
|
|
|
|
base: Arc<GuiSnippet>,
|
|
|
|
data: Vec<T>,
|
|
|
|
|
|
|
|
on_enable: Box<dyn Fn() -> Result<Vec<T>> + Send + Sync + 'static>,
|
|
|
|
|
|
|
|
page: usize,
|
|
|
|
pages: usize,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: Send + Sync> Content<T> {
|
2024-08-26 08:08:03 +00:00
|
|
|
pub fn new<F>(
|
|
|
|
engine: &Arc<Engine>,
|
|
|
|
reference: Weak<CharacterWindow>,
|
|
|
|
on_enable: F,
|
|
|
|
) -> Result<Self>
|
2024-08-25 12:30:03 +00:00
|
|
|
where
|
|
|
|
F: Fn() -> Result<Vec<T>> + Send + Sync + 'static,
|
|
|
|
{
|
2024-08-26 08:08:03 +00:00
|
|
|
let base = GuiSnippet::from_str(
|
|
|
|
engine.gui_handler(),
|
|
|
|
include_str!("../resources/content.xml"),
|
|
|
|
)?;
|
2024-08-25 12:30:03 +00:00
|
|
|
|
|
|
|
let left: Arc<Button> = base.element("left")?;
|
|
|
|
left.set_text("<")?;
|
2024-08-26 08:08:03 +00:00
|
|
|
left.set_info_icon(&engine.controller_icon(ControllerButton::LeftTrigger)?)?;
|
2024-08-25 12:30:03 +00:00
|
|
|
let right: Arc<Button> = base.element("right")?;
|
|
|
|
right.set_text(">")?;
|
2024-08-26 08:08:03 +00:00
|
|
|
right.set_info_icon(&engine.controller_icon(ControllerButton::RightTrigger)?)?;
|
2024-08-25 12:30:03 +00:00
|
|
|
|
|
|
|
Ok(Self {
|
|
|
|
reference,
|
|
|
|
base,
|
|
|
|
data: Vec::new(),
|
|
|
|
|
|
|
|
on_enable: Box::new(on_enable),
|
|
|
|
|
|
|
|
page: 0,
|
|
|
|
pages: 1,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
fn clear_grid(grid: &Arc<Grid>) -> Result<()> {
|
|
|
|
let (rows, columns) = grid.dimensions();
|
|
|
|
|
|
|
|
for x in 0..columns {
|
|
|
|
for y in 0..rows {
|
|
|
|
grid.detach(x, y)?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn set_tab(&self, label: &Arc<Label>) -> Result<()> {
|
|
|
|
label.set_text(format!("{} / {}", self.page + 1, self.pages))
|
|
|
|
}
|
|
|
|
|
2024-08-26 08:08:03 +00:00
|
|
|
pub fn update_base<F>(&mut self, engine: &Arc<Engine>, setup: F) -> Result<()>
|
2024-08-25 12:30:03 +00:00
|
|
|
where
|
|
|
|
Self: ContentWrapper,
|
|
|
|
F: Fn(&Arc<Button>, &T, usize) -> Result<()>,
|
|
|
|
{
|
|
|
|
self.refresh()?;
|
|
|
|
|
|
|
|
let grid: Arc<Grid> = self.base.element("content")?;
|
|
|
|
let label: Arc<Label> = self.base.element("tab_info")?;
|
|
|
|
|
|
|
|
Self::clear_grid(&grid)?;
|
|
|
|
self.set_tab(&label)?;
|
|
|
|
|
|
|
|
let (rows, columns) = grid.dimensions();
|
|
|
|
|
|
|
|
'outer: for y in 0..rows {
|
|
|
|
for x in 0..columns {
|
|
|
|
let index = (self.page * columns * rows) + y * columns + x;
|
|
|
|
|
|
|
|
match self.data.get(index) {
|
|
|
|
Some(t) => {
|
2024-08-26 08:08:03 +00:00
|
|
|
let snippet = GuiSnippet::from_str(
|
|
|
|
engine.gui_handler(),
|
|
|
|
include_str!("../resources/content_button.xml"),
|
|
|
|
)?;
|
2024-08-25 12:30:03 +00:00
|
|
|
|
|
|
|
let button: Arc<Button> = snippet.element("button")?;
|
|
|
|
setup(&button, t, index)?;
|
|
|
|
grid.attach(button, x, y, 1, 1)?;
|
|
|
|
}
|
|
|
|
None => break 'outer,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn select(&self) -> Result<()> {
|
|
|
|
let grid: Arc<Grid> = self.base.element("content")?;
|
|
|
|
|
|
|
|
if let Some(child) = grid.child_at(0, 0)? {
|
|
|
|
child.gridable().unwrap().selectable().unwrap().select()?;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: Send + Sync> ContentWrapper for Content<T>
|
|
|
|
where
|
|
|
|
Content<T>: ContentUpdate,
|
|
|
|
{
|
|
|
|
fn refresh(&mut self) -> Result<()> {
|
|
|
|
self.data = (self.on_enable)()?;
|
|
|
|
|
|
|
|
let grid: Arc<Grid> = self.base.element("content")?;
|
|
|
|
let (rows, columns) = grid.dimensions();
|
|
|
|
|
|
|
|
self.pages = 1.max((self.data.len() as f32 / (rows * columns) as f32).ceil() as usize);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2024-08-26 08:08:03 +00:00
|
|
|
fn next_tab(&mut self, engine: &Arc<Engine>, hero: Entity) -> Result<()> {
|
2024-08-25 12:30:03 +00:00
|
|
|
if self.page < (self.pages - 1) {
|
|
|
|
self.page += 1;
|
2024-08-26 08:08:03 +00:00
|
|
|
self.update(engine, hero)?;
|
2024-08-25 12:30:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2024-08-26 08:08:03 +00:00
|
|
|
fn previous_tab(&mut self, engine: &Arc<Engine>, hero: Entity) -> Result<()> {
|
2024-08-25 12:30:03 +00:00
|
|
|
if self.page > 0 {
|
|
|
|
self.page -= 1;
|
2024-08-26 08:08:03 +00:00
|
|
|
self.update(engine, hero)?;
|
2024-08-25 12:30:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn base(&self) -> &Arc<GuiSnippet> {
|
|
|
|
&self.base
|
|
|
|
}
|
|
|
|
|
|
|
|
fn is_empty(&self) -> bool {
|
|
|
|
self.data.is_empty()
|
|
|
|
}
|
|
|
|
}
|