2024-03-24 19:59:13 -05:00
use glam ::vec2 ;
2024-03-24 16:18:15 -05:00
use hui ::{
2024-03-24 20:30:51 -05:00
color ,
element ::{
container ::Container ,
2024-04-17 08:47:22 -05:00
frame_view ::FrameView ,
2024-03-24 20:30:51 -05:00
slider ::Slider ,
text ::Text ,
UiElementExt
} ,
frame ::nine_patch ::{ NinePatchAsset , NinePatchFrame } ,
layout ::Alignment ,
rect ::Rect ,
2024-03-25 12:14:36 -05:00
signal ::Signal ,
2024-03-24 20:30:51 -05:00
size ,
2024-03-24 16:18:15 -05:00
} ;
#[ path = " ../boilerplate.rs " ]
#[ macro_use ]
mod boilerplate ;
2024-03-25 11:51:34 -05:00
#[ derive(Signal) ]
2024-03-24 20:30:51 -05:00
struct SetValue ( f32 ) ;
2024-03-24 16:18:15 -05:00
ui_main! (
" hUI: 9-Patch demo " ,
2024-03-24 19:59:13 -05:00
init : | ui | {
2024-03-24 20:30:51 -05: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-24 19:59:13 -05:00
} ,
2024-03-24 20:30:51 -05:00
0.33 ,
)
2024-03-24 16:18:15 -05:00
} ,
2024-03-24 20:30:51 -05:00
run : | ui , size , ( asset , value ) | {
2024-03-24 16:18:15 -05:00
Container ::default ( )
2024-03-24 16:55:15 -05:00
. with_size ( size! ( 100 % ) )
2024-03-24 16:18:15 -05:00
. with_align ( Alignment ::Center )
2024-03-24 19:59:13 -05:00
. with_gap ( 5. )
2024-03-24 16:18:15 -05:00
. with_background ( color ::WHITE )
. with_children ( | ui | {
2024-03-24 19:59:13 -05:00
Container ::default ( )
2024-03-24 16:18:15 -05:00
. with_size ( size! ( 300 , 100 ) )
2024-03-24 19:59:13 -05:00
. with_background ( NinePatchFrame ::from_asset ( * asset ) . with_color ( color ::RED ) )
. with_padding ( 10. )
. with_children ( | ui | {
Text ::new ( " Hello, world! \n This is a 9-patch frame used as a background \n for Container with a Text element. \n It's scalable and looks great! \n Below, there are two FillRects with the same \n 9-patch frame used as the background. " )
. with_text_size ( 16 )
. add_child ( ui ) ;
2024-03-24 16:18:15 -05:00
} )
. add_child ( ui ) ;
2024-04-17 08:44:47 -05:00
FrameView ::default ( )
2024-03-24 19:59:13 -05:00
. 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 ) ;
2024-04-17 08:44:47 -05:00
FrameView ::default ( )
2024-03-25 11:52:24 -05:00
. with_size ( size! ( 700 , 50 ) )
2024-03-24 19:59:13 -05: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-24 20:10:15 -05:00
Text ::new ( " Slider customized with `NinePatchFrame`s: " )
. with_color ( color ::BLACK )
. with_text_size ( 32 )
. add_child ( ui ) ;
2024-03-24 20:30:51 -05:00
Slider ::new ( * value )
2024-03-24 20:05:26 -05:00
. with_size ( size! ( 50 % , 30 ) )
. with_track_height ( 1. )
2024-03-24 20:06:23 -05:00
. with_handle_size ( ( 20. , 1. ) )
2024-03-24 20:05:26 -05:00
. with_handle ( NinePatchFrame ::from_asset ( * asset ) . with_color ( color ::CYAN ) )
. with_track ( NinePatchFrame ::from_asset ( * asset ) )
2024-03-24 20:31:57 -05:00
. with_track_active ( NinePatchFrame ::from_asset ( * asset ) . with_color ( color ::SKY_BLUE ) )
2024-03-24 20:30:51 -05:00
. on_change ( SetValue )
2024-03-24 20:05:26 -05:00
. add_child ( ui ) ;
2024-03-24 16:18:15 -05:00
} )
. add_root ( ui , size ) ;
2024-03-24 20:30:51 -05:00
ui . process_signals ::< SetValue > ( | signal | * value = signal . 0 ) ;
2024-03-24 16:18:15 -05:00
}
) ;