Advertisement
Guest User

startup

a guest
Oct 19th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.02 KB | None | 0 0
  1. local mon = peripheral.wrap("top")
  2. mon.setTextScale(1)
  3. mon.setTextColor(colors.white)
  4. local button={}
  5. mon.setBackgroundColor(colors.black)
  6.  
  7. function clearTable()
  8.    button = {}
  9.    mon.clear()
  10. end
  11.                
  12. -- Creates a new button with a name
  13. function initButton(name, func, xmin, xmax, ymin, ymax)
  14.    button[name] = {}
  15.    button[name]["func"] = func
  16.    button[name]["active"] = false
  17.    button[name]["xmin"] = xmin
  18.    button[name]["ymin"] = ymin
  19.    button[name]["xmax"] = xmax
  20.    button[name]["ymax"] = ymax
  21. end
  22.  
  23. function fredClicked()
  24.    print("You clicked Fred")
  25. end
  26.  
  27. function demoButton()
  28.    initButton("I am Fred", fredClicked, 5, 25, 4, 8)
  29. end    
  30.  
  31. -- Fills the button with color and text
  32. function fillButton(text, color, bData)
  33.    mon.setBackgroundColor(color)
  34.    -- The vertical center of the button
  35.    local yspot = math.floor((bData["ymin"] + bData["ymax"]) /2)
  36.    -- The horizontal space where the text should start
  37.    local xspot = math.floor((bData["xmax"] - bData["xmin"] - string.len(text)) /2) +1
  38.    for j = bData["ymin"], bData["ymax"] do
  39.       mon.setCursorPos(bData["xmin"], j)
  40.       if j == yspot then
  41.          for k = 0, bData["xmax"] - bData["xmin"] - string.len(text) +1 do
  42.             if k == xspot then
  43.                mon.write(text)
  44.             else
  45.                mon.write(" ")
  46.             end
  47.          end
  48.       else
  49.          for i = bData["xmin"], bData["xmax"] do
  50.             mon.write(" ")
  51.          end
  52.       end
  53.    end
  54.    mon.setBackgroundColor(colors.black)
  55. end
  56.      
  57. -- Refreshes the screen
  58. function screen()
  59.    local currColor
  60.    for name,data in pairs(button) do
  61.       local isOn = data["active"]
  62.       if isOn then currColor = colors.lime else currColor = colors.red end
  63.       fillButton(name, currColor, data)
  64.    end
  65. end
  66.  
  67. -- Button functions --
  68.  
  69. -- Toggles a button between active an inactive
  70. function toggleButton(name)
  71.    button[name]["active"] = not button[name]["active"]
  72.    screen()
  73. end    
  74.  
  75. -- Causes a button to toggle for a short pulse
  76. function flash(name)
  77.    toggleButton(name)
  78.    sleep(0.15)
  79.    toggleButton(name)
  80. end
  81.  
  82. -- Determines which button was clicked, if any                
  83. function checkxy(x, y)
  84.    for name, data in pairs(button) do
  85.       if y>=data["ymin"] and  y <= data["ymax"] then
  86.          if x>=data["xmin"] and x<= data["xmax"] then
  87.             data["func"]()
  88.             data["active"] = not data["active"]
  89.             print(name)
  90.             return true
  91.          end
  92.       end
  93.    end
  94.    return false
  95. end
  96.  
  97. -- End button functions --
  98.  
  99. -- Sets a header on the top center of the monitor
  100. function heading(text)
  101.    w, h = mon.getSize()
  102.    mon.setCursorPos((w-string.len(text))/2+1, 1)
  103.    mon.write(text)
  104. end
  105.  
  106. -- Sets a text label at a specified position  
  107. function label(w, h, text)
  108.    mon.setCursorPos(w, h)
  109.    mon.write(text)
  110. end
  111.  
  112. function getClick()
  113.    event,side,x,y = os.pullEvent("monitor_touch")
  114.    checkxy(x,y)
  115. end
  116.  
  117. heading("TEST")
  118. demoButton()
  119. while true do
  120. sleep(.01)
  121. screen()
  122. getClick()
  123. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement