NMDanny

Energy Cell Management - Master Computer

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