Kind of Showtoast for non Android devices

You can post your code snippets here for others to use and learn from
Post Reply
zylesea
Posts: 227
Joined: Tue Feb 16, 2010 12:50 am
Location: Westfalen/Germany
Contact:

Kind of Showtoast for non Android devices

Post by zylesea »

I like to share my faketoast function I wrote yesterday evening.
It's not as elegant as the Android showtoast() (no fading and blocking while shown), but it's useful to get a better impression of how the programm looks like on Android (needs more parameter polishing, the parameters used here were a quick shot).

Code: Select all

Function p_faketoast(my_ftstring$, Xpos$, Ypos$)
SetFillStyle (#FILLCOLOR)
SetFont(#SANS, 20)
SetFontColor (#WHITE)
CreateTextObject (1000, my_ftstring$)
;DebugPrint(GetAttribute (#TEXTOBJECT,1000, #ATTRHEIGHT))
Local xftoff$=40 
Local yftoff$=16
Local ftwidth$=GetAttribute (#TEXTOBJECT,1000, #ATTRWIDTH, #ATTRHEIGHT)
Local ftheight$=GetAttribute (#TEXTOBJECT,1000, #ATTRHEIGHT)
Local ftradius$=18 ;radius of the edges
If ftheight$<ftradius$+5 Then ftheight$=ftradius$+5 ;bis Radius=20
CreateBrush (1000, xftoff$+ftwidth$, yftoff$+ftheight$, #BLACK)
SelectAlphaChannel(1000)
SetAlphaIntensity(0)
Box(1,1,ftwidth$+xftoff$, ftheight$+yftoff$)
SetAlphaIntensity(120)
Arc(ftwidth$-ftradius$+xftoff$-ftradius$,0,ftradius$,ftradius$,270,360);re oben
Arc(0,ftheight$-ftradius$+yftoff$-ftradius$,ftradius$,ftradius$,90,180) ;li unten
Arc(0,0,ftradius$,ftradius$,180,270) ;li oben
Arc(ftwidth$-ftradius$+xftoff$-ftradius$,ftheight$-ftradius$+yftoff$-ftradius$,ftradius$,ftradius$,0,90);re unten
Box(0,ftradius$,xftoff$+ftwidth$+1,ftheight$+yftoff$-ftradius$-ftradius$+1)
Box(ftradius$,0,ftwidth$+xftoff$-ftradius$-ftradius$+1,xftoff$+ftheight$)
EndSelect
SelectBrush(1000)
DisplayTextObject(1000, xftoff$/2,yftoff$/2)
EndSelect
CreateSprite (1000, #BRUSH, 1000, xftoff$+ftwidth$, yftoff$+ftheight$,1,1)
DisplaySprite (1000, Xpos$, Ypos$ )
Wait (150)
DisplaySprite (1000, -1000, -1000 )
EndFunction                     
Use it like a real toast. Mind that the function does not change font properties back to the properties before calling the function. Radius of the edges can get adjusted as can the thickness of the borders of the toast or the duration of getting it shown (a simple wait). I use a fixed id (1000) for brush and sprite here, probaby a dynamically ID woud be better, but I was too lazy...
zylesea
Posts: 227
Joined: Tue Feb 16, 2010 12:50 am
Location: Westfalen/Germany
Contact:

Re: Kind of Showtoast for non Android devices

Post by zylesea »

A very small change to make the faketoast non blocking:

Code: Select all

Function p_faketoast(my_ftstring$, Xpos$, YPos$)   
SetFillStyle (#FILLCOLOR)
SetFont(#SANS, 20)
SetFontColor (#WHITE)
CreateTextObject (1000, my_ftstring$)
Local xftoff$=40
Local yftoff$=16
Local ftwidth$=GetAttribute (#TEXTOBJECT,1000, #ATTRWIDTH, #ATTRHEIGHT)
Local ftheight$=GetAttribute (#TEXTOBJECT,1000, #ATTRHEIGHT)
Local ftradius$=18
If ftheight$<ftradius$+5 Then ftheight$=ftradius$+5 ;bis Radius=20
CreateBrush (1000, xftoff$+ftwidth$, yftoff$+ftheight$, #BLACK)
SelectAlphaChannel(1000)
SetAlphaIntensity(0)
Box(1,1,ftwidth$+xftoff$, ftheight$+yftoff$)
SetAlphaIntensity(140)
Arc(ftwidth$-ftradius$+xftoff$-ftradius$,0,ftradius$,ftradius$,270,360);re oben
Arc(0,ftheight$-ftradius$+yftoff$-ftradius$,ftradius$,ftradius$,90,180) ;li unten
Arc(0,0,ftradius$,ftradius$,180,270) ;li oben
Arc(ftwidth$-ftradius$+xftoff$-ftradius$,ftheight$-ftradius$+yftoff$-ftradius$,ftradius$,ftradius$,0,90);re unten
Box(0,ftradius$,xftoff$+ftwidth$+1,ftheight$+yftoff$-ftradius$-ftradius$+1)
Box(ftradius$,0,ftwidth$+xftoff$-ftradius$-ftradius$+1,xftoff$+ftheight$)
EndSelect
SelectBrush(1000)
DisplayTextObject(1000, xftoff$/2,yftoff$/2)
EndSelect
CreateSprite (1000, #BRUSH, 1000, xftoff$+ftwidth$, yftoff$+ftheight$,1,1)
DisplaySprite (1000, Xpos$, YPos$ )
SetTimeout(1000, p_disposetoast,2500)
SetFontColor (#BLACK)
EndFunction

Function  p_disposetoast()
    DisplaySprite(1000,-1000,-1000) ; get rid if the toast again: display the sprite ouside the visible region (more robust than removesprite())
EndFunction    
Mind you to check the id for the toast sprite and the timeout - i just chose 1000 rather randomly.
This version has one behavioral difference to original toasts: If a new toast comes in before the last diappeared it gets shown immediately and the old one gets disposed (rather overwritten). In some cases this behaviour has advantages in some disadvantages. Hence i kept it.
Post Reply