Posted: 14th Aug 2016 0:12
AGK

Came up with this effect for a media player idea. Characters of a string slide in one at a time rather than the whole word. For the demo, left click your mouse to bring the string in to the current position of the mouse. Right click to send the characters back out off screen.

+ Code Snippet
setVirtualResolution(480,320)


#CONSTANT MAX_CHARS = 48
#CONSTANT TEXT_STATE_IN  = 1
#CONSTANT TEXT_STATE_OUT = 2



Type OBJ_Text
    id as integer
    x as integer
    y as integer
    lx as integer
    ly as integer
    move as integer
EndType

Type Text_Properties
    start as integer
    timestamp as integer
    moving as integer
    targetX as integer
    targetY as integer
    startX as integer
    startY as integer
    charDelay as integer
    charSpeed as integer
    textLength as integer
    textWidth as integer
EndType


//createSprite(loadImage("background.png"))


Global _Text_Props as Text_Properties



dim text[MAX_CHARS] as OBJ_Text


for i = 1 to MAX_CHARS
    text[i].id = createText("")
    setTextSize(text[i].id, 24)
    setTextPosition(text[i].id, i*12, 100)
    text[i].lx = i*12
    text[i].ly = 100
next i


setText("This is a test.")





repeat


    if getRawMouseLeftPressed() = 1
        setTextStart(getRawMouseX(), getRawMouseY(), getVirtualWidth(), getRawMouseY(), 50, 30, TEXT_STATE_IN)
    endif

    if getRawMouseRightPressed() = 1
        setTextStart(-_Text_Props.textWidth, _Text_Props.targetY, _Text_Props.targetX, _Text_Props.targetY, 50, 30, TEXT_STATE_OUT)
    endif




    doTextStuff()

    sync()
until getRawKeyPressed(27)=1
end



/*
 * targetX, targetY = Where start of string should come to rest
 * startX, startY   = Where string characters should start before moving
 *                    (when moving out, starting position should be "target" from when chars were moved in)
 * delay            = Delay between moving the next character in the string
 * speed            = Speed at which the character moves
 * state            = TEXT_STATE_IN to move characters into position on screen,
 *                    TEXT_STATE_OUT when characters are moving back out of position
 */
function setTextStart(targetX, targetY, startX, startY, delay, speed, state)
    _Text_Props.start = 1
    _Text_Props.moving = 0
    _Text_Props.charDelay = delay
    _Text_Props.charSpeed = speed


    _Text_Props.targetX = targetX
    _Text_Props.targetY = targetY
    _Text_Props.startX = startX
    _Text_Props.startY = startY

    for i = 1 to _Text_Props.textLength
        text[i].x = 0
        text[i].y = 0
        if state = TEXT_STATE_IN then setTextPosition(text[i].id, startX, startY)
    next i
endfunction



/*
 * Where all the fancy fairy magic happens.
 */
function doTextStuff()
    if _Text_Props.start = 1
        t = getMilliseconds()
        if _Text_Props.timestamp+_Text_Props.charDelay < t
            inc _Text_Props.moving
            if _Text_Props.moving > _Text_Props.textLength
                _Text_Props.moving = _Text_Props.textLength
            else
                text[_Text_Props.moving].move = 1
            endif
            _Text_Props.timestamp = t
        endif

        // Move characters into position that have been flagged to do so
        for i = 1 to _Text_Props.moving
            if text[i].move = 1
                // Char's X-pos is relative to edge of screen (0 is far right)
                text[i].x = text[i].x + _Text_Props.charSpeed
                // Subtract char's position from edge of screen
                x = _Text_Props.startX - text[i].x
                // If char has reached target position
                if x <= _Text_Props.targetX + text[i].lx
                    text[i].move = 0
                    x = _Text_Props.targetX + text[i].lx
                endif
                setTextPosition(text[i].id, x, _Text_Props.targetY)
            endif
        next i
        // If last character in string has finished moving
        if _Text_Props.moving = _Text_Props.textLength
            if text[_Text_Props.moving].move = 0 then _Text_Props.start = 0
        endif
    endif
endfunction





/*
 * Assigns string to text object array
 */
function setText(s$)
    _Text_Props.textLength = len(s$)
    for i = 1 to _Text_Props.textLength
        setTextString(text[i].id, mid(s$, i, 1))
    next i
    _Text_Props.textWidth = _Text_Props.textLength*12
endfunction