hUI/hui-examples/examples/ui_test_7_9patch.rs

93 lines
2.8 KiB
Rust
Raw Normal View History

2024-03-25 00:59:13 +00:00
use glam::vec2;
2024-03-24 21:18:15 +00:00
use hui::{
2024-03-25 01:30:51 +00:00
color,
element::{
container::Container,
fill_rect::FillRect,
slider::Slider,
text::Text,
UiElementExt
},
frame::nine_patch::{NinePatchAsset, NinePatchFrame},
layout::Alignment,
rect::Rect,
signal::Signal,
2024-03-25 01:30:51 +00:00
size,
2024-03-24 21:18:15 +00:00
};
#[path = "../boilerplate.rs"]
#[macro_use]
mod boilerplate;
2024-03-25 16:51:34 +00:00
#[derive(Signal)]
2024-03-25 01:30:51 +00:00
struct SetValue(f32);
2024-03-24 21:18:15 +00:00
ui_main!(
"hUI: 9-Patch demo",
2024-03-25 00:59:13 +00:00
init: |ui| {
2024-03-25 01:30:51 +00:00
(
NinePatchAsset {
image: ui.add_image_file_path("./hui-examples/assets/ninepatch_button.png").unwrap(),
size: (190, 49),
scalable_region: Rect {
position: vec2(8. / 190., 8. / 49.),
size: vec2(1. - 16. / 190., 1. - 18. / 49.),
},
2024-03-25 00:59:13 +00:00
},
2024-03-25 01:30:51 +00:00
0.33,
)
2024-03-24 21:18:15 +00:00
},
2024-03-25 01:30:51 +00:00
run: |ui, size, (asset, value)| {
2024-03-24 21:18:15 +00:00
Container::default()
2024-03-24 21:55:15 +00:00
.with_size(size!(100%))
2024-03-24 21:18:15 +00:00
.with_align(Alignment::Center)
2024-03-25 00:59:13 +00:00
.with_gap(5.)
2024-03-24 21:18:15 +00:00
.with_background(color::WHITE)
.with_children(|ui| {
2024-03-25 00:59:13 +00:00
Container::default()
2024-03-24 21:18:15 +00:00
.with_size(size!(300, 100))
2024-03-25 00:59:13 +00:00
.with_background(NinePatchFrame::from_asset(*asset).with_color(color::RED))
.with_padding(10.)
.with_children(|ui| {
Text::new("Hello, world!\nThis is a 9-patch frame used as a background \nfor Container with a Text element.\nIt's scalable and looks great!\nBelow, there are two FillRects with the same \n9-patch frame used as the background.")
.with_text_size(16)
.add_child(ui);
2024-03-24 21:18:15 +00:00
})
.add_child(ui);
2024-03-25 00:59:13 +00:00
FillRect::default()
.with_size(size!(600, 75))
.with_frame(NinePatchFrame::from_asset(*asset).with_color(color::GREEN))
.add_child(ui);
Text::new("This one's fancy:")
.with_color(color::BLACK)
.with_text_size(32)
.add_child(ui);
FillRect::default()
2024-03-25 16:52:24 +00:00
.with_size(size!(700, 50))
2024-03-25 00:59:13 +00:00
.with_frame(NinePatchFrame::from_asset(*asset).with_color((
(1., 0., 1.),
(0., 1., 1.),
(1., 1., 0.),
(0., 0., 1.),
)))
.add_child(ui);
2024-03-25 01:10:15 +00:00
Text::new("Slider customized with `NinePatchFrame`s:")
.with_color(color::BLACK)
.with_text_size(32)
.add_child(ui);
2024-03-25 01:30:51 +00:00
Slider::new(*value)
2024-03-25 01:05:26 +00:00
.with_size(size!(50%, 30))
.with_track_height(1.)
2024-03-25 01:06:23 +00:00
.with_handle_size((20., 1.))
2024-03-25 01:05:26 +00:00
.with_handle(NinePatchFrame::from_asset(*asset).with_color(color::CYAN))
.with_track(NinePatchFrame::from_asset(*asset))
2024-03-25 01:31:57 +00:00
.with_track_active(NinePatchFrame::from_asset(*asset).with_color(color::SKY_BLUE))
2024-03-25 01:30:51 +00:00
.on_change(SetValue)
2024-03-25 01:05:26 +00:00
.add_child(ui);
2024-03-24 21:18:15 +00:00
})
.add_root(ui, size);
2024-03-25 01:30:51 +00:00
ui.process_signals::<SetValue>(|signal| *value = signal.0);
2024-03-24 21:18:15 +00:00
}
);