Advertisement
DerriDaX

GUI

Aug 4th, 2015
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.29 KB | None | 0 0
  1. local button = {
  2.   button_defaults = {
  3.     __index = {
  4.       color_bg  = colors.orange;
  5.       color_cl  = colors.blue;
  6.       color_txt = colors.black;
  7.  
  8.       height = 3;
  9.       padding = 2;
  10.       isClicked = false;
  11.     };
  12.   };
  13.  
  14.     mt = {
  15.     __call = function(self)
  16.       for index, btn in pairs(self.buttons) do
  17.         local color = btn.isClicked and btn.color_cl or btn.color_bg
  18.         term.setBackgroundColor(color)
  19.         term.setTextColor(btn.color_txt)
  20.         for yPos = btn.y, btn.bounds.y2 do
  21.           term.setCursorPos(btn.x ,yPos)
  22.           term.write(string.rep(" ",btn.width))
  23.         end
  24.         local text = btn.isClicked and btn.clickText or btn.text
  25.         term.setCursorPos(btn.x + (btn.width/2-#btn.text/2),btn.y + (btn.height/2))
  26.     end;
  27.  
  28.     __newindex = function(t,k,v)
  29.       assert(type(value)=="table","Requires a table")
  30.       assert(value.x,"Requires initial x")
  31.       assert(value.y,"Requires initial y")
  32.       assert(value.text,"Requires text value")
  33.       setmetatable(value,t.button_defaults)
  34.       value.width = #value.text + (value.padding * 2)
  35.       value.bounds = {
  36.         x1 = value.x;
  37.         y1 = value.y;
  38.         x2 = value.x + value.width - 1;
  39.         y2 = value.y + value.height - 1;
  40.       }
  41.  
  42.       rawset(t.button, key, value)
  43.     end;
  44.   };
  45.  
  46.   checkClick = function(self,x,y)
  47.     for index, btn in pairs(self.buttons) do
  48.       if x>=btn.x and x<=btn.bounds.x2 and y>=btn.y and y<=btn.bounds.y2 then
  49.         btn.isClicked = true
  50.         if btn.onClick then
  51.           btn:onClick()
  52.         end
  53.         return index
  54.       end
  55.     end
  56.   end;
  57.   buttons = {};
  58. }
  59. setmetatable(button,button.mt)
  60.  
  61. button[1] = {
  62.   x = 1;
  63.   y = 1;
  64.   text = "This is a button";
  65.   clickText = "You just clicked me!"
  66. }
  67.  
  68. button[2] = {
  69.   x = 5;
  70.   y = 5;
  71.   height = 5;
  72.   text = "Another button";
  73.   clickText = "CLICLED ME!!";
  74.   onClick = function()
  75.    
  76.   end;
  77. }
  78.  
  79. local timer = {
  80.   index = false;
  81.   timer = false;
  82. }
  83.  
  84. while true do
  85.   button()
  86.   local e = {os.pullEvent()}
  87.   if e[1] == "mouse_click" then
  88.     local index = button:checkClick(e[3],e[4])
  89.     timer.index = index
  90.     timer.timer = os.startTimer(1)
  91.   elseif e[1] == "timer" and e[2] == timer.timer then
  92.     button.buttons[timer.index].isClicked = false
  93.     timer = {}
  94.   end
  95. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement