Advertisement
Theshadow989

Clickable Buttons

Jul 7th, 2016
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.63 KB | None | 0 0
  1. local button = { --Our main button table. Contains everything from button draw functions to the button information.
  2.   button_defaults = { --This is the metatable which we give set new buttons to. It provides color/size 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.   mt = { --This is the main metatable of the button table. It changes the behavior of the table to allow for calling and adding new indexes.
  14.     __call = function(self) --This allows us to call the table as if it were a function.
  15.       for index, btn in pairs(self.buttons) do
  16.         local color = btn.isClicked and btn.color_cl or btn.color_bg
  17.         term.setBackgroundColor(color)
  18.         term.setTextColor(btn.color_txt)
  19.         for yPos = btn.y, btn.bounds.y2 do
  20.           term.setCursorPos(btn.x, yPos)
  21.           term.write(string.rep(" ", btn.width))
  22.         end
  23.         local text = btn.isClicked and btn.clickText or btn.text
  24.         term.setCursorPos(btn.x + (btn.width/2 - #text/2), btn.y + (btn.height/2))
  25.         term.write(text)
  26.       end
  27.     end;
  28.    
  29.     __newindex = function(t, key, value) --This changes the behavior of the table upon adding a new button
  30.       assert(type(value)=="table", "Requires a table") --assert will check that a condition is true; if it is not, it will error with the provided text
  31.       assert(value.x, "Requires initial x")
  32.       assert(value.y, "Requires initial y")
  33.       assert(value.text, "Requires text value")
  34.       setmetatable(value, t.button_defaults) --Give our new button its defaults with the __index metamethod
  35.       value.width = #value.text + (value.padding * 2)
  36.       value.bounds = {
  37.         x1 = value.x; --I don't use the x1 or y1 vars from the bounds table due to the fact that it's shorter to simply type btn.x than btn.bounds.x, but they are equal to the same thing (obviously)
  38.         y1 = value.y;
  39.         x2 = value.x + value.width - 1; --In order to draw and detect clicks correctly, you need to subtract 1 from the width and height.
  40.         y2 = value.y + value.height - 1;
  41.       }
  42.       t.buttons[key]=value --In the video, I am aware that I used rawset. However, it is actually not necessary because we are not changing the button table directly, but rather the button.buttons table (which has no newindex metamethod)
  43.     end;
  44.   };
  45.  
  46.   checkClick = function(self, x,y) --This checks whether you have actually clicked on a table
  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 --If we have actually clicked the button then set its click value to true
  50.         if btn.onClick then --And check if it has an onClick function
  51.           btn:onClick() --If so, then execute it and pass the button's table/info into it by using the colon operator
  52.         end
  53.         return index --Return the index of the button so we can unhighlight it
  54.       end
  55.     end
  56.   end;
  57.  
  58.   buttons = {}; --This is the table that we'll keep all the buttons in
  59.  
  60. }
  61. setmetatable(button, button.mt) --Set the metatable of button to button.mt
  62.  
  63. button[1] = {
  64.   x = 1;
  65.   y = 1;
  66.   text = "This is a button";
  67.   clickText = "You just clicked me";
  68. }
  69.  
  70. button[2] = {
  71.   x = 5;
  72.   y = 5;
  73.   height = 5;
  74.   text = "Another button";
  75.   clickText = "Clicked.";
  76.   onClick = function(self) --What is performed upon clicking the button
  77.     term.setBackgroundColor(colors.black)
  78.     term.setCursorPos(1,1)
  79.     term.write("New button title: ")
  80.     local input = read()
  81.     self.text = input --Set the button[2] text to input
  82.   end;
  83. }
  84.  
  85. local timer = { --This will keep track of clicked buttons/the timers associated with them
  86.   index = false;
  87.   timer = false;
  88. }
  89. while true do
  90.   button() --Always draw the button first
  91.   local e = {os.pullEvent()} --Then pull our events
  92.   if e[1] == "mouse_click" then
  93.     local index = button:checkClick(e[3], e[4]) --Check the click: make sure to pass the button table into the checkClick function
  94.     if index then
  95.       timer.index =  index--The index of the button that is clicked
  96.       timer.timer = os.startTimer(1)
  97.     end
  98.   elseif e[1] == "timer" and e[2] == timer.timer then --If we get a timer event and the ID is equal to the timer.timer var then
  99.     button.buttons[timer.index].isClicked = false --Deselect the button
  100.     timer = {} --This is actually fairly memory inneficient, but for such a small program it doesn't really matter. Rather than do this though, you should probably just manually set the values of timer.index/timer.timer to false
  101.   end
  102. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement