Advertisement
BigSHinyToys

[Computer Craft].net API

Jan 11th, 2013
284
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.74 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 win_meta = {
  11.     __call = function(tWin,ins)
  12.         if ins == "kill" then
  13.             tWin.run = false
  14.         elseif ins == "redraw" then
  15.             tWin.drawFlag = true
  16.         end
  17.     end
  18. }
  19.  
  20. function newWindow()
  21.     local tempWin = {
  22.         components = {},
  23.         drawFlag = true,
  24.         run = true
  25.     }
  26.     setmetatable(tempWin,win_meta)
  27.     return tempWin
  28. end
  29.  
  30. function run(tWindo)
  31.     local function redraw()
  32.         for k,v in pairs(tWindo.components) do
  33.             v("redraw")
  34.         end
  35.     end
  36.     redraw()
  37.     while tWindo.run do
  38.         event = {os.pullEvent()}
  39.         if event[1] == "redraw" then
  40.             redraw()
  41.         elseif event[1] == "mouse_click" then
  42.             if event[2] == 1 then -- left click
  43.                 for k,v in pairs(tWindo.components) do
  44.                     if inBouwndry(event[3],event[4],v("box")) then
  45.                         v("call",event[3],event[4])
  46.                     end
  47.                 end
  48.             end
  49.         elseif event[1] == "mouse_scroll" then
  50.             for k,v in pairs(tWindo.components) do
  51.                 if inBouwndry(event[3],event[4],v("box")) then
  52.                     v("scroll",event[2])
  53.                 end
  54.             end
  55.         end
  56.         if tWindo.drawFlag then
  57.             redraw()
  58.             tWindo.drawFlag = false
  59.         end
  60.     end
  61. end
  62.  
  63. local button_meta = {
  64.     __call = function(tButton,ins,value)
  65.         if ins == "redraw" then
  66.             term.setBackgroundColor(colors[tButton.bgColor] or tButton.bgColor)
  67.             term.setTextColor(colors[tButton.textColor] or tButton.textColor)
  68.             term.setCursorPos(tButton.posX,tButton.posY)
  69.             term.write(tButton.sText)
  70.         elseif ins == "box" then
  71.             return tButton.posX,tButton.posY,#tButton.sText,1
  72.         elseif ins == "call" then
  73.             return tButton.buttonFunction()
  74.         elseif ins == "set" then
  75.             tButton.sText = value
  76.             tButton.drawFlag()
  77.         end
  78.     end
  79. }
  80.  
  81. function button(window,label,func,posX,posY,tCol,bCol)
  82.     local tempButton =  {
  83.         posY = posY,
  84.         posX = posX,
  85.         buttonFunction = func,
  86.         sText = label,
  87.         textColor = tCol,
  88.         bgColor = bCol,
  89.         drawFlag = function() window.drawFlag = true end
  90.     }
  91.     setmetatable(tempButton,button_meta)
  92.     table.insert(window.components,tempButton)
  93.     return tempButton
  94. end
  95.  
  96. local list_meta = {
  97.     __call = function(tButton,ins,clickX,clickY)
  98.         if ins == "redraw" then
  99.                 term.setBackgroundColor(colors[tButton.bgColor] or tButton.bgColor)
  100.                 term.setTextColor(colors[tButton.textColor] or tButton.textColor)
  101.             for i = 1,tButton.hight do
  102.                 term.setCursorPos(tButton.posX,tButton.posY+i-1)
  103.                 term.write(string.sub((tButton.tList[i + tButton.offset] or "")..string.rep(" ",tButton.width),1,tButton.width))
  104.             end
  105.         elseif ins == "box" then
  106.             return tButton.posX,tButton.posY,tButton.width,tButton.hight
  107.         elseif ins == "call" then
  108.             local item = tButton.tList[(clickY - tButton.posY) + tButton.offset +1]
  109.             if item then
  110.                 return tButton.buttonFunction(item)
  111.             end
  112.         elseif ins == "clear" then
  113.             tButton.tList = {}
  114.             tButton.offset = 0
  115.             tButton.drawFlag()
  116.         elseif ins == "add" then
  117.             if clickX then
  118.                 table.insert(tButton.tList,clickX)
  119.             end
  120.             tButton.drawFlag()
  121.         elseif ins == "scroll" then
  122.             tButton.offset = tButton.offset + (clickX or 0)
  123.             tButton.drawFlag()
  124.         elseif ins == "set" then
  125.             tButton.tList = clickX
  126.             tButton.offset = 0
  127.             tButton.drawFlag()
  128.         end
  129.     end
  130. }
  131.  
  132. function listBox(window,list,func,posX,posY,width,hight,tCol,bCol)
  133.     local tempList =    {
  134.         posY = posY,
  135.         posX = posX,
  136.         buttonFunction = func,
  137.         sText = label,
  138.         textColor = tCol,
  139.         bgColor = bCol,
  140.         width = width,
  141.         hight = hight,
  142.         offset = 0,
  143.         tList = list,
  144.         drawFlag = function() window.drawFlag = true end
  145.     }
  146.     setmetatable(tempList,list_meta)
  147.     table.insert(window.components,tempList)
  148.     return tempList
  149. end
  150.  
  151. -- end api
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement