Basic GUI Library

  • FeynmanLogomaker
    28th Sep 2013 Member 0 Permalink

    function guiSetup()
    guiButton = function(x, y, c, t)
    w = tpt.textwidth(t)
    h = 7
    local c = c
    if not c[4] then c[4] = 255 end
    local k = 3
    if guiMousebound(x - 2, y - 2, x + w + 3, y + h + 3) then
    k = 1.5
    end
    tpt.drawrect(x - 2, y - 2, w + 3, h + 3, c[1], c[2], c[3], c[4])
    tpt.fillrect(x - 2, y - 2, w + 3, h + 3, c[1], c[2], c[3], c[4] / k)
    tpt.drawtext(x, y, t, c[1], c[2], c[3], c[4])
    return x - 2, y - 2, x + w + 3, y + h + 3
    end
    local guiclicked = function(x, y, b, e)
    if e == 1 then
    guiClick = 'press'
    elseif e == 3 then
    guiClick = 'hold'
    elseif e == 2 then
    guiClick = 'release'
    else
    guiClick = nil
    end
    return false
    end
    tpt.register_mouseclick(guiclicked)
    guiMousebound = function(x1, y1, x2, y2)
    if type(x1) ~= 'number' or type(x2) ~= 'number' or type(y1) ~= 'number' or type(y2) ~= 'number' then return false end
    return tpt.mousex >= x1 and tpt.mousex <= x2 and tpt.mousey >= y1 and tpt.mousey <= y2
    end
    guiWindow = function(x, y, w, h, c)
    tpt.drawrect(x - 1, y - 1, w + 2, h + 2, c[1], c[2], c[3], c[4])
    tpt.fillrect(x, y, w, h, c[1], c[2], c[3], c[4] / 5)
    end
    end
    guiInit = function()
    guiSetup()
    if guiDraw then
    tpt.register_step(guiDraw)
    end
    end
    guiQuit = function()
    if guiDraw then
    tpt.unregister_step(guiDraw)
    end
    end

     

    ...And some example code:

     

    function guiDraw()
    if not b1 then
    b1, b2, b3 = 0, 255, 0
    end
    guiWindow(100, 100, 400, 180, {b1, b2, b3, 255})
    local str, greetA
    if tpt.get_name() == '' then
    str = 'Hello! If you sign in, then I may be able to greet you properly!'
    else
    str = 'Hello, ' .. tpt.get_name() .. '!'
    end
    if guiMousebound(102, 89, 102 + tpt.textwidth(str), 96) then
    greetA = 255
    else
    greetA = 125
    end
    tpt.drawtext(102, 89, str, 255, 125, 0, greetA)
    a, b, c, d = guiButton(104, 104, {255, 255, 255, 255}, 'I tell you when I\'m pressed!')
    be, bf, bg, bh = guiButton(104, 117, {255, 125, 0, 255}, 'Random Background')
    i, j, k, l = guiButton(104, 270, {255, 0, 0, 255}, 'Quit')
    if guiMousebound(a, b, c, d) and guiClick == 'hold' then
    tpt.drawtext(c + 2, b + 2, 'Button was pressed!', 255, 0, 0, 255)
    b1, b2, b3 = 0, 255, 0
    elseif guiMousebound(be, bf, bg, bh) and guiClick == 'hold' then
    b1, b2, b3 = math.random(0, 255), math.random(0, 255), math.random(0, 255)
    elseif guiMousebound(i, j, k, l) and guiClick == 'hold' then
    guiQuit()
    else
    b1, b2, b3 = 0, 255, 0
    end
    end
    function guitest()
    guiInit()
    end

     

    To use the sample, type in the console 'guitest()'. It looks really simple on the outside, but it's pretty complex on the inside. 

     

    If you come up with any really good scripts using this, post them here!