Advertisement
BigSHinyToys

[Computer Craft] buttons .NET kinda

Nov 27th, 2012
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.82 KB | None | 0 0
  1. --[[
  2.         Experement .net kinda
  3.         by Big SHiny Toys
  4. ]]--
  5. local net = {}
  6.  
  7. function net.newWindow()
  8.     return {}
  9. end
  10.  
  11. function net.run(tWin)
  12.     local function draw()
  13.         for k,v in pairs(tWin) do
  14.             coroutine.resume(v,"redraw")
  15.         end
  16.     end
  17.     local function checkForClick(clickX,clickY)
  18.         for k,v in pairs(tWin) do
  19.             local test,posX,posY,sizX,sizY = coroutine.resume(v,"box")
  20.             if clickX >= posX and clickX < posX + sizX and clickY >= posY and clickY < posY + sizY then
  21.                 local test,func = coroutine.resume(v,"func")
  22.                 func()
  23.             end
  24.         end
  25.     end
  26.     draw()
  27.     draw()
  28.     while true do
  29.         local event = {os.pullEvent()}
  30.         if event[1] == "redraw" then
  31.             draw()
  32.         elseif event[1] == "mouse_click" then
  33.             if event[2] == 1 then -- left click
  34.                 checkForClick(event[3],event[4])
  35.             end
  36.         end
  37.     end
  38. end
  39.  
  40. function net.button(windo,label,func,posX,posY,tCol,bCol)
  41.     local myX,myY = posX,posY
  42.     table.insert(windo,
  43.         coroutine.create(
  44.             function()
  45.                 local function draw()
  46.                     term.setCursorPos(myX,myY)
  47.                     term.setBackgroundColor(colors[tCol] or tCol)
  48.                     term.setTextColor(colors[bCol] or bCol)
  49.                     term.write(label)
  50.                 end
  51.                 local retdata = {}
  52.                 while true do
  53.                     local event = {coroutine.yield(unpack(retdata))}
  54.                     if event[1] == "redraw" then
  55.                         draw()
  56.                         retdata = {}
  57.                     elseif event[1] == "box" then
  58.                         retdata = {myX,myY,#label,1}
  59.                     elseif event[1] == "func" then
  60.                         retdata = {func}
  61.                     end
  62.                 end
  63.             end
  64.         )
  65.     )
  66. end
  67.  
  68. -- test
  69. local function x()
  70.     print("X")
  71.     error()
  72. end
  73.  
  74. local function y()
  75.     print("Y")
  76.     error()
  77. end
  78.  
  79. term.setTextColor(colors.white)
  80. term.setBackgroundColor(colors.black)
  81. term.clear()
  82. term.setCursorPos(1,1)
  83.  
  84. local win = net.newWindow()
  85. net.button(win,"option1",x,3,3,"white","black")
  86. net.button(win,"option2",y,3,5,"white","black")
  87. net.run(win)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement