Guest User

buttons

a guest
Aug 5th, 2014
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.38 KB | None | 0 0
  1. local mon = peripheral.wrap("top")
  2. mon.setTextScale(1)
  3. mon.setTextColor(colors.black)
  4. backGroundColor = colors.lightBlue
  5. local button={}
  6. mon.setBackgroundColor(backGroundColor)
  7.  
  8. function clearTable()
  9.   button = {}
  10.   mon.clear()
  11. end
  12.  
  13. function loadingScreen(text,nTime)
  14.   local w,h = mon.getSize()
  15.   xPos = (w-string.len(text)+3)/2
  16.   yPos = h/2
  17.   for i=1,nTime do
  18.     mon.clear()
  19.     mon.setCursorPos(xPos,yPos)
  20.     mon.write(text)
  21.     for j=1,3 do
  22.       mon.write(".")
  23.       sleep(0.2)
  24.     end    
  25.   end
  26. end
  27. function setTable(name, func, var, xmin, xmax, ymin, ymax)
  28.   button[name] = {}
  29.   button[name]["func"] = func
  30.   button[name]["active"] = false
  31.   button[name]["var"] = var
  32.   button[name]["xmin"] = xmin
  33.   button[name]["ymin"] = ymin
  34.   button[name]["xmax"] = xmax
  35.   button[name]["ymax"] = ymax
  36. end
  37.          
  38. function funcName()
  39.   print("You clicked buttonText")
  40. end
  41.                
  42. function fillTable()
  43.   setTable("ButtonText", funcName, 5, 25, 4, 8)
  44. end    
  45.          
  46. function fill(text, color, bData)
  47.   mon.setBackgroundColor(color)
  48.   if string.len(text) > bData["xmax"]-bData["xmin"] then
  49.     text = string.sub(text,1,(bData["xmax"]-bData["xmin"]-1))
  50.   end
  51.   local yspot = math.floor((bData["ymin"] + bData["ymax"]) /2)
  52.   local xspot = math.floor((bData["xmax"] - bData["xmin"] - string.len(text)) /2) +1
  53.   for j = bData["ymin"], bData["ymax"] do
  54.     mon.setCursorPos(bData["xmin"], j)
  55.     if j == yspot then
  56.       for k = 0, bData["xmax"] - bData["xmin"] - string.len(text) +1 do
  57.         if k == xspot then
  58.           mon.write(text)
  59.         else
  60.           mon.write(" ")
  61.         end
  62.       end
  63.     else
  64.       for i = bData["xmin"], bData["xmax"] do
  65.         mon.write(" ")
  66.       end
  67.     end
  68.   end
  69.   mon.setBackgroundColor(backGroundColor)
  70. end
  71.              
  72. function screen()
  73.   local currColor
  74.   for name,data in pairs(button) do
  75.     local on = data["active"]
  76.     if on == true then currColor = colors.white else currColor = colors.blue end
  77.     fill(name, currColor, data)
  78.   end
  79. end
  80.          
  81. function toggleButton(name)
  82.   button[name]["active"] = not button[name]["active"]
  83.   screen()
  84. end    
  85.          
  86. function flash(name)
  87.   toggleButton(name)
  88.   screen()
  89.   sleep(0.15)
  90.   toggleButton(name)
  91.   screen()
  92. end
  93.    
  94. function checkButton(name)
  95.     for Oname, data in pairs(button) do
  96.       if data["active"] then
  97.         toggleButton(Oname)
  98.         break
  99.       end
  100.     end
  101.   toggleButton(name)
  102.   return true
  103. end
  104.                                                                                                      
  105. function checkxy(x, y)
  106.   for name, data in pairs(button) do
  107.     if y>=data["ymin"] and  y <= data["ymax"] then
  108.       if x>=data["xmin"] and x<= data["xmax"]+1 then
  109.         if data["active"] then return false end  
  110.         if checkButton(name) then
  111.           data["func"](data["var"])
  112.         end
  113.         return true
  114.       end
  115.     end
  116.   end
  117.   return false
  118. end
  119.              
  120. function heading(text,colorheader,sizeheader)
  121.   mon.setTextColor(colorheader)
  122.   mon.setTextScale(sizeheader)
  123.   w, h = mon.getSize()
  124.   mon.setCursorPos(math.floor((w-string.len(text)/sizeheader)/2+1), 1)
  125.   mon.write(text)
  126.   mon.setTextColor(colors.black)
  127.   mon.setTextScale(1)
  128. end
  129.              
  130. function label(w, h, text)
  131.   mon.setCursorPos(w, h)
  132.   mon.write(text)
  133. end
  134.          
  135. function checkActive(name)
  136.   return button[name]["active"]
  137. end
Advertisement
Add Comment
Please, Sign In to add comment