Advertisement
SolidSnake96AS

SolidButton API

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