Advertisement
surferpup

Monitor Tutorial P2ex5

Feb 8th, 2014
264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.79 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 hasBorder = false
  13. local borderColorPressed = colors.pink
  14. local borderColorNormal = colors.lightBlue
  15.  
  16. local width=10
  17. local height = 3
  18.  
  19. local label="Button"
  20.  
  21. local startColumn=3
  22. local startRow=3
  23.  
  24. local labelPad = 2
  25.  
  26. -- our draw function
  27.  
  28. local function draw(isPressed)
  29.     if isPressed == false or isPressed then
  30.         isPressed = isPressed
  31.     else
  32.         isPressed = false
  33.     end
  34.    
  35.     -- store values
  36.     local width_old = width
  37.     local height_old = height
  38.     local startColumn_old = startColumn
  39.     local startRow_old = startRow
  40.    
  41.     -- set border params and draw border if hasBorder
  42.     if hasBorder == true then
  43.         -- button must be at least 3x3, if not, make it so
  44.         if width < 3 then
  45.             width = 3
  46.         end
  47.         if height < 3 then
  48.             height = 3
  49.         end
  50.        
  51.         -- set border colors
  52.         if not isPressed then
  53.             if not attachedMonitor.isColor() then
  54.                 attachedMonitor.setBackgroundColor(colors.white)
  55.             else
  56.                 attachedMonitor.setBackgroundColor(borderColorNormal)
  57.             end
  58.         else
  59.             if not attachedMonitor.isColor() then
  60.                 attachedMonitor.setBackgroundColor(colors.white)
  61.             else
  62.                 attachedMonitor.setBackgroundColor(borderColorPressed)
  63.             end
  64.         end
  65.        
  66.         -- draw button border
  67.         attachedMonitor.setCursorPos(startColumn,startRow)
  68.         attachedMonitor.write(string.rep(" ",width))
  69.         for row = 2,height-1 do
  70.             attachedMonitor.setCursorPos(startColumn,startRow+row -1)
  71.             attachedMonitor.write(" ")
  72.             attachedMonitor.setCursorPos(startColumn+width -1 ,startRow + row-1)
  73.             attachedMonitor.write(" ")
  74.         end
  75.         attachedMonitor.setCursorPos(startColumn,startRow+height-1)
  76.         attachedMonitor.write(string.rep(" ",width))
  77.        
  78.         -- reset startColumn,startRow,width,column to inset button and label
  79.         startColumn=startColumn+1
  80.         startRow = startRow +1
  81.         width = width - 2
  82.         height = height - 2  
  83.     end
  84.    
  85.     -- set the button/label colors
  86.     if not isPressed then
  87.         if not attachedMonitor.isColor() then
  88.             attachedMonitor.setBackgroundColor(colors.black)
  89.             attachedMonitor.setTextColor(colors.white)
  90.         else
  91.             attachedMonitor.setBackgroundColor(backgroundColorNormal)
  92.             attachedMonitor.setTextColor(textColorNormal)
  93.         end
  94.     else
  95.         if not attachedMonitor.isColor() then
  96.             attachedMonitor.setBackgroundColor(colors.white)
  97.             attachedMonitor.setTextColor(colors.black)
  98.         else
  99.             attachedMonitor.setBackgroundColor(backgroundColorPressed)
  100.             attachedMonitor.setTextColor(textColorPressed)
  101.         end
  102.     end
  103.    
  104.     -- Display the button Background
  105.     for row = startRow,startRow+height-1 do
  106.         attachedMonitor.setCursorPos(startColumn,row)
  107.         attachedMonitor.write(string.rep(" ",width))
  108.     end
  109.    
  110.     -- prepare label, truncate label if necessary
  111.     if width  < 3 then
  112.         labelPad = 0
  113.     end
  114.     if #label > width - labelPad then
  115.         label = label:sub(1,width - labelPad)
  116.     end
  117.    
  118.     --Display the label (auto centered)
  119.     attachedMonitor.setCursorPos(startColumn + math.floor((width - #label)/2),startRow + math.floor((height-1)/2))
  120.     attachedMonitor.write(label)
  121.    
  122.     -- reset to original text/background colors
  123.     if not attachedMonitor.isColor() then
  124.         attachedMonitor.setBackgroundColor(colors.black)
  125.         attachedMonitor.setTextColor(colors.white)
  126.     else
  127.         attachedMonitor.setBackgroundColor(defaultBackgroundColor)
  128.         attachedMonitor.setTextColor(defaultTextColor)
  129.     end
  130.    
  131.     --restore values if changed
  132.     if hasBorder then
  133.         width = width_old  
  134.         height = height_old
  135.         startColumn = startColumn_old
  136.         startRow = startRow_old
  137.     end
  138.    
  139. end
  140.  
  141. -- Set up Monitor
  142. if not attachedMonitor.isColor() then
  143.     attachedMonitor.setBackgroundColor(colors.black)
  144.     attachedMonitor.setTextColor(colors.white)
  145. else
  146.     attachedMonitor.setBackgroundColor(defaultBackgroundColor)
  147.     attachedMonitor.setTextColor(defaultTextColor)
  148. end
  149. --attachedMonitor.setTextScale(.5)
  150. attachedMonitor.clear()
  151. hasBorder = true
  152. draw(false)
  153. sleep(3)
  154. draw(true)
  155.  
  156. attachedMonitor.setCursorPos(1,8)    
  157. attachedMonitor.write("Done.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement