VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
  Persistable = 0  'NotPersistable
  DataBindingBehavior = 0  'vbNone
  DataSourceBehavior  = 0  'vbNone
  MTSTransactionMode  = 0  'NotAnMTSObject
END
Attribute VB_Name = "classGAFAnimation"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
' classGAFAnimation.cls
'   GAF animation class.

Option Explicit

Public Name As String
Public Frames As New Collection
Public Animated As Boolean ' Are these frames animated? '

' Load the animtion from the GAF file. '
Public Sub Load(File As Integer)
    Dim EntryHeader As GAFEntry
    Dim pFrames() As GAFFrameInfo
    Dim Index As Integer
    Dim Frame As New classGAFFrame
    
    ' Load the entry header. '
    Get File, , EntryHeader
    Name = FixString(EntryHeader.Name)
    If EntryHeader.Frames <= 0 Then Exit Sub
    
    ' Load frame pointers. '
    ReDim pFrames(EntryHeader.Frames - 1)
    Get File, , pFrames()
    
    ' Load the frames. '
    For Index = 0 To EntryHeader.Frames - 1
        If pFrames(Index).Animated = 2 Then
            Animated = True
        End If
        If pFrames(Index).PTRFrame > 0 Then
            Seek File, pFrames(Index).PTRFrame + 1
            Set Frame = New classGAFFrame
            If Frame.Load(File) Then
                Frames.Add Frame
            End If
        End If
    Next
End Sub

' Save the animation to the GAF file. '
Public Sub Save(File As Integer)
    Dim Buffer As Byte, LongBuffer As Long
    Dim AnimName As String
    Dim Pointers() As Long
    Dim FrameFiles() As String
    Dim Count As Integer
    Dim Unknown1 As Integer ' 1 '
    Dim Unknown2 As Long ' 0 '
    Dim GAFFramePtrs() As GAFFrameInfo
    Dim nFrames As Integer
    
    Dim EntryHeader As GAFEntry
    Dim pFrames() As GAFFrameInfo
    Dim offset As Long, NewOffset As Long
    Dim Index As Integer
    
    ' Save the entry header. '
    EntryHeader.Frames = Frames.Count
    EntryHeader.Unknown1 = 1
    EntryHeader.Name = Name & Chr(0)
    Put File, , EntryHeader
    If Frames.Count = 0 Then Exit Sub
    
    ' Save the frame pointers. '
    offset = Seek(File)
    ReDim pFrames(Frames.Count - 1)
    Put File, , pFrames()
    
    ' Save the frames. '
    For Index = 1 To Frames.Count
        If Animated Then
            pFrames(Index - 1).Animated = 2
        Else
            pFrames(Index - 1).Animated = 10
        End If
        pFrames(Index - 1).PTRFrame = Seek(File) - 1
        Frames(Index).Save File
    Next
    
    ' Rewrite the frame pointers. '
    NewOffset = Seek(File)
    Seek File, offset
    Put File, , pFrames()
    Seek File, NewOffset
End Sub

' Export all images to the specified location. '
Public Sub ExportAllImages(Location As String)
    Dim Index As Integer
    
    For Index = 1 To Frames.Count
        Frames(Index).ExportAllImages Location & Name & "_" & Format(Index, "00")
    Next
End Sub

' Import a sequence of image files. '
Public Sub ImportSequence(FileList() As classFrameList, Center As Boolean, WhiteBG As Boolean, Compress As Boolean)
    Dim Index As Integer
    Dim Frame As New classGAFFrame
    
    On Error GoTo Error

    Animated = True
    For Index = 0 To UBound(FileList)
        If FileList(Index).Name <> "" Then
            Set Frame = New classGAFFrame
            If Name = "" Then
                Name = Left(GetFilename(FileList(Index).Name), Len(GetFilename(FileList(Index).Name)) - 4)
            End If
            Frame.ImportSequence FileList(Index), Center, WhiteBG, Compress
            Frames.Add Frame
        End If
    Next
Error:
End Sub

' Display this animation on frmMain. '
Public Sub Display(Optional SelectFrame As Long = 1)
    On Error Resume Next
    With FrmGAFBuilder
        If Frames.Count <= 1 Then
            .ScrollFrames.Enabled = False
            .ScrollFrames.Max = 2
        Else
            .ScrollFrames.Enabled = True
            .ScrollFrames.Max = Frames.Count
        End If
        SelectedFrame = SelectFrame
        .ScrollFrames.Value = SelectFrame
        Frames(SelectedFrame).Display
        
        If Frames.Count = 0 Then
            SelectedSubframe = 0
            .ScrollSubframes.Value = 1
            .ScrollSubframes.Max = 2
            .ScrollSubframes.Enabled = False
            .PicFrame.Picture = .Picture
            .TxtWidth = "0"
            .TxtHeight = "0"
            .ScrollFPS = 0
            .ScrollXPos = 0
            .ScrollYPos = 0
            .chkCompressed.Value = vbUnchecked
            .chkCompressed.Enabled = False
            If Name = "" Then
                .CmdFrame.Enabled = False
            Else
                .CmdFrame.Enabled = True
            End If
            .CmdSubframe.Enabled = False
            .ScrollXPos.Enabled = False
            .ScrollYPos.Enabled = False
            .ScrollFPS.Enabled = False
            .TxtWidth.Enabled = False
            .TxtHeight.Enabled = False
            .TxtXPos.Enabled = False
            .TxtYPos.Enabled = False
            .TxtFPS.Enabled = False
        Else
            .CmdFrame.Enabled = True
            .CmdSubframe.Enabled = True
            .ScrollXPos.Enabled = True
            .ScrollYPos.Enabled = True
            .ScrollFPS.Enabled = True
            .TxtWidth.Enabled = True
            .TxtHeight.Enabled = True
            .TxtXPos.Enabled = True
            .TxtYPos.Enabled = True
            .TxtFPS.Enabled = True
            .chkCompressed.Enabled = True
        End If
    End With
End Sub

' Import frame images. '
Public Sub ImportFrames(Files As Collection, Optional Center As Boolean = False, Optional WhiteBG As Boolean = False, Optional Subframes As Boolean = False, Optional Compress As Boolean = False)
    Dim Frame As New classGAFFrame
    Dim Index As Long
    
    On Error GoTo Error
    For Index = 1 To Files.Count
        If Subframes Then
            Frames(SelectedFrame).ImportSubframe Files(Index), Center, WhiteBG, Compress
        Else
            Set Frame = New classGAFFrame
            Frame.ImportFrame Files(Index), Center, WhiteBG, Compress
            Frames.Add Frame
        End If
    Next
Error:
    Display
End Sub

' Return the maximum width of a frame in this animation. '
Public Function GetMaxWidth() As Long
    Dim Index As Long, i As Long
    Dim MaxWidth As Long
    
    For Index = 1 To Frames.Count
        If Frames(Index).Width > MaxWidth Then
            MaxWidth = Frames(Index).Width
        End If
        For i = 1 To Frames(Index).Subframes.Count
            If Frames(Index).Subframes(i).Width > MaxWidth Then
                MaxWidth = Frames(Index).Subframes(i).Width
            End If
        Next
    Next
    GetMaxWidth = MaxWidth
End Function

' Return the maximum height of a frame in this animation. '
Public Function GetMaxHeight() As Long
    Dim Index As Long, i As Long
    Dim MaxHeight As Long
    
    For Index = 1 To Frames.Count
        If Frames(Index).Height > MaxHeight Then
            MaxHeight = Frames(Index).Height
        End If
        For i = 1 To Frames(Index).Subframes.Count
            If Frames(Index).Subframes(i).Height > MaxHeight Then
                MaxHeight = Frames(Index).Subframes(i).Height
            End If
        Next
    Next
    GetMaxHeight = MaxHeight
End Function
