2024-03-24 16:18:15 -05:00
use std ::time ::Instant ;
2024-03-24 19:59:13 -05:00
use glam ::vec2 ;
2024-03-24 16:18:15 -05:00
use hui ::{
color , element ::{
2024-03-24 20:05:26 -05:00
container ::Container , fill_rect ::FillRect , slider ::Slider , text ::Text , UiElementExt
2024-03-24 19:59:13 -05:00
} , frame ::nine_patch ::{ NinePatchAsset , NinePatchFrame } , frame_rect , layout ::{ Alignment , Direction } , rect ::Rect , size
2024-03-24 16:18:15 -05:00
} ;
#[ path = " ../boilerplate.rs " ]
#[ macro_use ]
mod boilerplate ;
ui_main! (
" hUI: 9-Patch demo " ,
2024-03-24 19:59:13 -05:00
init : | ui | {
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 16:18:15 -05:00
} ,
2024-03-24 19:59:13 -05:00
run : | ui , size , asset | {
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-03-24 19:59:13 -05: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 ( )
. with_size ( size! ( 800 , 50 ) )
. 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:05:26 -05:00
Slider ::new ( 0.33 )
. with_size ( size! ( 50 % , 30 ) )
. with_track_height ( 1. )
. with_handle_size ( ( 20. , 0.9 ) )
. with_handle ( NinePatchFrame ::from_asset ( * asset ) . with_color ( color ::CYAN ) )
. with_track ( NinePatchFrame ::from_asset ( * asset ) )
. with_track_active ( color ::TRANSPARENT )
. add_child ( ui ) ;
2024-03-24 16:18:15 -05:00
} )
. add_root ( ui , size ) ;
}
) ;