Advertisement
mikebald

Button API

Jun 3rd, 2014
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.28 KB | None | 0 0
  1.     local mon
  2.     local button = {}
  3.      
  4.     function clear()
  5.        button = {}
  6.     end
  7.      
  8.     function startup(monDirection)
  9.             mon = peripheral.wrap(monDirection)
  10.             mon.setTextScale(1)
  11.             mon.setTextColor(colors.white)
  12.             mon.setBackgroundColor(colors.black)
  13.     end
  14.      
  15.     function addButton(name, func, param, x, y, width, height, backColorInactive, backColorActive, textColorInactive, textColorActive)
  16.             if (backColorInactive == nil or backColorInactive == "") then
  17.                     backColorInactive = colors.red
  18.             end
  19.             if (backColorActive == nil or backColorActive == "") then
  20.                     backColorActive = colors.lime
  21.             end
  22.             if (textColorInactive == nil or textColorInactive == "") then
  23.                     textColorInactive = colors.white
  24.             end
  25.             if (textColorActive == nil or textColorInactive == "") then
  26.                     textColorActive = colors.white
  27.             end
  28.      
  29.             button[name] = {}
  30.             button[name]["func"] = func
  31.             button[name]["active"] = false
  32.             button[name]["enabled"] = true
  33.             button[name]["param"] = param
  34.             button[name]["xmin"] = x
  35.             button[name]["ymin"] = y
  36.             button[name]["xmax"] = x + width
  37.             button[name]["ymax"] = y + height
  38.             button[name]["bcolin"] = backColorInactive
  39.             button[name]["bcolac"] = backColorActive
  40.             button[name]["tcolin"] = textColorInactive
  41.             button[name]["tcolac"] = textColorActive
  42.     end
  43.      
  44.     -- This doesn't remove the button from any screen it's drawn on, it simply removes all functionality of the button and prevents it from being re-drawn.
  45.     function removeButton(name)
  46.             if (button[name] ~= nil) then
  47.                     button[name] = nil
  48.             end
  49.     end
  50.      
  51.     function renameButton(oldName, newName, reDraw)
  52.             local k,v
  53.            
  54.             button[newName] = {}
  55.            
  56.             for k,v in pairs(button[oldName]) do
  57.                     button[newName][k] = v
  58.             end
  59.            
  60.             button[oldName] = nil
  61.            
  62.             if (reDraw == true) then
  63.                     drawButton(newName)
  64.             end
  65.     end
  66.      
  67.     function disableButton(name)
  68.             if (button[name] ~= nil) then
  69.                     button[name]["enabled"] = false
  70.             end
  71.     end
  72.      
  73.     function enableButton(name)
  74.             if (button[name] ~= nil) then
  75.                     button[name]["enabled"] = true
  76.             end
  77.     end
  78.      
  79.     function drawButton(name)
  80.             local backColor
  81.             local textColor
  82.             local y
  83.             local padding = "                                                                                                    "
  84.            
  85.             if (button[name]["active"] == true) then
  86.                     backColor = button[name]["bcolac"]
  87.                     textColor = button[name]["tcolac"]
  88.             else
  89.                     backColor = button[name]["bcolin"]
  90.                     textColor = button[name]["tcolin"]
  91.             end
  92.            
  93.             mon.setBackgroundColor(backColor)
  94.             mon.setTextColor(textColor)
  95.            
  96.             -- Writes the button active/inactive color
  97.             for y = button[name]["ymin"], button[name]["ymax"] do
  98.                     mon.setCursorPos(button[name]["xmin"], y)
  99.                     mon.write(string.sub(padding, 1, button[name]["xmax"] - button[name]["xmin"]))
  100.             end
  101.            
  102.             -- Write the button text centered in the button
  103.             mon.setCursorPos(button[name]["xmin"] + math.floor((button[name]["xmax"] - button[name]["xmin"] - string.len(name)) / 2), button[name]["ymin"] + math.floor((button[name]["ymax"] - button[name]["ymin"]) / 2))
  104.             mon.write(string.upper(string.sub(name, 1, 1)) .. string.sub(name, 2))
  105.     end
  106.      
  107.     function drawButtons()
  108.             local name
  109.            
  110.             for name in pairs(button) do
  111.                     drawButton(name)
  112.             end
  113.     end
  114.      
  115.     function toggleButton(name, reDraw)
  116.             if (button[name] ~= nil) then
  117.                     button[name]["active"] = not button[name]["active"]
  118.                    
  119.                     if (reDraw == true) then
  120.                             drawButton(name)
  121.                     end
  122.             end
  123.     end    
  124.      
  125.     function flash(name, duration)
  126.             if (duration == nil or duration == "") then
  127.                     duration = 0.15
  128.             end
  129.            
  130.             toggleButton(name, true)
  131.             sleep(duration)
  132.             toggleButton(name, true)
  133.     end
  134.      
  135.     function activate(name)
  136.             if (button[name] ~= nil) then
  137.                     button[name]["active"] = true
  138.                     drawButton(name)
  139.             end
  140.     end
  141.      
  142.     function deActivate(name)
  143.             if (button[name] ~= nil) then
  144.                     button[name]["active"] = false
  145.                     drawButton(name)
  146.             end
  147.     end
  148.      
  149.     function checkxy(x, y)
  150.             local name
  151.            
  152.             if (button ~= nill) then
  153.                     for name in pairs(button) do
  154.                             if (y >= button[name]["ymin"] and  y <= button[name]["ymax"]) then
  155.                                     if (x >= button[name]["xmin"] and x <= button[name]["xmax"]) then
  156.                                             if (button[name]["enabled"] == true) then
  157.                                                     if (button[name]["param"] == "") then
  158.                                                             button[name]["func"]()
  159.                                                     else
  160.                                                             button[name]["func"](button[name]["param"])
  161.                                                     end
  162.                                                    
  163.                                                     return true
  164.                                             end
  165.                                     end
  166.                             end
  167.                     end
  168.             end
  169.            
  170.             return false
  171.     end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement