Guest User

ComputerCraft - Button Tutorial (with video)

a guest
Apr 19th, 2013
270
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.87 KB | None | 0 0
  1. --[[
  2. You are free to use and distribute this code as your own: I claim no rights to it
  3.  
  4. This tutorial has a video associated with it, which can be found here: http://youtu.be/ppZMCAF7cWw
  5. ]]
  6. local button = { --Our main button table. Contains everything from button draw functions to the button information.
  7.   button_defaults = { --This is the metatable which we give set new buttons to. It provides color/size defaults.
  8.     __index = {
  9.       color_bg = colors.orange;
  10.       color_cl = colors.blue;
  11.       color_txt = colors.black;
  12.      
  13.       height = 3;
  14.       padding = 2;
  15.       isClicked = false;
  16.     };
  17.   };
  18.   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.
  19.     __call = function(self) --This allows us to call the table as if it were a function.
  20.       for index, btn in pairs(self.buttons) do
  21.         local color = btn.isClicked and btn.color_cl or btn.color_bg
  22.         term.setBackgroundColor(color)
  23.         term.setTextColor(btn.color_txt)
  24.         for yPos = btn.y, btn.bounds.y2 do
  25.           term.setCursorPos(btn.x, yPos)
  26.           term.write(string.rep(" ", btn.width))
  27.         end
  28.         local text = btn.isClicked and btn.clickText or btn.text
  29.         term.setCursorPos(btn.x + (btn.width/2 - #text/2), btn.y + (btn.height/2))
  30.         term.write(text)
  31.       end
  32.     end;
  33.    
  34.     __newindex = function(t, key, value) --This changes the behavior of the table upon adding a new button
  35.       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
  36.       assert(value.x, "Requires initial x")
  37.       assert(value.y, "Requires initial y")
  38.       assert(value.text, "Requires text value")
  39.       setmetatable(value, t.button_defaults) --Give our new button its defaults with the __index metamethod
  40.       value.width = #value.text + (value.padding * 2)
  41.       value.bounds = {
  42.         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)
  43.         y1 = value.y;
  44.         x2 = value.x + value.width - 1; --In order to draw and detect clicks correctly, you need to subtract 1 from the width and height.
  45.         y2 = value.y + value.height - 1;
  46.       }
  47.       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)
  48.     end;
  49.   };
  50.  
  51.   checkClick = function(self, x,y) --This checks whether you have actually clicked on a table
  52.     for index, btn in pairs(self.buttons) do
  53.       if x>=btn.x and x<=btn.bounds.x2 and y>=btn.y and y<=btn.bounds.y2 then
  54.         btn.isClicked = true --If we have actually clicked the button then set its click value to true
  55.         if btn.onClick then --And check if it has an onClick function
  56.           btn:onClick() --If so, then execute it and pass the button's table/info into it by using the colon operator
  57.         end
  58.         return index --Return the index of the button so we can unhighlight it
  59.       end
  60.     end
  61.   end;
  62.  
  63.   buttons = {}; --This is the table that we'll keep all the buttons in
  64.  
  65. }
  66. setmetatable(button, button.mt) --Set the metatable of button to button.mt
  67.  
  68. button[1] = {
  69.   x = 1;
  70.   y = 1;
  71.   text = "This is a button";
  72.   clickText = "You just clicked me";
  73. }
  74.  
  75. button[2] = {
  76.   x = 5;
  77.   y = 5;
  78.   height = 5;
  79.   text = "Another button";
  80.   clickText = "Clicked.";
  81.   onClick = function(self) --What is performed upon clicking the button
  82.     term.setBackgroundColor(colors.black)
  83.     term.setCursorPos(1,1)
  84.     term.write("New button title: ")
  85.     local input = read()
  86.     self.text = input --Set the button[2] text to input
  87.   end;
  88. }
  89.  
  90. local timer = { --This will keep track of clicked buttons/the timers associated with them
  91.   index = false;
  92.   timer = false;
  93. }
  94. while true do
  95.   button() --Always draw the button first
  96.   local e = {os.pullEvent()} --Then pull our events
  97.   if e[1] == "mouse_click" then
  98.     local index = button:checkClick(e[3], e[4]) --Check the click: make sure to pass the button table into the checkClick function
  99.     timer.index = index --The index of the button that is clicked
  100.     timer.timer = os.startTimer(1) --Start the timer itself and store its ID inside the timer.timer variable
  101.   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
  102.     button.buttons[timer.index].isClicked = false --Deselect the button
  103.     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
  104.   end
  105. end
Advertisement
Add Comment
Please, Sign In to add comment