Re: [Userlib & Source] - ImageGadget Animations
Posted: Sun May 09, 2010 9:42 am
You're Welcome
All working well?

http://www.purebasic.com
https://www.purebasic.fr/english/
Code: Select all
; ====================================================================================================
; Title: Animated Image Gadget
; Description: Animated Image Gadget User Library Source
; Revision: 9
; Author(s): Michael R. King
; ====================================================================================================
; Quick Guide
; -----------
;
; In order to use Prefixed Functions (I.e. AIG_ApplyAnimation), Declare '#AIG_UsePrefix' in your Main Source.
;
; PLEASE NOTE: For Compilers before the 4.50 release, only 32 frames can be loaded into an animation.
; All compilers beyond 4.50 can use any number of frames... (Because of the List support in structures.)
;
; Usage License
; -------------
; You are free to use this library without any restriction, providing you do not redistribute the libraries (individually, or fully),
; Or the source code (either in part, Or fully) And claim it As your own work.
;
; You do not have to have to give me any credit when this source code is used in your work, however, it would be nice for you to let me
; know you are using it, either on the PureBasic Forums, or via E-Mail: mrking2910 at google mail com
;
; Thanks... XD
; - Select Features based on Compiler -
CompilerIf #PB_Compiler_Version < 450
#_AIG_OLDPBSUPPORT = #True
CompilerElse
#_AIG_OLDPBSUPPORT = #False
CompilerEndIf
; - Enable or Disable Prefixed Procedure Support -
CompilerIf Defined(_AIG_UsePrefix, #PB_Constant)
#_AIG_PROCPREFIX = #True
CompilerElse
#_AIG_PROCPREFIX = #False
CompilerEndIf
; - (Internal Use) - AnimImage Structure -
Structure _aig_animImgObject
lImageGadget.l ; - The Host Image Gadget ID
lCurrentFrame.l ; - Current Frame Index -
lNextFrameCue.l ; - When the frame will change -
lFrameCount.l ; - The number of frames available.
lFrameWidth.l ; - The width of the frames.
lFrameHeight.l ; - The height of the frames.
lFramesPerSecond.l ; - The number of frames the gadget should display per second.
lOriginalState.l ; - The Original Image contained in the Gadget.
bIsPlaying.b ; - Specifies if the Animation is playing, or not.
CompilerIf #_AIG_OLDPBSUPPORT = #True
lFrameList.l[32] ; - (Supporting Previous Versions of the Compiler)
CompilerElse
List lFrameList.l() ; - A list of all the frames PureBasic images.
CompilerEndIf
EndStructure
; -----------------------
; - Internal Procedures -
; -----------------------
CompilerIf Defined(_AIG_BuildUserLib, #PB_Constant)
; - (Internal Use) - Initialize Library -
ProcedureDLL AnimImageGadget_Init()
Global NewList _aig_animImgList._aig_animImgObject()
EndProcedure
; - (Internal Use) - Free Library -
ProcedureDLL AnimImageGadget_End()
ClearList(_aig_animImgList())
EndProcedure
CompilerElse
Global NewList _aig_animImgList._aig_animImgObject()
CompilerEndIf
; - (Internal Use) - Free Animated Image by ID -
Procedure _aig_animImgFree(lID.l)
ForEach _aig_animImgList()
If _aig_animImgList()\lImageGadget = lID
DeleteElement(_aig_animImgList())
EndIf
Next
EndProcedure
; - (Internal Use) - Select Animated Image by ID -
Procedure.l _aig_animImgSelect(lID.l)
Define.l itemLocation = -1
ForEach _aig_animImgList()
If _aig_animImgList()\lImageGadget = lID
itemLocation = ListIndex(_aig_animImgList())
EndIf
Next
If itemLocation > -1
SelectElement(_aig_animImgList(), itemLocation)
EndIf
ProcedureReturn itemLocation
EndProcedure
; - (Internal Use) - Apply Animated Image -
Procedure _aig_animImgApply(ImageGadget, Image.l, FrameWidth.l, FrameHeight.l, FramesPerSecond.l)
Define.l frameCount, imgWidth, imgHeight, origState, frameIndex
; - Check to ensure both the Image Gadget and Image exist -
If IsGadget(ImageGadget) And IsImage(Image) And GadgetType(ImageGadget) = #PB_GadgetType_Image
imgWidth = ImageWidth(Image)
imgHeight = ImageHeight(Image)
; - Calculate Frame Width & Height using a simple method (Assumes both Width & Height are the same.) -
If FrameWidth = -1 Or FrameHeight = -1
frameHeight = imgHeight
frameWidth = frameHeight
EndIf
; - Check to see if this Gadget is already Applied -
If _aig_animImgSelect(ImageGadget) > -1
origState = _aig_animImgList()\lOriginalState
If FramesPerSecond = -1
FramesPerSecond = _aig_animImgList()\lFramesPerSecond
EndIf
EndIf
; - Set Default for Frames Per Second -
If FramesPerSecond = -1
FramesPerSecond = 20
EndIf
; - Calculate Frame Count -
frameCount = imgWidth / frameWidth
; - Ensure Framecount doesn't exceed limit -
CompilerIf #_AIG_OLDPBSUPPORT = #True
If frameCount > 32
frameCount = 32
EndIf
CompilerEndIf
; - Create an entry in the Animated Images List -
AddElement(_aig_animImgList())
With _aig_animImgList()
\lFrameCount = frameCount
\lFrameHeight = frameHeight
\lFramesPerSecond = FramesPerSecond
\lFrameWidth = frameWidth
\lImageGadget = ImageGadget
\lCurrentFrame = 0
\lNextFrameCue = 0
\bIsPlaying = #True
ResizeGadget(\lImageGadget, #PB_Ignore, #PB_Ignore, frameWidth, frameHeight)
For Frame = 0 To frameCount - 1
fX.l = Frame * frameWidth
fY.l = 0
CompilerIf #_AIG_OLDPBSUPPORT = #True
\lFrameList[frameIndex] = GrabImage(Image, #PB_Any, fX, fY, frameWidth, frameHeight)
frameIndex + 1
CompilerElse
AddElement(\lFrameList())
\lFrameList() = GrabImage(Image, #PB_Any, fX, fY, frameWidth, frameHeight)
CompilerEndIf
Next
EndWith
EndIf
EndProcedure
; - (Internal Use) - Remove Animated Image -
Procedure _aig_animImgRemove(lID.l)
Define.l lOrigState
If _aig_animImgSelect(lID) > -1
lOrigState = _aig_animImgList()\lOriginalState
_aig_animImgFree(lID)
SetGadgetState(_aig_animImgList()\lImageGadget, lOrigState)
EndIf
EndProcedure
; - (Internal Use) - Enable Animation -
Procedure _aig_animImgEnable(lID.l)
If _aig_animImgSelect(lID) > -1
_aig_animImgList()\bIsPlaying = #True
EndIf
EndProcedure
; - (Internal Use) - Disable Animation -
Procedure _aig_animImgDisable(lID.l)
If _aig_animImgSelect(lID) > -1
_aig_animImgList()\bIsPlaying = #False
SetGadgetState(_aig_animImgList()\lImageGadget, _aig_animImgList()\lOriginalState)
EndIf
EndProcedure
; - (Internal Use) - Stop Animation -
Procedure _aig_animImgStop(lID.l)
If _aig_animImgSelect(lID) > -1
_aig_animImgList()\bIsPlaying = #False
_aig_animImgList()\lCurrentFrame = 1
SetGadgetState(_aig_animImgList()\lImageGadget, _aig_animImgList()\lOriginalState)
EndIf
EndProcedure
; - (Internal Use) - Do Cycle -
Procedure _aig_animImgCycle()
Define.l curTime = ElapsedMilliseconds()
ForEach _aig_animImgList()
If IsGadget(_aig_animImgList()\lImageGadget)
If _aig_animImgList()\bIsPlaying = #True
If _aig_animImgList()\lNextFrameCue < curTime
_aig_animImgList()\lCurrentFrame + 1
If _aig_animImgList()\lCurrentFrame > _aig_animImgList()\lFrameCount
_aig_animImgList()\lCurrentFrame = 1
EndIf
CompilerIf #_AIG_OLDPBSUPPORT = #True
SetGadgetState(_aig_animImgList()\lImageGadget, ImageID(_aig_animImgList()\lFrameList[_aig_animImgList()\lCurrentFrame - 1]))
CompilerElse
SelectElement(_aig_animImgList()\lFrameList(), _aig_animImgList()\lCurrentFrame - 1)
SetGadgetState(_aig_animImgList()\lImageGadget, ImageID(_aig_animImgList()\lFrameList()))
CompilerEndIf
_aig_animImgList()\lNextFrameCue = (curTime + ( 1000 / _aig_animImgList()\lFramesPerSecond))
EndIf
EndIf
Else
_aig_animImgFree(_aig_animImgList()\lImageGadget)
EndIf
Next
EndProcedure
; ---------------------------
; - Non-Prefixed Procedures -
; ---------------------------
CompilerIf #_AIG_PROCPREFIX = #False
; - Apply Animated Image -
ProcedureDLL ApplyImageGadgetAnimation(ImageGadget.l, Image.l) ; Apply Animation to the Specified Image Gadget
_aig_animImgApply(ImageGadget, Image, -1, -1, -1)
EndProcedure
ProcedureDLL ApplyImageGadgetAnimation2(ImageGadget.l, Image.l, FrameWidth.l, FrameHeight.l) ; Apply Animation to the Specified Image Gadget
_aig_animImgApply(ImageGadget, Image, FrameWidth, FrameHeight, -1)
EndProcedure
ProcedureDLL ApplyImageGadgetAnimation3(ImageGadget.l, Image.l, FrameWidth.l, FrameHeight.l, FramesPerSecond.l) ; Apply Animation to the Specified Image Gadget
_aig_animImgApply(ImageGadget, Image, FrameWidth, FrameHeight, FramesPerSecond)
EndProcedure
; - Remove Animated Image -
ProcedureDLL RemoveImageGadgetAnimation(ImageGadget.l) ; Remove the Animation from the Image Gadget, and Restore it's original Image.
_aig_animImgRemove(ImageGadget)
EndProcedure
; - Play Animation -
ProcedureDLL PlayImageGadgetAnimation(ImageGadget.l)
_aig_animImgEnable(ImageGadget)
EndProcedure
; - Pause Animation -
ProcedureDLL PauseImageGadgetAnimation(ImageGadget.l)
_aig_animImgDisable(ImageGadget)
EndProcedure
; - Stop Animation -
ProcedureDLL StopImageGadgetAnimation(ImageGadget.l)
_aig_animImgStop(ImageGadget)
EndProcedure
; - Set Animation Speed -
ProcedureDLL SetImageGadgetAnimationSpeed(ImageGadget, FramesPerSecond) ; Set the Animation Cycle Frequency (In Frames per Second)
ForEach _aig_animImgList()
If _aig_animImgList()\lImageGadget = lID
_aig_animImgList()\lFramesPerSecond = FramesPerSecond
EndIf
Next
EndProcedure
; - Refresh Animations -
ProcedureDLL AnimateImageGadgets() ; Cycle Animation of all applied Image Gadgets
_aig_animImgCycle()
EndProcedure
CompilerEndIf
; -----------------------
; - Prefixed Procedures -
; -----------------------
CompilerIf #_AIG_PROCPREFIX = #True
; - Apply Animated Image -
ProcedureDLL AIG_ApplyAnimation(ImageGadget.l, Image.l) ; Apply Animation to the Specified Image Gadget
_aig_animImgApply(ImageGadget, Image, -1, -1, -1)
EndProcedure
ProcedureDLL AIG_ApplyAnimation2(ImageGadget.l, Image.l, FrameWidth.l, FrameHeight.l) ; Apply Animation to the Specified Image Gadget
_aig_animImgApply(ImageGadget, Image, FrameWidth, FrameHeight, -1)
EndProcedure
ProcedureDLL AIG_ApplyAnimation3(ImageGadget.l, Image.l, FrameWidth.l, FrameHeight.l, FramesPerSecond.l) ; Apply Animation to the Specified Image Gadget
_aig_animImgApply(ImageGadget, Image, FrameWidth, FrameHeight, FramesPerSecond)
EndProcedure
; - Remove Animated Image -
ProcedureDLL AIG_RemoveAnimation(ImageGadget.l) ; Remove the Animation from the Image Gadget, and Restore it's original Image.
_aig_animImgRemove(ImageGadget)
EndProcedure
; - Play Animation -
ProcedureDLL AIG_PlayAnimation(ImageGadget.l)
_aig_animImgEnable(ImageGadget)
EndProcedure
; - Pause Animation -
ProcedureDLL AIG_PauseAnimation(ImageGadget.l)
_aig_animImgDisable(ImageGadget)
EndProcedure
; - Stop Animation -
ProcedureDLL AIG_StopAnimation(ImageGadget.l)
_aig_animImgStop(ImageGadget)
EndProcedure
; - Set Animation Speed -
ProcedureDLL AIG_SetSpeed(ImageGadget, FramesPerSecond) ; Set the Animation Cycle Frequency (In Frames per Second)
ForEach _aig_animImgList()
If _aig_animImgList()\lImageGadget = lID
_aig_animImgList()\lFramesPerSecond = FramesPerSecond
EndIf
Next
EndProcedure
; - Refresh Animations -
ProcedureDLL AIG_Animate() ; Cycle Animation of all applied Image Gadgets
_aig_animImgCycle()
EndProcedure
CompilerEndIf
Not anymore ~ please try againIdeasVacuum wrote:Invalid link?