VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
END
Attribute VB_Name = "classGAF"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
' classGAF.cls
'   GAF animation file class.

Option Explicit

' Animation sequences. '
Public Animations As New Collection

' Load a GAF file. '
Public Function Load(Filename As String)
    Dim Header As GAFHeader
    Dim pAnims() As Long
    Dim File As Integer
    Dim Index As Long
    Dim Animation As New classGAFAnimation
    
    On Error Resume Next
    
    ' Open the GAF file. '
    Load = False
    File = FreeFile
    FrmProgress.Show , FrmGAFBuilder
    FrmProgress.Status = "Loading GAF file..."
    Open Filename For Binary As File
    
    ' Get the Header. '
    Get File, , Header
    
    ' Get the GAF Anim Pointers. '
    ReDim pAnims(Header.Entries - 1)
    Get File, , pAnims()
    
    ' Get the GAF Animations. '
    For Index = 0 To Header.Entries - 1
        FrmProgress.Update Index, Header.Entries - 1
        Seek File, pAnims(Index) + 1
        Set Animation = New classGAFAnimation
        Animation.Load File
        Animations.Add Animation, Animation.Name
    Next
        
    ' Close the GAF file. '
    Close File
    Unload FrmProgress
    Load = True
End Function

' Save the GAF file. '
Public Function Save(Filename As String)
    Dim Header As GAFHeader
    Dim pAnims() As Long
    Dim File As Integer
    Dim Index As Long
    Dim Count As Integer
    
    ' Initialize. '
    On Error GoTo Error
    FrmProgress.Show , FrmGAFBuilder
    FrmProgress.Status = "Saving GAF file..."
    File = FreeFile
    Open Filename For Binary As File
    
    ' Write header. '
    Header.Signature = &H10100
    Header.Entries = Animations.Count
    Header.Always0 = 0
    Put File, , Header
    If Animations.Count = 0 Then GoTo Error
    
    ' Write GAF anim pointers. '
    ReDim pAnims(Animations.Count - 1)
    Put File, , pAnims()
    
    ' Write the GAF frames. '
    For Index = 1 To Animations.Count
        FrmProgress.Update Index, Animations.Count
        pAnims(Index - 1) = Seek(File) - 1
        Animations(Index).Save File
    Next
    
    ' Rewrite anim pointers. '
    Seek File, 13
    Put File, , pAnims()
    
Error:
    Unload FrmProgress
    Close File
End Function

' Create the animation list on FrmMain. '
Public Sub CreateList()
    Dim Index As Long, i As Long
    Dim NewItem As ListItem
    Dim Count As Long
    
    On Error Resume Next
    With FrmGAFBuilder
        .ListGAF.ListItems.Clear
        For Index = 1 To Animations.Count
            Count = 0
            For i = 1 To Animations(Index).Frames.Count
                Count = Count + Animations(Index).Frames(i).Subframes.Count
            Next
            Set NewItem = .ListGAF.ListItems.Add(, Animations(Index).Name, Animations(Index).Name, , 1)
            If Animations(Index).Animated Then
                NewItem.SubItems(1) = "Yes"
            Else
                NewItem.SubItems(1) = "No"
            End If
            NewItem.SubItems(2) = CStr(Animations(Index).Frames.Count)
            NewItem.SubItems(3) = CStr(Count)
        Next
    End With
End Sub

' Refresh the animation list on frmMain. '
Public Sub RefreshList()
    Dim Index As Long, i As Long
    Dim Count As Long
    
    On Error Resume Next
    With FrmGAFBuilder
        For Index = 1 To Animations.Count
            Count = 0
            For i = 1 To Animations(Index).Frames.Count
                Count = Count + Animations(Index).Frames(i).Subframes.Count
            Next
            If Animations(Index).Animated Then
                .ListGAF.ListItems(Animations(Index).Name).SubItems(1) = "Yes"
            Else
                .ListGAF.ListItems(Animations(Index).Name).SubItems(1) = "No"
            End If
            .ListGAF.ListItems(Animations(Index).Name).SubItems(2) = CStr(Animations(Index).Frames.Count)
            .ListGAF.ListItems(Animations(Index).Name).SubItems(3) = CStr(Count)
        Next
    End With
End Sub

' Export all images to the specified location. '
Public Sub ExportAllImages(Location As String)
    Dim Index As Long
    
    For Index = 1 To Animations.Count
        Animations(Index).ExportAllImages Location
    Next
End Sub

' Import a sequence of image files. '
Public Sub ImportSequence(Images() As classFrameList, Center As Boolean, WhiteBG As Boolean, Compress As Boolean)
    Dim Animation As New classGAFAnimation
    
    On Error Resume Next
    Animation.ImportSequence Images(), Center, WhiteBG, Compress
    If Animation.Name <> "" Then
        Animations.Add Animation, Animation.Name
    End If
End Sub

' Compress all images. '
Public Sub CompressAll()
    Dim Index As Long, i As Long, ii As Long
    
    On Error Resume Next
    For Index = 1 To Animations.Count
        For i = 1 To Animations(Index).Frames.Count
            Animations(Index).Frames(i).Compressed = True
            For ii = 1 To Animations(Index).Frames(i).Subframes.Count
                Animations(Index).Frames(i).Subframes(ii).Compressed = True
                FrmGAFBuilder.chkCompressed = vbChecked
            Next
        Next
    Next
End Sub
