monster010

CC - Button API

Apr 20th, 2016
1,894
1
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.64 KB | None | 1 0
  1. -- Button API modifiert - (c) monster010
  2. local monitor
  3. local button = {}
  4.  
  5. function construct(montor)
  6.     monitor = montor
  7. end
  8.  
  9. function getBtns()
  10.     return button
  11. end
  12.  
  13. function add(name, label, func, fargs, x, y, width, height, bgnormal, bgpressed, color, colorp)
  14.     if not bgnormal then bgnormal = colors.lime end
  15.     if not bgpressed then bgpressed = colors.red end
  16.     if not color then color = colors.white end
  17.     if not colorp then colorp = colors.white end
  18.  
  19.     button[name] = {}
  20.     button[name]["label"] = label
  21.     button[name]["func"] = func
  22.     button[name]["args"] = fargs
  23.     button[name]["xmin"] = x
  24.     button[name]["ymin"] = y
  25.     button[name]["xmax"] = x + width - 1
  26.     button[name]["ymax"] = y + height - 1
  27.     button[name]["bgn"] = bgnormal
  28.     button[name]["bgp"] = bgpressed
  29.     button[name]["color"] = color
  30.     button[name]["colorp"] = colorp
  31.     button[name]["active"] = false
  32. end
  33.  
  34. function clear()
  35.     button = {}
  36. end
  37.  
  38. function getClick()
  39.     event,side,x,y = os.pullEvent("monitor_touch")
  40.     checkxy(x,y)
  41. end
  42.  
  43. function checkxy(x, y)
  44.     for name, data in pairs(button) do
  45.         if y >= data["ymin"] and y <= data["ymax"] then
  46.             if x >= data["xmin"] and x <= data["xmax"] then
  47.                 if not data["args"] then
  48.                     data["func"]()
  49.                 else
  50.                     data["func"](data["args"])
  51.                 end
  52.  
  53.                 return true
  54.             end
  55.         end
  56.     end
  57.  
  58.     return false
  59. end
  60.  
  61. function screen()
  62.     local bgcolor
  63.     local color
  64.  
  65.     for name, data in pairs(button) do
  66.         if data["active"] == true then
  67.             bgcolor = data["bgp"]
  68.             color = data["colorp"]
  69.         else
  70.             bgcolor = data["bgn"]
  71.             color = data["color"]
  72.         end
  73.  
  74.         fill(data["label"], bgcolor, color, data)
  75.     end
  76. end
  77.  
  78. function fill(label, bgcolor, color, bData)
  79.     monitor.setBackgroundColor(bgcolor)
  80.     monitor.setTextColor(color)
  81.  
  82.     local yspot = math.floor((bData["ymin"] + bData["ymax"]) / 2)
  83.     local xspot = math.floor((bData["xmax"] - bData["xmin"] - string.len(label)) / 2) + 1
  84.  
  85.     for j = bData["ymin"], bData["ymax"] do
  86.         monitor.setCursorPos(bData["xmin"], j)
  87.  
  88.         if j == yspot then
  89.             for k = 0, bData["xmax"] - bData["xmin"] - string.len(label) + 1 do
  90.                 if k == xspot then
  91.                     monitor.write(label)
  92.                 else
  93.                     monitor.write(" ")
  94.                 end
  95.             end
  96.         else
  97.             for i = bData["xmin"], bData["xmax"] do
  98.                 monitor.write(" ")
  99.             end
  100.         end
  101.     end
  102.  
  103.     monitor.setBackgroundColor(colors.black)
  104.     monitor.setTextColor(colors.white)
  105. end
  106.  
  107. function set(name, stat, refresh)
  108.     if not refresh then refresh = false end
  109.  
  110.     button[name]["active"] = stat
  111.  
  112.     if refresh then
  113.         screen()
  114.     end
  115. end
  116.  
  117. function toggle(name)
  118.     button[name]["active"] = not button[name]["active"]
  119.     screen()
  120. end
  121.  
  122. function flash(name)
  123.     toggle(name)
  124.     sleep(0.15)
  125.     toggle(name)
  126. end
Advertisement
Add Comment
Please, Sign In to add comment