Advertisement
surferpup

Monitor Tutorial P2ex4

Feb 8th, 2014
522
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.74 KB | None | 0 0
  1. -- Set up variables
  2. local attachedMonitor = peripheral.wrap("top")
  3. local defaultBackgroundColor =colors.black
  4. local defaultTextColor=colors.white
  5.  
  6. local backgroundColorNormal = colors.blue                        
  7. local backgroundColorPressed = colors.red
  8.  
  9. local textColorNormal = colors.white
  10. local textColorPressed = colors.yellow
  11.  
  12. local width=10
  13. local height = 3
  14.  
  15. local label="Button"
  16.  
  17. local startColumn=3
  18. local startRow=3
  19.  
  20. local labelPad = 2
  21.  
  22. -- our draw function
  23.  
  24. local function draw(isPressed)
  25.     if isPressed == false or isPressed then
  26.         isPressed = isPressed
  27.     else
  28.         isPressed = false
  29.     end
  30.    
  31.     -- set the button/label colors
  32.     if not isPressed then
  33.         if not attachedMonitor.isColor() then
  34.             attachedMonitor.setBackgroundColor(colors.black)
  35.             attachedMonitor.setTextColor(colors.white)
  36.         else
  37.             attachedMonitor.setBackgroundColor(backgroundColorNormal)
  38.             attachedMonitor.setTextColor(textColorNormal)
  39.         end
  40.     else
  41.         if not attachedMonitor.isColor() then
  42.             attachedMonitor.setBackgroundColor(colors.white)
  43.             attachedMonitor.setTextColor(colors.black)
  44.         else
  45.             attachedMonitor.setBackgroundColor(backgroundColorPressed)
  46.             attachedMonitor.setTextColor(textColorPressed)
  47.         end
  48.     end
  49.    
  50.     -- Display the button Background
  51.     for row = startRow,startRow+height-1 do
  52.         attachedMonitor.setCursorPos(startColumn,row)
  53.         attachedMonitor.write(string.rep(" ",width))
  54.     end
  55.    
  56.     -- prepare label, truncate label if necessary
  57.     if width  < 3 then
  58.         labelPad = 0
  59.     end
  60.     if #label > width - labelPad then
  61.         label = label:sub(1,width - labelPad)
  62.     end
  63.    
  64.     --Display the label (auto centered)
  65.     attachedMonitor.setCursorPos(startColumn + math.floor((width - #label)/2),startRow + math.floor((height-1)/2))
  66.     attachedMonitor.write(label)
  67.    
  68.     -- reset to original text/background colors
  69.     if not attachedMonitor.isColor() then
  70.         attachedMonitor.setBackgroundColor(colors.black)
  71.         attachedMonitor.setTextColor(colors.white)
  72.     else
  73.         attachedMonitor.setBackgroundColor(defaultBackgroundColor)
  74.         attachedMonitor.setTextColor(defaultTextColor)
  75.     end
  76. end
  77.  
  78. -- Set up Monitor
  79. if not attachedMonitor.isColor() then
  80.     attachedMonitor.setBackgroundColor(colors.black)
  81.     attachedMonitor.setTextColor(colors.white)
  82. else
  83.     attachedMonitor.setBackgroundColor(defaultBackgroundColor)
  84.     attachedMonitor.setTextColor(defaultTextColor)
  85. end
  86. --attachedMonitor.setTextScale(.5)
  87. attachedMonitor.clear()
  88.  
  89. draw(false)
  90. sleep(3)
  91. draw(true)
  92.  
  93. attachedMonitor.setCursorPos(1,8)    
  94. attachedMonitor.write("Done.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement