Advertisement
Corona

[Computercraft] Context Menu

Jan 1st, 2014
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.46 KB | None | 0 0
  1. os.loadAPI(shell.dir().."/screen")
  2.  
  3. local mo1 = {x1=0,x2=0,y1=0,y2=0,color=colors.lime,text="green",handle= function(ca) term.setBackgroundColor(colors.lime) shell.run('clear') end}
  4. ca = false
  5. local ah
  6.  
  7. function def()
  8. mo2 = {x1=mo1.x1,x2=mo1.x2,y1=mo1.y2+1,y2=mo1.y2+1,color=colors.blue,text="blue",handle= function(ca) term.setBackgroundColor(colors.blue) shell.run('clear') end}
  9. mo3 = {x1=mo1.x1,x2=mo1.x2,y1=mo2.y2+1,y2=mo2.y2+1,color=colors.purple,text="clear",handle= function(ca) term.setBackgroundColor(colors.black) shell.run('clear') end}
  10. mo4 = {x1=mo1.x1,x2=mo1.x2,y1=mo3.y2+1,y2=mo3.y2+1,color=colors.red,text="quit",handle= function() term.setCursorPos(1,1) error('Quitted Program') end}
  11. contextmenu = {mo1,mo2,mo3,mo4}
  12. end
  13.  
  14. function drawMenu()
  15.   for i = 1,#contextmenu do
  16.     screen.drawButton(contextmenu[i])
  17.   end
  18. end
  19.  
  20.  
  21. while true do
  22. if ah ~= nil then
  23.   contextmenu[ah].handle(ca)
  24. end
  25. if ca then
  26.   drawMenu()
  27. end
  28. local ev,btn,x,y = os.pullEvent("mouse_click")
  29. if btn == 2 then
  30.   screen.cls()
  31.   mo1.x1 = x
  32.   mo1.x2 = x+5
  33.   mo1.y1 = y
  34.   mo1.y2 = y
  35.   ca = true
  36.   def()
  37.   drawMenu()
  38. elseif btn == 1 then
  39.   local tp = false
  40.   if not ca then
  41.     screen.cls()
  42.   else
  43.     screen.cls()
  44.     for i = 1,#contextmenu do
  45.       if screen.pressed(contextmenu[i],x,y) then
  46.         ah = i
  47.         contextmenu[i].handle(ca)
  48.         ca = true
  49.         tp = true
  50.       end
  51.     end
  52.     if not tp then
  53.       ca = false
  54.     end
  55.   end
  56. end
  57. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement