wgui: checkbox component

This commit is contained in:
Aleksander
2025-08-13 19:42:48 +02:00
parent faec8866c5
commit df35dba24f
8 changed files with 503 additions and 35 deletions

View File

@@ -0,0 +1,57 @@
use crate::{
components::{Component, checkbox},
i18n::Translation,
layout::WidgetID,
parser::{
ParserContext, ParserFile, iter_attribs, parse_check_f32, parse_check_i32, process_component,
style::parse_style,
},
};
pub fn parse_component_checkbox<'a, U1, U2>(
file: &'a ParserFile,
ctx: &mut ParserContext<U1, U2>,
node: roxmltree::Node<'a, 'a>,
parent_id: WidgetID,
) -> anyhow::Result<()> {
let mut box_size = 24.0;
let mut translation = Translation::default();
let mut checked = 0;
let attribs: Vec<_> = iter_attribs(file, ctx, &node, false).collect();
let style = parse_style(&attribs);
for (key, value) in attribs {
match key.as_ref() {
"text" => {
translation = Translation::from_raw_text(&value);
}
"translation" => {
translation = Translation::from_translation_key(&value);
}
"box_size" => {
parse_check_f32(value.as_ref(), &mut box_size);
}
"checked" => {
parse_check_i32(value.as_ref(), &mut checked);
}
_ => {}
}
}
let component = checkbox::construct(
ctx.layout,
ctx.listeners,
parent_id,
checkbox::Params {
box_size,
text: translation,
checked: checked != 0,
style,
},
)?;
process_component(file, ctx, node, Component(component))?;
Ok(())
}

View File

@@ -1,4 +1,5 @@
mod component_button;
mod component_checkbox;
mod component_slider;
mod style;
mod widget_div;
@@ -14,9 +15,10 @@ use crate::{
globals::WguiGlobals,
layout::{Layout, LayoutState, Widget, WidgetID},
parser::{
component_button::parse_component_button, component_slider::parse_component_slider,
widget_div::parse_widget_div, 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,
component_slider::parse_component_slider, widget_div::parse_widget_div,
widget_label::parse_widget_label, widget_rectangle::parse_widget_rectangle,
widget_sprite::parse_widget_sprite,
},
};
use ouroboros::self_referencing;
@@ -92,12 +94,12 @@ impl ParserState {
}
}
pub fn fetch_widget<T: 'static>(&self, state: &LayoutState, id: &str) -> anyhow::Result<Widget> {
pub fn fetch_widget(&self, state: &LayoutState, id: &str) -> anyhow::Result<Widget> {
let widget_id = self.get_widget_id(id)?;
let widget = state
.widgets
.get(widget_id)
.ok_or_else(|| anyhow::anyhow!("fetch_widget_as({}): widget not found", id))?;
.ok_or_else(|| anyhow::anyhow!("fetch_widget({}): widget not found", id))?;
Ok(widget.clone())
}
@@ -253,10 +255,24 @@ fn parse_percent(value: &str) -> Option<f32> {
Some(val / 100.0)
}
fn parse_i32(value: &str) -> Option<i32> {
value.parse::<i32>().ok()
}
fn parse_f32(value: &str) -> Option<f32> {
value.parse::<f32>().ok()
}
fn parse_check_i32(value: &str, num: &mut i32) -> bool {
if let Some(value) = parse_i32(value) {
*num = value;
true
} else {
print_invalid_value(value);
false
}
}
fn parse_check_f32(value: &str, num: &mut f32) -> bool {
if let Some(value) = parse_f32(value) {
*num = value;
@@ -660,6 +676,9 @@ fn parse_children<'a, U1, U2>(
"slider" => {
parse_component_slider(file, ctx, child_node, parent_id)?;
}
"check_box" => {
parse_component_checkbox(file, ctx, child_node, parent_id)?;
}
"" => { /* ignore */ }
other_tag_name => {
parse_widget_other(other_tag_name, file, ctx, child_node, parent_id)?;