1ng0

Computercraft - Touchscreen Contol

Jul 26th, 2015
292
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.51 KB | None | 0 0
  1. -- genericCtrl
  2.  
  3. -- monSide: side of the monitor
  4. local monSide = "back"
  5. local term = peripheral.wrap(monSide)
  6.  
  7. --local txtScale = 1.0
  8.  
  9. -- rsSide: Redstone Bundled Output
  10. local rsSide = "right"
  11.  
  12. -- Position in cfg arrays
  13. local bLabel = 1
  14. local bActive = 2
  15.  
  16. -- cfg:
  17. local cfg = {}
  18. cfg[1] = {"Quarry", false}
  19. cfg[2] = {"Sugar", false}
  20. cfg[3] = {"Melons", false}
  21. cfg[4] = {"Oak Wood", true}
  22. cfg[5] = {"Birch", true}
  23. cfg[6] = {"Spruce", true}
  24. cfg[7] = {"Rubber", false}
  25. cfg[8] = {"Peat Bog", true}
  26.  
  27. -- columns & rows:
  28. columns = 3
  29. rows = 3
  30.  
  31. -- buttons: this holds the buttons definitions
  32. local buttons = {}
  33.  
  34. -- Screen size
  35. local screenX, screenY
  36.  
  37. -- getBundledColor: returns colors codes ( white (n=1) / black (n=16) )
  38. function getBundledColor(n)
  39.   return bit.blshift(1,(n-1))
  40. end
  41.  
  42. -- fills the buttons table
  43. function setButton(name, active, colorCode, callback, xMin, xMax, yMin, yMax)
  44.   buttons[name] = {}
  45.   buttons[name]["active"] = active
  46.   buttons[name]["color"] = colorCode
  47.   buttons[name]["func"] = callback
  48.   buttons[name]["xMin"] = xMin
  49.   buttons[name]["xMax"] = xMax
  50.   buttons[name]["yMin"] = yMin
  51.   buttons[name]["yMax"] = yMax
  52. end
  53.  
  54. -- defineButtons: define buttons in a dynamic "centered" layout
  55. function defineButtons()
  56.  
  57.   screenX, screenY = term.getSize()
  58.  
  59.   vertTrim = math.floor(screenY/(rows+1)) + 1
  60.   horizTrim = math.floor(screenX/(columns+1)) + 1
  61.  
  62.   i = 1  
  63.   for row = 1, rows do
  64.    
  65.     if i > # cfg then break end
  66.    
  67.     local yMin = (row * vertTrim) - 1
  68.     local yMax = (row * vertTrim) + 1
  69.    
  70.     for col =  1, columns do
  71.      
  72.       if i > # cfg then break end
  73.      
  74.       local labelLen = string.len(cfg[i][bLabel])
  75.       local xMin = (col * horizTrim) - math.floor(labelLen/2) - 1
  76.       local xMax = xMin + labelLen + 2
  77.      
  78.       setButton(cfg[i][bLabel], cfg[i][bActive], getBundledColor(i), toggleBundledOutput, xMin, xMax, yMin, yMax)
  79.       --print(xMin..","..xMax..","..yMin..","..yMax)
  80.       i = i + 1
  81.            
  82.     end
  83.   end
  84.  
  85. end
  86.  
  87. -- toggleBundledOutput: toggle redstone output by button name
  88. function toggleBundledOutput(name)
  89.   local activeColors = rs.getBundledOutput(rsSide)
  90.   if buttons[name]["active"] then
  91.     rs.setBundledOutput(rsSide, (activeColors - buttons[name]["color"]))
  92.     buttons[name]["active"] = false
  93.   else
  94.     rs.setBundledOutput(rsSide, (activeColors + buttons[name]["color"]))
  95.     buttons[name]["active"] = true
  96.   end
  97. end
  98.  
  99.  
  100. -- initBundledOutput: Init Bundled Output at reboot/restart
  101. function initBundledOutput()
  102.   local initColors = 0
  103.   for name, btn in pairs(buttons) do
  104.     if buttons[name]["active"] then
  105.       initColors = initColors + buttons[name]["color"]
  106.     end
  107.   end
  108.   rs.setBundledOutput(rsSide, initColors)
  109. end
  110.  
  111.  
  112. -- returns len whitespaces
  113. function fillSpace(len)
  114.   len = len or 1
  115.   fill = ""
  116.   for i = 1, len do
  117.     fill = fill .. " "
  118.   end
  119.   return fill
  120. end
  121.  
  122. -- as it says
  123. function resetTermColors()
  124.   term.setTextColor(colors.white)
  125.   term.setBackgroundColor(colors.black)
  126. end
  127.  
  128. -- drawScreen:
  129. function drawScreen()
  130.  
  131.   for name, btn in pairs(buttons) do
  132.    
  133.     if buttons[name]["active"] then
  134.       term.setTextColor(colors.white)
  135.       term.setBackgroundColor(colors.green)
  136.     else
  137.       term.setTextColor(colors.black)
  138.       term.setBackgroundColor(colors.red)
  139.     end
  140.    
  141.     labelLen = string.len(name)
  142.    
  143.     term.setCursorPos(buttons[name]["xMin"], buttons[name]["yMin"])
  144.     term.write(fillSpace(labelLen + 4))
  145.  
  146.     term.setCursorPos(buttons[name]["xMin"], (buttons[name]["yMin"] + 1))
  147.     term.write("  "..name.."  ")
  148.  
  149.     term.setCursorPos(buttons[name]["xMin"], buttons[name]["yMax"])
  150.     term.write(fillSpace(labelLen + 4))
  151.  
  152.     resetTermColors()
  153.    
  154.   end
  155.  
  156. end
  157.  
  158. -- touchEvent: process touch events
  159. function touchEvent(x, y)
  160.   for name, btn in pairs(buttons) do
  161.    
  162.     if (y >= buttons[name]["yMin"]) and (y <= buttons[name]["yMax"])
  163.       and (x >= buttons[name]["xMin"]) and (x <= buttons[name]["xMax"]) then
  164.    
  165.       print("Touch on button: "..name)
  166.       toggleBundledOutput(name)
  167.       break
  168.      
  169.     end
  170.   end
  171. end
  172.  
  173. --------------------------------------------
  174. -- MAIN --
  175. --------------------------------------------
  176. term.clear()
  177. --term.setTextScale(txtScale)
  178.  
  179. defineButtons()
  180. initBundledOutput()
  181.  
  182. while true do
  183.  
  184.   drawScreen()
  185.  
  186.   local evtp, side, x, y = os.pullEvent()
  187.  
  188.   if evtp == "monitor_touch" then
  189.    
  190.     touchEvent(x, y)
  191.  
  192.   else
  193.  
  194.     print("Ignoring event "..evtp)
  195.    
  196.   end
  197.  
  198. end
Add Comment
Please, Sign In to add comment