RadioBox & RadioGroup

This commit is contained in:
galister
2026-01-03 16:14:29 +09:00
parent 96cbc5af78
commit 648e9ae219
8 changed files with 311 additions and 15 deletions

View File

@@ -1,18 +1,27 @@
use crate::{
components::{Component, checkbox},
components::{checkbox, radio_group::ComponentRadioGroup, Component},
i18n::Translation,
layout::WidgetID,
parser::{AttribPair, ParserContext, parse_check_f32, parse_check_i32, process_component, style::parse_style},
parser::{
parse_check_f32, parse_check_i32, process_component, style::parse_style, AttribPair, Fetchable, ParserContext,
},
};
pub enum CheckboxKind {
CheckBox,
RadioBox,
}
pub fn parse_component_checkbox(
ctx: &mut ParserContext,
parent_id: WidgetID,
attribs: &[AttribPair],
kind: CheckboxKind,
) -> anyhow::Result<WidgetID> {
let mut box_size = 24.0;
let mut translation = Translation::default();
let mut checked = 0;
let mut component_value = None;
let style = parse_style(attribs);
@@ -25,6 +34,9 @@ pub fn parse_component_checkbox(
"translation" => {
translation = Translation::from_translation_key(value);
}
"value" => {
component_value = Some(value.into());
}
"box_size" => {
parse_check_f32(value, &mut box_size);
}
@@ -35,6 +47,28 @@ pub fn parse_component_checkbox(
}
}
let mut radio_group = None;
if matches!(kind, CheckboxKind::RadioBox) {
let mut maybe_parent_id = Some(parent_id);
while let Some(parent_id) = maybe_parent_id {
if let Ok(radio) = ctx
.data_local
.fetch_component_from_widget_id_as::<ComponentRadioGroup>(parent_id)
{
radio_group = Some(radio);
break;
}
maybe_parent_id = ctx.layout.get_parent(parent_id).map(|(widget_id, _)| widget_id);
}
if radio_group.is_none() {
log::error!("RadioBox component without a Radio group!");
}
}
let (widget, component) = checkbox::construct(
&mut ctx.get_construct_essentials(parent_id),
checkbox::Params {
@@ -42,6 +76,8 @@ pub fn parse_component_checkbox(
text: translation,
checked: checked != 0,
style,
radio_group,
value: component_value,
},
)?;

View File

@@ -0,0 +1,22 @@
use crate::{
components::{radio_group, Component},
layout::WidgetID,
parser::{parse_children, process_component, style::parse_style, AttribPair, ParserContext, ParserFile},
};
pub fn parse_component_radio_group<'a>(
file: &'a ParserFile,
ctx: &mut ParserContext,
node: roxmltree::Node<'a, 'a>,
parent_id: WidgetID,
attribs: &[AttribPair],
) -> anyhow::Result<WidgetID> {
let style = parse_style(attribs);
let (widget, component) = radio_group::construct(&mut ctx.get_construct_essentials(parent_id), style)?;
process_component(ctx, Component(component), widget.id, attribs);
parse_children(file, ctx, node, widget.id)?;
Ok(widget.id)
}

View File

@@ -1,5 +1,6 @@
mod component_button;
mod component_checkbox;
mod component_radio_group;
mod component_slider;
mod style;
mod widget_div;
@@ -9,15 +10,13 @@ mod widget_rectangle;
mod widget_sprite;
use crate::{
assets::{AssetPath, AssetPathOwned, normalize_path},
assets::{normalize_path, AssetPath, AssetPathOwned},
components::{Component, ComponentWeak},
drawing::{self},
globals::WguiGlobals,
layout::{Layout, LayoutParams, LayoutState, Widget, WidgetID, WidgetMap, WidgetPair},
parser::{
component_button::parse_component_button, component_checkbox::parse_component_checkbox,
component_slider::parse_component_slider, widget_div::parse_widget_div, widget_image::parse_widget_image,
widget_label::parse_widget_label, widget_rectangle::parse_widget_rectangle, widget_sprite::parse_widget_sprite,
component_button::parse_component_button, component_checkbox::{parse_component_checkbox, CheckboxKind}, component_radio_group::parse_component_radio_group, component_slider::parse_component_slider, widget_div::parse_widget_div, widget_image::parse_widget_image, widget_label::parse_widget_label, widget_rectangle::parse_widget_rectangle, widget_sprite::parse_widget_sprite
},
widget::ConstructEssentials,
};
@@ -909,7 +908,13 @@ fn parse_child<'a>(
new_widget_id = Some(parse_component_slider(ctx, parent_id, &attribs)?);
}
"CheckBox" => {
new_widget_id = Some(parse_component_checkbox(ctx, parent_id, &attribs)?);
new_widget_id = Some(parse_component_checkbox(ctx, parent_id, &attribs, CheckboxKind::CheckBox)?);
}
"RadioBox" => {
new_widget_id = Some(parse_component_checkbox(ctx, parent_id, &attribs, CheckboxKind::RadioBox)?);
}
"RadioGroup" => {
new_widget_id = Some(parse_component_radio_group(file, ctx, child_node, parent_id, &attribs)?);
}
"" => { /* ignore */ }
other_tag_name => {