Basic Univers
;----------------------------------------------------------------------------------------------
;
; Nom :       GadAnim.pbi
; Auteur :    Flype
; Date :      03/05/2004
; Purebasic : 3.90
;
; Explication du programme :
;
;   . Jeu de fonctions pour la création de gadgets animés
;   . L'animation est produite par une image dite "tube"
;     de la même manière que le jeu de fonctions AnimSprite (Librarie PureTools)
;
; Fonctions Publiques :
;
;   . GadAnimButton( #Gadget, x, y, w, h, #Image,ImageNB )
;   . GadAnimImage ( #Gadget, x, y, w, h, #Image,ImageNB, Flags )
;   . GadAnimStart ( hWnd, #Timer, Delay )
;   . GadAnimStop  ( hWnd, #Timer )
;   . GadAnimPause ()
;
; Fonctions Privées :
;
;   . GadAnimRefresh()
;
;----------------------------------------------------------------------------------------------


Declare GadAnimButton(GadgetID.l, x.l, y.l, w.l, h.l, ImageID.l, ImageNB.l)
Declare GadAnimImage(GadgetID.l, x.l, y.l, w.l, h.l, ImageID.l, ImageNB.l, flags.l)
Declare GadAnimStart(hWindow.l, TimerID.l, Delay.l)
Declare GadAnimStop(hWindow.l, TimerID.l)
Declare GadAnimPause()
Declare GadAnimRefresh()


;----------------------------------------------------------------------------------------------


#GadAnim_MaxImages = 20


;----------------------------------------------------------------------------------------------


Structure GadAnimTimer_Struct
  
  hWindow.l
  TimerID.l
  Delay.l
  
EndStructure

Structure GadAnimList_Struct
  
  GadgetID.l
  Index.l
  Number.l
  Images.l[ #GadAnim_MaxImages ]
  
EndStructure


;----------------------------------------------------------------------------------------------


Global GadAnimTimer.GadAnimTimer_Struct
NewList GadAnimList.GadAnimList_Struct()


;----------------------------------------------------------------------------------------------

Procedure GadAnimButton(GadgetID.l, x.l, y.l, w.l, h.l, ImageID.l, ImageNB.l)
  
  hImage.l = UseImage(ImageID)
  
  If hImage =#Null
    ProcedureReturn #Null
  EndIf
  
  imgWidth     = ImageWidth()
  imgHeight    = ImageHeight()
  imgTileWidth = imgWidth/ImageNB
  
  AddElement(GadAnimList())
  
  GadAnimList()\GadgetID = GadgetID
  GadAnimList()\Number   = ImageNB - 1
  GadAnimList()\Index    = 0
  
  For i = 0 To ImageNB - 1
    GadAnimList()\Images[i] = UseImage(GrabImage(ImageID, #PB_Any, i*imgTileWidth, 0, imgTileWidth, imgHeight))
  Next
  
  ProcedureReturn ButtonImageGadget(GadgetID, x, y, w, h, hImage)
  
EndProcedure

Procedure GadAnimImage(GadgetID.l, x.l, y.l, w.l, h.l, ImageID.l, ImageNB.l, flags.l)
  
  hImage.l = UseImage(ImageID)
  
  If hImage =#Null
    ProcedureReturn #Null
  EndIf
  
  imgWidth     = ImageWidth()
  imgHeight    = ImageHeight()
  imgTileWidth = imgWidth/ImageNB
  
  AddElement(GadAnimList())
  
  GadAnimList()\GadgetID = GadgetID
  GadAnimList()\Number   = ImageNB - 1
  GadAnimList()\Index    = 0
  
  For i = 0 To ImageNB - 1
    GadAnimList()\Images[i] = UseImage(GrabImage(ImageID, #PB_Any, i*imgTileWidth, 0, imgTileWidth, imgHeight))
  Next
  
  ProcedureReturn ImageGadget(GadgetID, x, y, w, h, hImage, flags)
  
EndProcedure

Procedure GadAnimRefresh()
  
  ForEach GadAnimList()
    
    GadAnimList()\Index + 1
    
    If GadAnimList()\Index > GadAnimList()\Number
      GadAnimList()\Index = 0
    EndIf
    
    SetGadgetState( GadAnimList()\GadgetID, GadAnimList()\Images[GadAnimList()\Index])
    
  Next
  
EndProcedure

Procedure GadAnimStart(hWindow.l, TimerID.l, Delay.l)
  
  If hWindow<>#Null
    
    GadAnimTimer\hWindow = hWindow
    GadAnimTimer\TimerID = TimerID
    GadAnimTimer\Delay   = Delay
    
    SetTimer_(hWindow, TimerID, Delay, @GadAnimRefresh())
    
  EndIf
  
EndProcedure

Procedure GadAnimStop(hWindow.l, TimerID.l)
  
  If hWindow<>#Null
    KillTimer_(hWindow, TimerID.l)
  EndIf
  
EndProcedure

Procedure GadAnimPause()
  
  Static GadAnimSwitch.b
  
  If GadAnimSwitch
    GadAnimStart(GadAnimTimer\hWindow, GadAnimTimer\TimerID, GadAnimTimer\Delay)
  Else
    GadAnimStop(GadAnimTimer\hWindow, GadAnimTimer\TimerID)
  EndIf
  
  GadAnimSwitch = #True - GadAnimSwitch
  
EndProcedure




;----------------------------------------------------------------------------------------------
;
; Nom :       GadAnim.pb
; Auteur :    Flype
; Date :      03/05/2004
; Purebasic : 3.90
;
; Explication du programme : Exemple d'utilisation du fichier Include GadAnim.pbi
;
;----------------------------------------------------------------------------------------------


; IncludeFile "GadAnim.pbi"


;- Initialisation du programme ----------------------------------------------------------------


Enumeration
  #Gad_1
  #Gad_2
  #Gad_3
EndEnumeration

Enumeration
  #Img_1
  #Img_2
  #Img_3
EndEnumeration


;- Chargement des images ----------------------------------------------------------------------

UsePNGImageDecoder()
UseJPEGImageDecoder()

If LoadImage(#Img_1, "fighter.png") = #Null : End : EndIf
If LoadImage(#Img_2, "soldier.png") = #Null : End : EndIf
If LoadImage(#Img_3, "zylinder.png") = #Null : End : EndIf


;- Création de l'interface --------------------------------------------------------------------


If OpenWindow(0, 0, 0, 338, 215, #PB_Window_SystemMenu | #PB_Window_ScreenCentered, "GadAnim") = #Null : End : EndIf
If CreateGadgetList(WindowID()) = #Null : End : EndIf

GadAnimButton(#Gad_1, 5, 5, 120, 100, #Img_1, 17)
GadAnimButton(#Gad_2, 5, 110, 120, 100, #Img_2, 9)
GadAnimImage(#Gad_3, 130, 5, 200, 200, #Img_3, 12, #PB_Image_Border)


;- Boucle Principale --------------------------------------------------------------------------


GadAnimStart(WindowID(), 1, 40)


Repeat
  
  Select WaitWindowEvent()
    Case #WM_CLOSE : Break
    Case #PB_Event_Gadget
      Select EventGadgetID()
        Case #Gad_1 : GadAnimPause()
        Case #Gad_2 : Break
      EndSelect
  EndSelect
  
ForEver


GadAnimStop(WindowID(), 1)


;- Fin du programme ---------------------------------------------------------------------------


End