Advertisement
Guest User

buttonAPI.lua

a guest
Sep 1st, 2014
573
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.46 KB | None | 0 0
  1. local button={}
  2.  
  3. local component = require("component")
  4. local colors = require("colors")
  5. local term = require("term")
  6. local mon = component.gpu
  7. local w, h = mon.getResolution()
  8. local Green = 0x00AA00
  9. local Red = 0xAA0000
  10. local Black = 0x000000
  11.  
  12. function clear()
  13.   mon.setBackground(Black)
  14.   mon.fill(1, 1, w, h, " ")
  15. end
  16.  
  17. function clearTable()
  18.   button = {}
  19.   clear()
  20. end
  21.                
  22. function setTable(name, func, xmin, xmax, ymin, ymax)
  23.   button[name] = {}
  24.   button[name]["func"] = func
  25.   button[name]["active"] = false
  26.   button[name]["xmin"] = xmin
  27.   button[name]["ymin"] = ymin
  28.   button[name]["xmax"] = xmax
  29.   button[name]["ymax"] = ymax
  30. end
  31.  
  32. function funcName()
  33.   print("You clicked buttonText")
  34. end
  35.        
  36. function fillTable() -- not needed(?)
  37.   setTable("ButtonText", funcName, 5, 25, 4, 8)
  38. end    
  39.  
  40. function fill(text, color, bData)
  41.   mon.setBackground(color)
  42.   local yspot = math.floor((bData["ymin"] + bData["ymax"]) /2)
  43.   local xspot = math.floor((bData["xmax"] - bData["xmin"] - string.len(text)) /2) +1
  44.   for j = bData["ymin"], bData["ymax"] do
  45.     term.setCursor(bData["xmin"], j)
  46.     if j == yspot then
  47.       for k = 0, bData["xmax"] - bData["xmin"] - string.len(text) +1 do
  48.          if k == xspot then
  49.             term.write(text)
  50.           else
  51.             term.write(" ")
  52.           end
  53.        end
  54.      else
  55.        for i = bData["xmin"], bData["xmax"] do
  56.          term.write(" ")
  57.        end
  58.      end
  59.   end
  60.   mon.setBackground(Black)
  61. end
  62.      
  63. function screen()
  64.   local currColor
  65.   for name,data in pairs(button) do
  66.     local on = data["active"]
  67.     if on == true then currColor = Green else currColor = Red end
  68.     fill(name, currColor, data)
  69.   end
  70. end
  71.  
  72. function toggleButton(name)
  73.   button[name]["active"] = not button[name]["active"]
  74.   screen()
  75. end    
  76.  
  77. function flash(name,length)
  78.   toggleButton(name)
  79.   screen()
  80.   os.sleep(length)
  81.   toggleButton(name)
  82.   screen()
  83. end
  84.                                              
  85. function checkxy(x, y)
  86.   for name, data in pairs(button) do
  87.     if y>=data["ymin"] and  y <= data["ymax"] then
  88.       if x>=data["xmin"] and x<= data["xmax"] then
  89.         data["func"]()
  90.           return true
  91.       end
  92.     end
  93.   end
  94.   return false
  95. end
  96.      
  97. function heading(text)
  98.   w, h = mon.getResolution()
  99.   term.setCursor((w-string.len(text))/2+1, 1)
  100.   term.write(text)
  101. end
  102.      
  103. function label(w, h, text)
  104.   mon.setCursorPos(w, h)
  105.   mon.write(text)
  106. end
  107.  
  108.  
  109.  
  110. --eof
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement