Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- second attempt at a net like frame work
- by Big SHiny Toys
- ]]--
- local function inBouwndry(clickX,clickY,boxX,boxY,width,hight)
- return ( clickX >= boxX and clickX < boxX + width and clickY >= boxY and clickY < boxY + hight )
- end
- local net = {}
- function net.newWindow()
- return {components = {},drawFlag = true}
- end
- function net.run(tWindo)
- local function redraw()
- for k,v in pairs(tWindo.components) do
- v("redraw")
- end
- end
- redraw()
- while true do
- event = {os.pullEvent()}
- if event[1] == "redraw" then
- redraw()
- elseif event[1] == "mouse_click" then
- if event[2] == 1 then -- left click
- for k,v in pairs(tWindo.components) do
- if inBouwndry(event[3],event[4],v("box")) then
- v("call",event[3],event[4])
- end
- end
- end
- elseif event[1] == "mouse_scroll" then
- for k,v in pairs(tWindo.components) do
- if inBouwndry(event[3],event[4],v("box")) then
- v("scroll",event[2])
- end
- end
- end
- if tWindo.drawFlag then
- redraw()
- tWindo.drawFlag = false
- end
- end
- end
- local button_meta = {
- __call = function(tButton,ins,value)
- if ins == "redraw" then
- term.setBackgroundColor(colors[tButton.bgColor] or tButton.bgColor)
- term.setTextColor(colors[tButton.textColor] or tButton.textColor)
- term.setCursorPos(tButton.posX,tButton.posY)
- term.write(tButton.sText)
- elseif ins == "box" then
- return tButton.posX,tButton.posY,#tButton.sText,1
- elseif ins == "call" then
- return tButton.buttonFunction()
- elseif ins == "set" then
- tButton.sText = value
- tButton.drawFlag()
- end
- end
- }
- function net.button(window,label,func,posX,posY,tCol,bCol)
- local tempButton = {
- posY = posY,
- posX = posX,
- buttonFunction = func,
- sText = label,
- textColor = tCol,
- bgColor = bCol,
- drawFlag = function() window.drawFlag = true end
- }
- setmetatable(tempButton,button_meta)
- table.insert(window.components,tempButton)
- return tempButton
- end
- local list_meta = {
- __call = function(tButton,ins,clickX,clickY)
- if ins == "redraw" then
- term.setBackgroundColor(colors[tButton.bgColor] or tButton.bgColor)
- term.setTextColor(colors[tButton.textColor] or tButton.textColor)
- for i = 1,tButton.hight do
- term.setCursorPos(tButton.posX,tButton.posY+i-1)
- term.write(string.sub((tButton.tList[i + tButton.offset] or "")..string.rep(" ",tButton.width),1,tButton.width))
- end
- elseif ins == "box" then
- return tButton.posX,tButton.posY,tButton.width,tButton.hight
- elseif ins == "call" then
- local item = tButton.tList[(clickY - tButton.posY) + tButton.offset +1]
- if item then
- return tButton.buttonFunction(item)
- end
- elseif ins == "clear" then
- tButton.tList = {}
- tButton.offset = 0
- tButton.drawFlag()
- elseif ins == "add" then
- if clickX then
- table.insert(tButton.tList,clickX)
- end
- tButton.drawFlag()
- elseif ins == "scroll" then
- tButton.offset = tButton.offset + (clickX or 0)
- tButton.drawFlag()
- elseif ins == "set" then
- tButton.tList = clickX
- tButton.offset = 0
- tButton.drawFlag()
- end
- end
- }
- function net.listBox(window,list,func,posX,posY,width,hight,tCol,bCol)
- local tempList = {
- posY = posY,
- posX = posX,
- buttonFunction = func,
- sText = label,
- textColor = tCol,
- bgColor = bCol,
- width = width,
- hight = hight,
- offset = 0,
- tList = list,
- drawFlag = function() window.drawFlag = true end
- }
- setmetatable(tempList,list_meta)
- table.insert(window.components,tempList)
- return tempList
- end
- -- end api
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- term.clear()
- term.setCursorPos(1,1)
- local win = net.newWindow()
- local files
- local path = {}
- local function sPath()
- return table.concat(path,"/")
- end
- local function newlist()
- local test,this = pcall(fs.list,sPath())
- return (this or {})
- end
- local function goBack()
- table.remove(path)
- files("set",newlist())
- end
- local function goForward(step)
- table.insert(path,step)
- files("set",newlist())
- end
- files = net.listBox(win,newlist(),goForward,1,3,40,10,"lightGray","gray")
- net.button(win,"[back]",goBack,1,2,"lime","green")
- net.run(win)
- --[[ -- fully functional test 1
- local function testC(...)
- print("test C")
- print(...)
- error()
- end
- term.clear()
- term.setCursorPos(1,1)
- local win = net.newWindow()
- local list1 = net.listBox(win,{"option 1","options 2","options 3","options 4"},testC,2,6,10,10,"lightGray","gray")
- local list2 = net.listBox(win,{"option 1","options 2","options 3","options 4"},testC,13,6,10,10,"lightGray","gray")
- local list3 = net.listBox(win,{"option 1","options 2","options 3","options 4"},testC,24,6,10,10,"lightGray","gray")
- local list4 = net.listBox(win,{"option 1","options 2","options 3","options 4"},testC,35,6,10,10,"lightGray","gray")
- local function testA()
- list4("clear")
- end
- local function testB()
- list4("add","hello")
- end
- local button1 = net.button(win,"[button1]",testA,2,2,"lime","green")
- local button2 = net.button(win,"[button2]",testB,2,4,"lime","green")
- local button3 -- needs to be declared before chicken or egg probblem
- local count = 1
- local function testC()
- count = count + 1
- button3("set","[click "..tostring(count).."]")
- end
- button3 = net.button(win,"[button3]",testC,12,2,"lime","green")
- net.run(win)
- ]]--
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement