Implement custom layer for grids

This commit is contained in:
hodasemi 2024-05-16 08:45:43 +02:00
parent e0053d93e2
commit 34b569b59b
2 changed files with 37 additions and 12 deletions

View file

@ -102,12 +102,9 @@ impl GuiBuilder {
&mut opt_default_select,
&mut custom_neighbours,
validator.dimensions(),
validator.layer().unwrap_or(5),
)?;
if let Some(layer) = validator.layer() {
tree.set_layer(layer)?;
}
grids.push(tree);
}
@ -341,10 +338,11 @@ impl GuiBuilder {
default_button: &mut Option<Arc<Button>>,
neighbour_infos: &mut Vec<(String, Vec<NeighbourInfo>)>,
(reference_width, reference_height): (Option<u32>, Option<u32>),
layer: i32,
) -> Result<Arc<Grid>> {
// let mut ids = HashMap::new();
let root_grid = Grid::try_from(root_grid_info, gui_handler, true)?;
let root_layer = root_grid_info.layer.unwrap_or(layer);
root_grid.set_layer(root_layer)?;
if let Some(ref_width) = reference_width {
if let Some(ref_height) = reference_height {
@ -391,6 +389,7 @@ impl GuiBuilder {
ids,
default_button,
neighbour_infos,
root_layer,
)?;
}
@ -404,6 +403,7 @@ impl GuiBuilder {
ids: &mut HashMap<String, UiElement>,
default_button: &mut Option<Arc<Button>>,
neighbour_infos: &mut Vec<(String, Vec<NeighbourInfo>)>,
layer: i32,
) -> Result<()> {
match child {
UiInfoElement::Button(button_info) => {
@ -425,6 +425,8 @@ impl GuiBuilder {
button_info.y_dim,
)?;
button.set_layer(layer)?;
neighbour_infos.push((button_info.id.clone(), button_info.neighbour_infos.clone()));
if button_info.select {
@ -444,12 +446,14 @@ impl GuiBuilder {
Self::insert_id(ids, &label_info.id, &label)?;
grid.attach(
label,
label.clone(),
label_info.x_slot.get().with_context(|| "x_slot of label")?,
label_info.y_slot.get().with_context(|| "y_slot of label")?,
label_info.x_dim,
label_info.y_dim,
)?;
label.set_layer(layer)?;
}
UiInfoElement::MultiLineLabel(multi_line_label_info) => {
let multi_line_label =
@ -458,7 +462,7 @@ impl GuiBuilder {
GuiBuilder::insert_id(ids, &multi_line_label_info.id, &multi_line_label)?;
grid.attach(
multi_line_label,
multi_line_label.clone(),
multi_line_label_info
.x_slot
.get()
@ -470,6 +474,8 @@ impl GuiBuilder {
multi_line_label_info.x_dim,
multi_line_label_info.y_dim,
)?;
multi_line_label.set_layer(layer)?;
}
UiInfoElement::MultiLineTextField(multi_line_text_field_info) => {
let multi_line_text_field =
@ -478,7 +484,7 @@ impl GuiBuilder {
GuiBuilder::insert_id(ids, &multi_line_text_field_info.id, &multi_line_text_field)?;
grid.attach(
multi_line_text_field,
multi_line_text_field.clone(),
multi_line_text_field_info
.x_slot
.get()
@ -490,6 +496,8 @@ impl GuiBuilder {
multi_line_text_field_info.x_dim,
multi_line_text_field_info.y_dim,
)?;
multi_line_text_field.set_layer(layer)?;
}
UiInfoElement::TextField(text_field_info) => {
let text_field = TextField::try_from(text_field_info, gui_handler)?;
@ -497,7 +505,7 @@ impl GuiBuilder {
Self::insert_id(ids, &text_field_info.id, &text_field)?;
grid.attach(
text_field,
text_field.clone(),
text_field_info
.x_slot
.get()
@ -509,6 +517,8 @@ impl GuiBuilder {
text_field_info.x_dim,
text_field_info.y_dim,
)?;
text_field.set_layer(layer)?;
}
UiInfoElement::Icon(icon_info) => {
let icon = Icon::try_from(icon_info, gui_handler)?;
@ -516,12 +526,14 @@ impl GuiBuilder {
Self::insert_id(ids, &icon_info.id, UiElement::Icon(Arc::downgrade(&icon)))?;
grid.attach(
icon,
icon.clone(),
icon_info.x_slot.get().with_context(|| "x_slot of icon")?,
icon_info.y_slot.get().with_context(|| "y_slot of icon")?,
icon_info.x_dim,
icon_info.y_dim,
)?;
icon.set_layer(layer)?;
}
UiInfoElement::ProgressBar(progress_bar_info) => {
let progress_bar = ProgressBar::try_from(progress_bar_info, gui_handler)?;
@ -529,7 +541,7 @@ impl GuiBuilder {
Self::insert_id(ids, &progress_bar_info.id, &progress_bar)?;
grid.attach(
progress_bar,
progress_bar.clone(),
progress_bar_info
.x_slot
.get()
@ -541,6 +553,8 @@ impl GuiBuilder {
progress_bar_info.x_dim,
progress_bar_info.y_dim,
)?;
progress_bar.set_layer(layer)?;
}
UiInfoElement::Grid(grid_info) => {
let sub_grid = Grid::try_from(grid_info, gui_handler, false)?;
@ -555,6 +569,9 @@ impl GuiBuilder {
grid_info.y_dim,
)?;
let sub_grid_layer = grid_info.layer.unwrap_or(layer);
sub_grid.set_layer(sub_grid_layer)?;
for child in grid_info.children.read().unwrap().iter() {
Self::create_child(
gui_handler,
@ -563,6 +580,7 @@ impl GuiBuilder {
ids,
default_button,
neighbour_infos,
sub_grid_layer,
)?;
}
}

View file

@ -45,6 +45,8 @@ pub struct GridInfo {
pub children: RwLock<Vec<UiInfoElement>>,
pub parent: Option<Weak<GridInfo>>,
pub layer: Option<i32>,
}
impl GridInfo {
@ -82,6 +84,8 @@ impl GridInfo {
children: RwLock::new(Vec::new()),
parent: None,
layer: None,
};
for attribute_res in attributes {
@ -122,6 +126,9 @@ impl GridInfo {
b"hover_sound" => {
grid_info.hover_sound = Some(cow_to_path(attribute.value));
}
b"layer" => {
grid_info.layer = Some(str_into(attribute.value)?);
}
_ => {
return Err(anyhow::Error::msg(format!(
"Unsupported attribute in Grid: {}",