Guest User

Radio

a guest
Jul 20th, 2014
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.84 KB | None | 0 0
  1. local button = {
  2.   buton_defaults = {
  3.     __index = {
  4.       color_bg = colors.gray;
  5.       color_cl = colors.red;
  6.       color_txt = colors.black
  7.       height = 3;
  8.       padding = 2;
  9.       isClicked = false;
  10.     };
  11.   };
  12.   mt = {
  13.     __call = function(self)
  14.       for index, btn in pairs(self.buttons) do
  15.         local color = btn.isClicked and btn.color_cl or btn.color_bg
  16.         term.setBackgroundColor(color)
  17.         term.setTextColor(color_txt)
  18.         for yPos = btn.y, btn.bounds.y2 do
  19.           term.setCursorPos(btn.x, yPos)
  20.           term.write(string.rep(" ", btn.width)
  21.         end
  22.         local text = btn.isClicked and btn.clickText or btn.text
  23.         term.setCursorPos(btn.x + (btn.width/2 - #text/2), btn.y + (btn.height/2))
  24.         term.write(text)
  25.         end
  26.     end;
  27.     __newindex = function(t, key, value)
  28.       assert(type(value)=="table", "Requires a table")
  29.       assert(value.x, "Requires initial x")
  30.       assert(value.y, "Requires initial y")
  31.       assert(value.text, "Requires text value")
  32.       setmetatable(value, t.button_defaults)
  33.       value.width = #text + (value.padding *2)
  34.       value.bounds = {
  35.         x1 = value.x;
  36.         y1 = value.y;
  37.         x2 = value.x + value.width - 1;
  38.         y2 = value.y + value.height - 1;
  39.       }
  40.       t.buttons[key]=value  
  41.     end;
  42.   };
  43.  
  44.   checkClick = function(self, x,y)
  45.     for index, btn in pairs(self.button) do
  46.       if x>=btn.x and x<=btn.bounds.x2 and y>=btn.y and y<=btn.bounds.y2 then
  47.         btn.isClicked = true
  48.         if btn.onClick then
  49.           btn:onClick()
  50.         end
  51.         return index
  52.       end
  53.     end
  54.   end;
  55.  
  56.   buttons = {};
  57.  
  58. }
  59. setmetatable(button, button.mt)
  60.  
  61. button[1] = {
  62.   x = 5;
  63.   y = 3;
  64.   text = "On"
  65.   clickText = "Working...";
  66.   onClick = function(self)
  67.     if Power == 0 then
  68.       self.text = "Off"
  69.       Power = 1  
  70.     else
  71.       self.text = "On"
  72.       Power = 0
  73.     end
  74.   end;
  75. }
  76. button[2] = {
  77.   x = 5
  78.   y = 5
  79.   text = "Volume +"
  80.   onClick = function(self)
  81.     Volume + 1
  82.   end;
  83. }
  84. button [3] = {
  85.   x = 5
  86.   y = 6
  87.   text = "Volume -"
  88.   onClick = function(self)
  89.     Volume - 1
  90.   end;
  91. }
  92. local timer = {
  93.   index = false
  94.   timer = false
  95. }
  96. Power = 0
  97. Volume = 5
  98.  
  99. while true do
  100.   button()
  101.   local e = {os.pullEvent()}
  102.   if e[1] == "mouse_click" then
  103.     local index = mouse.checkClick(e[3], e[4])
  104.     if index then
  105.       timer.index = index
  106.       timer.timer = os.startTimer
  107.     end
  108.   elseif e[1] == "timer" and e[2] == timer.timer then
  109.     button.buttons[timer.index].isClicked = false
  110.     timer = {}
  111.   end
  112.   if Power == 1 then
  113.     if Volume == 1 then
  114.       redstone.setAnalogOutput("left", 1)
  115.     elseif Volume == 2 then
  116.       redstone.setAnalogOutput("left", 2)
  117.     elseif Volume == 3 then
  118.       redstone.setAnalogOutput("left", 3)
  119.     elseif Volume == 4 then
  120.       redstone.setAnalogOutput("left", 4)
  121.     elseif Volume == 5 then
  122.       redstone.setAnalogOutput("left", 5)
  123.     elseif Volume == 6 then
  124.       redstone.setAnalogOutput("left", 6)
  125.     elseif Volume == 7 then
  126.       redstone.setAnalogOutput("left", 7)
  127.     elseif Volume == 8 then
  128.       redstone.setAnalogOutput("left", 8)
  129.     elseif Volume == 9 then
  130.       redstone.setAnalogOutput("left", 9)
  131.     elseif Volume == 10 then
  132.       redstone.setAnalogOutput("left", 10)
  133.     elseif Volume == 11 then
  134.       redstone.setAnalogOutput("left", 11)
  135.     elseif Volume == 12 then
  136.       redstone.setAnalogOutput("left", 12)
  137.     elseif Volume == 13 then
  138.       redstone.setAnalogOutput("left", 13)    
  139.     elseif Volume == 14 then
  140.       redstone.setAnalogOutput("left", 14)
  141.     elseif Volume == 15 then
  142.       redstone.setAnalogOutput("left", 15)
  143.     elseif Volume == 16 then
  144.       redstone.setAnalogOutput("left", 16)
  145.     elseif Volume == 0 then
  146.       Volume = 1
  147.     elseif Volume == 17 then
  148.       Volume = 16
  149.     end
  150.   end
  151. end
Advertisement
Add Comment
Please, Sign In to add comment