NMDanny

Energy Cell Information

Apr 10th, 2014
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.60 KB | None | 0 0
  1. local cell=peripheral.wrap("bottom")
  2. local monitor=peripheral.wrap("monitor_0")
  3.  
  4. local function Button(
  5.                                 width,
  6.                                 height,
  7.                                 label,
  8.                                 backgroundColorNormal,                                
  9.                                 backgroundColorPressed,
  10.                                 textColorNormal,
  11.                                 textColorPressed,
  12.                                 hasBorder,
  13.                                 borderColorNormal,
  14.                                 borderColorPressed,
  15.                                 startColumn,
  16.                                 startRow,
  17.                                 isPressed,
  18.                                 defaultBackgroundColor,
  19.                                 defaultTextColor
  20.                             )
  21.     local button = {}
  22.     button.height=height or 1
  23.     button.width=width or 1
  24.     button.label=label or ""
  25.     button.backgroundColorNormal=backgroundColorNormal or colors.black
  26.     button.backgroundColorPressed=backgroundColorPressed or colors.white
  27.     button.textColorNormal=textColorNormal or colors.white
  28.     button.textColorPressed=textColorPressed or colors.black
  29.     button.hasBorder = hasBorder or false
  30.     button.borderColorNormal = borderColorNormal or backGroundColorNormal
  31.     button.borderColorPressed = borderColorPressed or backGroundColorPressed
  32.     button.defaultBackgroundColor = defaultBackgroundColor or colors.black
  33.     button.defaultTextColor = defaultTextColor or colors.white
  34.     button.startColumn = startColumn or 1
  35.     button.startRow = startRow or 1
  36.     button.isPressed=isPressed or false
  37.     function button.press()
  38.         button.isPressed = not button.isPressed
  39.     end
  40.    
  41.     function button.draw(display,isPressed,startColumn,startRow)
  42.                            
  43.         button.startColumn = startColumn or button.startColumn
  44.         button.startRow = startRow or button.startRow
  45.         display = display or term
  46.         if isPressed == false or isPressed then
  47.             button.isPressed = isPressed
  48.         else isPressed = button.isPressed
  49.         end
  50.         local width = button.width
  51.         local height = button.height
  52.         startRow = button.startRow
  53.         startColumn = button.startColumn
  54.        
  55.         local label = button.label
  56.         local labelPad = 2
  57.        
  58.         -- set border params and draw border if hasBorder
  59.         if button.hasBorder == true then
  60.             -- button must be at least 3x3, if not, make it so
  61.             if width < 3 then
  62.                 width = 3
  63.             end
  64.             if height < 3 then
  65.                 height = 3
  66.             end
  67.            
  68.             -- set border colors
  69.             if not isPressed then
  70.                 if not display.isColor() then
  71.                     display.setBackgroundColor(colors.white)
  72.                 else
  73.                     display.setBackgroundColor(button.borderColorNormal)
  74.                 end
  75.             else
  76.                 if not display.isColor() then
  77.                     display.setBackgroundColor(colors.white)
  78.                 else
  79.                     display.setBackgroundColor(button.borderColorPressed)
  80.                 end
  81.             end
  82.            
  83.             -- draw button border (inset)
  84.             display.setCursorPos(startColumn,startRow)
  85.             display.write(string.rep(" ",width))
  86.             for row = 2,height-1 do
  87.                 display.setCursorPos(startColumn,button.startRow+row -1)
  88.                 display.write(" ")
  89.                 display.setCursorPos(startColumn+width -1 ,startRow + row-1)
  90.                 display.write(" ")
  91.             end
  92.             display.setCursorPos(startColumn,startRow+height-1)
  93.             display.write(string.rep(" ",width))
  94.            
  95.             -- reset startColumn,startRow,width,column to inset button and label
  96.             startColumn=startColumn+1
  97.             startRow = startRow +1
  98.             width = width - 2
  99.             height = height - 2
  100.         end
  101.        
  102.         --set button background and text colors
  103.         if not isPressed then
  104.             if not display.isColor() then
  105.                 display.setBackgroundColor(colors.black)
  106.                 display.setTextColor(colors.white)
  107.             else
  108.                 display.setBackgroundColor(button.backgroundColorNormal)
  109.                 display.setTextColor(button.textColorNormal)
  110.             end
  111.         else
  112.             if not display.isColor() then
  113.                 display.setBackgroundColor(colors.white)
  114.                 display.setTextColor(colors.black)
  115.             else
  116.                 display.setBackgroundColor(button.backgroundColorPressed)
  117.                 display.setTextColor(button.textColorPressed)
  118.             end
  119.         end
  120.        
  121.         -- draw button background (will be inside border if there is one)
  122.         for row = 1,height do
  123.             --print(tostring(startColumn)..","..tostring(startRow-row))
  124.             display.setCursorPos(startColumn,startRow + row -1)
  125.             display.write(string.rep(" ",width))
  126.         end
  127.        
  128.         -- prepare label, truncate label if necessary
  129.        
  130.         -- prepare label, truncate label if necessary
  131.         if width < 3 then
  132.             labelPad = 0
  133.         end
  134.         if #label > width - labelPad then
  135.             label = label:sub(1,width - labelPad)
  136.         end
  137.        
  138.         -- draw label
  139.         display.setCursorPos(startColumn + math.floor((width - #label)/2),startRow + math.floor((height-1)/2))
  140.         display.write(label)
  141.         display.setBackgroundColor(button.defaultBackgroundColor)
  142.         display.setTextColor(button.defaultTextColor)
  143.     end
  144.     button.toggle = function ()
  145.             button.isPressed = not button.isPressed
  146.             return button.isPressed
  147.     end
  148.     function button.clicked(column,row)
  149.         return (column >= button.startColumn and column < button.startColumn + button.width and row >= button.startRow and row < button.startRow + button.height)
  150.     end    
  151.     return button
  152. end
  153.  
  154.     local width,height=monitor.getSize()
  155.     monitor.clear()
  156.     monitor.setBackgroundColor(colors.purple)
  157.     monitor.setTextColor(colors.yellow)
  158.     monitor.setCursorPos(1,1)
  159.     monitor.write("Energy Cell Management"..string.rep(" ",width-#"Energy Cell Management"))
  160.     monitor.setTextScale(.5)
  161.    
  162.     local quit=Button(12,7,"Quit")
  163.  
  164.    
  165.    
  166. while true do
  167.     monitor.setBackgroundColor(colors.black)
  168.     monitor.setTextColor(colors.white)
  169.     for row=1,height do
  170.         monitor.write(string.rep(" ",width))
  171.     end
  172.  
  173.     quit.startRow=8
  174.     quit.startColumn=5
  175.     quit.backgroundColorNormal=colors.red
  176.     quit.backgroundColorPressed=colors.white
  177.     quit.hasBorder=true
  178.     quit.borderColorNormal=colors.white
  179.     quit.borderColorPressed=colors.red
  180.     quit.textColorNormal=colors.white
  181.     quit.textColorPressed=colors.red
  182.     quit.draw(monitor)
  183.  
  184.     local info={[2]={"Energy Stored",cell.getEnergyStored("unknown")," RF"},[1]={"Maximum Energy Stored",cell.getMaxEnergyStored("unknown")," RF"}}
  185.     info[3]={"Energy Percent",math.floor(100*(info[2][2]/info[1][2])),"%"}
  186.  
  187.     local maxLength=0
  188.     for key,value in pairs(info) do
  189.         maxLength=math.max(maxLength,#value[1])
  190.     end
  191.    
  192.     local currentRow=3
  193.     for key,value in pairs(info) do
  194.         monitor.setCursorPos(1,currentRow)
  195.         monitor.write(string.rep(" ",maxLength-#value[1])..value[1]..": "..value[2]..value[3])
  196.         currentRow=currentRow+1
  197.     end
  198.    
  199.     event={os.pullEvent()}
  200.     if event[1]=="monitor_touch" then
  201.         if quit.clicked(event[3],event[4])==true then
  202.             monitor.clear()
  203.             break
  204.         end
  205.     elseif event[1]=="key" and event[2]==keys.q then
  206.         monitor.clear()
  207.         break
  208.     end
  209. end
Advertisement
Add Comment
Please, Sign In to add comment