Advertisement
BigSHinyToys

[Computer Craft] .NET attempt 2

Nov 28th, 2012
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.73 KB | None | 0 0
  1. --[[
  2.         second attempt at a net like frame work
  3.         by Big SHiny Toys
  4. ]]--
  5.  
  6. local net = {}
  7.  
  8. function net.newWindow()
  9.     return {}
  10. end
  11.  
  12. function net.run(tWindo)
  13.     local function inBouwndry(clickX,clickY,boxX,boxY,width,hight)
  14.         return ( clickX >= boxX and clickX < boxX + width and clickY >= boxY and clickY < boxY + hight )
  15.     end
  16.     local function redraw()
  17.         for k,v in pairs(tWindo) do
  18.             v("redraw")
  19.         end
  20.     end
  21.     redraw()
  22.     while true do
  23.         event = {os.pullEvent()}
  24.         if event[1] == "redraw" then
  25.             redraw()
  26.         elseif event[1] == "mouse_click" then
  27.             if event[2] == 1 then -- left click
  28.                 for k,v in pairs(tWindo) do
  29.                     if inBouwndry(event[3],event[4],v("box")) then
  30.                         v("call")()
  31.                     end
  32.                 end
  33.             end
  34.         end
  35.     end
  36. end
  37.  
  38. local button_meta = {
  39.     __call = function(tButton,ins)
  40.         if ins == "redraw" then
  41.             term.setBackgroundColor(colors[tButton.bgColor] or tButton.bgColor)
  42.             term.setTextColor(colors[tButton.textColor] or tButton.textColor)
  43.             term.setCursorPos(tButton.posX,tButton.posY)
  44.             term.write(tButton.sText)
  45.         elseif ins == "box" then
  46.             return tButton.posX,tButton.posY,#tButton.sText,1
  47.         elseif ins == "call" then
  48.             return tButton.buttonFunction
  49.         end
  50.     end
  51. }
  52.  
  53. function net.button(window,label,func,posX,posY,tCol,bCol)
  54.     local tempButton =  {
  55.         posY = posY,
  56.         posX = posX,
  57.         buttonFunction = func,
  58.         sText = label,
  59.         textColor = tCol,
  60.         bgColor = bCol
  61.     }
  62.     setmetatable(tempButton,button_meta)
  63.     table.insert(window,tempButton)
  64. end
  65.  
  66. local function testA()
  67.     print("test A")
  68.     error()
  69. end
  70.  
  71. local function testB()
  72.     print("test B")
  73.     error()
  74. end
  75.  
  76. local win = net.newWindow()
  77. net.button(win,"[button1]",testA,2,2,"lime","green")
  78. net.button(win,"[button2]",testB,2,4,"lime","green")
  79. net.run(win)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement