1
1
Fork 0
mirror of https://github.com/FellippeHeitor/InForm.git synced 2024-05-12 06:50:12 +00:00

Add GIF_GetElapsedTime & GIF_GetBackgroundColor

This commit is contained in:
Samuel Gomes 2023-12-02 21:42:38 +05:30
parent af6eb0957d
commit b62e99935b
5 changed files with 104 additions and 40 deletions

View file

@ -105,6 +105,12 @@ FUNCTION GIF_GetFrameDuration~&& (Id AS LONG)
Returns the total runtime of the current frame in ms.
```vb
FUNCTION GIF_GetElapsedTime~&& (Id AS LONG)
```
Returns the current runtime of the animation in ms.
```vb
SUB GIF_SetLoop (Id AS LONG, loops AS LONG)
```
@ -123,7 +129,14 @@ FUNCTION GIF_GetFrame& (Id AS LONG)
```
Returns the current rendered frame image to be played. Playback is time sensitive so frames may be skipped or the last frame may be returned.
Use this if you want to do your own rendering. Do not free the image. The library will do that when it is no longer needed.
Use this if you want to do your own final _PUTIMAGE. Do not free the image. The library will do that when it is no longer needed.
```vb
FUNCTION GIF_GetBackgroundColor~& (Id AS LONG)
```
Returns the background color that should be used to clear the surface before drawing the final rendered frame.
Use this if you want to do your own final _PUTIMAGE.
```vb
SUB GIF_EnableOverlay (Id AS LONG, isEnabled AS _BYTE)

View file

@ -220,6 +220,9 @@ $IF GIFPLAY_BAS = UNDEFINED THEN
__GIFPlay(idx).frame = __GIFPlay(idx).firstFrame
__GIFPlay(idx).frameNumber = 0
__GIFPlay(idx).loopCounter = 0
__GIFPlay(idx).elapsedTime = 0
__GIFPlay(idx).lastFrameRendered = -1
__GIFPlay(idx).hasSavedImage = __GIF_FALSE
END IF
END SUB
@ -306,6 +309,8 @@ $IF GIFPLAY_BAS = UNDEFINED THEN
DO UNTIL currentTick < __GIFPlay(idx).lastTick + __GIFPlayFrame(__GIFPlay(idx).frame).duration
' Add the current frame duration to lastTick so that we can do frame skips if needed
__GIFPlay(idx).lastTick = __GIFPlay(idx).lastTick + __GIFPlayFrame(__GIFPlay(idx).frame).duration
' Increment elapsed time
__GIFPlay(idx).elapsedTime = __GIFPlay(idx).elapsedTime + __GIFPlayFrame(__GIFPlay(idx).frame).duration
' We crossed the duration of the current frame, so move to the next one
__GIFPlay(idx).frame = __GIFPlayFrame(__GIFPlay(idx).frame).nextFrame ' this should correctly loop back to the first frame
' Increment the frame counter and loop back to 0 if needed
@ -314,7 +319,11 @@ $IF GIFPLAY_BAS = UNDEFINED THEN
__GIFPlay(idx).frameNumber = 0
__GIFPlay(idx).loopCounter = __GIFPlay(idx).loopCounter + 1
IF __GIFPlay(idx).loops < 0 THEN __GIFPlay(idx).loopCounter = -1 ' single-shot animation
IF __GIFPlay(idx).loops < 0 THEN
__GIFPlay(idx).loopCounter = -1 ' single-shot animation
ELSE
__GIFPlay(idx).elapsedTime = 0 ' only reset the elapsed time for looping playback
END IF
END IF
LOOP
@ -364,6 +373,17 @@ $IF GIFPLAY_BAS = UNDEFINED THEN
END FUNCTION
' Returns the background color that should be used to clear the surface before drawing the final rendered frame
FUNCTION GIF_GetBackgroundColor~& (Id AS LONG)
SHARED __GIFPlayHashTable() AS HashTableType
SHARED __GIFPlay() AS __GIFPlayType
IF HashTable_IsKeyPresent(__GIFPlayHashTable(), Id) THEN
GIF_GetBackgroundColor = __GIFPlay(HashTable_LookupLong(__GIFPlayHashTable(), Id)).bgColor
END IF
END FUNCTION
' Returns the total runtime of the animation in ms
FUNCTION GIF_GetTotalDuration~&& (Id AS LONG)
SHARED __GIFPlayHashTable() AS HashTableType
@ -387,6 +407,17 @@ $IF GIFPLAY_BAS = UNDEFINED THEN
END FUNCTION
' Returns the current runtime of the animation in ms
FUNCTION GIF_GetElapsedTime~&& (Id AS LONG)
SHARED __GIFPlayHashTable() AS HashTableType
SHARED __GIFPlay() AS __GIFPlayType
IF HashTable_IsKeyPresent(__GIFPlayHashTable(), Id) THEN
GIF_GetElapsedTime = __GIFPlay(HashTable_LookupLong(__GIFPlayHashTable(), Id)).elapsedTime
END IF
END FUNCTION
' Set the looping behavior
SUB GIF_SetLoop (Id AS LONG, loops AS LONG)
SHARED __GIFPlayHashTable() AS HashTableType
@ -652,6 +683,7 @@ $IF GIFPLAY_BAS = UNDEFINED THEN
__GIFPlay(idx).loopCounter = 0
__GIFPlay(idx).duration = 0
__GIFPlay(idx).lastTick = 0
__GIFPlay(idx).elapsedTime = 0
__GIFPlay(idx).lastFrameRendered = -1
__GIFPlay(idx).hasSavedImage = __GIF_FALSE
__GIFPlay(idx).overlayEnabled = __GIF_TRUE

View file

@ -27,6 +27,7 @@ $IF GIFPLAY_BI = UNDEFINED THEN
loopCounter AS _UNSIGNED LONG ' this counts the number of loops
duration AS _UNSIGNED _INTEGER64 ' total duration in ticks (ms)
lastTick AS _UNSIGNED _INTEGER64 ' the tick recorded when the last frame was played
elapsedTime AS _UNSIGNED _INTEGER64 ' the time (ms) that has passed since the animation was started
lastFrameRendered AS LONG ' index of the last frame that was rendered
savedImage AS LONG ' copy of the current frame when disposal method 3 (restore to previous) is encountered
hasSavedImage AS _BYTE ' set to true if we have a valid saved frame

View file

@ -8,11 +8,12 @@ DEFLNG A-Z
OPTION _EXPLICIT
': Controls' IDs: ------------------------------------------------------------------
DIM SHARED AboutBT AS LONG
DIM SHARED gifplaySample AS LONG
DIM SHARED PictureBox1 AS LONG
DIM SHARED LoadBT AS LONG
DIM SHARED PlayBT AS LONG
DIM SHARED DoneProgressBar AS LONG
DIM SHARED AboutButton AS LONG
DIM SHARED GIFPlaySample AS LONG
DIM SHARED GIFPictureBox AS LONG
DIM SHARED LoadButton AS LONG
DIM SHARED PlayButton AS LONG
': External modules: ---------------------------------------------------------------
'$INCLUDE:'../../InForm/extensions/GIFPlay.bi'
@ -29,11 +30,15 @@ SUB __UI_BeforeInit
END SUB
SUB __UI_OnLoad
Control(PlayBT).Disabled = TRUE
Control(PlayButton).Disabled = TRUE
Control(DoneProgressBar).Disabled = TRUE
END SUB
SUB __UI_BeforeUpdateDisplay
GIF_Draw PictureBox1
IF GIF_IsLoaded(GIFPictureBox) THEN
GIF_Draw GIFPictureBox
Control(DoneProgressBar).Value = (GIF_GetElapsedTime(GIFPictureBox) / GIF_GetTotalDuration(GIFPictureBox)) * 100
END IF
END SUB
SUB __UI_BeforeUnload
@ -41,44 +46,49 @@ END SUB
SUB __UI_Click (id AS LONG)
SELECT CASE id
CASE AboutBT
MessageBox "GIFPlay library + InForm-PE demo." + STRING$(2, 10) + "Get it from https://github.com/a740g/InForm-PE", "About " + Caption(gifplaySample), MsgBox_OkOnly + MsgBox_Information
CASE DoneProgressBar
CASE gifplaySample
CASE AboutButton
MessageBox "GIFPlay library + InForm-PE demo." + STRING$(2, 10) + "Get it from https://github.com/a740g/InForm-PE", "About " + Caption(GIFPlaySample), MsgBox_OkOnly + MsgBox_Information
CASE LoadBT
DIM fileName AS STRING: fileName = _OPENFILEDIALOG$(Caption(gifplaySample), , "*.gif|*.GIF|*.Gif", "GIF Files")
CASE GIFPlaySample
CASE LoadButton
DIM fileName AS STRING: fileName = _OPENFILEDIALOG$(Caption(GIFPlaySample), , "*.gif|*.GIF|*.Gif", "GIF Files")
IF LEN(fileName) > 0 THEN
IF GIF_LoadFromFile(PictureBox1, fileName) THEN
IF GIF_LoadFromFile(GIFPictureBox, fileName) THEN
' Calculate picturebox width based on the aspect ratio of the GIF
Control(PictureBox1).Width = GIF_GetWidth(PictureBox1) / GIF_GetHeight(PictureBox1) * Control(PictureBox1).Height
Control(gifplaySample).Width = Control(PictureBox1).Width + Control(LoadBT).Width + 24
Control(GIFPictureBox).Width = GIF_GetWidth(GIFPictureBox) / GIF_GetHeight(GIFPictureBox) * Control(GIFPictureBox).Height
Control(GIFPlaySample).Width = Control(GIFPictureBox).Width + Control(LoadButton).Width + 24
Control(DoneProgressBar).Width = Control(GIFPictureBox).Width
Control(PlayBT).Disabled = FALSE
Control(DoneProgressBar).Disabled = FALSE
IF GIF_GetTotalFrames(PictureBox1) > 1 THEN
Caption(PlayBT) = "Play"
IF GIF_GetTotalFrames(GIFPictureBox) > 1 THEN
Control(PlayButton).Disabled = FALSE
Caption(PlayButton) = "Play"
ELSE
Control(PlayBT).Disabled = TRUE
Control(PlayButton).Disabled = TRUE
END IF
ELSE
Control(PlayBT).Disabled = TRUE
Control(PlayButton).Disabled = TRUE
Control(DoneProgressBar).Disabled = TRUE
MessageBox fileName + " failed to load!", "", MsgBox_Exclamation
END IF
END IF
CASE PlayBT
IF GIF_IsPlaying(PictureBox1) THEN
GIF_Pause PictureBox1
Caption(PlayBT) = "Play"
CASE PlayButton
IF GIF_IsPlaying(GIFPictureBox) THEN
GIF_Pause GIFPictureBox
Caption(PlayButton) = "Play"
ELSE
GIF_Play PictureBox1
Caption(PlayBT) = "Pause"
GIF_Play GIFPictureBox
Caption(PlayButton) = "Pause"
END IF
CASE PictureBox1
GIF_EnableOverlay PictureBox1, FALSE
CASE GIFPictureBox
GIF_EnableOverlay GIFPictureBox, FALSE
END SELECT
END SUB

View file

@ -7,13 +7,13 @@ SUB __UI_LoadForm
DIM __UI_NewID AS LONG, __UI_RegisterResult AS LONG
__UI_NewID = __UI_NewControl(__UI_Type_Form, "gifplaySample", 425, 339, 0, 0, 0)
__UI_NewID = __UI_NewControl(__UI_Type_Form, "GIFPlaySample", 425, 367, 0, 0, 0)
__UI_RegisterResult = 0
SetCaption __UI_NewID, "GIFPlay Sample"
Control(__UI_NewID).Font = SetFont("arial.ttf", 12)
Control(__UI_NewID).HasBorder = False
__UI_NewID = __UI_NewControl(__UI_Type_PictureBox, "PictureBox1", 320, 320, 95, 10, 0)
__UI_NewID = __UI_NewControl(__UI_Type_PictureBox, "GIFPictureBox", 320, 320, 95, 10, 0)
__UI_RegisterResult = 0
Control(__UI_NewID).Stretch = True
Control(__UI_NewID).HasBorder = True
@ -21,31 +21,39 @@ SUB __UI_LoadForm
Control(__UI_NewID).VAlign = __UI_Middle
Control(__UI_NewID).BorderSize = 1
__UI_NewID = __UI_NewControl(__UI_Type_Button, "LoadBT", 80, 40, 10, 10, 0)
__UI_NewID = __UI_NewControl(__UI_Type_Button, "LoadButton", 80, 40, 10, 10, 0)
__UI_RegisterResult = 0
SetCaption __UI_NewID, "&Load"
Control(__UI_NewID).HasBorder = False
Control(__UI_NewID).CanHaveFocus = True
__UI_NewID = __UI_NewControl(__UI_Type_Button, "PlayBT", 80, 40, 10, 55, 0)
__UI_NewID = __UI_NewControl(__UI_Type_Button, "PlayButton", 80, 40, 10, 55, 0)
__UI_RegisterResult = 0
SetCaption __UI_NewID, "&Play"
Control(__UI_NewID).HasBorder = False
Control(__UI_NewID).CanHaveFocus = True
__UI_NewID = __UI_NewControl(__UI_Type_Button, "AboutBT", 80, 40, 10, 100, 0)
__UI_NewID = __UI_NewControl(__UI_Type_Button, "AboutButton", 80, 40, 10, 100, 0)
__UI_RegisterResult = 0
SetCaption __UI_NewID, "&About"
Control(__UI_NewID).Stretch = True
Control(__UI_NewID).HasBorder = False
Control(__UI_NewID).CanHaveFocus = True
__UI_NewID = __UI_NewControl(__UI_Type_ProgressBar, "DoneProgressBar", 320, 23, 95, 335, 0)
__UI_RegisterResult = 0
SetCaption __UI_NewID, "\92;#"
Control(__UI_NewID).HasBorder = False
Control(__UI_NewID).Max = 100
Control(__UI_NewID).ShowPercentage = True
END SUB
SUB __UI_AssignIDs
gifplaySample = __UI_GetID("gifplaySample")
PictureBox1 = __UI_GetID("PictureBox1")
LoadBT = __UI_GetID("LoadBT")
PlayBT = __UI_GetID("PlayBT")
AboutBT = __UI_GetID("AboutBT")
GIFPlaySample = __UI_GetID("GIFPlaySample")
GIFPictureBox = __UI_GetID("GIFPictureBox")
LoadButton = __UI_GetID("LoadButton")
PlayButton = __UI_GetID("PlayButton")
AboutButton = __UI_GetID("AboutButton")
DoneProgressBar = __UI_GetID("DoneProgressBar")
END SUB