Advertisement
surferpup

Monitor Tutorial P2ex3

Feb 8th, 2014
303
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.22 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. -- Set up Monitor
  23. if not attachedMonitor.isColor() then
  24.     attachedMonitor.setBackgroundColor(colors.black)
  25.     attachedMonitor.setTextColor(colors.white)
  26. else
  27.     attachedMonitor.setBackgroundColor(defaultBackgroundColor)
  28.     attachedMonitor.setTextColor(defaultTextColor)
  29. end
  30. --attachedMonitor.setTextScale(.5)
  31. attachedMonitor.clear()
  32.  
  33. -- set the button/label color to normal (not pressed) colors
  34. if not attachedMonitor.isColor() then
  35.     attachedMonitor.setBackgroundColor(colors.black)
  36.     attachedMonitor.setTextColor(colors.white)
  37. else
  38.     attachedMonitor.setBackgroundColor(backgroundColorNormal)
  39.     attachedMonitor.setTextColor(textColorNormal)
  40. end
  41.  
  42. -- Display the button Background
  43. for row = startRow,startRow+height-1 do
  44.     attachedMonitor.setCursorPos(startColumn,row)
  45.     attachedMonitor.write(string.rep(" ",width))
  46. end
  47.  
  48. -- prepare label, truncate label if necessary
  49. if width < 3 then
  50.     labelPad = 0
  51. end
  52. if #label > width - labelPad then
  53.     label = label:sub(1,width - labelPad)
  54. end
  55.  
  56. --Display the label (auto centered)
  57. attachedMonitor.setCursorPos(startColumn + math.floor((width - #label)/2),startRow + math.floor((height-1)/2))
  58. attachedMonitor.write(label)
  59.  
  60. -- reset to original text/background colors
  61. if not attachedMonitor.isColor() then
  62.     attachedMonitor.setBackgroundColor(colors.black)
  63.     attachedMonitor.setTextColor(colors.white)
  64. else
  65.     attachedMonitor.setBackgroundColor(defaultBackgroundColor)
  66.     attachedMonitor.setTextColor(defaultTextColor)
  67. end
  68.  
  69. -- sleep and then let's do it again with our "pressed" colors.
  70. sleep(3)
  71.  
  72. -- set button/label colors to pressed colors
  73. if not attachedMonitor.isColor() then
  74.     attachedMonitor.setBackgroundColor(colors.white)
  75.     attachedMonitor.setTextColor(colors.black)
  76. else
  77.     attachedMonitor.setBackgroundColor(backgroundColorPressed)
  78.     attachedMonitor.setTextColor(textColorPressed)
  79. end
  80.  
  81. -- Display the button Background
  82. for row = startRow,startRow+height-1 do
  83.     attachedMonitor.setCursorPos(startColumn,row)
  84.     attachedMonitor.write(string.rep(" ",width))
  85. end
  86.  
  87. -- prepare label, truncate label if necessary
  88. if width < 3 then
  89.     labelPad = 0
  90. end
  91. if #label > width - labelPad then
  92.     label = label:sub(1,width - labelPad)
  93. end
  94.  
  95.  
  96. --Display the label (auto centered)
  97. attachedMonitor.setCursorPos(startColumn + math.floor((width - #label)/2),startRow + math.floor((height-1)/2))
  98. attachedMonitor.write(label)
  99.  
  100. -- reset monitor
  101. if not attachedMonitor.isColor() then
  102.     attachedMonitor.setBackgroundColor(colors.black)
  103.     attachedMonitor.setTextColor(colors.white)
  104. else
  105.     attachedMonitor.setBackgroundColor(defaultBackgroundColor)
  106.     attachedMonitor.setTextColor(defaultTextColor)
  107. end
  108.  
  109. attachedMonitor.setCursorPos(1,8)    
  110. attachedMonitor.write("Done.")
  111. print()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement