clean up example, make slider work

This commit is contained in:
griffi-gh 2024-03-25 02:30:51 +01:00
parent af0bf04ffc
commit 579b7c5484

View file

@ -1,28 +1,43 @@
use std::time::Instant;
use glam::vec2; use glam::vec2;
use hui::{ use hui::{
color, element::{ color,
container::Container, fill_rect::FillRect, slider::Slider, text::Text, UiElementExt element::{
}, frame::nine_patch::{NinePatchAsset, NinePatchFrame}, frame_rect, layout::{Alignment, Direction}, rect::Rect, size container::Container,
fill_rect::FillRect,
slider::Slider,
text::Text,
UiElementExt
},
frame::nine_patch::{NinePatchAsset, NinePatchFrame},
layout::Alignment,
rect::Rect,
signal::Signal,
size,
}; };
#[path = "../boilerplate.rs"] #[path = "../boilerplate.rs"]
#[macro_use] #[macro_use]
mod boilerplate; mod boilerplate;
struct SetValue(f32);
impl Signal for SetValue {}
ui_main!( ui_main!(
"hUI: 9-Patch demo", "hUI: 9-Patch demo",
init: |ui| { init: |ui| {
NinePatchAsset { (
image: ui.add_image_file_path("./hui-examples/assets/ninepatch_button.png").unwrap(), NinePatchAsset {
size: (190, 49), image: ui.add_image_file_path("./hui-examples/assets/ninepatch_button.png").unwrap(),
scalable_region: Rect { size: (190, 49),
position: vec2(8. / 190., 8. / 49.), scalable_region: Rect {
size: vec2(1. - 16. / 190., 1. - 18. / 49.), position: vec2(8. / 190., 8. / 49.),
size: vec2(1. - 16. / 190., 1. - 18. / 49.),
},
}, },
} 0.33,
)
}, },
run: |ui, size, asset| { run: |ui, size, (asset, value)| {
Container::default() Container::default()
.with_size(size!(100%)) .with_size(size!(100%))
.with_align(Alignment::Center) .with_align(Alignment::Center)
@ -60,15 +75,18 @@ ui_main!(
.with_color(color::BLACK) .with_color(color::BLACK)
.with_text_size(32) .with_text_size(32)
.add_child(ui); .add_child(ui);
Slider::new(0.33) Slider::new(*value)
.with_size(size!(50%, 30)) .with_size(size!(50%, 30))
.with_track_height(1.) .with_track_height(1.)
.with_handle_size((20., 1.)) .with_handle_size((20., 1.))
.with_handle(NinePatchFrame::from_asset(*asset).with_color(color::CYAN)) .with_handle(NinePatchFrame::from_asset(*asset).with_color(color::CYAN))
.with_track(NinePatchFrame::from_asset(*asset)) .with_track(NinePatchFrame::from_asset(*asset))
.with_track_active(color::TRANSPARENT) .with_track_active(color::TRANSPARENT)
.on_change(SetValue)
.add_child(ui); .add_child(ui);
}) })
.add_root(ui, size); .add_root(ui, size);
ui.process_signals::<SetValue>(|signal| *value = signal.0);
} }
); );