sort of tweened text button. Based on RoundedRectangle

some code that will create text buttons that zoom across the screen. touch to send it away. im not knowing technical name for this. heavily based on roundedrectangle. my documentation is probably very bad… but anyway here is code :slight_smile:

p.s. u can find rounded rectangle on forums, dont remember who made it

function setup()
    displayMode(FULLSCREEN)
end

function draw()
    background(0,0,0,255)
    fontSize(144)
    fill(100)
    text('GAME. OVER.', WIDTH/2, HEIGHT/2)
    TweenedText('Continue?',WIDTH/2,HEIGHT/2-100,40*9,40,5,32,1)
end

function touched(touch)
    gameoverscreen.touched(touch)
end
gameover = false

gameoverscreen = {}

--[[
The continue button will scroll onto the screen
uses tweens and rrect 
and special custom button class
]]
local button = class() 
local timer = 1000

function button:init(t, x, y, w, h, s, c, fillColor, textColor, time, time2, target, target2)
    self.t = t
    self.x = x
    self.y = y
    self.w = w
    self.h = h
    self.s = s
    self.c = c
    self.target2 = target2  --called after a touch
    self.time2 = time2
    self.fillColor = fillColor
    self.textColor = textColor
    tween(time, self, target)
end

function button:draw()
    fill(self.fillColor)
    RoundedRectangle(self.x,self.y,self.w,self.h,self.s,self.c,self.a)
    fontSize(32)
    fill(self.textColor)
    text(self.t, self.x+self.w*.5, self.y+self.h*.5)
end

function button:touched(t)
    if t.x > self.x and t.x < self.x + self.w
    and t.y > self.y and t.y < self.y + self.h and t.state == ENDED then
        print('touched button '..self.t)
        return true
    end
    
end

function button:tweenToTarget2()
    tween(self.time2, self, self.target2)
end
__TextButtons = {}

--[[
TweenedText is an auxilary function for creating text that will 
tween into a location as seperate strings. The first parameter 
specifies the string. The next four parameters specify the rectangle.
The fifth specifies the rounding on corners. The sixth picks the 
fontSize used.
*Heavily relies on the RoundedRectangle auxilary function.*
--]]
function TweenedText(text, x, y, w, h, s, fs, time)
    local len = string.len(text)
    local buttons = {}
    local time, nt = time or 2;
    local t
    local nx    --new x
    fs = fs or w/len
    nw = w/len  --new width for mini blocks
    local a
    for i = 1, len do
        nt = time + time/(len*2)*(i-1)
        t = string.sub(text, i, i)
        nx = x + (i*nw)
        if i == 1 then a = 12 elseif i == len then a = 3 else a = 15 end
        local targ = {x = nx, y = y}
        local targ2 = {x = -400 + i*nw, y=y}
        local label = table.concat({t,nx,y,nw,h,a},',')
        if not __TextButtons[label] then
        __TextButtons[label] = button(t, 1274, y, nw, h, s, a, color(100), color(200), nt, time, targ, targ2)
        end
        __TextButtons[label]:draw()
    end
end

local function touchedText(t)
    for k,v in pairs(__TextButtons) do
        if v:touched(t) and timer == 1000 then
            timer = nil
            for k,v in pairs(__TextButtons) do
                v:tweenToTarget2()
            end
        end
    end
end


local xy = {x=WIDTH*.75, y=HEIGHT/2-100}
local wh = {w=256, h=56}

function gameoverscreen.init()
    
end

function gameoverscreen.draw()



    
end

function gameoverscreen.touched(t)
    touchedText(t)
end

--[[
This is an auxilliary function for drawing a rectangle with possibly
curved cornders.  The first four parameters specify the rectangle.
The fifth is the radius of the corner rounding.  The optional sixth is
a way for specifying which corners should be rounded by passing a
number between 0 and 15.  The first bit corresponds to the lower-left
corner and it procedes clockwise from there.
--]]

local __RRects = {}



function RoundedRectangle(x,y,w,h,s,c,a)
    c = c or 0
    w = w or 0
    h = h or 0
    if w < 0 then
        x = x + w
        w = -w
    end
    if h < 0 then
        y = y + h
        h = -h
    end
    w = math.max(w,2*s)
    h = math.max(h,2*s)
    a = a or 0
    pushMatrix()
    translate(x,y)
    rotate(a)
    local label = table.concat({w,h,s,c},",")
    if __RRects[label] then
        __RRects[label]:setColors(fill())
        __RRects[label]:draw()
    else
    local rr = mesh()
    local v = {}
    local ce = vec2(w/2,h/2)
    local n = 4
    local o,dx,dy
    for j = 1,4 do
        dx = -1 + 2*(j%2)
        dy = -1 + 2*(math.floor(j/2)%2)
        o = ce + vec2(dx * (w/2 - s), dy * (h/2 - s))
        if math.floor(c/2^(j-1))%2 == 0 then
    for i = 1,n do
        table.insert(v,o)
        table.insert(v,o + vec2(dx * s * math.cos((i-1) * math.pi/(2*n)), dy * s * math.sin((i-1) * math.pi/(2*n))))
        table.insert(v,o + vec2(dx * s * math.cos(i * math.pi/(2*n)), dy * s * math.sin(i * math.pi/(2*n))))
    end
    else
        table.insert(v,o)
        table.insert(v,o + vec2(dx * s,0))
        table.insert(v,o + vec2(dx * s,dy * s))
        table.insert(v,o)
        table.insert(v,o + vec2(0,dy * s))
        table.insert(v,o + vec2(dx * s,dy * s))
    end
    end
    rr.vertices = v
    rr:addRect(ce.x,ce.y,w,h-2*s)
    rr:addRect(ce.x,ce.y + (h-s)/2,w-2*s,s)
    rr:addRect(ce.x,ce.y - (h-s)/2,w-2*s,s)
    rr:setColors(fill())
    rr:draw()
    __RRects[label] = rr
    end
    popMatrix()
end




It looks like the RoundedRectangle code from this comment. Linking to the source helps others find it (and see alternatives - there’s a whole thread there about rounded rectangles).