Toast animation

Just starting out? Need help? Post your questions and find answers here.
Dirk Geppert
Posts: 282
Joined: Fri Sep 22, 2017 7:02 am

Toast animation

Post by Dirk Geppert »

Hi guys, I am currently working on a toast that is to be pushed into view from the bottom edge and then pushes itself out of view again after 5 seconds.

My problem is the fly-in and fly-out animation - its to fast. So my question is: how to smooth animate a window?

Code: Select all

#tWnd = 0
Define w, h

Procedure Close_Toast()
  Protected n, w, h, y
  
  If IsWindow(#tWnd)
    
    ExamineDesktops()
    w = DesktopWidth(0)
    h = DesktopHeight(0)
    
    UnbindEvent(#PB_Event_Timer, @Close_Toast(), #tWnd)
    RemoveWindowTimer(#tWnd, #tWnd)
    
    y = WindowY(#tWnd)
    For n = y To h
      ResizeWindow(#tWnd, #PB_Ignore, n, #PB_Ignore, #PB_Ignore)
    Next
    CloseWindow(#tWnd)
  EndIf
EndProcedure

Procedure Show_Toast()
  Protected w, h, y, n
  
  ExamineDesktops()
  w = DesktopWidth(0)
  h = DesktopHeight(0)
  
  If IsWindow(#tWnd) = 0
    OpenWindow(#tWnd, w/4, h + 200, w/2, 200, "Toast", #PB_Window_TitleBar)
    TextGadget(0, 0, 0, WindowWidth(#tWnd), WindowHeight(#tWnd), "<p>Some importend text here</p>")
    
    BindEvent(#PB_Event_Timer, @Close_Toast(), #tWnd)
    AddWindowTimer(#tWnd, #tWnd, 5000)
    
    y = h - WindowHeight(#tWnd) - 50
    
    For n = h To y Step -1
      ResizeWindow(#tWnd, #PB_Ignore, n, #PB_Ignore, #PB_Ignore)
    Next
    
  EndIf
EndProcedure

Show_Toast()

User avatar
Kurzer
Posts: 90
Joined: Mon May 26, 2014 9:33 am

Re: Toast animation

Post by Kurzer »

Hi Dirk,

the best way to do this is to use a finely resolved timer as a kind of timeline counter (in my exmple a timer with 10 ms).
At certain points of the timeline the actions fade in, stop animation and fade out are executed.
Together with a SIN() function, the window can also be faded in and out smoothly.

Code: Select all

#tWnd = 0

Global dw, dh, th, timecounter, offset, twait

Declare AnimTimer()
Declare Close_Toast()
Declare Show_Toast()


ExamineDesktops()
dw = DesktopWidth(0)
dh = DesktopHeight(0)
th = 200
twait = 2 * 100 ; Show the toast for 2 seconds
offset = 0
timecounter = 0

Procedure AnimTimer()
	If EventTimer() = 0
		timecounter + 1
		Debug timecounter
		If timecounter <= 90
			; In-animation
			ResizeWindow(#tWnd, #PB_Ignore, dh - Sin(Radian(offset)) * th, #PB_Ignore, #PB_Ignore)
			offset + 1	
		ElseIf timecounter > 90 And timecounter <= 90 + twait
			; Show the toast for 'twait' ticks
		ElseIf timecounter > 90 + twait And timecounter <= 180 + twait
			; Out-animation
			ResizeWindow(#tWnd, #PB_Ignore, dh - Sin(Radian(offset)) * th, #PB_Ignore, #PB_Ignore)
			offset + 1
		ElseIf timecounter > 180 + twait
			; Close the toast window and remove the timer
			offset = 0
			timecounter = 0
			UnbindEvent(#PB_Event_Timer, @AnimTimer(), #tWnd)
			RemoveWindowTimer(#tWnd, 0)
			Close_Toast()
			Debug "Window closed"
		EndIf
	EndIf
EndProcedure

Procedure Close_Toast()
	
	If IsWindow(#tWnd)
		CloseWindow(#tWnd)
	EndIf
EndProcedure

Procedure Show_Toast()
	
	If IsWindow(#tWnd) = 0
		OpenWindow(#tWnd, dw/4, dh, dw/2, th, "Toast", #PB_Window_TitleBar)
		TextGadget(0, 0, 0, WindowWidth(#tWnd), WindowHeight(#tWnd), "<p>Some importend text here</p>")
		
		BindEvent(#PB_Event_Timer, @AnimTimer(), #tWnd)
		AddWindowTimer(#tWnd, 0, 10)
	EndIf
EndProcedure

Show_Toast()
Last edited by Kurzer on Fri Apr 21, 2023 7:42 am, edited 1 time in total.
SB 2.32 x86, Browser: Iron Portable V. 88.0.4500.0 (Chromium based), User age in 2023: 55y
"Happiness is a pet." | "Never run a changing system!"
User avatar
Paul
Posts: 195
Joined: Wed Feb 26, 2014 6:46 pm
Location: Canada
Contact:

Re: Toast animation

Post by Paul »

Maybe javascript?

Code: Select all

Declare Toast()
Declare ToastOut()
#tWnd=0

Procedure jsToast_In(Window)  
  ExamineDesktops()
  Protected wid=WindowID(Window) 
  Protected windowheight=DesktopHeight(0)-WindowHeight(Window) 
  ! $(v_wid.element).animate({
  !   top: v_windowheight 
  ! });  
EndProcedure

Procedure jsToast_Out(Window) 
  ExamineDesktops() 
  Protected wid=WindowID(Window) 
  Protected windowheight=DesktopHeight(0) 
  ! $(v_wid.element).animate({
  !   top: v_windowheight
  ! });  
EndProcedure

Procedure ToastOut()
  jsToast_Out(#tWnd)
  UnbindEvent(#PB_Event_Timer,@ToastOut(),#tWnd)
  RemoveWindowTimer(#tWnd,0)  
  ;remove the next 2 lines to stop this demo from repeating
  AddWindowTimer(#tWnd,0,3000)
  BindEvent(#PB_Event_Timer,@Toast(),#tWnd)
EndProcedure

Procedure Toast()
  jsToast_In(#tWnd)
  UnbindEvent(#PB_Event_Timer,@Toast(),#tWnd)
  RemoveWindowTimer(#tWnd,0)
  AddWindowTimer(#tWnd,0,4000)
  BindEvent(#PB_Event_Timer,@ToastOut(),#tWnd)
EndProcedure



ExamineDesktops()
w=DesktopWidth(0)
h=DesktopHeight(0)
OpenWindow(#tWnd, w/4, h + 200, w/2, 200, "Toast", #PB_Window_TitleBar)
TextGadget(0, 0, 0, WindowWidth(#tWnd), WindowHeight(#tWnd), "<p>Some importend text here</p>")


AddWindowTimer(#tWnd,0,2000)
BindEvent(#PB_Event_Timer,@Toast(),#tWnd)
(based on some code I saw a few years back by Peter)
Dirk Geppert
Posts: 282
Joined: Fri Sep 22, 2017 7:02 am

Re: Toast animation

Post by Dirk Geppert »

Thank you Paul. Now it looks exactly as I wanted :D
Dirk Geppert
Posts: 282
Joined: Fri Sep 22, 2017 7:02 am

Re: Toast animation

Post by Dirk Geppert »

Hi guys, I have another question about animation. Is it possible to define a callback that is triggered after the animation is completed?
I need the information when the animation is finished.

kind regards

Dirk
User avatar
Peter
Posts: 1086
Joined: Mon Feb 24, 2014 10:17 pm
Location: 127.0.0.1:9080
Contact:

Re: Toast animation

Post by Peter »

Here is Paul's code with the callback extension:

Code: Select all

Declare Toast()
Declare ToastOut()
Declare ToastInCallback()
Declare ToastOutCallback()

#tWnd=0

Procedure jsToast_In(Window, Callback)  
  ExamineDesktops()
  Protected wid=WindowID(Window) 
  Protected windowheight=DesktopHeight(0)-WindowHeight(Window) 
  ! $(v_wid.element).animate({
  !   top: v_windowheight 
  ! }, 500, function() { v_callback() } );  
EndProcedure

Procedure jsToast_Out(Window, Callback) 
  ExamineDesktops() 
  Protected wid=WindowID(Window) 
  Protected windowheight=DesktopHeight(0) 
  ! $(v_wid.element).animate({
  !   top: v_windowheight
  ! }, 500, function() { v_callback() } );  
EndProcedure

Procedure ToastOut()
  jsToast_Out(#tWnd, @ToastOutCallback())
  UnbindEvent(#PB_Event_Timer,@ToastOut(),#tWnd)
  RemoveWindowTimer(#tWnd,0)  
  ;remove the next 2 lines to stop this demo from repeating
  AddWindowTimer(#tWnd,0,3000)
  BindEvent(#PB_Event_Timer,@Toast(),#tWnd)
EndProcedure

Procedure Toast()
  jsToast_In(#tWnd, @ToastInCallback())
  UnbindEvent(#PB_Event_Timer,@Toast(),#tWnd)
  RemoveWindowTimer(#tWnd,0)
  AddWindowTimer(#tWnd,0,4000)
  BindEvent(#PB_Event_Timer,@ToastOut(),#tWnd)
EndProcedure

Procedure ToastInCallback()
  Debug "ToastInCallback() triggered"
EndProcedure

Procedure ToastOutCallback()
  Debug "ToastOutCallback() triggered"
EndProcedure

ExamineDesktops()
w=DesktopWidth(0)
h=DesktopHeight(0)
OpenWindow(#tWnd, w/4, h + 200, w/2, 200, "Toast", #PB_Window_TitleBar)
TextGadget(0, 0, 0, WindowWidth(#tWnd), WindowHeight(#tWnd), "<p>Some importend text here</p>")


AddWindowTimer(#tWnd,0,2000)
BindEvent(#PB_Event_Timer,@Toast(),#tWnd)
Dirk Geppert
Posts: 282
Joined: Fri Sep 22, 2017 7:02 am

Re: Toast animation

Post by Dirk Geppert »

Great!!! :D Thank you very much Peter!
Post Reply