TGC Codebase Backup



Typing Text by Naphier

15th Apr 2013 23:33
Summary

Takes a text file and types out each line with a pause and shows a blinking cursor. Employs Ched80's WordWrap function to ensure wrapping.



Description



Code
                                    ` This code was downloaded from The Game Creators
                                    ` It is reproduced here with full permission
                                    ` http://www.thegamecreators.com
                                    
                                    function TypeText(filename$ , iNumLines, iTextSize , x# , y# , iWrapWidth , iLineDelay , iCharDelay)
// Types out a line of text one character at a time
// Employs LoadLines() and WordWrap()
    b = 400
    LoadLines(filename$ , iNumLines)

    for i = 1 to iNumLines
        line$ = text_lines$[i]
        textID = CreateText(line$)
        SetTextSize(textID , iTextSize)
        SetTextPosition(textID , x# , y#)

        line$ = WordWrap(textID , iWrapWidth)

        length = Len(line$)

        for j = 1 to length
            typeLine$ = left(line$ , j)

            // Add and remove blinking cursor
            charTime = GetMilliseconds()
            repeat
                if setOnce = 0
                    SetTextString(textID , typeLine$ + chr(144))
                    setOnce = 1
                endif

                Sync()
                if GetPointerPressed() = 1
                    skip = 1
                endif
            until skip = 1 or GetMilliseconds() - charTime > iCharDelay

            setOnce = 0

            if skip = 1
                EXIT
            endif
        next j

        lineTime = GetMilliseconds()

        repeat
            skip = 0
            if blink = 0 and GetMilliseconds() - blinkTime > b
                SetTextString(textID , line$ + chr(144))
                blink = 1
                blinkTime = GetMilliseconds()
            elseif GetMilliseconds() - blinkTime > b and blink = 1
                SetTextString(textID , line$)
                blink = 0
                blinkTime = GetMilliseconds()
            endif

            Sync()
        until GetPointerPressed() = 1 or GetMilliseconds() - lineTime > iLineDelay

        DeleteText(textID)
        Sync()
    next i

endfunction



function DisplayLines(filename$ , iNumLines, iTextSize , x# , y# , iWrapWidth , iDelay)
// Displays lines of a text file one by one
// Employs LoadLines() and WordWrap()

    LoadLines(filename$ , iNumLines)

    for i = 1 to iNumLines
        line$ = text_lines$[i]
        textID = CreateText(line$)
        SetTextSize(textID , iTextSize)
        SetTextPosition(textID , x# , y#)
        WordWrap(textID , iWrapWidth)

        time0 = GetMilliseconds()

        repeat
            Sync()
        until GetPointerPressed() = 1 or GetMilliseconds() - time0 > iDelay

        DeleteText(textID)
    next i

endfunction




function LoadLines(filename$ , numLines)
// Loads a line from the file and stores it into a string
    if GetFileExists(filename$) = 0
        Message("Invalid file called in LoadLine("+filename$+").")
        EXITFUNCTION
    endif

    f = OpenToRead(filename$)

    dim text_lines$[numLines]

    for i = 1 to numLines
        text_lines$[i] = ReadLine(f)
    next i

    CloseFile(f)
endfunction




function WordWrap(TextID as integer,MaxWidth as float)
// Created by Ched80
// wraps word of a text string to a fixed length
    if gettextexists(TextID)=1
        OriginalString$ = gettextstring(TextID)
        Tokens = countstringtokens(OriginalString$," ")
        TempString$ = ""

        for TokenID=1 to Tokens
            CurrentString$ = TempString$

            //Add next word
            if len(TempString$)>0
                TempString$ = TempString$+" "+getstringtoken(OriginalString$," ",TokenID)
            else
                TempString$ = getstringtoken(OriginalString$," ",TokenID)
            endif

            settextstring(TextID,TempString$)
            if gettexttotalwidth(TextID)>=MaxWidth and TokenID>1
                //string too big
                TempString$ = CurrentString$
                TempString$ = TempString$+chr(10)+getstringtoken(OriginalString$," ",TokenID)
            endif
        next TokenID
        settextstring(TextID,TempString$)
    endif
endfunction TempString$