2024-01-14 14:13:56 +00:00
use core ::fmt ::Write ;
2022-01-21 07:01:56 +00:00
use graphics_server ::api ::GlyphStyle ;
2022-01-20 16:34:12 +00:00
use graphics_server ::* ;
use num_traits ::* ;
#[ derive(Debug, num_derive::FromPrimitive, num_derive::ToPrimitive) ]
enum TestType {
2023-10-16 06:25:47 +00:00
BusyAnimation = 9 ,
BoundingBox = 10 ,
LowerRight = 11 ,
LowerLeft = 12 ,
TopLeft = 13 ,
TopRight = 14 ,
Overflow = 15 ,
Insert = 16 ,
End = 17 ,
2022-01-20 16:34:12 +00:00
}
2023-07-05 05:37:41 +00:00
const TEST_STYLE : GlyphStyle = GlyphStyle ::Tall ;
2022-01-20 16:34:12 +00:00
pub fn tests ( ) {
let _ = std ::thread ::spawn ( {
move | | {
let xns = xous_names ::XousNames ::new ( ) . unwrap ( ) ;
let gfx = graphics_server ::Gfx ::new ( & xns ) . unwrap ( ) ;
let ticktimer = ticktimer_server ::Ticktimer ::new ( ) . expect ( " Couldn't connect to Ticktimer " ) ;
2023-10-16 06:25:47 +00:00
for index in TestType ::BusyAnimation . to_usize ( ) . unwrap ( ) .. TestType ::End . to_usize ( ) . unwrap ( ) {
// pause between each tests
2022-01-20 16:34:12 +00:00
ticktimer . sleep_ms ( 1000 ) . unwrap ( ) ;
// draw a black screen
let screensize = gfx . screen_size ( ) . expect ( " Couldn't get screen size " ) ;
let blackout = Rectangle ::new_with_style (
Point ::new ( 0 , 0 ) ,
screensize ,
2024-01-14 14:13:56 +00:00
DrawStyle ::new ( PixelColor ::Dark , PixelColor ::Dark , 1 ) ,
2022-01-20 16:34:12 +00:00
) ;
gfx . draw_rectangle ( blackout ) . unwrap ( ) ;
gfx . flush ( ) . unwrap ( ) ;
// start the test
ticktimer . sleep_ms ( 1000 ) . unwrap ( ) ;
2022-01-20 18:30:58 +00:00
let clipping_area = Rectangle ::new_coords ( 50 , 50 , 290 , 450 ) ;
2022-01-20 16:34:12 +00:00
let text_bounds = Rectangle ::new_coords ( 10 , 10 , 240 , 300 ) ;
2022-01-20 18:30:58 +00:00
//let mut checkbound = clipping_area.clone(); // this checks against the final clipping area.
let mut checkbound = text_bounds . clone ( ) ; // this is just around the bounds specified by the TV
2022-01-20 16:34:12 +00:00
checkbound . translate ( clipping_area . tl ( ) ) ;
checkbound . margin_out ( Point ::new ( 1 , 1 ) ) ;
checkbound . style = DrawStyle ::new ( PixelColor ::Light , PixelColor ::Light , 1 ) ;
gfx . draw_rectangle ( checkbound ) . unwrap ( ) ;
gfx . flush ( ) . unwrap ( ) ;
match FromPrimitive ::from_usize ( index ) {
2023-10-16 06:25:47 +00:00
Some ( TestType ::BusyAnimation ) = > {
let anim_rect = Rectangle ::new_coords ( 10 , 10 , 240 , 30 ) ;
2024-01-14 14:13:56 +00:00
let mut tv =
TextView ::new ( Gid ::new ( [ 0 , 0 , 0 , 0 ] ) , TextBounds ::BoundingBox ( anim_rect ) ) ;
2023-10-16 06:25:47 +00:00
tv . clip_rect = Some ( clipping_area ) ;
2023-10-28 14:50:23 +00:00
tv . style = GlyphStyle ::Small ;
2023-10-16 06:25:47 +00:00
tv . ellipsis = false ;
write! ( tv , " Test of busy animation " ) . unwrap ( ) ;
tv . insertion = None ;
tv . draw_border = false ;
tv . busy_animation_state = Some ( 0 ) ;
2023-10-18 06:13:25 +00:00
for y in 0 .. 30 {
gfx . draw_rectangle ( checkbound ) . unwrap ( ) ;
2024-01-14 14:13:56 +00:00
tv . bounds_hint =
TextBounds ::BoundingBox ( Rectangle ::new_coords ( 10 , 10 + y , 240 , 30 + y ) ) ;
2023-10-18 06:13:25 +00:00
for _ in 0 .. 5 {
gfx . draw_textview ( & mut tv ) . unwrap ( ) ;
gfx . flush ( ) . unwrap ( ) ;
ticktimer . sleep_ms ( 100 ) . unwrap ( ) ;
}
2023-10-16 06:25:47 +00:00
}
}
2022-01-20 16:34:12 +00:00
Some ( TestType ::BoundingBox ) = > {
2024-01-14 14:13:56 +00:00
let mut tv =
TextView ::new ( Gid ::new ( [ 0 , 0 , 0 , 0 ] ) , TextBounds ::BoundingBox ( text_bounds ) ) ;
2022-01-20 16:34:12 +00:00
tv . clip_rect = Some ( clipping_area ) ;
2022-01-21 09:28:23 +00:00
tv . style = TEST_STYLE ;
2022-01-20 16:34:12 +00:00
tv . ellipsis = true ;
tv . rounded_border = Some ( 4 ) ;
2022-01-21 10:06:55 +00:00
write! ( tv , " This is a test of basic word wrapping 自动换行 inside a 😃 bounding box. \n This should be a new line. \n \n Two new lines. \n New line \n with a leading space. \n Done. " ) . unwrap ( ) ;
write! ( tv , " Let's add more text😃 until it overflows this is just another test with https://github.com/samblenny/blitstr2/commit/bb7d4ab6a2d8913dcb520895a3c242c933413aae more words and words and words and whatever. " ) . unwrap ( ) ;
2022-01-20 16:34:12 +00:00
log ::info! ( " rendering: {:?} " , tv ) ;
tv . insertion = None ;
gfx . draw_textview ( & mut tv ) . unwrap ( ) ;
gfx . flush ( ) . unwrap ( ) ;
log ::info! ( " rendered: {:?} " , tv ) ;
ticktimer . sleep_ms ( 1000 ) . unwrap ( ) ;
}
Some ( TestType ::LowerRight ) = > {
2024-01-14 14:13:56 +00:00
let mut tv = TextView ::new (
Gid ::new ( [ 0 , 0 , 0 , 0 ] ) ,
TextBounds ::GrowableFromBr (
Point ::new ( text_bounds . br ( ) . x - 4 , text_bounds . br ( ) . y - 4 ) ,
( ( text_bounds . br ( ) . x - text_bounds . tl ( ) . x ) / 2 ) as u16 ,
) ,
) ;
2022-01-20 16:34:12 +00:00
tv . clip_rect = Some ( clipping_area ) ;
2022-01-21 09:28:23 +00:00
tv . style = TEST_STYLE ;
2022-01-20 16:34:12 +00:00
tv . ellipsis = true ;
tv . rounded_border = Some ( 8 ) ;
2022-01-21 10:06:55 +00:00
write! ( tv , " This is a test of basic word😃 wrapping 自动换行 inside a bounding box. \n This should be a new line. \n \n Two new lines. \n New line \n with a leading space. \n Done. " ) . unwrap ( ) ;
2022-01-20 16:34:12 +00:00
log ::info! ( " rendering: {:?} " , tv ) ;
2022-01-21 10:16:02 +00:00
tv . insertion = Some ( 0 ) ;
2022-01-20 16:34:12 +00:00
gfx . draw_textview ( & mut tv ) . unwrap ( ) ;
gfx . flush ( ) . unwrap ( ) ;
log ::info! ( " rendered: {:?} " , tv ) ;
ticktimer . sleep_ms ( 1000 ) . unwrap ( ) ;
}
Some ( TestType ::LowerLeft ) = > {
2024-01-14 14:13:56 +00:00
let mut tv = TextView ::new (
Gid ::new ( [ 0 , 0 , 0 , 0 ] ) ,
TextBounds ::GrowableFromBl (
Point ::new ( text_bounds . tl ( ) . x + 4 , text_bounds . br ( ) . y - 4 ) ,
( ( text_bounds . br ( ) . x - text_bounds . tl ( ) . x ) / 2 ) as u16 ,
) ,
) ;
2022-01-20 16:34:12 +00:00
tv . clip_rect = Some ( clipping_area ) ;
2022-01-21 09:28:23 +00:00
tv . style = TEST_STYLE ;
2022-01-20 16:34:12 +00:00
tv . ellipsis = true ;
tv . rounded_border = None ;
tv . invert = true ;
2022-01-21 10:06:55 +00:00
write! ( tv , " This is a test of basic word😃 wrapping 自动换行 inside a bounding box. \n This should be a new line. \n \n Two new lines. \n New line \n with a leading space. \n Done. " ) . unwrap ( ) ;
2022-01-20 16:34:12 +00:00
log ::info! ( " rendering: {:?} " , tv ) ;
2022-01-21 10:16:02 +00:00
tv . insertion = Some ( 3 ) ;
2022-01-20 16:34:12 +00:00
gfx . draw_textview ( & mut tv ) . unwrap ( ) ;
gfx . flush ( ) . unwrap ( ) ;
log ::info! ( " rendered: {:?} " , tv ) ;
ticktimer . sleep_ms ( 1000 ) . unwrap ( ) ;
}
Some ( TestType ::TopLeft ) = > {
2024-01-14 14:13:56 +00:00
let mut tv = TextView ::new (
Gid ::new ( [ 0 , 0 , 0 , 0 ] ) ,
TextBounds ::GrowableFromTl (
Point ::new ( text_bounds . tl ( ) . x + 4 , text_bounds . tl ( ) . y + 4 ) ,
( ( text_bounds . br ( ) . x - text_bounds . tl ( ) . x ) / 2 ) as u16 + 20 ,
) ,
) ;
2022-01-20 16:34:12 +00:00
tv . clip_rect = Some ( clipping_area ) ;
2022-01-21 09:28:23 +00:00
tv . style = TEST_STYLE ;
2022-01-20 16:34:12 +00:00
tv . ellipsis = false ;
tv . rounded_border = Some ( 12 ) ;
2022-01-21 10:06:55 +00:00
write! ( tv , " This is a test of basic word😃 wrapping 自动换行 inside a bounding box. \n This should be a new line. \n \n Two new lines. \n New line \n with a leading space. \n Done. " ) . unwrap ( ) ;
2022-01-20 16:34:12 +00:00
log ::info! ( " rendering: {:?} " , tv ) ;
2022-01-21 10:16:02 +00:00
tv . insertion = Some ( 4 ) ;
2022-01-20 16:34:12 +00:00
gfx . draw_textview ( & mut tv ) . unwrap ( ) ;
gfx . flush ( ) . unwrap ( ) ;
log ::info! ( " rendered: {:?} " , tv ) ;
ticktimer . sleep_ms ( 1000 ) . unwrap ( ) ;
}
2022-01-20 18:30:58 +00:00
Some ( TestType ::TopRight ) = > {
2024-01-14 14:13:56 +00:00
let mut tv = TextView ::new (
Gid ::new ( [ 0 , 0 , 0 , 0 ] ) ,
TextBounds ::GrowableFromTr (
Point ::new ( text_bounds . tr ( ) . x - 4 , text_bounds . tl ( ) . y + 4 ) ,
( ( text_bounds . br ( ) . x - text_bounds . tl ( ) . x ) / 2 ) as u16 + 30 ,
) ,
) ;
2022-01-20 18:30:58 +00:00
tv . clip_rect = Some ( clipping_area ) ;
2022-01-21 09:28:23 +00:00
tv . style = TEST_STYLE ;
2022-01-20 18:30:58 +00:00
tv . ellipsis = false ;
tv . rounded_border = None ;
2022-01-21 10:06:55 +00:00
write! ( tv , " This is a test of basic word😃 wrapping 自动换行 inside a bounding box. \n This should be a new line. \n \n Two new lines. \n New line \n with a leading space. \n Done. " ) . unwrap ( ) ;
2022-01-20 18:30:58 +00:00
log ::info! ( " rendering: {:?} " , tv ) ;
2022-01-21 10:16:02 +00:00
tv . insertion = Some ( 5 ) ;
2022-01-20 18:30:58 +00:00
gfx . draw_textview ( & mut tv ) . unwrap ( ) ;
gfx . flush ( ) . unwrap ( ) ;
log ::info! ( " rendered: {:?} " , tv ) ;
ticktimer . sleep_ms ( 1000 ) . unwrap ( ) ;
}
Some ( TestType ::Overflow ) = > {
//gfx.draw_rectangle(blackout).unwrap();
//gfx.flush().unwrap();
for x_off in 0 .. 32 {
let clipping_area = Rectangle ::new_coords ( 100 , 100 , 200 , 200 ) ;
let text_bounds = Rectangle ::new_coords ( - 20 + x_off , - 20 + x_off , 150 , 150 ) ;
let mut checkbound = clipping_area . clone ( ) ; // this checks against the final clipping area.
checkbound . margin_out ( Point ::new ( 1 , 1 ) ) ;
checkbound . style = DrawStyle ::new ( PixelColor ::Light , PixelColor ::Dark , 1 ) ;
gfx . draw_rectangle ( checkbound ) . unwrap ( ) ;
gfx . flush ( ) . unwrap ( ) ;
2024-01-14 14:13:56 +00:00
let mut tv =
TextView ::new ( Gid ::new ( [ 0 , 0 , 0 , 0 ] ) , TextBounds ::BoundingBox ( text_bounds ) ) ;
2022-01-20 18:30:58 +00:00
tv . clip_rect = Some ( clipping_area ) ;
2022-01-21 09:28:23 +00:00
tv . style = TEST_STYLE ;
2022-01-20 18:30:58 +00:00
tv . ellipsis = false ;
tv . rounded_border = None ;
2022-01-21 10:06:55 +00:00
write! ( tv , " This is a test of basic word wrapping 自动换行 inside a bounding box. \n This should be a new line. \n \n Two new lines. \n New line \n with a leading space. \n Done. " ) . unwrap ( ) ;
2022-01-20 18:30:58 +00:00
log ::info! ( " rendering: {:?} " , tv ) ;
2022-01-21 10:16:02 +00:00
tv . insertion = Some ( x_off as i32 + 10 ) ;
2022-01-20 18:30:58 +00:00
gfx . draw_textview ( & mut tv ) . unwrap ( ) ;
gfx . flush ( ) . unwrap ( ) ;
log ::info! ( " rendered: {:?} " , tv ) ;
ticktimer . sleep_ms ( 200 ) . unwrap ( ) ;
}
}
2022-01-21 10:50:34 +00:00
Some ( TestType ::Insert ) = > {
gfx . draw_rectangle ( blackout ) . unwrap ( ) ;
gfx . flush ( ) . unwrap ( ) ;
gfx . draw_rectangle ( checkbound ) . unwrap ( ) ;
gfx . flush ( ) . unwrap ( ) ;
2024-01-14 14:13:56 +00:00
let mut tv =
TextView ::new ( Gid ::new ( [ 0 , 0 , 0 , 0 ] ) , TextBounds ::BoundingBox ( text_bounds ) ) ;
2022-01-21 10:50:34 +00:00
tv . clip_rect = Some ( clipping_area ) ;
tv . style = TEST_STYLE ;
tv . ellipsis = true ;
tv . rounded_border = Some ( 4 ) ;
write! ( tv , " This is a test of basic 自动换行 inside \n a \n \n 😃 bounding box. \n This should be a new line. \n \n Two new lines. \n New line \n with a leading space. \n Done. " ) . unwrap ( ) ;
write! ( tv , " Let's add more text😃 until it overflows this is just another test with https://github.com/samblenny/blitstr2/commit/bb7d4ab6a2d8913dcb520895a3c242c933413aae more words and words and words and whatever. " ) . unwrap ( ) ;
for i in 20 .. 60 {
log ::info! ( " insertion point at {},{:?} " , i , tv . to_str ( ) . chars ( ) . skip ( i ) . next ( ) ) ;
tv . insertion = Some ( i as i32 ) ;
gfx . draw_textview ( & mut tv ) . unwrap ( ) ;
gfx . flush ( ) . unwrap ( ) ;
ticktimer . sleep_ms ( 250 ) . unwrap ( ) ;
}
}
2022-01-20 16:34:12 +00:00
_ = > { }
}
}
}
} ) ;
2024-01-14 14:13:56 +00:00
}