Advertisement
Redxone

[RedMenu] - SuperSimpleMenuAPI

Jul 28th, 2015
253
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.40 KB | None | 0 0
  1. function createMenu(sx,sy,type)
  2.     if(not sx or not sy or not type)then
  3.         error("Argumets invalid - start x, starty, type - valid types are (mouse and key)")
  4.     end
  5.  
  6.     menu = {
  7.         mtype = type,
  8.         x=sx,
  9.         y=sy,
  10.         selop = 1,
  11.  
  12.         options = {
  13.         },
  14.  
  15.         addOption = function(self,name,textcolor,backcolor,selectedtextcolor,selectedbackcolor,mfunction)
  16.        
  17.             self.options[#self.options+1] = {name=name,textcolor=textcolor,backcolor=backcolor,selectedtextcolor=selectedtextcolor,selectedbackcolor=selectedbackcolor,func=mfunction}
  18.        
  19.         end,
  20.  
  21.         update = function(self)
  22.             if(#self.options <= 0)then
  23.                 error("Menu empty! run <menu>:addOption(option) ")
  24.             end
  25.  
  26.             if(self.mtype == "mouse")then
  27.                 for i = 1, #self.options do
  28.                         term.setCursorPos(sx,sy+i)
  29.                         term.setTextColor(self.options[i].textcolor)
  30.                         term.setBackgroundColor(self.options[i].backcolor)
  31.                         print("  "..self.options[i].name.."  ")
  32.                 end
  33.  
  34.             elseif(self.mtype == "key")then
  35.                 for i = 1, #self.options do
  36.                     if(self.selop == i)then
  37.                         term.setCursorPos(sx,sy+i)
  38.                         term.setTextColor(self.options[i].selectedtextcolor)
  39.                         term.setBackgroundColor(self.options[i].selectedbackcolor)
  40.                         print("> "..self.options[i].name.." <")
  41.                     else
  42.                         term.setCursorPos(sx,sy+i)
  43.                         term.setTextColor(self.options[i].textcolor)
  44.                         term.setBackgroundColor(self.options[i].backcolor)
  45.                         print("  "..self.options[i].name.."  ")
  46.                     end
  47.                 end
  48.             end
  49.  
  50.             a = {os.pullEvent()}
  51.  
  52.             if(self.mtype == "mouse" and a[1] == "mouse_click" and a[2] == 1)then
  53.                 for i = 1, #self.options do
  54.                     if(a[3] >= self.x and a[3] <= self.x+(#self.options[i].name)+2 and a[4] == math.floor(self.y+i))then
  55.                         term.setCursorPos(sx,sy+i)
  56.                         term.setTextColor(self.options[i].selectedtextcolor)
  57.                         term.setBackgroundColor(self.options[i].selectedbackcolor)
  58.                         print("  "..self.options[i].name.."  ")
  59.                         sleep(0.3)
  60.                         self.options[i].func()
  61.                     --else
  62.                         --error(a[3].."|"..self.x.."| "..self.x+(#self.options[i].name+2).."|"..a[4].."|"..(self.y+i) )
  63.                     end
  64.                 end
  65.  
  66.  
  67.             elseif(self.mtype == "key" and a[1] == "key")then
  68.                 if(a[2] == keys.up and self.selop > 1)then self.selop = self.selop - 1 end
  69.                 if(a[2] == keys.down and self.selop < (#self.options))then self.selop = self.selop + 1 end
  70.                 if(a[2] == keys.enter)then self.options[self.selop].func() end
  71.             end
  72.  
  73.         end,
  74.  
  75.     }
  76.  
  77.     return menu
  78.  
  79. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement