wgui: interactable components, rename TextLabel -> WidgetLabel

This commit is contained in:
Aleksander
2025-08-10 11:46:01 +02:00
parent 91e584383f
commit 93a3fee349
26 changed files with 294 additions and 174 deletions

View File

@@ -8,12 +8,16 @@
<label text="Raw text" color="#FFFFFF" />
<label translation="TESTBED.HELLO_WORLD" color="#FFFFFF" />
<div margin_left="16" gap="8" flex_direction="column">
<label id="label_current_option" text="Click any of these buttons" size="20" weight="bold" />
<div>
<button id="button_red" text="Red button" width="220" height="32" color="#FF0000" />
<button id="button_aqua" text="Aqua button" width="220" height="32" color="#00FFFF" />
<button id="button_yellow" text="Yellow button" width="220" height="32" color="#FFFF00" />
</div>
<button id="button_click_me" text="Click me" width="128" height="24" color="#FFFFFF" />
</div>
<label id="label_current_option" />
<button margin_left="16" id="button_red" text="Red button" width="128" height="32" color="#FF0000" />
<button margin_left="16" id="button_aqua" text="Aqua button" width="128" height="32" color="#00FFFF" />
<button margin_left="16" id="button_yellow" text="Yellow button" width="128" height="32" color="#FFFF00" />
<div flex_direction="row" gap="16">
<div flex_direction="column" gap="8">

View File

@@ -108,7 +108,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
render_context.update_viewport(&mut shared_context, swapchain_size, scale)?;
println!("new swapchain_size: {swapchain_size:?}");
let mut profiler = profiler::Profiler::new(100);
let mut profiler = profiler::Profiler::new(1000);
let mut frame_index: u64 = 0;
let mut timestep = Timestep::new();

View File

@@ -1,8 +1,18 @@
use std::rc::Rc;
use crate::{assets, testbed::Testbed};
use glam::Vec2;
use wgui::{
components::button::ComponentButton, event::EventListenerCollection, globals::WguiGlobals,
i18n::Translation, layout::Layout, parser::ParserState,
components::{
Component,
button::{ButtonClickCallback, ComponentButton},
},
event::EventListenerCollection,
globals::WguiGlobals,
i18n::Translation,
layout::{Layout, Widget},
parser::ParserState,
widget::label::WidgetLabel,
};
pub struct TestbedGeneric {
@@ -12,24 +22,63 @@ pub struct TestbedGeneric {
state: ParserState,
}
fn button_click_callback(
button: Component,
label: Widget,
text: &'static str,
) -> ButtonClickCallback {
Box::new(move |e| {
label.get_as_mut::<WidgetLabel>().set_text(
&mut e.state.globals.i18n(),
Translation::from_raw_text(text),
);
// FIXME: remove unwrap
button.try_cast::<ComponentButton>().unwrap().set_text(
e.state,
e.alterables,
Translation::from_raw_text("this button has been clicked"),
);
})
}
fn handle_button_click(button: Rc<ComponentButton>, label: Widget, text: &'static str) {
button.on_click(button_click_callback(
Component(button.clone()),
label,
text,
));
}
impl TestbedGeneric {
pub fn new(listeners: &mut EventListenerCollection<(), ()>) -> anyhow::Result<Self> {
const XML_PATH: &str = "gui/various_widgets.xml";
let globals = WguiGlobals::new(Box::new(assets::Asset {}))?;
let (mut layout, state) =
let (layout, state) =
wgui::parser::new_layout_from_assets(globals, listeners, XML_PATH, false)?;
let label_current_option = state.fetch_widget("label_current_option")?;
let b1 = state.fetch_component_as::<ComponentButton>("button_red")?;
let b2 = state.fetch_component_as::<ComponentButton>("button_aqua")?;
let b3 = state.fetch_component_as::<ComponentButton>("button_yellow")?;
let label_cur_option =
state.fetch_widget::<WidgetLabel>(&layout.state, "label_current_option")?;
b1.set_text(
&mut layout.state,
Translation::from_raw_text("hello, world!"),
);
let button_click_me = state.fetch_component_as::<ComponentButton>("button_click_me")?;
let button = button_click_me.clone();
button_click_me.on_click(Box::new(move |e| {
button.set_text(
e.state,
e.alterables,
Translation::from_raw_text("congrats!"),
);
}));
let button_red = state.fetch_component_as::<ComponentButton>("button_red")?;
let button_aqua = state.fetch_component_as::<ComponentButton>("button_aqua")?;
let button_yellow = state.fetch_component_as::<ComponentButton>("button_yellow")?;
handle_button_click(button_red, label_cur_option.clone(), "Clicked red");
handle_button_click(button_aqua, label_cur_option.clone(), "Clicked aqua");
handle_button_click(button_yellow, label_cur_option.clone(), "Clicked yellow");
Ok(Self { layout, state })
}