Advertisement
BigSHinyToys

[Computer Craft] form elements buttons lists

Dec 6th, 2012
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.29 KB | None | 0 0
  1. --[[
  2.         second attempt at a net like frame work
  3.         by Big SHiny Toys
  4. ]]--
  5.  
  6. local function inBouwndry(clickX,clickY,boxX,boxY,width,hight)
  7.     return ( clickX >= boxX and clickX < boxX + width and clickY >= boxY and clickY < boxY + hight )
  8. end
  9.  
  10. local net = {}
  11.  
  12. function net.newWindow()
  13.     return {components = {},drawFlag = true}
  14. end
  15.  
  16. function net.run(tWindo)
  17.     local function redraw()
  18.         for k,v in pairs(tWindo.components) do
  19.             v("redraw")
  20.         end
  21.     end
  22.     redraw()
  23.     while true do
  24.         event = {os.pullEvent()}
  25.         if event[1] == "redraw" then
  26.             redraw()
  27.         elseif event[1] == "mouse_click" then
  28.             if event[2] == 1 then -- left click
  29.                 for k,v in pairs(tWindo.components) do
  30.                     if inBouwndry(event[3],event[4],v("box")) then
  31.                         v("call",event[3],event[4])
  32.                     end
  33.                 end
  34.             end
  35.         elseif event[1] == "mouse_scroll" then
  36.             for k,v in pairs(tWindo.components) do
  37.                 if inBouwndry(event[3],event[4],v("box")) then
  38.                     v("scroll",event[2])
  39.                 end
  40.             end
  41.         end
  42.         if tWindo.drawFlag then
  43.             redraw()
  44.             tWindo.drawFlag = false
  45.         end
  46.     end
  47. end
  48.  
  49. local button_meta = {
  50.     __call = function(tButton,ins,value)
  51.         if ins == "redraw" then
  52.             term.setBackgroundColor(colors[tButton.bgColor] or tButton.bgColor)
  53.             term.setTextColor(colors[tButton.textColor] or tButton.textColor)
  54.             term.setCursorPos(tButton.posX,tButton.posY)
  55.             term.write(tButton.sText)
  56.         elseif ins == "box" then
  57.             return tButton.posX,tButton.posY,#tButton.sText,1
  58.         elseif ins == "call" then
  59.             return tButton.buttonFunction()
  60.         elseif ins == "set" then
  61.             tButton.sText = value
  62.             tButton.drawFlag()
  63.         end
  64.     end
  65. }
  66.  
  67. function net.button(window,label,func,posX,posY,tCol,bCol)
  68.     local tempButton =  {
  69.         posY = posY,
  70.         posX = posX,
  71.         buttonFunction = func,
  72.         sText = label,
  73.         textColor = tCol,
  74.         bgColor = bCol,
  75.         drawFlag = function() window.drawFlag = true end
  76.     }
  77.     setmetatable(tempButton,button_meta)
  78.     table.insert(window.components,tempButton)
  79.     return tempButton
  80. end
  81.  
  82. local list_meta = {
  83.     __call = function(tButton,ins,clickX,clickY)
  84.         if ins == "redraw" then
  85.                 term.setBackgroundColor(colors[tButton.bgColor] or tButton.bgColor)
  86.                 term.setTextColor(colors[tButton.textColor] or tButton.textColor)
  87.             for i = 1,tButton.hight do
  88.                 term.setCursorPos(tButton.posX,tButton.posY+i-1)
  89.                 term.write(string.sub((tButton.tList[i + tButton.offset] or "")..string.rep(" ",tButton.width),1,tButton.width))
  90.             end
  91.         elseif ins == "box" then
  92.             return tButton.posX,tButton.posY,tButton.width,tButton.hight
  93.         elseif ins == "call" then
  94.             local item = tButton.tList[(clickY - tButton.posY) + tButton.offset +1]
  95.             if item then
  96.                 return tButton.buttonFunction(item)
  97.             end
  98.         elseif ins == "clear" then
  99.             tButton.tList = {}
  100.             tButton.offset = 0
  101.             tButton.drawFlag()
  102.         elseif ins == "add" then
  103.             if clickX then
  104.                 table.insert(tButton.tList,clickX)
  105.             end
  106.             tButton.drawFlag()
  107.         elseif ins == "scroll" then
  108.             tButton.offset = tButton.offset + (clickX or 0)
  109.             tButton.drawFlag()
  110.         elseif ins == "set" then
  111.             tButton.tList = clickX
  112.             tButton.offset = 0
  113.             tButton.drawFlag()
  114.         end
  115.     end
  116. }
  117.  
  118. function net.listBox(window,list,func,posX,posY,width,hight,tCol,bCol)
  119.     local tempList =    {
  120.         posY = posY,
  121.         posX = posX,
  122.         buttonFunction = func,
  123.         sText = label,
  124.         textColor = tCol,
  125.         bgColor = bCol,
  126.         width = width,
  127.         hight = hight,
  128.         offset = 0,
  129.         tList = list,
  130.         drawFlag = function() window.drawFlag = true end
  131.     }
  132.     setmetatable(tempList,list_meta)
  133.     table.insert(window.components,tempList)
  134.     return tempList
  135. end
  136.  
  137. -- end api
  138. term.setBackgroundColor(colors.black)
  139. term.setTextColor(colors.white)
  140. term.clear()
  141. term.setCursorPos(1,1)
  142.  
  143. local win = net.newWindow()
  144. local files
  145. local path = {}
  146.  
  147. local function sPath()
  148.     return table.concat(path,"/")
  149. end
  150.  
  151. local function newlist()
  152.     local test,this = pcall(fs.list,sPath())
  153.     return (this or {})
  154. end
  155.  
  156. local function goBack()
  157.     table.remove(path)
  158.     files("set",newlist())
  159. end
  160.  
  161. local function goForward(step)
  162.     table.insert(path,step)
  163.     files("set",newlist())
  164. end
  165.  
  166. files = net.listBox(win,newlist(),goForward,1,3,40,10,"lightGray","gray")
  167. net.button(win,"[back]",goBack,1,2,"lime","green")
  168.  
  169. net.run(win)
  170.  
  171. --[[  -- fully functional test 1
  172.  
  173. local function testC(...)
  174.     print("test C")
  175.     print(...)
  176.     error()
  177. end
  178.  
  179. term.clear()
  180. term.setCursorPos(1,1)
  181. local win = net.newWindow()
  182. local list1 = net.listBox(win,{"option 1","options 2","options 3","options 4"},testC,2,6,10,10,"lightGray","gray")
  183. local list2 = net.listBox(win,{"option 1","options 2","options 3","options 4"},testC,13,6,10,10,"lightGray","gray")
  184. local list3 = net.listBox(win,{"option 1","options 2","options 3","options 4"},testC,24,6,10,10,"lightGray","gray")
  185. local list4 = net.listBox(win,{"option 1","options 2","options 3","options 4"},testC,35,6,10,10,"lightGray","gray")
  186.  
  187. local function testA()
  188.     list4("clear")
  189. end
  190.  
  191. local function testB()
  192.     list4("add","hello")
  193. end
  194.  
  195. local button1 = net.button(win,"[button1]",testA,2,2,"lime","green")
  196. local button2 = net.button(win,"[button2]",testB,2,4,"lime","green")
  197.  
  198. local button3 -- needs to be declared before chicken or egg probblem
  199. local count = 1
  200. local function testC()
  201.     count = count + 1
  202.     button3("set","[click "..tostring(count).."]")
  203. end
  204. button3 = net.button(win,"[button3]",testC,12,2,"lime","green")
  205.  
  206. net.run(win)
  207. ]]--
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement