sharpiepoop icon

Lua actions menu

sharpiepoop | PRO | 12/12/13 03:25:21 PM UTC | 0 ⭐ | 278 👁️ | Never ⏰ | []
Lua |

5.41 KB

|

None

|

0 👍

/

0 👎

 
 
gui = {}
 
gui.rgb = function(r,g,b)
    return r * 256 * 256 + g * 256 + b
end
 
gui.controls = {}
gui.baseid = 0
gui.defaultbg = gui.rgb(0,128,0)
gui.defaultbc = gui.rgb(255,255,255)
gui.defaultba = 0.5
 
gui.control = {}
 
gui.control.show = function(c,p)
    --print('gui.show:'..tostring(c)..':'..tostring(c.x)..','..tostring(c.y))
    
    -- auto close members of same autoCloseGroup
    if c.autoCloseGroup then
        for i,c2 in ipairs(gui.controls) do
            if c.id ~= c2.id and c.autoCloseGroup == c2.autoCloseGroup then
                c2:hide(p)
            end
        end
    end
    
    -- call onShow
    if c.onShow then
        c:onShow()
    end
 
    -- show
    ui.addTextArea(gui.baseid + c.id, c.html, p, c.x, c.y, c.w, c.h, c.bg, c.bc, c.ba)
    if p then
        c.isVisible[p] = true
    else
        c.isVisible = {}
        c.isVisible['*'] = true
    end
end
 
gui.control.hide = function(c,p)
    -- call onHide
    if c.onHide then
        c:onHide()
    end
    -- hide
    ui.removeTextArea(gui.baseid + c.id, p)
    if p then
        c.isVisible[p] = false
    else
        c.isVisible = {}
    end
end
 
gui.control.toggle = function(c,p)
    if c:isVisibleForPlayer(p) then
        c:hide(p)
    else
        c:show(p)
    end
end
 
gui.control.isVisibleForPlayer = function(c,p)
    if c.isVisible[p] == true then
        return true
    end
    if c.isVisible['*'] == true then
        return true
    end     
    return false
end
 
 
gui.newControl = function(x,y,w,h)
    local c = {}
    c.x    = x
    c.y    = y
    c.w    = w
    c.h    = h
    c.id   = table.getn(gui.controls)+1
    c.text = ""
    c.html = ""
    c.isVisible = {}
    c.isVisibleForPlayer = gui.control.isVisibleForPlayer
    c.bg   = gui.defaultbg
    c.bc   = gui.defaultbc
    c.ba   = gui.defaultba
    --print("id:" .. c.id)
    table.insert(gui.controls,c)
    
    -- set up methods
    c.show   = gui.control.show
    c.hide   = gui.control.hide
    c.toggle = gui.control.toggle
    
    return c
end
 
-- Label ----------------------------------------------------------------------
gui.label = {}
gui.label.setText = function(c,text)
    c.text = text
    c.html = text
end
 
gui.newLabel = function(x,y,w,h,text)
    c = gui.newControl(x,y,w,h)
    c.type = "gui.label"
    c.setText = gui.label.setText
    c:setText(text)
    return c
end
 
-- Button ---------------------------------------------------------------------
gui.button = {}
gui.button.setText = function(c,text)
    c.text = text
    c.html = '<a href="event:' .. c.id .. '" >' .. text .. '</a>'
end
 
gui.newButton = function(x,y,w,h,text)
    c = gui.newControl(x,y,w,h)
    c.type = "gui.button"
    c.setText = gui.button.setText
    c:setText(text)
    return c
end
    
-- List -----------------------------------------------------------------------
gui.list = {}   
 
gui.list.resetItems = function(c)
    c.children = {}
end
 
gui.list.addItem = function(c,child)
    -- update the id to reflect it's position in the table
    child.parent = c
    child.id = table.getn(c.children) + 1
    child:setText(child.text)
    table.insert(c.children, child)
    c.html = '<ol>'
    for i,child in ipairs(c.children) do
        if child.type == "gui.label" then
            c.html = c.html .. child.html .. '<br>'
        elseif child.type == "gui.button" then
            c.html = c.html .. '<li>' .. child.html .. '</li>'
        end
    end
    c.html = c.html .. '</ol>'
end
 
gui.newList = function(x,y,w,h)
    c = gui.newControl(x,y,w,h)
    c.type = "gui.list"
    c.children = {}
    c.addItem    = gui.list.addItem
    c.resetItems = gui.list.resetItems
    return c
end
 
 
-- Default event callback handler.
gui.eventTextAreaCallback = function(id,p,callback)
    --tfm.exec.chatMessage('['..p..'] id:'.. id .. ' event:' .. callback)
    local c = gui.controls[id]
    if c then
        if c.onClick then
            c:onClick(id,p,callback)
        end
    end
end
 
gui.eventMouse = function(p,x,y)
    bAutoClose = true
    r = false
    
    -- see if the click was on an open control
    for i,c in ipairs(gui.controls) do
        if c:isVisibleForPlayer(p) and x > c.x and x < (c.x+c.w) and y > c.y and y < (c.y + c.h) then
            bAutoClose = false
            r = true
        end
    end
 
    -- auto close members of same autoCloseGroup
    if bAutoClose == true then
        for i,c in ipairs(gui.controls) do
            if c.autoCloseGroup and c:isVisibleForPlayer(p) then
                c:hide(p)
                r = true
            end
        end
    end
    
    --print('gui.eventMouse: consumed:' .. tostring(r))
    return r
end
 
 
 
a = { "setShaman", "killPlayer", "respawnPlayer", "giveCheese", "playerVictory" , "giveMeep", "setVampirePlayer" }
m = {}
for i,f in ipairs(a) do
    list = gui.newList(140,i*29,120,200)
    list.autoCloseGroup = 1
    list.header = f
 
    list.onShow = function(self)
        self:resetItems()
        self:addItem( gui.newLabel(0,0,0,0,self.header)  )
        for p,_ in pairs(tfm.get.room.playerList) do
            self:addItem( gui.newButton(0,0,0,0,p)  )
        end
    end
 
    list.onClick = function(self,id,p,callback)
        --tfm.exec.chatMessage('list1: onClick: event:' .. callback)
        player = self.children[tonumber(callback)].text
        tfm.exec[self.children[1].text](player)
    end
    
    button = gui.newButton(20,i*29,100,20,f..' >')
    m[button.id] = list
    button.onClick = function(self,id,p,callback)
        m[self.id]:toggle(p)
    end
    button:show()
end
 
 
 
 
tfm.exec.newGame()
 
function eventNewGame()
    for p,_ in pairs(tfm.get.room.playerList) do
        system.bindMouse(p,yes)
    end
end
 
function eventTextAreaCallback(id,p,callback)
    --print('eventTextAreaCallback')
    gui.eventTextAreaCallback(id,p,callback)
end
 
function eventMouse(p,x,y)
    --print('eventMouse')
    if not gui.eventMouse(p,x,y) then
        -- gui did not handle this event
    end
end

Comments