Advertisement
DerMarten

multi api v2

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