'/title Animations (version 2) using thumbs
'/type utility
'========================================================
' Controls while playing animation
'..................................................
'          - = decrease animation speed
'         + = increase animation speed
'       TAB = change playback direction (forward/reverse)
' Backspace = back to selection screen
'========================================================

_TITLE "Animations (version 2) using thumbs"

DIM SHARED UserWarned
DIM SHARED MenuIconsPath$, reload, BGrd, BGgr, BGbl
x$ = _CWD$
MenuIconsPath$ = x$ + "\AniFrames"
wallpaperPath$ = x$ + "\AniPlayer2wallpapers\"
DIM SHARED Catagory$(5), catagories, Cat#
DIM SHARED catX(10), catY(10), catW(10), catH(10)
GetCatagories
DIM SHARED iconX(50), iconY(50), iconW(50), iconH(50), icon#, icons
DIM SHARED IconName$(50)
DIM SHARED ScrnWidth: ScrnWidth = 600
DIM SHARED ScrnHeight: ScrnHeight = 300
DIM SHARED MaxFrames: MaxFrames = 5000
DIM SHARED FrameName$(MaxFrames), frames, direction$, AniDelay

' value of 'MaxSlowDelay' is the largest value allowed to slow down
' an animation. Could really be any number wanted.
DIM SHARED MaxSlowDelay: MaxSlowDelay = 200

DIM SHARED CountPerSec
GetLabelValue "settings.ini", "CountPerSec", "", CountPerSec

configfile$ = "Animation2.ini"
GetLabelValue configfile$, "BGrd", "", BGrd
GetLabelValue configfile$, "BGgr", "", BGgr
GetLabelValue configfile$, "BGbl", "", BGbl

SetTheScreen:

GetLabelValue configfile$, "ScrnWidth", "", ScrnWidth
GetLabelValue configfile$, "ScrnHeight", "", ScrnHeight

DIM SHARED wallpaper$
GetLabelValue configfile$, "wallpaper", wallpaper$, n
wallpaper$ = wallpaperPath$ + wallpaper$
CheckForFile wallpaper$, found
IF found = 0 THEN
    IF UserWarned = 0 THEN
        CLS
        PRINT "warning!   following wallpaper image not found..."
        PRINT wallpaper$
        PRINT "press any key.."
        DO UNTIL INKEY$ <> "": LOOP
        UserWarned = 1
    END IF
    wallpaper$ = ""
    GetLabelValue configfile$, "ScrnWidth", "", ScrnWidth
    GetLabelValue configfile$, "ScrnHeight", "", ScrnHeight
    SCREEN _NEWIMAGE(ScrnWidth, ScrnHeight, 32)
ELSE
    SCREEN _NEWIMAGE(150, 150, 32)
    image& = _LOADIMAGE(wallpaper$)
    imgW% = _WIDTH(image&) '  get image width
    imgH% = _HEIGHT(image&) ' get image height
    ScrnWidth = imgW%
    ScrnHeight = imgH%
END IF

CLS

IF wallpaper$ <> "" THEN
    SCREEN _NEWIMAGE(ScrnWidth, ScrnHeight, 32)
    _PUTIMAGE (0, 0), image&
ELSE
    BGrd = 10
    BGgr = 100
    BGbl = 20
    fill = 1
    DrawBox 0, 0, ScrnWidth, ScrnHeight, BGrd, BGgr, BGbl, fill
END IF


ShowCatagories
LaodIcons
DrawIconBoxes

reload = 0

DO

    DO WHILE _MOUSEINPUT ' get latest mouse information
    LOOP

    x% = _MOUSEX: mx = x%
    y% = _MOUSEY: my = y%
    LeftClick% = _MOUSEBUTTON(1) '                 retrieve left button status
    RightClick% = _MOUSEBUTTON(2) '                retrieve right button status
    ''LOCATE 10: PRINT LeftClick%; RightClick%

    IF LeftClick% = -1 THEN
        IF clkd = 0 THEN
            TestClickPos mx, my
            IF reload THEN GOTO SetTheScreen
            clkd = 1
        END IF
    ELSE
        clkd = 0
    END IF

    k$ = UCASE$(INKEY$)

    SELECT CASE k$
        CASE CHR$(27): CLS: SYSTEM
    END SELECT

LOOP

SUB TestClickPos (mx, my) '################################################

    '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    '-- clicked on a icon?

    FOR i = 1 TO icons

        x = iconX(i)
        y = iconY(i)
        w = iconW(i)
        h = iconH(i)

        IF my >= y AND my <= y + h - 1 THEN
            IF mx >= x AND mx <= x + w - 1 THEN
                IF icon# = i THEN
                    LoadAnimationFrames i
                    reload = 1
                    EXIT SUB
                END IF
                icon# = i
                DrawIconBoxes
                EXIT SUB
            END IF
        END IF

    NEXT

    '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    '--clicked on a catagory label ?

    FOR i = 1 TO catagories

        x = catX(i)
        y = catY(i)
        w = catW(i)
        h = catH(i)

        IF my >= y AND my <= y + h - 1 THEN
            IF mx >= x AND mx <= x + w - 1 THEN
                IF Cat# <> i THEN
                    Cat# = i
                    reload = 1
                    EXIT SUB
                    ShowCatagories
                END IF
            END IF
        END IF

    NEXT


END SUB

SUB DrawIconBoxes '########################################################

    FOR i = 1 TO icons

        x = iconX(i)
        y = iconY(i)
        w = iconW(i)
        h = iconH(i)

        IF i = icon# THEN
            clr& = _RGB32(0, 200, 255)
        ELSE
            clr& = _RGB32(BGrd, BGgr, BGbl)
        END IF

        LINE (x - 2, y - 2)-(x + w + 1, y + h + 1), clr&, B
        LINE (x - 3, y - 3)-(x + w + 2, y + h + 2), clr&, B

    NEXT

END SUB

SUB RemoveDecimal (n) '##################################################
    x$ = LTRIM$(STR$(n))
    p = INSTR(x$, ".")
    IF p > 0 THEN n = VAL(LEFT$(x$, p - 1))
END SUB

SUB DrawBox (x, y, w, h, Rd, Gr, Bl, fill) '###########################################
    clr& = _RGB32(Rd, Gr, Bl)
    IF fill THEN
        LINE (x, y)-(x + w - 1, y + h - 1), clr&, BF
    ELSE
        LINE (x, y)-(x + w - 1, y + h - 1), clr&, B
    END IF
END SUB

SUB ShowCatagories '######################################################

    x = 10
    y = 10
    h = 40

    'LOCATE 1, 1: PRINT CHR$(219);
    'clr = POINT(0, 0)
    'LOCATE 5, 1: PRINT clr: END

    w = ScrnWidth / catagories
    RemoveDecimal w
    ''DrawBox 0, 0, w, 30, 50, 100, 200, 0
    'END

    FOR c = 1 TO catagories

        'COLOR 7
        'IF c = Cat# THEN COLOR 15

        ''PRINT Catagory$(c); " "; CHR$(179); " ";

        img$ = MenuIconsPath$ + "\" + Catagory$(c) + ".png"
        image& = _LOADIMAGE(img$)
        w% = _WIDTH(image&) '                                get image width
        h% = _HEIGHT(image&) '                              get image height
        clr = 0
        IF c = Cat# THEN clr = 255
        DrawBox x - 3, y - 3, w% + 6, h% + 6, clr, clr, clr, 1

        catX(c) = x
        catY(c) = y
        catW(c) = w%
        catH(c) = h%


        _PUTIMAGE (x, y), image&
        ''x = x + w%

        'clr = 128
        'DrawBox x, y, w, h, clr, clr, clr, 1
        'clr = 0
        'IF c = Cat# THEN clr = 255
        'DrawBox x - 3, y - 3, w% + 6, h% + 6, clr, clr, clr, 1

        x = x + w% + 10

    NEXT


END SUB

SUB GetCatagories '######################################################

    tf$ = "~cats.tmp"
    q$ = CHR$(34)
    cmd$ = "dir " + q$ + MenuIconsPath$ + q$ + " /b >" + tf$
    SHELL cmd$

    OPEN tf$ FOR INPUT AS #1

    DO UNTIL EOF(1)

        LINE INPUT #1, cat$

        IF INSTR(cat$, ".") = 0 THEN
            i = i + 1
            PRINT i; cat$
            Catagory$(i) = cat$
        END IF

    LOOP

    CLOSE 1

    catagories = i
    Cat# = 1

END SUB

SUB LaodIcons '##########################################################

    tf$ = "~SubCat.tmp"
    path$ = MenuIconsPath$ + "\" + Catagory$(Cat#) + "\"
    q$ = CHR$(34)
    cmd$ = "dir " + q$ + path$ + "*" + q$ + " /b >" + tf$
    SHELL cmd$
    ''SHELL "notepad.exe " + tf$: END

    DIM image&
    IconSqr = 50
    y = 60
    x1 = 5
    x = x1

    OPEN tf$ FOR INPUT AS #1

    DO UNTIL EOF(1)

        LINE INPUT #1, fldr$

        IF 1 = 1 THEN

            img$ = path$ + fldr$ + "\$tn.jpg"
            ''PRINT img$; "<": END
            image& = _LOADIMAGE(img$)
            _PUTIMAGE (x, y), image&
            w% = _WIDTH(image&) '                                get image width
            h% = _HEIGHT(image&) '                              get image height
            IF h% > tallest THEN tallest = h%
            i = i + 1
            iconX(i) = x
            iconY(i) = y
            iconW(i) = w%
            iconH(i) = h%
            IconName$(i) = LEFT$(img$, LEN(img$) - 4) 'trim off .XXX

            x = x + w% + 10
            IF x + IconSqr >= ScrnWidth THEN
                y = y + tallest + 10
                x = x1
                tallest = 0
            END IF
        END IF

    LOOP

    CLOSE 1

    icon# = 1
    icons = i

END SUB

SUB GetLabelValue (lblfile$, lbl$, v$, n) '##################################
    v$ = "": n = FREEFILE: OPEN lblfile$ FOR INPUT AS #n
    DO UNTIL EOF(n)
        LINE INPUT #n, dta$: dta$ = RTRIM$(LTRIM$(dta$))
        IF LEFT$(dta$, 1) = "'" THEN dta$ = ""
        p = INSTR(dta$, "=")
        IF p THEN
            l$ = RTRIM$(LEFT$(dta$, p - 1))
            IF LCASE$(l$) = LCASE$(lbl$) THEN
                v$ = LTRIM$(RIGHT$(dta$, LEN(dta$) - p))
                EXIT DO
            END IF
        END IF
    LOOP: CLOSE #n
    n = VAL(v$)
END SUB

SUB LoadAnimationFrames (i) '#########################################################

    ''CLS: PRINT IconName$(i): END
    FOR p = LEN(IconName$(i)) TO 1 STEP -1
        IF MID$(IconName$(i), p, 1) = "\" THEN EXIT FOR
    NEXT
    path$ = LEFT$(IconName$(i), p - 1)
    '--------------------------------------------------------------
    FOR p = LEN(path$) TO 1 STEP -1
        IF MID$(path$, p, 1) = "\" THEN EXIT FOR
    NEXT
    folder$ = RIGHT$(path$, LEN(path$) - p)
    '---------------------------------------------------------------
    '' CLS: PRINT folder$; "<": END

    _TITLE "Animation : " + folder$

    ''path$ = StartPath$ + "\" + Catagory$(Cat#) + "\" + folder$(i)
    tf$ = "~~animates.tmp"
    q$ = CHR$(34)
    cmd$ = "dir " + q$ + path$ + q$ + " /b >" + tf$
    ''CLS: PRINT cmd$: END
    SHELL cmd$
    '' SHELL "notepad.exe " + tf$: END

    DelayFile$ = path$ + "\delay.dat"
    ''PRINT DelayFile$
    CheckForFile DelayFile$, found
    'AniDelay is a percent of CountPerSec
    ''PRINT found: END
    IF found THEN
        OPEN DelayFile$ FOR INPUT AS #1
        INPUT #1, AniDelay
        CLOSE 1
    ELSE
        AniDelay = 1
    END IF

    COLOR 7, 0

    OPEN tf$ FOR INPUT AS #1

    i = 0

    DO UNTIL EOF(1)

        LINE INPUT #1, f$

        IF LEFT$(LCASE$(f$), 4) = "$tn." THEN
            f$ = "" 'this is a thumbnail image
        END IF

        ft$ = LCASE$(RIGHT$(f$, 4))

        SELECT CASE ft$

            CASE ".jpg", ".png", ".bmp"
                i = i + 1
                FrameName$(i) = path$ + "\" + f$
                'CLS :
                PRINT i; FrameName$(i); "<"
                IF i = MaxFrames THEN EXIT DO
                'EXIT DO

        END SELECT

    LOOP

    CLOSE 1

    frames = i

    ScrnWidth = 600
    ScrnHeight = 500
    ScreenMode = 32

    SCREEN _NEWIMAGE(ScrnWidth, ScrnHeight, ScreenMode)

    img$ = FrameName$(1)
    'CLS: PRINT img$; "<": END
    image& = _LOADIMAGE(img$)
    w% = _WIDTH(image&)
    h% = _HEIGHT(image&)
    ScrnWidth = w%
    ScrnHeight = h% + 15
    SCREEN _NEWIMAGE(ScrnWidth, ScrnHeight, ScreenMode)
    direction$ = "F"

    DO

        PlayTheAnimation k$
        IF k$ = CHR$(8) THEN EXIT DO 'back to main menu
        IF k$ = CHR$(27) THEN EXIT DO

    LOOP

    OPEN DelayFile$ FOR OUTPUT AS #1
    WRITE #1, AniDelay
    CLOSE 1

    IF k$ = CHR$(27) THEN CLS: SYSTEM


END SUB

SUB CheckForFile (f$, found) '##############################################
    found = 0
    ''LOCATE 1, 1: PRINT ">>"; f$; "<": END
    IF _FILEEXISTS(f$) THEN found = 1
END SUB

SUB PlayTheAnimation (k$) '################################################

    ForStart = 1
    ForStop = frames
    ForStep = 1

    IF direction$ = "R" THEN
        ForStart = frames
        ForStop = 1
        ForStep = -1
    END IF

    ''FOR i = 1 TO frames
    FOR i = ForStart TO ForStop STEP ForStep

        img$ = FrameName$(i)
        image& = _LOADIMAGE(img$)
        ''LOCATE 1, 1: PRINT img$
        _PUTIMAGE (0, 0), image&
        dly = 0
        ShowDelayBar

        DO

            dly = dly + 1
            CalcDelayValue delay
            ''LOCATE 1, 1: PRINT delay: END
            ''LOCATE 1, 1: PRINT delay; dly; AniDelay
            IF dly >= delay THEN EXIT DO

            ''LOCATE 1, 1: PRINT AniDelay

            k$ = UCASE$(INKEY$)
            SELECT CASE k$
                CASE CHR$(27): EXIT SUB
                CASE CHR$(8): EXIT SUB 'back to main menu
                CASE "-"
                    IF AniDelay < MaxSlowDelay THEN
                        AniDelay = AniDelay + 1
                    END IF
                CASE "+"
                    AniDelay = AniDelay - 1
                    IF AniDelay < 1 THEN AniDelay = 1
                CASE CHR$(9) 'change playback direction
                    IF direction$ = "F" THEN
                        direction$ = "R"
                    ELSE
                        direction$ = "F"
                    END IF
                    EXIT DO
            END SELECT

            ''EXIT DO 'for testing

        LOOP

        IF k$ = CHR$(9) THEN EXIT FOR

        per = i / frames
        ln = ScrnWidth * per

        x = 0
        y = ScrnHeight - 13
        h = 5
        fill = 1

        w = ScrnWidth
        Rd = 0 ' 255
        Gr = 0 ' 255
        Bl = 0 '' 255
        'black out the progress bar
        DrawBox x, y, w, h, Rd, Gr, Bl, fill

        w = ln
        Rd = 255
        Gr = 255
        Bl = 255
        'draw the progress bar
        DrawBox x, y, w, h, Rd, Gr, Bl, fill

    NEXT

END SUB

SUB ShowDelayBar '##########################################################

    x = 0
    y = ScrnHeight - 5
    h = 5
    fill = 1

    w = ScrnWidth
    Rd = 0 ' 255
    Gr = 0 ' 255
    Bl = 0 '' 255
    'black out the delay value bar
    DrawBox x, y, w, h, Rd, Gr, Bl, fill

    per = (AniDelay / MaxSlowDelay) * 100
    IF per >= 0 THEN 'fast
        Rd = 0: Gr = 255: Bl = 0
    END IF
    IF per >= 50 THEN 'medium
        Rd = 255: Gr = 255: Bl = 0
    END IF
    IF per >= 75 THEN 'slow
        Rd = 255: Gr = 0: Bl = 0
    END IF

    per = per / 100
    w = ScrnWidth * per
    'draw the delay value bar
    DrawBox x, y, w, h, Rd, Gr, Bl, fill

END SUB

SUB CalcDelayValue (dly) '###############################################
    dly = CountPerSec * (AniDelay * .0001)
    'dly = CountPerSec * .0001
    'AniDelay value of '50' is actually .50 (50%)
    RemoveDecimal dly
END SUB

